src: rework optimizeReloc handling; cleanups
This commit is contained in:
+45
-25
@@ -179,38 +179,42 @@ void MemBuffer::fill(unsigned off, unsigned len, int value) {
|
||||
void MemBuffer::checkState() const {
|
||||
if (!ptr)
|
||||
throwInternalError("block not allocated");
|
||||
assert(size_in_bytes > 0);
|
||||
if (use_simple_mcheck()) {
|
||||
if (get_ne32(ptr - 4) != MAGIC1(ptr))
|
||||
const unsigned char *p = (const unsigned char *) ptr;
|
||||
if (get_ne32(p - 4) != MAGIC1(p))
|
||||
throwInternalError("memory clobbered before allocated block 1");
|
||||
if (get_ne32(ptr - 8) != size_in_bytes)
|
||||
if (get_ne32(p - 8) != size_in_bytes)
|
||||
throwInternalError("memory clobbered before allocated block 2");
|
||||
if (get_ne32(ptr + size_in_bytes) != MAGIC2(ptr))
|
||||
if (get_ne32(p + size_in_bytes) != MAGIC2(p))
|
||||
throwInternalError("memory clobbered past end of allocated block");
|
||||
}
|
||||
}
|
||||
|
||||
void MemBuffer::alloc(upx_uint64_t size) {
|
||||
void MemBuffer::alloc(upx_uint64_t bytes) {
|
||||
// NOTE: we don't automatically free a used buffer
|
||||
assert(ptr == nullptr);
|
||||
assert(size_in_bytes == 0);
|
||||
//
|
||||
assert(size > 0);
|
||||
assert(bytes > 0);
|
||||
debug_set(debug.last_return_address_alloc, upx_return_address());
|
||||
size_t bytes = mem_size(1, size, use_simple_mcheck() ? 32 : 0);
|
||||
unsigned char *p = (unsigned char *) malloc(bytes);
|
||||
NO_printf("MemBuffer::alloc %llu: %p\n", size, p);
|
||||
size_t malloc_bytes = mem_size(1, bytes);
|
||||
if (use_simple_mcheck())
|
||||
malloc_bytes += 32;
|
||||
unsigned char *p = (unsigned char *) ::malloc(malloc_bytes);
|
||||
NO_printf("MemBuffer::alloc %llu: %p\n", bytes, p);
|
||||
if (!p)
|
||||
throwOutOfMemoryException();
|
||||
ptr = p;
|
||||
size_in_bytes = ACC_ICONV(unsigned, size);
|
||||
size_in_bytes = ACC_ICONV(unsigned, bytes);
|
||||
if (use_simple_mcheck()) {
|
||||
ptr = p + 16;
|
||||
p += 16;
|
||||
// store magic constants to detect buffer overruns
|
||||
set_ne32(ptr - 8, size_in_bytes);
|
||||
set_ne32(ptr - 4, MAGIC1(ptr));
|
||||
set_ne32(ptr + size_in_bytes, MAGIC2(ptr));
|
||||
set_ne32(ptr + size_in_bytes + 4, stats.global_alloc_counter);
|
||||
set_ne32(p - 8, size_in_bytes);
|
||||
set_ne32(p - 4, MAGIC1(p));
|
||||
set_ne32(p + size_in_bytes, MAGIC2(p));
|
||||
set_ne32(p + size_in_bytes + 4, stats.global_alloc_counter);
|
||||
}
|
||||
ptr = (pointer) (void *) p;
|
||||
#if !defined(__SANITIZE_ADDRESS__) && 0
|
||||
fill(0, size_in_bytes, (rand() & 0xff) | 1); // debug
|
||||
(void) VALGRIND_MAKE_MEM_UNDEFINED(ptr, size_in_bytes);
|
||||
@@ -218,23 +222,29 @@ void MemBuffer::alloc(upx_uint64_t size) {
|
||||
stats.global_alloc_counter += 1;
|
||||
stats.global_total_bytes += size_in_bytes;
|
||||
stats.global_total_active_bytes += size_in_bytes;
|
||||
#if DEBUG
|
||||
checkState();
|
||||
#endif
|
||||
}
|
||||
|
||||
void MemBuffer::dealloc() {
|
||||
if (ptr != nullptr) {
|
||||
debug_set(debug.last_return_address_dealloc, upx_return_address());
|
||||
checkState();
|
||||
stats.global_dealloc_counter += 1;
|
||||
stats.global_total_active_bytes -= size_in_bytes;
|
||||
if (use_simple_mcheck()) {
|
||||
unsigned char *p = (unsigned char *) ptr;
|
||||
// clear magic constants
|
||||
set_ne32(ptr - 8, 0);
|
||||
set_ne32(ptr - 4, 0);
|
||||
set_ne32(ptr + size_in_bytes, 0);
|
||||
set_ne32(ptr + size_in_bytes + 4, 0);
|
||||
set_ne32(p - 8, 0);
|
||||
set_ne32(p - 4, 0);
|
||||
set_ne32(p + size_in_bytes, 0);
|
||||
set_ne32(p + size_in_bytes + 4, 0);
|
||||
//
|
||||
::free(ptr - 16);
|
||||
} else
|
||||
::free(p - 16);
|
||||
} else {
|
||||
::free(ptr);
|
||||
}
|
||||
ptr = nullptr;
|
||||
size_in_bytes = 0;
|
||||
} else {
|
||||
@@ -263,16 +273,26 @@ TEST_CASE("MemBuffer") {
|
||||
CHECK_NOTHROW(64 + mb);
|
||||
CHECK_THROWS(65 + 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));
|
||||
if (use_simple_mcheck()) {
|
||||
upx_byte *b = raw_bytes(mb, 0);
|
||||
unsigned magic1 = get_ne32(b - 4);
|
||||
set_ne32(b - 4, magic1 ^ 1);
|
||||
unsigned char *p = raw_bytes(mb, 0);
|
||||
unsigned magic1 = get_ne32(p - 4);
|
||||
set_ne32(p - 4, magic1 ^ 1);
|
||||
CHECK_THROWS(mb.checkState());
|
||||
set_ne32(b - 4, magic1);
|
||||
set_ne32(p - 4, magic1);
|
||||
mb.checkState();
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("MemBuffer unused") {
|
||||
MemBuffer mb;
|
||||
CHECK(mb.raw_ptr() == nullptr);
|
||||
CHECK(mb.raw_size_in_bytes() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("MemBuffer::getSizeForCompression") {
|
||||
CHECK_THROWS(MemBuffer::getSizeForCompression(0));
|
||||
CHECK_THROWS(MemBuffer::getSizeForDecompression(0));
|
||||
|
||||
@@ -49,9 +49,10 @@ public:
|
||||
MemBufferBase() : ptr(nullptr), size_in_bytes(0) {}
|
||||
|
||||
// NOTE: implicit conversion to underlying pointer
|
||||
// NOTE: for fully bound-checked pointer use XSPAN_S from xspan.h
|
||||
// HINT: for fully bound-checked pointer use XSPAN_S from xspan.h
|
||||
operator pointer() const { return ptr; }
|
||||
|
||||
// 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
|
||||
@@ -59,7 +60,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
// NOT allowed; use raw_bytes() instead
|
||||
// membuffer - n -> pointer; not allowed - use raw_bytes() if needed
|
||||
template <class U>
|
||||
typename std::enable_if<std::is_integral<U>::value, pointer>::type operator-(U n) const
|
||||
DELETED_FUNCTION;
|
||||
@@ -82,13 +83,13 @@ public: // raw access
|
||||
class MemBuffer final : public MemBufferBase<unsigned char> {
|
||||
public:
|
||||
MemBuffer() : MemBufferBase<unsigned char>() {}
|
||||
explicit MemBuffer(upx_uint64_t size_in_bytes);
|
||||
explicit MemBuffer(upx_uint64_t bytes);
|
||||
~MemBuffer();
|
||||
|
||||
static unsigned getSizeForCompression(unsigned uncompressed_size, unsigned extra = 0);
|
||||
static unsigned getSizeForDecompression(unsigned uncompressed_size, unsigned extra = 0);
|
||||
|
||||
void alloc(upx_uint64_t size);
|
||||
void alloc(upx_uint64_t bytes);
|
||||
void allocForCompression(unsigned uncompressed_size, unsigned extra = 0);
|
||||
void allocForDecompression(unsigned uncompressed_size, unsigned extra = 0);
|
||||
|
||||
@@ -96,7 +97,7 @@ public:
|
||||
void checkState() const;
|
||||
unsigned getSize() const { return size_in_bytes; }
|
||||
|
||||
// explicit converstion
|
||||
// explicit conversion
|
||||
void *getVoidPtr() { return (void *) ptr; }
|
||||
const void *getVoidPtr() const { return (const void *) ptr; }
|
||||
|
||||
@@ -119,6 +120,7 @@ private:
|
||||
// static debug stats
|
||||
struct Stats {
|
||||
upx_std_atomic(upx_uint32_t) global_alloc_counter;
|
||||
upx_std_atomic(upx_uint32_t) global_dealloc_counter;
|
||||
upx_std_atomic(upx_uint64_t) global_total_bytes;
|
||||
upx_std_atomic(upx_uint64_t) global_total_active_bytes;
|
||||
};
|
||||
|
||||
+2
-1
@@ -46,7 +46,8 @@
|
||||
|
||||
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_MEM == UPX_RSIZE_MAX)
|
||||
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_STR <= UPX_RSIZE_MAX / 256)
|
||||
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * UPX_RSIZE_MAX * 9 / 8 + 16 * 1024 * 1024 < INT_MAX)
|
||||
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * UPX_RSIZE_MAX * 9 / 8 + 256 * 1024 * 1024 < INT_MAX)
|
||||
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * UPX_RSIZE_MAX * 10 / 8 + 128 * 1024 * 1024 <= INT_MAX + 1u)
|
||||
ACC_COMPILE_TIME_ASSERT_HEADER(5ull * UPX_RSIZE_MAX < UINT_MAX)
|
||||
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX >= 8192 * 65536)
|
||||
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_STR >= 1024)
|
||||
|
||||
@@ -95,6 +95,12 @@ forceinline pointer ensureBase() const {
|
||||
|
||||
public:
|
||||
inline ~CSelf() {}
|
||||
void destroy() {
|
||||
assertInvariants();
|
||||
base = ptr;
|
||||
size_in_bytes = 0;
|
||||
assertInvariants();
|
||||
}
|
||||
// constructors from pointers
|
||||
CSelf(pointer first, XSpanCount count)
|
||||
: ptr(makePtr(first)), base(makeBase(first)), size_in_bytes(xspan_mem_size<T>(count.count)) {
|
||||
|
||||
Reference in New Issue
Block a user