all: more assorted cleanups; NFCI
This commit is contained in:
@@ -360,6 +360,7 @@ private:
|
||||
pointer ptr;
|
||||
reference operator[](std::ptrdiff_t) noexcept DELETED_FUNCTION;
|
||||
const_reference operator[](std::ptrdiff_t) const noexcept DELETED_FUNCTION;
|
||||
UPX_CXX_DISABLE_ADDRESS(OwningPointer) // UPX convention
|
||||
UPX_CXX_DISABLE_NEW_DELETE_NO_VIRTUAL(OwningPointer) // UPX convention
|
||||
};
|
||||
// must overload mem_clear()
|
||||
|
||||
+41
-14
@@ -90,7 +90,7 @@ void *MemBuffer::subref_impl(const char *errfmt, size_t skip, size_t take) {
|
||||
// printf is using unsigned formatting
|
||||
if (!errfmt || !errfmt[0])
|
||||
errfmt = "bad subref %#x %#x";
|
||||
snprintf(buf, sizeof(buf), errfmt, (unsigned) skip, (unsigned) take);
|
||||
upx_safe_snprintf(buf, sizeof(buf), errfmt, (unsigned) skip, (unsigned) take);
|
||||
throwCantPack(buf);
|
||||
}
|
||||
return ptr + skip;
|
||||
@@ -189,7 +189,7 @@ void MemBuffer::alloc(upx_uint64_t bytes) {
|
||||
set_ne32(p + size_in_bytes + 0, MAGIC2(p));
|
||||
set_ne32(p + size_in_bytes + 4, stats.global_alloc_counter);
|
||||
}
|
||||
ptr = (pointer) (void *) p;
|
||||
ptr = upx::ptr_static_cast<pointer>(p);
|
||||
#if !defined(__SANITIZE_MEMORY__) && DEBUG
|
||||
memset(ptr, 0xfb, size_in_bytes);
|
||||
(void) VALGRIND_MAKE_MEM_UNDEFINED(ptr, size_in_bytes);
|
||||
@@ -247,26 +247,33 @@ void MemBuffer::dealloc() noexcept {
|
||||
**************************************************************************/
|
||||
|
||||
TEST_CASE("MemBuffer core") {
|
||||
constexpr size_t N = 64;
|
||||
MemBuffer mb;
|
||||
CHECK_THROWS(mb.checkState());
|
||||
CHECK_THROWS(mb.alloc(0x30000000 + 1));
|
||||
CHECK(raw_bytes(mb, 0) == nullptr);
|
||||
CHECK_THROWS(raw_bytes(mb, 1));
|
||||
mb.alloc(64);
|
||||
mb.alloc(N);
|
||||
mb.checkState();
|
||||
CHECK(raw_bytes(mb, 64) != nullptr);
|
||||
CHECK(raw_bytes(mb, 64) == mb.getVoidPtr());
|
||||
CHECK_THROWS(raw_bytes(mb, 65));
|
||||
CHECK_NOTHROW(mb + 64);
|
||||
CHECK_THROWS(mb + 65);
|
||||
CHECK(mb.begin() == mb.cbegin());
|
||||
CHECK(mb.end() == mb.cend());
|
||||
CHECK(mb.begin() == &mb[0]);
|
||||
CHECK(mb.end() == &mb[0] + N);
|
||||
CHECK(mb.cbegin() == &mb[0]);
|
||||
CHECK(mb.cend() == &mb[0] + N);
|
||||
CHECK(raw_bytes(mb, N) != nullptr);
|
||||
CHECK(raw_bytes(mb, N) == mb.getVoidPtr());
|
||||
CHECK_THROWS(raw_bytes(mb, N + 1));
|
||||
CHECK_NOTHROW(mb + N);
|
||||
CHECK_THROWS(mb + (N + 1));
|
||||
#if ALLOW_INT_PLUS_MEMBUFFER
|
||||
CHECK_NOTHROW(64 + mb);
|
||||
CHECK_THROWS(65 + mb);
|
||||
CHECK_NOTHROW(N + mb);
|
||||
CHECK_THROWS((N + 1) + mb);
|
||||
#endif
|
||||
CHECK_NOTHROW(mb.subref("", 0, 64));
|
||||
CHECK_NOTHROW(mb.subref("", 64, 0));
|
||||
CHECK_THROWS(mb.subref("", 1, 64));
|
||||
CHECK_THROWS(mb.subref("", 64, 1));
|
||||
CHECK_NOTHROW(mb.subref("", 0, N));
|
||||
CHECK_NOTHROW(mb.subref("", N, 0));
|
||||
CHECK_THROWS(mb.subref("", 1, N));
|
||||
CHECK_THROWS(mb.subref("", N, 1));
|
||||
if (use_simple_mcheck()) {
|
||||
byte *p = raw_bytes(mb, 0);
|
||||
unsigned magic1 = get_ne32(p - 4);
|
||||
@@ -314,6 +321,26 @@ TEST_CASE("MemBuffer unused") {
|
||||
CHECK(mb.raw_size_in_bytes() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("MemBuffer array access") {
|
||||
constexpr size_t N = 16;
|
||||
MemBuffer mb(N);
|
||||
mb.clear();
|
||||
for (size_t i = 0; i != N; ++i)
|
||||
mb[i] += 1;
|
||||
for (byte *ptr = mb; ptr != mb + N; ++ptr)
|
||||
*ptr += 1;
|
||||
for (byte *ptr = mb + 0; ptr < mb + N; ++ptr)
|
||||
*ptr += 1;
|
||||
for (byte *ptr = &mb[0]; ptr != mb.end(); ++ptr)
|
||||
*ptr += 1;
|
||||
for (byte *ptr = mb.begin(); ptr < mb.end(); ++ptr)
|
||||
*ptr += 1;
|
||||
for (size_t i = 0; i != N; ++i)
|
||||
assert(mb[i] == 5);
|
||||
CHECK_NOTHROW((void) &mb[N - 1]);
|
||||
CHECK_THROWS((void) &mb[N]); // NOT legal for containers like std::vector or MemBuffer
|
||||
}
|
||||
|
||||
TEST_CASE("MemBuffer::getSizeForCompression") {
|
||||
CHECK_THROWS(MemBuffer::getSizeForCompression(0));
|
||||
CHECK_THROWS(MemBuffer::getSizeForDecompression(0));
|
||||
|
||||
+48
-20
@@ -42,6 +42,11 @@ public:
|
||||
typedef typename std::add_lvalue_reference<T>::type reference;
|
||||
typedef typename std::add_pointer<T>::type pointer;
|
||||
typedef unsigned size_type; // limited by UPX_RSIZE_MAX
|
||||
typedef pointer iterator;
|
||||
typedef typename std::add_pointer<const T>::type const_iterator;
|
||||
protected:
|
||||
static constexpr size_t element_size = sizeof(element_type);
|
||||
static_assert(element_size >= 1 && element_size <= UPX_RSIZE_MAX_MEM);
|
||||
|
||||
protected:
|
||||
pointer ptr;
|
||||
@@ -57,9 +62,9 @@ public:
|
||||
|
||||
// array access
|
||||
reference operator[](ptrdiff_t i) const may_throw {
|
||||
// TODO: &array[SIZE] == array + SIZE, this is legal; but element access is not
|
||||
if very_unlikely (i < 0 || mem_size(sizeof(element_type), i) > size_in_bytes)
|
||||
throwCantPack("MemBuffer invalid index %td (%u bytes)", i, size_in_bytes);
|
||||
// NOTE: &array[SIZE] is *not* legal for containers like std::vector and MemBuffer !
|
||||
if very_unlikely (i < 0 || mem_size(element_size, i) + element_size > size_in_bytes)
|
||||
throwCantPack("MemBuffer invalid array index %td (%u bytes)", i, size_in_bytes);
|
||||
return ptr[i];
|
||||
}
|
||||
// dereference
|
||||
@@ -67,11 +72,33 @@ public:
|
||||
// arrow operator
|
||||
pointer operator->() const DELETED_FUNCTION;
|
||||
|
||||
iterator begin() const may_throw {
|
||||
if very_unlikely (ptr == nullptr)
|
||||
throwCantPack("MemBuffer begin() unexpected NULL ptr");
|
||||
return ptr;
|
||||
}
|
||||
const_iterator cbegin() const may_throw {
|
||||
if very_unlikely (ptr == nullptr)
|
||||
throwCantPack("MemBuffer cbegin() unexpected NULL ptr");
|
||||
return ptr;
|
||||
}
|
||||
iterator end() const may_throw {
|
||||
if very_unlikely (ptr == nullptr)
|
||||
throwCantPack("MemBuffer end() unexpected NULL ptr");
|
||||
return ptr + size_in_bytes / element_size;
|
||||
}
|
||||
const_iterator cend() const may_throw {
|
||||
if very_unlikely (ptr == nullptr)
|
||||
throwCantPack("MemBuffer cend() unexpected NULL ptr");
|
||||
return ptr + size_in_bytes / element_size;
|
||||
}
|
||||
|
||||
// membuffer + n -> pointer
|
||||
template <class U>
|
||||
typename std::enable_if<std::is_integral<U>::value, pointer>::type operator+(U n) const {
|
||||
size_t bytes = mem_size(sizeof(T), n); // check mem_size
|
||||
return raw_bytes(bytes) + n; // and check bytes
|
||||
typename std::enable_if<std::is_integral<U>::value, pointer>::type operator+(U n) const
|
||||
may_throw {
|
||||
size_t bytes = mem_size(element_size, n); // check mem_size
|
||||
return raw_bytes(bytes) + n; // and check bytes
|
||||
}
|
||||
private:
|
||||
// membuffer - n -> pointer; not allowed - use raw_bytes() if needed
|
||||
@@ -83,7 +110,7 @@ public: // raw access
|
||||
pointer raw_ptr() const noexcept { return ptr; }
|
||||
size_type raw_size_in_bytes() const noexcept { return size_in_bytes; }
|
||||
|
||||
pointer raw_bytes(size_t bytes) const {
|
||||
pointer raw_bytes(size_t bytes) const may_throw {
|
||||
if (bytes > 0) {
|
||||
if very_unlikely (ptr == nullptr)
|
||||
throwCantPack("MemBuffer raw_bytes unexpected NULL ptr");
|
||||
@@ -161,39 +188,40 @@ inline typename MemBufferBase<T>::pointer raw_index_bytes(const MemBufferBase<T>
|
||||
class MemBuffer final : public MemBufferBase<byte> {
|
||||
public:
|
||||
explicit inline MemBuffer() noexcept : MemBufferBase<byte>() {}
|
||||
explicit MemBuffer(upx_uint64_t bytes);
|
||||
explicit MemBuffer(upx_uint64_t bytes) may_throw;
|
||||
~MemBuffer() noexcept;
|
||||
|
||||
static unsigned getSizeForCompression(unsigned uncompressed_size, unsigned extra = 0);
|
||||
static unsigned getSizeForDecompression(unsigned uncompressed_size, unsigned extra = 0);
|
||||
static unsigned getSizeForCompression(unsigned uncompressed_size, unsigned extra = 0) may_throw;
|
||||
static unsigned getSizeForDecompression(unsigned uncompressed_size, unsigned extra = 0)
|
||||
may_throw;
|
||||
|
||||
void alloc(upx_uint64_t bytes);
|
||||
void allocForCompression(unsigned uncompressed_size, unsigned extra = 0);
|
||||
void allocForDecompression(unsigned uncompressed_size, unsigned extra = 0);
|
||||
void alloc(upx_uint64_t bytes) may_throw;
|
||||
void allocForCompression(unsigned uncompressed_size, unsigned extra = 0) may_throw;
|
||||
void allocForDecompression(unsigned uncompressed_size, unsigned extra = 0) may_throw;
|
||||
|
||||
void dealloc() noexcept;
|
||||
void checkState() const;
|
||||
unsigned getSize() const noexcept { return size_in_bytes; }
|
||||
void checkState() const may_throw;
|
||||
|
||||
// explicit conversion
|
||||
void *getVoidPtr() noexcept { return (void *) ptr; }
|
||||
const void *getVoidPtr() const noexcept { return (const void *) ptr; }
|
||||
unsigned getSize() const noexcept { return size_in_bytes; }
|
||||
|
||||
// util
|
||||
void fill(unsigned off, unsigned len, int value);
|
||||
forceinline void clear(unsigned off, unsigned len) { fill(off, len, 0); }
|
||||
forceinline void clear() { fill(0, size_in_bytes, 0); }
|
||||
noinline void fill(unsigned off, unsigned len, int value) may_throw;
|
||||
forceinline void clear(unsigned off, unsigned len) may_throw { fill(off, len, 0); }
|
||||
forceinline void clear() may_throw { fill(0, size_in_bytes, 0); }
|
||||
|
||||
// If the entire range [skip, skip+take) is inside the buffer,
|
||||
// then return &ptr[skip]; else throwCantPack(sprintf(errfmt, skip, take)).
|
||||
// This is similar to BoundedPtr, except only checks once.
|
||||
// skip == offset, take == size_in_bytes
|
||||
forceinline pointer subref(const char *errfmt, size_t skip, size_t take) {
|
||||
forceinline pointer subref(const char *errfmt, size_t skip, size_t take) may_throw {
|
||||
return (pointer) subref_impl(errfmt, skip, take);
|
||||
}
|
||||
|
||||
private:
|
||||
void *subref_impl(const char *errfmt, size_t skip, size_t take);
|
||||
void *subref_impl(const char *errfmt, size_t skip, size_t take) may_throw;
|
||||
|
||||
// static debug stats
|
||||
struct Stats {
|
||||
|
||||
+5
-5
@@ -39,8 +39,8 @@
|
||||
#include "../conf.h"
|
||||
|
||||
/*************************************************************************
|
||||
// assert sane memory buffer sizes to protect against integer overflows
|
||||
// and malicious header fields
|
||||
// upx_rsize_t and mem_size: assert sane memory buffer sizes to protect
|
||||
// against integer overflows and malicious header fields
|
||||
// see C 11 standard, Annex K
|
||||
**************************************************************************/
|
||||
|
||||
@@ -253,7 +253,7 @@ TEST_CASE("ptr_check_no_overlap 3") {
|
||||
// stdlib
|
||||
**************************************************************************/
|
||||
|
||||
void *upx_calloc(size_t n, size_t element_size) {
|
||||
void *upx_calloc(size_t n, size_t element_size) may_throw {
|
||||
size_t bytes = mem_size(element_size, n); // assert size
|
||||
void *p = malloc(bytes);
|
||||
if (p != nullptr)
|
||||
@@ -262,7 +262,7 @@ void *upx_calloc(size_t n, size_t element_size) {
|
||||
}
|
||||
|
||||
// simple unoptimized memswap()
|
||||
void upx_memswap(void *a, void *b, size_t n) {
|
||||
void upx_memswap(void *a, void *b, size_t n) noexcept {
|
||||
if (a != b && n != 0) {
|
||||
byte *x = (byte *) a;
|
||||
byte *y = (byte *) b;
|
||||
@@ -277,7 +277,7 @@ void upx_memswap(void *a, void *b, size_t n) {
|
||||
}
|
||||
|
||||
// much better memswap(), optimized for our use case in sort functions below
|
||||
static void memswap_no_overlap(byte *a, byte *b, size_t n) {
|
||||
static void memswap_no_overlap(byte *a, byte *b, size_t n) noexcept {
|
||||
#if defined(__clang__) && __clang_major__ < 15
|
||||
// work around a clang < 15 ICE (Internal Compiler Error)
|
||||
// @COMPILER_BUG @CLANG_BUG
|
||||
|
||||
+4
-4
@@ -28,8 +28,8 @@
|
||||
#pragma once
|
||||
|
||||
/*************************************************************************
|
||||
// assert sane memory buffer sizes to protect against integer overflows
|
||||
// and malicious header fields
|
||||
// upx_rsize_t and mem_size: assert sane memory buffer sizes to protect
|
||||
// against integer overflows and malicious header fields
|
||||
// see C 11 standard, Annex K
|
||||
**************************************************************************/
|
||||
|
||||
@@ -73,7 +73,7 @@ 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)
|
||||
size_t bytes = mem_size(sizeof(T), n); // assert size
|
||||
upx_rsize_t bytes = mem_size(sizeof(T), n); // assert size
|
||||
T *array = new T[size_t(n)];
|
||||
#if !defined(__SANITIZE_MEMORY__)
|
||||
if (array != nullptr && bytes > 0) {
|
||||
@@ -145,7 +145,7 @@ inline void ptr_invalidate_and_poison(T *(&ptr)) noexcept {
|
||||
|
||||
void *upx_calloc(size_t n, size_t element_size) may_throw;
|
||||
|
||||
void upx_memswap(void *a, void *b, size_t n);
|
||||
void upx_memswap(void *a, void *b, size_t n) noexcept;
|
||||
|
||||
typedef int(__acc_cdecl_qsort *upx_compare_func_t)(const void *, const void *);
|
||||
typedef void (*upx_sort_func_t)(void *array, size_t n, size_t element_size, upx_compare_func_t);
|
||||
|
||||
@@ -54,11 +54,11 @@ void xspan_check_range(const void *ptr, const void *base, ptrdiff_t size_in_byte
|
||||
// help constructor to distinguish between number of elements and bytes
|
||||
struct XSpanCount final {
|
||||
explicit forceinline_constexpr XSpanCount(size_t n) noexcept : count(n) {}
|
||||
size_t count; // public
|
||||
const size_t count; // public
|
||||
};
|
||||
struct XSpanSizeInBytes final {
|
||||
explicit forceinline_constexpr XSpanSizeInBytes(size_t bytes) noexcept : size_in_bytes(bytes) {}
|
||||
size_t size_in_bytes; // public
|
||||
const size_t size_in_bytes; // public
|
||||
};
|
||||
|
||||
template <class T>
|
||||
|
||||
Reference in New Issue
Block a user