src: use 'byte' instead of 'upx_byte'; NFCI

This commit is contained in:
Markus F.X.J. Oberhumer
2023-02-21 19:27:23 +01:00
parent 4a8efd2e2f
commit e1edc5f50a
50 changed files with 535 additions and 513 deletions
+4 -4
View File
@@ -181,7 +181,7 @@ void MemBuffer::checkState() const {
throwInternalError("block not allocated");
assert(size_in_bytes > 0);
if (use_simple_mcheck()) {
const unsigned char *p = (const unsigned char *) ptr;
const byte *p = (const byte *) ptr;
if (get_ne32(p - 4) != MAGIC1(p))
throwInternalError("memory clobbered before allocated block 1");
if (get_ne32(p - 8) != size_in_bytes)
@@ -201,7 +201,7 @@ void MemBuffer::alloc(upx_uint64_t bytes) {
size_t malloc_bytes = mem_size(1, bytes);
if (use_simple_mcheck())
malloc_bytes += 32;
unsigned char *p = (unsigned char *) ::malloc(malloc_bytes);
byte *p = (byte *) ::malloc(malloc_bytes);
NO_printf("MemBuffer::alloc %llu: %p\n", bytes, p);
if (!p)
throwOutOfMemoryException();
@@ -234,7 +234,7 @@ void MemBuffer::dealloc() {
stats.global_dealloc_counter += 1;
stats.global_total_active_bytes -= size_in_bytes;
if (use_simple_mcheck()) {
unsigned char *p = (unsigned char *) ptr;
byte *p = (byte *) ptr;
// clear magic constants
set_ne32(p - 8, 0);
set_ne32(p - 4, 0);
@@ -278,7 +278,7 @@ TEST_CASE("MemBuffer") {
CHECK_THROWS(mb.subref("", 1, 64));
CHECK_THROWS(mb.subref("", 64, 1));
if (use_simple_mcheck()) {
unsigned char *p = raw_bytes(mb, 0);
byte *p = raw_bytes(mb, 0);
unsigned magic1 = get_ne32(p - 4);
set_ne32(p - 4, magic1 ^ 1);
CHECK_THROWS(mb.checkState());
+2 -2
View File
@@ -80,9 +80,9 @@ public: // raw access
}
};
class MemBuffer final : public MemBufferBase<unsigned char> {
class MemBuffer final : public MemBufferBase<byte> {
public:
MemBuffer() : MemBufferBase<unsigned char>() {}
MemBuffer() : MemBufferBase<byte>() {}
explicit MemBuffer(upx_uint64_t bytes);
~MemBuffer();
+81
View File
@@ -0,0 +1,81 @@
/* xspan -- a minimally invasive checked memory smart pointer
This file is part of the UPX executable compressor.
Copyright (C) 1996-2023 Markus Franz Xaver Johannes Oberhumer
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
<markus@oberhumer.com>
*/
#pragma once
/*************************************************************************
// raw_bytes() - get underlying memory from checked buffers/pointers.
// This is overloaded by various utility classes like MemBuffer and XSpan.
//
// Note that the pointer type is retained, the "_bytes" hints size_in_bytes
**************************************************************************/
// default: for any regular pointer, raw_bytes() is just the pointer itself
template <class T>
inline
typename std::enable_if<std::is_pointer<T>::value && !std_is_bounded_array<T>::value, T>::type
raw_bytes(T ptr, size_t size_in_bytes) {
if (size_in_bytes > 0) {
if very_unlikely (ptr == nullptr)
throwCantPack("raw_bytes unexpected NULL ptr");
if very_unlikely (__acc_cte(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, size_in_bytes) != 0))
throwCantPack("raw_bytes valgrind-check-mem");
}
return ptr;
}
// default: for any regular pointer, raw_index_bytes() is just "pointer + index"
// NOTE: index == number of elements, *NOT* size in bytes!
template <class T>
inline
typename std::enable_if<std::is_pointer<T>::value && !std_is_bounded_array<T>::value, T>::type
raw_index_bytes(T ptr, size_t index, size_t size_in_bytes) {
typedef typename std::remove_pointer<T>::type element_type;
if very_unlikely (ptr == nullptr)
throwCantPack("raw_index_bytes unexpected NULL ptr");
size_in_bytes = mem_size(sizeof(element_type), index, size_in_bytes); // assert size
if very_unlikely (__acc_cte(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, size_in_bytes) != 0))
throwCantPack("raw_index_bytes valgrind-check-mem");
UNUSED(size_in_bytes);
return ptr + index;
}
// same for bounded arrays
template <class T, size_t N>
inline T *raw_bytes(T (&a)[N], size_t size_in_bytes) {
typedef T element_type;
if very_unlikely (size_in_bytes > mem_size(sizeof(element_type), N))
throwCantPack("raw_bytes out of range");
return a;
}
template <class T, size_t N>
inline T *raw_index_bytes(T (&a)[N], size_t index, size_t size_in_bytes) {
typedef T element_type;
return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
}
/* vim:set ts=4 sw=4 et: */
+10 -10
View File
@@ -261,8 +261,8 @@ int find(const void *buf, int blen, const void *what, int wlen) {
if (buf == nullptr || blen <= 0 || what == nullptr || wlen <= 0)
return -1;
const unsigned char *b = (const unsigned char *) buf;
unsigned char first_byte = *(const unsigned char *) what;
const byte *b = (const byte *) buf;
byte first_byte = *(const byte *) what;
blen -= wlen;
for (int i = 0; i <= blen; i++, b++)
@@ -273,44 +273,44 @@ int find(const void *buf, int blen, const void *what, int wlen) {
}
int find_be16(const void *b, int blen, unsigned what) {
unsigned char w[2];
byte w[2];
set_be16(w, what);
return find(b, blen, w, 2);
}
int find_be32(const void *b, int blen, unsigned what) {
unsigned char w[4];
byte w[4];
set_be32(w, what);
return find(b, blen, w, 4);
}
int find_be64(const void *b, int blen, upx_uint64_t what) {
unsigned char w[8];
byte w[8];
set_be64(w, what);
return find(b, blen, w, 8);
}
int find_le16(const void *b, int blen, unsigned what) {
unsigned char w[2];
byte w[2];
set_le16(w, what);
return find(b, blen, w, 2);
}
int find_le32(const void *b, int blen, unsigned what) {
unsigned char w[4];
byte w[4];
set_le32(w, what);
return find(b, blen, w, 4);
}
int find_le64(const void *b, int blen, upx_uint64_t what) {
unsigned char w[8];
byte w[8];
set_le64(w, what);
return find(b, blen, w, 8);
}
TEST_CASE("find") {
CHECK(find(nullptr, -1, nullptr, -1) == -1);
static const unsigned char b[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
static const byte b[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
CHECK(find(b, 16, b, 0) == -1);
for (int i = 0; i < 16; i++) {
CHECK(find(b, 16, b + i, 1) == i);
@@ -330,7 +330,7 @@ TEST_CASE("find") {
}
int mem_replace(void *buf, int blen, const void *what, int wlen, const void *replacement) {
unsigned char *b = (unsigned char *) buf;
byte *b = (byte *) buf;
int boff = 0;
int n = 0;
+6 -6
View File
@@ -46,28 +46,28 @@ static XSpanStats xspan_stats;
// HINT: set env-var "UPX_DEBUG_DOCTEST_DISABLE=1" for improved debugging experience
noinline void xspan_fail_nullptr() {
xspan_stats.fail_nullptr += 1;
throwCantUnpack("xspan unexpected NULL pointer; take care!");
throwCantPack("xspan unexpected NULL pointer; take care!");
}
noinline void xspan_fail_nullbase() {
xspan_stats.fail_nullbase += 1;
throwCantUnpack("xspan unexpected NULL base; take care!");
throwCantPack("xspan unexpected NULL base; take care!");
}
noinline void xspan_fail_not_same_base() {
xspan_stats.fail_not_same_base += 1;
throwInternalError("xspan unexpected base pointer; take care!");
throwCantPack("xspan unexpected base pointer; take care!");
}
noinline void xspan_fail_range_nullptr() {
xspan_stats.fail_range_nullptr += 1;
throwCantUnpack("xspan_check_range: unexpected NULL pointer; take care!");
throwCantPack("xspan_check_range: unexpected NULL pointer; take care!");
}
noinline void xspan_fail_range_nullbase() {
xspan_stats.fail_range_nullbase += 1;
throwCantUnpack("xspan_check_range: unexpected NULL base; take care!");
throwCantPack("xspan_check_range: unexpected NULL base; take care!");
}
noinline void xspan_fail_range_range() {
xspan_stats.fail_range_range += 1;
throwCantUnpack("xspan_check_range: pointer out of range; take care!");
throwCantPack("xspan_check_range: pointer out of range; take care!");
}
void xspan_check_range(const void *p, const void *base, ptrdiff_t size_in_bytes) {
-52
View File
@@ -159,56 +159,4 @@ inline R *xspan_make_helper__(R * /*dummy*/, MemBuffer &first) {
#define SPAN_S_VAR XSPAN_S_VAR
#endif
/*************************************************************************
// raw_bytes() - get underlying memory from checked buffers/pointers.
// This is overloaded by various utility classes like MemBuffer and XSpan.
//
// Note that the pointer type is retained, the "_bytes" hints size_in_bytes
**************************************************************************/
// default: for any regular pointer, raw_bytes() is just the pointer itself
template <class T>
inline
typename std::enable_if<std::is_pointer<T>::value && !std_is_bounded_array<T>::value, T>::type
raw_bytes(T ptr, size_t size_in_bytes) {
if (size_in_bytes > 0) {
if very_unlikely (ptr == nullptr)
throwInternalError("raw_bytes unexpected NULL ptr");
if very_unlikely (__acc_cte(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, size_in_bytes) != 0))
throwInternalError("raw_bytes valgrind-check-mem");
}
return ptr;
}
// default: for any regular pointer, raw_index_bytes() is just "pointer + index"
// NOTE: index == number of elements, *NOT* size in bytes!
template <class T>
inline
typename std::enable_if<std::is_pointer<T>::value && !std_is_bounded_array<T>::value, T>::type
raw_index_bytes(T ptr, size_t index, size_t size_in_bytes) {
typedef typename std::remove_pointer<T>::type element_type;
if very_unlikely (ptr == nullptr)
throwInternalError("raw_index_bytes unexpected NULL ptr");
size_in_bytes = mem_size(sizeof(element_type), index, size_in_bytes); // assert size
if very_unlikely (__acc_cte(VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, size_in_bytes) != 0))
throwInternalError("raw_index_bytes valgrind-check-mem");
UNUSED(size_in_bytes);
return ptr + index;
}
// same for bounded arrays
template <class T, size_t N>
inline T *raw_bytes(T (&a)[N], size_t size_in_bytes) {
typedef T element_type;
if very_unlikely (size_in_bytes > mem_size(sizeof(element_type), N))
throwInternalError("raw_bytes out of range");
return a;
}
template <class T, size_t N>
inline T *raw_index_bytes(T (&a)[N], size_t index, size_t size_in_bytes) {
typedef T element_type;
return raw_bytes(a, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
}
/* vim:set ts=4 sw=4 et: */
+8 -2
View File
@@ -94,9 +94,15 @@ forceinline pointer ensureBase() const {
}
public:
inline ~CSelf() {}
void destroy() {
inline ~CSelf() {
#if DEBUG
invalidate();
#endif
}
noinline void invalidate() {
assertInvariants();
ptr = (pointer) (acc_uintptr_t) 16; // point to non-null invalid address
// ptr = (pointer) (void *) &ptr; // point to self
base = ptr;
size_in_bytes = 0;
assertInvariants();
+11 -1
View File
@@ -62,7 +62,17 @@ public:
operator pointer() const { return ptr; }
#endif
inline ~CSelf() {}
inline ~CSelf() {
#if DEBUG
invalidate();
#endif
}
noinline void invalidate() {
assertInvariants();
ptr = (pointer) (acc_uintptr_t) 16; // point to non-null invalid address
// ptr = (pointer) (void *) &ptr; // point to self
assertInvariants();
}
inline CSelf() { assertInvariants(); }
// constructors from pointers