all: assorted cleanups

This commit is contained in:
Markus F.X.J. Oberhumer
2024-01-24 20:53:14 +01:00
parent 65b25adceb
commit 5d649f83ec
27 changed files with 535 additions and 367 deletions
+11 -1
View File
@@ -345,7 +345,6 @@ using OwningPointer = T *;
// also works: a trivial class with just a number of no-ops
template <class T>
struct OwningPointer final {
static_assert(std::is_class_v<T>); // UPX convention
typedef typename std::add_lvalue_reference<T>::type reference;
typedef typename std::add_lvalue_reference<const T>::type const_reference;
typedef typename std::add_pointer<T>::type pointer;
@@ -384,6 +383,17 @@ inline void owner_delete(OwningPointer(T)(&object)) noexcept {
assert_noexcept(object == nullptr);
}
template <class T>
inline void owner_free(OwningPointer(T)(&object)) noexcept {
static_assert(!std::is_class_v<T>); // UPX convention
if (object != nullptr) {
free((T *) object);
object = nullptr;
}
assert_noexcept((T *) object == nullptr);
assert_noexcept(object == nullptr);
}
// disable some overloads
#if defined(__clang__) || __GNUC__ != 7
template <class T>
+12 -1
View File
@@ -176,7 +176,7 @@ void uintptr_check_no_overlap(upx_uintptr_t a, size_t a_size, upx_uintptr_t b, s
throwCantPack("ptr_check_no_overlap-bc");
}
#if !defined(DOCTEST_CONFIG_DISABLE) && DEBUG
#if !defined(DOCTEST_CONFIG_DISABLE) && !defined(__wasi__) && DEBUG
TEST_CASE("ptr_check_no_overlap 2") {
byte p[4] = {};
@@ -922,4 +922,15 @@ TEST_CASE("get_ratio") {
CHECK(get_ratio(2 * UPX_RSIZE_MAX, 1024ull * UPX_RSIZE_MAX) == 9999999);
}
/*************************************************************************
// compat
**************************************************************************/
#if defined(__wasi__) // TODO later - wait for wasm/wasi exception handling proposal
extern "C" {
void __cxa_allocate_exception() { std::terminate(); }
void __cxa_throw() { std::terminate(); }
} // extern "C"
#endif
/* vim:set ts=4 sw=4 et: */
+16 -14
View File
@@ -40,14 +40,14 @@ bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extr
// will throw on invalid size
upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1,
upx_uint64_t extra2 = 0);
upx_uint64_t extra2 = 0) may_throw;
//
// inline fast paths:
//
// will throw on invalid size
inline upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n) {
inline upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n) may_throw {
upx_uint64_t bytes = element_size * n;
if very_unlikely (element_size == 0 || element_size > UPX_RSIZE_MAX || n > UPX_RSIZE_MAX ||
bytes > UPX_RSIZE_MAX)
@@ -56,20 +56,20 @@ inline upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n) {
}
// will throw on invalid size
inline upx_rsize_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n) {
inline upx_rsize_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n) may_throw {
(void) mem_size(element_size, n); // assert size
return ACC_ICONV(upx_rsize_t, n); // and return n
}
// will throw on invalid size
inline void mem_size_assert(upx_uint64_t element_size, upx_uint64_t n) {
inline void mem_size_assert(upx_uint64_t element_size, upx_uint64_t n) may_throw {
(void) mem_size(element_size, n); // assert size
}
// "new" with asserted size; will throw on invalid size
#if DEBUG
template <class T>
T *NewArray(upx_uint64_t n) {
T *NewArray(upx_uint64_t n) may_throw {
COMPILE_TIME_ASSERT(std::is_standard_layout<T>::value)
COMPILE_TIME_ASSERT(std::is_trivially_copyable<T>::value)
COMPILE_TIME_ASSERT(std::is_trivially_default_constructible<T>::value)
@@ -95,32 +95,34 @@ T *NewArray(upx_uint64_t n) {
// ptrdiff_t with nullptr checks and asserted size; will throw on failure
// NOTE: returns size_in_bytes, not number of elements!
int ptr_diff_bytes(const void *a, const void *b);
unsigned ptr_udiff_bytes(const void *a, const void *b); // asserts a >= b
int ptr_diff_bytes(const void *a, const void *b) may_throw;
unsigned ptr_udiff_bytes(const void *a, const void *b) may_throw; // asserts a >= b
// short names "ptr_diff" and "ptr_udiff" for types with sizeof(X) == 1
template <class T, class U>
inline typename std::enable_if<sizeof(T) == 1 && sizeof(U) == 1, int>::type ptr_diff(const T *a,
const U *b) {
const U *b)
may_throw {
return ptr_diff_bytes(a, b);
}
template <class T, class U>
inline typename std::enable_if<sizeof(T) == 1 && sizeof(U) == 1, unsigned>::type
ptr_udiff(const T *a, const U *b) {
ptr_udiff(const T *a, const U *b) may_throw {
return ptr_udiff_bytes(a, b);
}
// check that buffers do not overlap; will throw on error
noinline void uintptr_check_no_overlap(upx_uintptr_t a, size_t a_size, upx_uintptr_t b,
size_t b_size);
size_t b_size) may_throw;
noinline void uintptr_check_no_overlap(upx_uintptr_t a, size_t a_size, upx_uintptr_t b,
size_t b_size, upx_uintptr_t c, size_t c_size);
size_t b_size, upx_uintptr_t c, size_t c_size) may_throw;
forceinline void ptr_check_no_overlap(const void *a, size_t a_size, const void *b, size_t b_size) {
forceinline void ptr_check_no_overlap(const void *a, size_t a_size, const void *b, size_t b_size)
may_throw {
uintptr_check_no_overlap((upx_uintptr_t) a, a_size, (upx_uintptr_t) b, b_size);
}
forceinline void ptr_check_no_overlap(const void *a, size_t a_size, const void *b, size_t b_size,
const void *c, size_t c_size) {
const void *c, size_t c_size) may_throw {
uintptr_check_no_overlap((upx_uintptr_t) a, a_size, (upx_uintptr_t) b, b_size,
(upx_uintptr_t) c, c_size);
}
@@ -141,7 +143,7 @@ inline void ptr_invalidate_and_poison(T *(&ptr)) noexcept {
// stdlib
**************************************************************************/
void *upx_calloc(size_t n, size_t element_size);
void *upx_calloc(size_t n, size_t element_size) may_throw;
void upx_memswap(void *a, void *b, size_t n);
+6 -6
View File
@@ -43,12 +43,12 @@
XSPAN_NAMESPACE_BEGIN
// HINT: set env-var "UPX_DEBUG_DOCTEST_DISABLE=1" for improved debugging experience
noinline void xspan_fail_nullptr(void) may_throw;
noinline void xspan_fail_nullbase(void) may_throw;
noinline void xspan_fail_not_same_base(void) may_throw;
noinline void xspan_fail_range_nullptr(void) may_throw;
noinline void xspan_fail_range_nullbase(void) may_throw;
noinline void xspan_fail_range_range(void) may_throw;
noreturn void xspan_fail_nullptr(void) may_throw;
noreturn void xspan_fail_nullbase(void) may_throw;
noreturn void xspan_fail_not_same_base(void) may_throw;
noreturn void xspan_fail_range_nullptr(void) may_throw;
noreturn void xspan_fail_range_nullbase(void) may_throw;
noreturn void xspan_fail_range_range(void) may_throw;
void xspan_check_range(const void *ptr, const void *base, ptrdiff_t size_in_bytes) may_throw;
// help constructor to distinguish between number of elements and bytes
+1
View File
@@ -280,6 +280,7 @@ public:
return Self(Unchecked, end, (begin - end) * sizeof(T), end);
}
// cast to a different type (creates a new value)
template <class U>
inline CSelf<U> type_cast() const {
typedef CSelf<U> R;
+1
View File
@@ -123,6 +123,7 @@ public:
return assign(Self(other));
}
// cast to a different type (creates a new value)
template <class U>
inline CSelf<U> type_cast() const {
typedef CSelf<U> R;