all: assorted updates and cleanups

This commit is contained in:
Markus F.X.J. Oberhumer
2023-02-05 17:20:32 +01:00
parent 163377d1a0
commit b8d9c2b755
56 changed files with 691 additions and 853 deletions
-155
View File
@@ -1,155 +0,0 @@
/* bptr.h --
This file is part of the UPX executable compressor.
Copyright (C) 1996-2023 Markus Franz Xaver Johannes Oberhumer
Copyright (C) 1996-2023 Laszlo Molnar
All Rights Reserved.
UPX and the UCL library are free software; you can redistribute them
and/or modify them under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.
If not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Markus F.X.J. Oberhumer Laszlo Molnar
<markus@oberhumer.com> <ezerotven+github@gmail.com>
*/
#pragma once
#ifndef UPX_BPTR_H__
#define UPX_BPTR_H__ 1
#if WITH_XSPAN >= 2
#error "this file is deprecated, please use xspan.h instead"
#endif
/*************************************************************************
// BoundedPtr
**************************************************************************/
template <class T>
class BoundedPtr {
public:
typedef T element_type;
typedef typename std::add_pointer<T>::type pointer;
~BoundedPtr() {}
BoundedPtr(void *base, size_t size_in_bytes, T *ptr = nullptr)
: ptr_(ptr), base_(base), size_in_bytes_(0) {
assert(base_ != nullptr);
size_in_bytes_ = mem_size(1, size_in_bytes);
check();
}
// assignment
BoundedPtr &operator=(const BoundedPtr &other) {
assert(base_ == other.base_);
assert(size_in_bytes_ == other.size_in_bytes_);
ptr_ = other.ptr_;
check();
return *this;
}
BoundedPtr &operator=(T *other) {
ptr_ = other;
check();
return *this;
}
// dereference
T &operator*() {
checkNULL();
checkRange(ptr_ + 1);
return *ptr_;
}
const T &operator*() const {
checkNULL();
checkRange(ptr_ + 1);
return *ptr_;
}
operator T *() { return ptr_; }
operator const T *() const { return ptr_; }
BoundedPtr &operator+=(size_t n) {
checkNULL();
ptr_ += n;
checkRange();
return *this;
}
BoundedPtr &operator-=(size_t n) {
checkNULL();
ptr_ -= n;
checkRange();
return *this;
}
BoundedPtr &operator++(void) {
checkNULL();
ptr_ += 1;
checkRange();
return *this;
}
T *raw_bytes(size_t bytes) const {
checkNULL();
if (bytes > 0)
checkRange((const char *) (const void *) ptr_ + bytes);
return ptr_;
}
private:
void checkNULL() const {
if very_unlikely (!ptr_)
throwCantUnpack("unexpected NULL pointer; take care!");
}
forceinline void checkRange() const { checkRange(ptr_, base_, size_in_bytes_); }
forceinline void checkRange(const void *p) const { checkRange(p, base_, size_in_bytes_); }
static void checkRange(const void *ptr, const void *base, size_t size_in_bytes) {
size_t off = (const char *) ptr - (const char *) base;
if very_unlikely (off > size_in_bytes)
throwCantUnpack("pointer out of range; take care!");
}
void check() const { // check ptr_ invariant: either NULL or valid checkRange()
if (ptr_ != nullptr)
checkRange();
}
T *ptr_;
void *base_;
size_t size_in_bytes_;
// disable copy
BoundedPtr(const BoundedPtr &) = delete;
// disable dynamic allocation
ACC_CXX_DISABLE_NEW_DELETE
// disable taking the address => force passing by reference
// [I'm not too sure about this design decision, but we can always allow it if needed]
BoundedPtr<T> *operator&() const = delete;
};
// raw_bytes overload
template <class T>
inline typename BoundedPtr<T>::pointer raw_bytes(const BoundedPtr<T> &a, size_t size_in_bytes) {
return a.raw_bytes(size_in_bytes);
}
template <class T>
inline typename BoundedPtr<T>::pointer raw_index_bytes(const BoundedPtr<T> &a, size_t index,
size_t size_in_bytes) {
typedef typename BoundedPtr<T>::element_type element_type;
return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
}
#endif /* already included */
/* vim:set ts=4 sw=4 et: */
+64 -68
View File
@@ -47,18 +47,19 @@ unsigned membuffer_get_size(MemBuffer &mb) { return mb.getSize(); }
#if defined(__SANITIZE_ADDRESS__)
static forceinline constexpr bool use_simple_mcheck() { return false; }
#elif (WITH_VALGRIND) && defined(RUNNING_ON_VALGRIND)
static int use_simple_mcheck_flag = -1;
static noinline void use_simple_mcheck_init() {
use_simple_mcheck_flag = 1;
static bool use_simple_mcheck_flag;
static noinline void init_use_simple_mcheck() noexcept {
bool r = true;
if (RUNNING_ON_VALGRIND) {
use_simple_mcheck_flag = 0;
// fprintf(stderr, "upx: detected RUNNING_ON_VALGRIND\n");
r = false;
NO_fprintf(stderr, "upx: detected RUNNING_ON_VALGRIND\n");
}
use_simple_mcheck_flag = r;
}
static forceinline bool use_simple_mcheck() {
if very_unlikely (use_simple_mcheck_flag < 0)
use_simple_mcheck_init();
return (bool) use_simple_mcheck_flag;
static bool use_simple_mcheck() {
static upx_std_once_flag init_done;
upx_std_call_once(init_done, init_use_simple_mcheck);
return use_simple_mcheck_flag;
}
#else
static forceinline constexpr bool use_simple_mcheck() { return true; }
@@ -68,8 +69,8 @@ static forceinline constexpr bool use_simple_mcheck() { return true; }
//
**************************************************************************/
MemBuffer::MemBuffer(upx_uint64_t size_in_bytes) {
alloc(size_in_bytes);
MemBuffer::MemBuffer(upx_uint64_t bytes) {
alloc(bytes);
debug_set(debug.last_return_address_alloc, upx_return_address());
}
@@ -80,7 +81,7 @@ MemBuffer::~MemBuffer() { this->dealloc(); }
void *MemBuffer::subref_impl(const char *errfmt, size_t skip, size_t take) {
debug_set(debug.last_return_address_subref, upx_return_address());
// check overrun and wrap-around
if (skip + take > b_size_in_bytes || skip + take < skip) {
if (skip + take > size_in_bytes || skip + take < skip) {
char buf[100];
// printf is using unsigned formatting
if (!errfmt || !errfmt[0])
@@ -88,7 +89,7 @@ void *MemBuffer::subref_impl(const char *errfmt, size_t skip, size_t take) {
snprintf(buf, sizeof(buf), errfmt, (unsigned) skip, (unsigned) take);
throwCantPack(buf);
}
return &b[skip];
return ptr + skip;
}
static unsigned width(unsigned x) {
@@ -116,62 +117,55 @@ static unsigned width(unsigned x) {
return 1 + w;
}
static inline unsigned umax(unsigned a, unsigned b) { return (a >= b) ? a : b; }
static forceinline size_t umax(size_t a, size_t b) { return (a >= b) ? a : b; }
unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra) {
if (uncompressed_size == 0)
throwCantPack("invalid uncompressed_size");
const unsigned z = uncompressed_size; // fewer keystrokes and display columns
const unsigned w = umax(8, width(z - 1)); // ignore tiny offsets
unsigned bytes = ACC_ICONV(unsigned, mem_size(1, z)); // check
const size_t z = uncompressed_size; // fewer keystrokes and display columns
const size_t w = umax(8, width(z - 1)); // ignore tiny offsets
size_t bytes = mem_size(1, z); // check size
// Worst matching: All match at max_offset, which implies 3==min_match
// All literal: 1 bit overhead per literal byte
bytes = umax(bytes, z + z / 8);
// All literal: 1 bit overhead per literal byte; from UCL documentation
bytes = umax(bytes, z + z / 8 + 256);
// NRV2B: 1 byte plus 2 bits per width exceeding 8 ("ss11")
bytes = umax(bytes, (z / 3 * (8 + 2 * (w - 8) / 1)) / 8);
// NRV2E: 1 byte plus 3 bits per pair of width exceeding 7 ("ss12")
bytes = umax(bytes, (z / 3 * (8 + 3 * (w - 7) / 2)) / 8);
// zstd: ZSTD_COMPRESSBOUND
bytes = umax(bytes, z + (z >> 8) + ((z < (128 << 10)) ? (((128 << 10) - z) >> 11) : 0));
// extra + 256 safety for rounding
// add extra and 256 safety for various rounding/alignments
bytes = mem_size(1, bytes, extra, 256);
UNUSED(w);
return bytes;
return ACC_ICONV(unsigned, bytes);
}
unsigned MemBuffer::getSizeForDecompression(unsigned uncompressed_size, unsigned extra) {
if (uncompressed_size == 0)
throwCantPack("invalid uncompressed_size");
size_t bytes = mem_size(1, uncompressed_size, extra); // check
size_t bytes = mem_size(1, uncompressed_size, extra); // check size
return ACC_ICONV(unsigned, bytes);
}
void MemBuffer::allocForCompression(unsigned uncompressed_size, unsigned extra) {
if (uncompressed_size == 0)
throwCantPack("invalid uncompressed_size");
unsigned size = getSizeForCompression(uncompressed_size, extra);
alloc(size);
unsigned bytes = getSizeForCompression(uncompressed_size, extra);
alloc(bytes);
debug_set(debug.last_return_address_alloc, upx_return_address());
}
void MemBuffer::allocForDecompression(unsigned uncompressed_size, unsigned extra) {
if (uncompressed_size == 0)
throwCantPack("invalid uncompressed_size");
unsigned size = getSizeForDecompression(uncompressed_size, extra);
alloc(size);
unsigned bytes = getSizeForDecompression(uncompressed_size, extra);
alloc(bytes);
debug_set(debug.last_return_address_alloc, upx_return_address());
}
void MemBuffer::fill(unsigned off, unsigned len, int value) {
debug_set(debug.last_return_address_fill, upx_return_address());
checkState();
assert((int) off >= 0);
assert((int) len >= 0);
assert(off <= b_size_in_bytes);
assert(len <= b_size_in_bytes);
assert(off + len <= b_size_in_bytes);
if (off > size_in_bytes || len > size_in_bytes || off + len > size_in_bytes)
throwCantPack("MemBuffer::fill out of range; take care!");
if (len > 0)
memset(b + off, value, len);
memset(ptr + off, value, len);
}
/*************************************************************************
@@ -183,22 +177,22 @@ void MemBuffer::fill(unsigned off, unsigned len, int value) {
#define MAGIC2(p) ((PTR_BITS(p) ^ 0xfefdbeeb ^ 0x80024011) | 1)
void MemBuffer::checkState() const {
if (!b)
if (!ptr)
throwInternalError("block not allocated");
if (use_simple_mcheck()) {
if (get_ne32(b - 4) != MAGIC1(b))
if (get_ne32(ptr - 4) != MAGIC1(ptr))
throwInternalError("memory clobbered before allocated block 1");
if (get_ne32(b - 8) != b_size_in_bytes)
if (get_ne32(ptr - 8) != size_in_bytes)
throwInternalError("memory clobbered before allocated block 2");
if (get_ne32(b + b_size_in_bytes) != MAGIC2(b))
if (get_ne32(ptr + size_in_bytes) != MAGIC2(ptr))
throwInternalError("memory clobbered past end of allocated block");
}
}
void MemBuffer::alloc(upx_uint64_t size) {
// NOTE: we don't automatically free a used buffer
assert(b == nullptr);
assert(b_size_in_bytes == 0);
assert(ptr == nullptr);
assert(size_in_bytes == 0);
//
assert(size > 0);
debug_set(debug.last_return_address_alloc, upx_return_address());
@@ -207,44 +201,44 @@ void MemBuffer::alloc(upx_uint64_t size) {
NO_printf("MemBuffer::alloc %llu: %p\n", size, p);
if (!p)
throwOutOfMemoryException();
b = p;
b_size_in_bytes = ACC_ICONV(unsigned, size);
ptr = p;
size_in_bytes = ACC_ICONV(unsigned, size);
if (use_simple_mcheck()) {
b = p + 16;
ptr = p + 16;
// store magic constants to detect buffer overruns
set_ne32(b - 8, b_size_in_bytes);
set_ne32(b - 4, MAGIC1(b));
set_ne32(b + b_size_in_bytes, MAGIC2(b));
set_ne32(b + b_size_in_bytes + 4, stats.global_alloc_counter);
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);
}
#if !defined(__SANITIZE_ADDRESS__) && 0
fill(0, b_size_in_bytes, (rand() & 0xff) | 1); // debug
(void) VALGRIND_MAKE_MEM_UNDEFINED(b, b_size_in_bytes);
fill(0, size_in_bytes, (rand() & 0xff) | 1); // debug
(void) VALGRIND_MAKE_MEM_UNDEFINED(ptr, size_in_bytes);
#endif
stats.global_alloc_counter += 1;
stats.global_total_bytes += b_size_in_bytes;
stats.global_total_active_bytes += b_size_in_bytes;
stats.global_total_bytes += size_in_bytes;
stats.global_total_active_bytes += size_in_bytes;
}
void MemBuffer::dealloc() {
if (b != nullptr) {
if (ptr != nullptr) {
debug_set(debug.last_return_address_dealloc, upx_return_address());
checkState();
stats.global_total_active_bytes -= b_size_in_bytes;
stats.global_total_active_bytes -= size_in_bytes;
if (use_simple_mcheck()) {
// clear magic constants
set_ne32(b - 8, 0);
set_ne32(b - 4, 0);
set_ne32(b + b_size_in_bytes, 0);
set_ne32(b + b_size_in_bytes + 4, 0);
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);
//
::free(b - 16);
::free(ptr - 16);
} else
::free(b);
b = nullptr;
b_size_in_bytes = 0;
::free(ptr);
ptr = nullptr;
size_in_bytes = 0;
} else {
assert(b_size_in_bytes == 0);
assert(size_in_bytes == 0);
}
}
@@ -264,9 +258,11 @@ TEST_CASE("MemBuffer") {
CHECK(raw_bytes(mb, 64) == mb.getVoidPtr());
CHECK_THROWS(raw_bytes(mb, 65));
CHECK_NOTHROW(mb + 64);
CHECK_NOTHROW(64 + mb);
CHECK_THROWS(mb + 65);
#if ALLOW_INT_PLUS_MEMBUFFER
CHECK_NOTHROW(64 + mb);
CHECK_THROWS(65 + mb);
#endif
if (use_simple_mcheck()) {
upx_byte *b = raw_bytes(mb, 0);
unsigned magic1 = get_ne32(b - 4);
@@ -280,9 +276,9 @@ TEST_CASE("MemBuffer") {
TEST_CASE("MemBuffer::getSizeForCompression") {
CHECK_THROWS(MemBuffer::getSizeForCompression(0));
CHECK_THROWS(MemBuffer::getSizeForDecompression(0));
CHECK(MemBuffer::getSizeForCompression(1) == 320);
CHECK(MemBuffer::getSizeForCompression(256) == 576);
CHECK(MemBuffer::getSizeForCompression(1024) == 1408);
CHECK(MemBuffer::getSizeForCompression(1) == 513);
CHECK(MemBuffer::getSizeForCompression(256) == 800);
CHECK(MemBuffer::getSizeForCompression(1024) == 1664);
// CHECK(MemBuffer::getSizeForCompression(1024 * 1024) == 0); // TODO
// CHECK(MemBuffer::getSizeForCompression(UPX_RSIZE_MAX) == 0); // TODO
}
+29 -18
View File
@@ -37,18 +37,20 @@ template <class T>
class MemBufferBase {
public:
typedef T element_type;
typedef typename std::add_lvalue_reference<T>::type reference;
typedef typename std::add_pointer<T>::type pointer;
typedef unsigned size_type;
protected:
pointer b;
unsigned b_size_in_bytes;
pointer ptr;
size_type size_in_bytes;
public:
MemBufferBase() : b(nullptr), b_size_in_bytes(0) {}
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
operator pointer() const { return b; }
operator pointer() const { return ptr; }
template <class U>
typename std::enable_if<std::is_integral<U>::value, pointer>::type operator+(U n) const {
@@ -59,18 +61,21 @@ public:
private:
// NOT allowed; use raw_bytes() instead
template <class U>
typename std::enable_if<std::is_integral<U>::value, pointer>::type
operator-(U n) const DELETED_FUNCTION;
typename std::enable_if<std::is_integral<U>::value, pointer>::type operator-(U n) const
DELETED_FUNCTION;
public: // raw access
pointer raw_ptr() const { return ptr; }
size_type raw_size_in_bytes() const { return size_in_bytes; }
public:
pointer raw_bytes(size_t bytes) const {
if (bytes > 0) {
if very_unlikely (b == nullptr)
throwInternalError("MemBuffer raw_bytes unexpected NULL ptr");
if very_unlikely (bytes > b_size_in_bytes)
throwInternalError("MemBuffer raw_bytes invalid size");
if very_unlikely (ptr == nullptr)
throwCantPack("MemBuffer raw_bytes unexpected NULL ptr");
if very_unlikely (bytes > size_in_bytes)
throwCantPack("MemBuffer raw_bytes invalid size");
}
return b;
return ptr;
}
};
@@ -89,19 +94,19 @@ public:
void dealloc();
void checkState() const;
unsigned getSize() const { return b_size_in_bytes; }
unsigned getSize() const { return size_in_bytes; }
// explicit converstion
void *getVoidPtr() { return (void *) b; }
const void *getVoidPtr() const { return (const void *) b; }
void *getVoidPtr() { return (void *) ptr; }
const void *getVoidPtr() const { return (const void *) ptr; }
// 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, b_size_in_bytes, 0); }
forceinline void clear() { fill(0, size_in_bytes, 0); }
// If the entire range [skip, skip+take) is inside the buffer,
// then return &b[skip]; else throwCantPack(sprintf(errfmt, skip, take)).
// 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) {
@@ -130,7 +135,7 @@ private:
Debug debug;
#endif
// disable copy, assignment and move
// disable copy and move
MemBuffer(const MemBuffer &) DELETED_FUNCTION;
MemBuffer &operator=(const MemBuffer &) DELETED_FUNCTION;
#if __cplusplus >= 201103L
@@ -159,11 +164,17 @@ inline typename MemBufferBase<T>::pointer raw_index_bytes(const MemBufferBase<T>
}
// global operators
#if ALLOW_INT_PLUS_MEMBUFFER
// rewrite "n + membuffer" to "membuffer + n" so that this will get checked above
template <class T, class U>
inline typename std::enable_if<std::is_integral<U>::value, typename MemBufferBase<T>::pointer>::type
operator+(U n, const MemBufferBase<T> &mbb) {
return mbb + n;
}
#else
template <class T, class U>
inline typename std::enable_if<std::is_integral<U>::value, typename MemBufferBase<T>::pointer>::type
operator+(U n, const MemBufferBase<T> &mbb) DELETED_FUNCTION;
#endif
/* vim:set ts=4 sw=4 et: */
+3 -1
View File
@@ -47,7 +47,9 @@
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(UPX_RSIZE_MAX >= 65536 * 8192)
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)
upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1,
upx_uint64_t extra2) {
+1 -2
View File
@@ -161,8 +161,7 @@ inline R *xspan_make_helper__(R * /*dummy*/, MemBuffer &first) {
/*************************************************************************
// raw_bytes() - get underlying memory from checked buffers/pointers.
// This is overloaded by various utility classes like BoundedPtr,
// MemBuffer and XSpan.
// This is overloaded by various utility classes like MemBuffer and XSpan.
//
// Note that the pointer type is retained, the "_bytes" hints size_in_bytes
**************************************************************************/
+4
View File
@@ -31,6 +31,10 @@
template <class T, class U> \
inline XSPAN_REQUIRES_CONVERTIBLE_ANY_DIRECTION(T, U, RType)
template <class T, class U>
inline typename std::enable_if<std::is_integral<U>::value, void *>::type operator+(U, const C<T> &)
DELETED_FUNCTION;
/*************************************************************************
// overloads for standard functions
**************************************************************************/