all: more assorted cleanups; NFCI

This commit is contained in:
Markus F.X.J. Oberhumer
2024-02-02 10:17:16 +01:00
parent d429801498
commit 718ec468ff
26 changed files with 307 additions and 125 deletions
+71 -24
View File
@@ -15,8 +15,8 @@ env:
DEBIAN_FRONTEND: noninteractive DEBIAN_FRONTEND: noninteractive
UPX_CMAKE_BUILD_FLAGS: --verbose UPX_CMAKE_BUILD_FLAGS: --verbose
UPX_CMAKE_CONFIG_FLAGS: -Wdev --warn-uninitialized UPX_CMAKE_CONFIG_FLAGS: -Wdev --warn-uninitialized
# 2024-01-24 # 2024-01-25
ZIG_DIST_VERSION: 0.12.0-dev.2334+aef1da163 ZIG_DIST_VERSION: 0.12.0-dev.2341+92211135f
jobs: jobs:
job-rebuild-and-verify-stubs: job-rebuild-and-verify-stubs:
@@ -78,7 +78,7 @@ jobs:
run: | run: |
sudo dpkg --add-architecture i386 sudo dpkg --add-architecture i386
sudo apt-get update sudo apt-get update
sudo apt-get install -y g++-multilib g++-mingw-w64-i686 g++-mingw-w64-x86-64 sudo apt-get install -y g++-multilib g++-mingw-w64-i686 g++-mingw-w64-x86-64 valgrind
# make sure that we use posix-threads (pthread/winpthreads) and NOT win32-threads # make sure that we use posix-threads (pthread/winpthreads) and NOT win32-threads
for f in i686-w64-mingw32-g++ i686-w64-mingw32-gcc x86_64-w64-mingw32-g++ x86_64-w64-mingw32-gcc; do for f in i686-w64-mingw32-g++ i686-w64-mingw32-gcc x86_64-w64-mingw32-g++ x86_64-w64-mingw32-gcc; do
if test -f /usr/bin/$f-posix; then sudo update-alternatives --set $f /usr/bin/$f-posix; fi if test -f /usr/bin/$f-posix; then sudo update-alternatives --set $f /usr/bin/$f-posix; fi
@@ -123,8 +123,8 @@ jobs:
path: tmp/artifact path: tmp/artifact
- name: 'Run install tests' - name: 'Run install tests'
run: | run: |
(cd build/extra/gcc/release && DESTDIR=$PWD/Install-with-cmake cmake --install .) (cd build/extra/gcc/release && DESTDIR="$PWD/Install with cmake" cmake --install .)
(cd build/extra/gcc/release && DESTDIR=$PWD/Install-with-make make install) (cd build/extra/gcc/release && DESTDIR="$PWD/Install with make" make install)
- name: 'Run ctest tests' - name: 'Run ctest tests'
run: | run: |
make -C build/extra/gcc/debug test make -C build/extra/gcc/debug test
@@ -147,8 +147,29 @@ jobs:
run: | run: |
env -C build/extra/gcc-m32/debug bash "$PWD"/misc/testsuite/mimic_ctest.sh env -C build/extra/gcc-m32/debug bash "$PWD"/misc/testsuite/mimic_ctest.sh
env -C build/extra/gcc-m32/release bash "$PWD"/misc/testsuite/mimic_ctest.sh env -C build/extra/gcc-m32/release bash "$PWD"/misc/testsuite/mimic_ctest.sh
- name: 'Mimic ctest tests with Valgrind'
if: false # TODO later: valgrind problem/bug
run: |
if command -v valgrind >/dev/null; then
export upx_exe_runner="valgrind --leak-check=no --error-exitcode=1 --quiet"
env -C build/extra/gcc/debug bash "$PWD"/misc/testsuite/mimic_ctest.sh
env -C build/extra/gcc/release bash "$PWD"/misc/testsuite/mimic_ctest.sh
env -C build/extra/clang/debug bash "$PWD"/misc/testsuite/mimic_ctest.sh
env -C build/extra/clang/release bash "$PWD"/misc/testsuite/mimic_ctest.sh
fi
- name: 'Mimic ctest tests 32-bit with Valgrind'
if: ${{ matrix.use_m32 && false }} # TODO later: valgrind problem/bug
run: |
if command -v valgrind >/dev/null; then
export upx_exe_runner="valgrind --leak-check=no --error-exitcode=1 --quiet"
env -C build/extra/gcc-m32/debug bash "$PWD"/misc/testsuite/mimic_ctest.sh
env -C build/extra/gcc-m32/release bash "$PWD"/misc/testsuite/mimic_ctest.sh
fi
- name: 'Run file system test suite' - name: 'Run file system test suite'
run: | run: |
if command -v valgrind >/dev/null; then
export upx_exe_runner="valgrind --leak-check=no --error-exitcode=1 --quiet"
fi
env -C build/extra/gcc/release bash "$PWD"/misc/testsuite/test_symlinks.sh env -C build/extra/gcc/release bash "$PWD"/misc/testsuite/test_symlinks.sh
- name: 'Run test suite build/extra/gcc/release' - name: 'Run test suite build/extra/gcc/release'
run: | run: |
@@ -220,8 +241,8 @@ jobs:
path: tmp/artifact path: tmp/artifact
- name: 'Run install tests' - name: 'Run install tests'
run: | run: |
(cd build/extra/clang/release && DESTDIR=$PWD/Install-with-cmake cmake --install .) (cd build/extra/clang/release && DESTDIR="$PWD/Install with cmake" cmake --install .)
(cd build/extra/clang/release && DESTDIR=$PWD/Install-with-make make install) (cd build/extra/clang/release && DESTDIR="$PWD/Install with make" make install)
- name: 'Run ctest tests' - name: 'Run ctest tests'
if: ${{ !contains(matrix.os, 'macos-13') }} # FIXME: UPX on macos-13 is broken => disable self-test for now if: ${{ !contains(matrix.os, 'macos-13') }} # FIXME: UPX on macos-13 is broken => disable self-test for now
run: | run: |
@@ -345,7 +366,7 @@ jobs:
set RUN_CL=cl ${{ matrix.cl_machine_flags }} -MT set RUN_CL=cl ${{ matrix.cl_machine_flags }} -MT
set RUN_LIB=link -lib ${{ matrix.link_machine_flags }} set RUN_LIB=link -lib ${{ matrix.link_machine_flags }}
@rem UPX only uses the very basic Windows API @rem UPX only uses the very basic Windows API
set DEFS=-D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0400 set DEFS=-D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0501
set BDIR=%H%\build\%C%\%B% set BDIR=%H%\build\%C%\%B%
git rev-parse --short=12 HEAD > %BDIR%\upx\.GITREV.txt git rev-parse --short=12 HEAD > %BDIR%\upx\.GITREV.txt
@REM ===== build bzip2 ===== @REM ===== build bzip2 =====
@@ -415,31 +436,31 @@ jobs:
needs: [ job-rebuild-and-verify-stubs ] needs: [ job-rebuild-and-verify-stubs ]
name: ${{ format('zigcc {0} {1}', matrix.zig_target, matrix.zig_pic) }} name: ${{ format('zigcc {0} {1}', matrix.zig_target, matrix.zig_pic) }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
container: 'alpine:3.19' # older versions such as alpine:3.12 also work; no-container also works container: 'alpine:3.19'
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
include: include:
# only build a few targets => more targets are tested in the Weekly CI # only build a few targets => more targets are tested in the Weekly CI
- { zig_target: aarch64-linux-musl } - { zig_target: aarch64-linux-musl, qemu: qemu-aarch64 }
# { zig_target: aarch64-linux-musl, zig_pic: -fPIE } # { zig_target: aarch64-linux-musl, qemu: qemu-aarch64, zig_pic: -fPIE }
# { zig_target: aarch64-macos-none } # { zig_target: aarch64-macos-none }
- { zig_target: aarch64-macos.11.0-none } - { zig_target: aarch64-macos.11.0-none }
# { zig_target: aarch64-macos.12.0-none } # { zig_target: aarch64-macos.12.0-none }
# { zig_target: aarch64-macos.13.0-none } # { zig_target: aarch64-macos.13.0-none }
- { zig_target: aarch64-windows-gnu } - { zig_target: aarch64-windows-gnu }
- { zig_target: arm-linux-musleabihf } - { zig_target: arm-linux-musleabihf, qemu: qemu-arm }
# { zig_target: arm-linux-musleabihf, zig_pic: -fPIE } # { zig_target: arm-linux-musleabihf, qemu: qemu-arm, zig_pic: -fPIE }
- { zig_target: i386-linux-musl } - { zig_target: i386-linux-musl, qemu: qemu-i386 }
# { zig_target: i386-linux-musl, zig_pic: -fPIE } # { zig_target: i386-linux-musl, qemu: qemu-i386, zig_pic: -fPIE }
- { zig_target: i386-windows-gnu } - { zig_target: i386-windows-gnu }
# { zig_target: mips-linux-musl } - { zig_target: mips-linux-musl }
# { zig_target: mipsel-linux-musl } - { zig_target: mipsel-linux-musl }
# { zig_target: powerpc-linux-musl } - { zig_target: powerpc-linux-musl, qemu: qemu-ppc }
# { zig_target: powerpc64-linux-musl } - { zig_target: powerpc64-linux-musl, qemu: qemu-ppc64 }
# { zig_target: powerpc64le-linux-musl } - { zig_target: powerpc64le-linux-musl, qemu: qemu-ppc64le }
- { zig_target: x86_64-linux-musl } - { zig_target: x86_64-linux-musl, qemu: qemu-x86_64 }
# { zig_target: x86_64-linux-musl, zig_pic: -fPIE } # { zig_target: x86_64-linux-musl, qemu: qemu-x86_64, zig_pic: -fPIE }
# { zig_target: x86_64-macos-none } # { zig_target: x86_64-macos-none }
- { zig_target: x86_64-macos.11.0-none } - { zig_target: x86_64-macos.11.0-none }
# { zig_target: x86_64-macos.12.0-none } # { zig_target: x86_64-macos.12.0-none }
@@ -523,5 +544,31 @@ jobs:
- name: 'Run install tests' - name: 'Run install tests'
if: ${{ contains(matrix.zig_target, '-linux') }} if: ${{ contains(matrix.zig_target, '-linux') }}
run: | run: |
(cd build/zig/${ZIG_TARGET}${ZIG_PIC}/release && DESTDIR=$PWD/Install-with-cmake cmake --install .) (cd build/zig/${ZIG_TARGET}${ZIG_PIC}/release && DESTDIR="$PWD/Install with cmake" cmake --install .)
(cd build/zig/${ZIG_TARGET}${ZIG_PIC}/release && DESTDIR=$PWD/Install-with-make make install) (cd build/zig/${ZIG_TARGET}${ZIG_PIC}/release && DESTDIR="$PWD/Install with make" make install)
- name: 'Run ctest tests'
if: ${{ matrix.zig_target == 'i386-linux-musl' || matrix.zig_target == 'x86_64-linux-musl' }}
run: |
make -C build/zig/${ZIG_TARGET}${ZIG_PIC}/debug test
make -C build/zig/${ZIG_TARGET}${ZIG_PIC}/release test
- name: 'Mimic ctest tests'
if: ${{ matrix.zig_target == 'i386-linux-musl' || matrix.zig_target == 'x86_64-linux-musl' }}
run: |
apk add coreutils
env -C build/zig/${ZIG_TARGET}${ZIG_PIC}/debug bash "$PWD"/misc/testsuite/mimic_ctest.sh
env -C build/zig/${ZIG_TARGET}${ZIG_PIC}/release bash "$PWD"/misc/testsuite/mimic_ctest.sh
- name: ${{ format('Mimic ctest tests with QEMU {0}', matrix.qemu) }}
if: ${{ matrix.qemu }}
run: |
qemu="${{ matrix.qemu }}"
apk add coreutils $qemu
export upx_exe_runner="$qemu"
env -C build/zig/${ZIG_TARGET}${ZIG_PIC}/debug bash "$PWD"/misc/testsuite/mimic_ctest.sh
env -C build/zig/${ZIG_TARGET}${ZIG_PIC}/release bash "$PWD"/misc/testsuite/mimic_ctest.sh
- name: 'Mimic ctest tests with Valgrind'
if: ${{ matrix.zig_target == 'x86_64-linux-musl' && false }} # TODO later: valgrind problem/bug
run: |
apk add coreutils valgrind
export upx_exe_runner="valgrind --leak-check=no --error-exitcode=1 --quiet"
env -C build/zig/${ZIG_TARGET}${ZIG_PIC}/debug bash "$PWD"/misc/testsuite/mimic_ctest.sh
env -C build/zig/${ZIG_TARGET}${ZIG_PIC}/release bash "$PWD"/misc/testsuite/mimic_ctest.sh
+8 -4
View File
@@ -143,17 +143,19 @@ build/extra/cross-linux-gnu-arm-eabihf/%: CMAKE_CROSSCOMPILING_EMULATOR ?= qemu-
# cross compiler: Windows x86 win32 MinGW (i386) # cross compiler: Windows x86 win32 MinGW (i386)
build/extra/cross-windows-mingw32/debug: PHONY; $(call run_config_and_build,$@,Debug) build/extra/cross-windows-mingw32/debug: PHONY; $(call run_config_and_build,$@,Debug)
build/extra/cross-windows-mingw32/release: PHONY; $(call run_config_and_build,$@,Release) build/extra/cross-windows-mingw32/release: PHONY; $(call run_config_and_build,$@,Release)
build/extra/cross-windows-mingw32/%: export CC = i686-w64-mingw32-gcc -static -D_WIN32_WINNT=0x0400 build/extra/cross-windows-mingw32/%: export CC = i686-w64-mingw32-gcc -static -D_WIN32_WINNT=0x0501
build/extra/cross-windows-mingw32/%: export CXX = i686-w64-mingw32-g++ -static -D_WIN32_WINNT=0x0400 build/extra/cross-windows-mingw32/%: export CXX = i686-w64-mingw32-g++ -static -D_WIN32_WINNT=0x0501
build/extra/cross-windows-mingw32/%: CMAKE_SYSTEM_NAME ?= Windows build/extra/cross-windows-mingw32/%: CMAKE_SYSTEM_NAME ?= Windows
build/extra/cross-windows-mingw32/%: CMAKE_SYSTEM_PROCESSOR ?= X86
build/extra/cross-windows-mingw32/%: CMAKE_CROSSCOMPILING_EMULATOR ?= wine build/extra/cross-windows-mingw32/%: CMAKE_CROSSCOMPILING_EMULATOR ?= wine
# cross compiler: Windows x64 win64 MinGW (amd64) # cross compiler: Windows x64 win64 MinGW (amd64)
build/extra/cross-windows-mingw64/debug: PHONY; $(call run_config_and_build,$@,Debug) build/extra/cross-windows-mingw64/debug: PHONY; $(call run_config_and_build,$@,Debug)
build/extra/cross-windows-mingw64/release: PHONY; $(call run_config_and_build,$@,Release) build/extra/cross-windows-mingw64/release: PHONY; $(call run_config_and_build,$@,Release)
build/extra/cross-windows-mingw64/%: export CC = x86_64-w64-mingw32-gcc -static -D_WIN32_WINNT=0x0400 build/extra/cross-windows-mingw64/%: export CC = x86_64-w64-mingw32-gcc -static -D_WIN32_WINNT=0x0501
build/extra/cross-windows-mingw64/%: export CXX = x86_64-w64-mingw32-g++ -static -D_WIN32_WINNT=0x0400 build/extra/cross-windows-mingw64/%: export CXX = x86_64-w64-mingw32-g++ -static -D_WIN32_WINNT=0x0501
build/extra/cross-windows-mingw64/%: CMAKE_SYSTEM_NAME ?= Windows build/extra/cross-windows-mingw64/%: CMAKE_SYSTEM_NAME ?= Windows
build/extra/cross-windows-mingw64/%: CMAKE_SYSTEM_PROCESSOR ?= AMD64
build/extra/cross-windows-mingw64/%: CMAKE_CROSSCOMPILING_EMULATOR ?= wine64 build/extra/cross-windows-mingw64/%: CMAKE_CROSSCOMPILING_EMULATOR ?= wine64
# cross compiler: macOS arm64 (aarch64) # cross compiler: macOS arm64 (aarch64)
@@ -162,6 +164,7 @@ build/extra/cross-darwin-arm64/release: PHONY; $(call run_config_and_build,$@,Re
build/extra/cross-darwin-arm64/%: export CC = clang -target arm64-apple-darwin build/extra/cross-darwin-arm64/%: export CC = clang -target arm64-apple-darwin
build/extra/cross-darwin-arm64/%: export CXX = clang++ -target arm64-apple-darwin build/extra/cross-darwin-arm64/%: export CXX = clang++ -target arm64-apple-darwin
build/extra/cross-darwin-arm64/%: CMAKE_SYSTEM_NAME ?= Darwin build/extra/cross-darwin-arm64/%: CMAKE_SYSTEM_NAME ?= Darwin
build/extra/cross-darwin-arm64/%: CMAKE_SYSTEM_PROCESSOR ?= arm64
# cross compiler: macOS x86_64 (amd64) # cross compiler: macOS x86_64 (amd64)
build/extra/cross-darwin-x86_64/debug: PHONY; $(call run_config_and_build,$@,Debug) build/extra/cross-darwin-x86_64/debug: PHONY; $(call run_config_and_build,$@,Debug)
@@ -169,6 +172,7 @@ build/extra/cross-darwin-x86_64/release: PHONY; $(call run_config_and_build,$@,R
build/extra/cross-darwin-x86_64/%: export CC = clang -target x86_64-apple-darwin build/extra/cross-darwin-x86_64/%: export CC = clang -target x86_64-apple-darwin
build/extra/cross-darwin-x86_64/%: export CXX = clang++ -target x86_64-apple-darwin build/extra/cross-darwin-x86_64/%: export CXX = clang++ -target x86_64-apple-darwin
build/extra/cross-darwin-x86_64/%: CMAKE_SYSTEM_NAME ?= Darwin build/extra/cross-darwin-x86_64/%: CMAKE_SYSTEM_NAME ?= Darwin
build/extra/cross-darwin-x86_64/%: CMAKE_SYSTEM_PROCESSOR ?= x86_64
#*********************************************************************** #***********************************************************************
# C/C++ static analyzers # C/C++ static analyzers
+1 -1
View File
@@ -603,7 +603,7 @@ TEST_CASE("ptr_invalidate_and_poison") {
ptr_invalidate_and_poison(ip); ptr_invalidate_and_poison(ip);
assert(ip != nullptr); assert(ip != nullptr);
(void) ip; (void) ip;
double *dp; double *dp; // not initialized
ptr_invalidate_and_poison(dp); ptr_invalidate_and_poison(dp);
assert(dp != nullptr); assert(dp != nullptr);
(void) dp; (void) dp;
+33
View File
@@ -27,6 +27,22 @@
// lots of tests (and probably quite a number of redundant tests) // lots of tests (and probably quite a number of redundant tests)
// modern compilers will optimize away much of this code // modern compilers will optimize away much of this code
// libc++ hardenining
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ + 0 >= 18)
#if DEBUG
#define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_DEBUG
#else
#define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_EXTENSIVE
#endif
#endif
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ + 0 < 18)
#if DEBUG
#define _LIBCPP_ENABLE_ASSERTIONS 1
#endif
#endif
#include "../headers.h"
#include <vector>
#include "../conf.h" #include "../conf.h"
/************************************************************************* /*************************************************************************
@@ -103,6 +119,23 @@ ACC_COMPILE_TIME_ASSERT_HEADER(!compile_time::string_gt("abc", "abz"))
ACC_COMPILE_TIME_ASSERT_HEADER(!compile_time::string_ge("abc", "abz")) ACC_COMPILE_TIME_ASSERT_HEADER(!compile_time::string_ge("abc", "abz"))
ACC_COMPILE_TIME_ASSERT_HEADER(compile_time::string_le("abc", "abz")) ACC_COMPILE_TIME_ASSERT_HEADER(compile_time::string_le("abc", "abz"))
/*************************************************************************
//
**************************************************************************/
TEST_CASE("libc++") {
constexpr size_t N = 16;
std::vector<int> v(N);
CHECK(v.end() - v.begin() == N);
CHECK(&v[0] == &(*(v.begin())));
// CHECK(&v[0] + N == &(*(v.end()))); // TODO later: is this legal??
#if defined(_LIBCPP_HARDENING_MODE_DEBUG) && \
(_LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG)
CHECK_THROWS((void) &v[N]);
#endif
UNUSED(v);
}
/************************************************************************* /*************************************************************************
// UPX_CXX_DISABLE_xxx // UPX_CXX_DISABLE_xxx
**************************************************************************/ **************************************************************************/
+14
View File
@@ -28,6 +28,20 @@
// doctest support code implementation // doctest support code implementation
**************************************************************************/ **************************************************************************/
// libc++ hardenining
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ + 0 >= 18)
#if DEBUG
#define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_DEBUG
#else
#define _LIBCPP_HARDENING_MODE _LIBCPP_HARDENING_MODE_EXTENSIVE
#endif
#endif
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ + 0 < 18)
#if DEBUG
#define _LIBCPP_ENABLE_ASSERTIONS 1
#endif
#endif
#if defined(__has_include) #if defined(__has_include)
#if __has_include(<features.h>) #if __has_include(<features.h>)
#include <features.h> // for __GLIBC__ #include <features.h> // for __GLIBC__
+27
View File
@@ -241,6 +241,33 @@ TEST_CASE("basic xspan usage") {
} }
} }
TEST_CASE("xspan array access") {
constexpr size_t N = 16;
char buf[N];
memset(buf, 0, sizeof(buf));
XSPAN_0_VAR(char, c0, buf, sizeof(buf));
XSPAN_P_VAR(char, cp, buf, sizeof(buf));
XSPAN_S_VAR(char, cs, buf, sizeof(buf));
for (size_t i = 0; i != N; ++i)
c0[i] += 1;
for (size_t i = 0; i != N; ++i)
cp[i] += 1;
for (size_t i = 0; i != N; ++i)
cs[i] += 1;
for (auto ptr = c0; ptr != c0 + N; ++ptr)
*ptr += 1;
for (auto ptr = c0 + 0; ptr < c0 + N; ++ptr)
*ptr += 1;
for (auto ptr = cp; ptr != cp + N; ++ptr)
*ptr += 1;
for (auto ptr = cp + 0; ptr < cp + N; ++ptr)
*ptr += 1;
for (auto ptr = cs; ptr != cs + N; ++ptr)
*ptr += 1;
for (auto ptr = cs + 0; ptr < cs + N; ++ptr)
*ptr += 1;
}
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
+2 -3
View File
@@ -209,6 +209,8 @@ typedef upx_int64_t upx_off_t;
#undef dos #undef dos
#undef large #undef large
#undef linux #undef linux
#undef PAGE_MASK
#undef PAGE_SIZE
#undef small #undef small
#undef SP #undef SP
#undef SS #undef SS
@@ -297,9 +299,6 @@ typedef upx_int64_t upx_off_t;
#define index upx_renamed_index #define index upx_renamed_index
#define outp upx_renamed_outp #define outp upx_renamed_outp
#undef PAGE_MASK
#undef PAGE_SIZE
#if !defined(O_BINARY) || (O_BINARY + 0 == 0) #if !defined(O_BINARY) || (O_BINARY + 0 == 0)
#if (ACC_OS_CYGWIN || ACC_OS_DOS16 || ACC_OS_DOS32 || ACC_OS_EMX || ACC_OS_OS2 || ACC_OS_OS216 || \ #if (ACC_OS_CYGWIN || ACC_OS_DOS16 || ACC_OS_DOS32 || ACC_OS_EMX || ACC_OS_OS2 || ACC_OS_OS216 || \
ACC_OS_WIN16 || ACC_OS_WIN32 || ACC_OS_WIN64) ACC_OS_WIN16 || ACC_OS_WIN32 || ACC_OS_WIN64)
+12 -13
View File
@@ -31,7 +31,7 @@
// //
**************************************************************************/ **************************************************************************/
/*static*/ upx_std_atomic(size_t) Throwable::debug_counter; /*static*/ Throwable::Stats Throwable::stats;
Throwable::Throwable(const char *m, int e, bool w) noexcept : super(), Throwable::Throwable(const char *m, int e, bool w) noexcept : super(),
msg(nullptr), msg(nullptr),
@@ -41,10 +41,10 @@ Throwable::Throwable(const char *m, int e, bool w) noexcept : super(),
msg = strdup(m); msg = strdup(m);
assert_noexcept(msg != nullptr); assert_noexcept(msg != nullptr);
} }
#if 0 NO_fprintf(stderr, "construct exception: %zu %zu %s\n", stats.counter_current,
fprintf(stderr, "construct exception: %s %zu\n", msg, debug_counter); stats.counter_total, (const char *) msg);
debug_counter += 1; stats.counter_current += 1;
#endif stats.counter_total += 1;
} }
Throwable::Throwable(const Throwable &other) noexcept : super(other), Throwable::Throwable(const Throwable &other) noexcept : super(other),
@@ -55,17 +55,16 @@ Throwable::Throwable(const Throwable &other) noexcept : super(other),
msg = strdup(other.msg); msg = strdup(other.msg);
assert_noexcept(msg != nullptr); assert_noexcept(msg != nullptr);
} }
#if 0 NO_fprintf(stderr, "copy construct exception: %zu %zu %s\n", stats.counter_current,
fprintf(stderr, "copy exception: %s %zu\n", msg, debug_counter); stats.counter_total, (const char *) msg);
debug_counter += 1; stats.counter_current += 1;
#endif stats.counter_total += 1;
} }
Throwable::~Throwable() noexcept { Throwable::~Throwable() noexcept {
#if 0 stats.counter_current -= 1;
debug_counter -= 1; NO_fprintf(stderr, "destruct exception: %zu %zu %s\n", stats.counter_current,
fprintf(stderr, "destruct exception: %s %zu\n", msg, debug_counter); stats.counter_total, (const char *) msg);
#endif
upx::owner_free(msg); upx::owner_free(msg);
} }
+6 -1
View File
@@ -61,7 +61,12 @@ private:
UPX_CXX_DISABLE_ADDRESS(Throwable) UPX_CXX_DISABLE_ADDRESS(Throwable)
private: private:
static upx_std_atomic(size_t) debug_counter; // for debugging // static debug stats
struct Stats {
upx_std_atomic(size_t) counter_total;
upx_std_atomic(size_t) counter_current;
};
static Stats stats;
}; };
// Exceptions can/should be caught // Exceptions can/should be caught
+9 -8
View File
@@ -144,8 +144,8 @@ unsigned LeFile::getImageSize() const {
} }
void LeFile::readImage() { void LeFile::readImage() {
soimage = pages * mps; soimage = mem_size(mps, pages); // assert size
if (!soimage) // late detection, but protect against .alloc(0) if (!soimage) // late detection, but protect against .alloc(0)
throwCantPack("no soimage"); throwCantPack("no soimage");
mb_iimage.alloc(soimage); mb_iimage.alloc(soimage);
mb_iimage.clear(); mb_iimage.clear();
@@ -157,7 +157,7 @@ void LeFile::readImage() {
fif->seek(ih.data_pages_offset + exe_offset + fif->seek(ih.data_pages_offset + exe_offset +
(ipm_entries[ic].m * 0x100 + ipm_entries[ic].l - 1) * mps, (ipm_entries[ic].m * 0x100 + ipm_entries[ic].l - 1) * mps,
SEEK_SET); SEEK_SET);
auto bytes = ic != pages - 1 ? mps : ih.bytes_on_last_page; unsigned bytes = ic != pages - 1 ? mps : ih.bytes_on_last_page;
fif->readx(iimage + jc, bytes); fif->readx(iimage + jc, bytes);
} }
jc += mps; jc += mps;
@@ -184,7 +184,7 @@ void LeFile::writeNonResidentNames() {
} }
bool LeFile::readFileHeader() { bool LeFile::readFileHeader() {
#define H(x) get_le16(header + 2 * (x)) #define H(x) get_le16(header + (2 * (x)))
byte header[0x40]; byte header[0x40];
le_offset = exe_offset = 0; le_offset = exe_offset = 0;
int ic; int ic;
@@ -208,7 +208,7 @@ bool LeFile::readFileHeader() {
} else if (memcmp(header, "BW", 2) == 0) // used in dos4gw.exe } else if (memcmp(header, "BW", 2) == 0) // used in dos4gw.exe
le_offset += H(2) * 512 + H(1); le_offset += H(2) * 512 + H(1);
else if (memcmp(header, "LE", 2) == 0) else if (memcmp(header, "LE", 2) == 0)
break; break; // success
else if (memcmp(header, "PMW1", 4) == 0) else if (memcmp(header, "PMW1", 4) == 0)
throwCantPack("already packed with PMWLITE"); throwCantPack("already packed with PMWLITE");
else else
@@ -219,10 +219,11 @@ bool LeFile::readFileHeader() {
fif->seek(le_offset, SEEK_SET); fif->seek(le_offset, SEEK_SET);
fif->readx(&ih, sizeof(ih)); fif->readx(&ih, sizeof(ih));
if (mps < 512 || mps > 2097152 || (mps & (mps - 1)) != 0) if (mps < 512 || mps > 2097152 || (mps & (mps - 1)) != 0)
throwCantPack("file header invalid page size"); throwCantPack("LE file header invalid page size %u", (unsigned) mps);
if (ih.bytes_on_last_page > mps || pages == 0) if (ih.bytes_on_last_page > mps || pages == 0)
throwCantPack("bad file header"); throwCantPack("bad LE file header");
(void) mem_size(mps, pages); // assert size if (!mem_size_valid(mps, pages) || exe_offset > le_offset)
throwCantPack("bad LE file header");
return true; return true;
#undef H #undef H
} }
+1 -4
View File
@@ -223,10 +223,7 @@ protected:
private: private:
// disable copy and move // disable copy and move
LeFile(const LeFile &) DELETED_FUNCTION; UPX_CXX_DISABLE_COPY_MOVE(LeFile)
LeFile &operator=(const LeFile &) DELETED_FUNCTION;
LeFile(LeFile &&) noexcept DELETED_FUNCTION;
LeFile &operator=(LeFile &&) noexcept DELETED_FUNCTION;
}; };
/* vim:set ts=4 sw=4 et: */ /* vim:set ts=4 sw=4 et: */
+1 -1
View File
@@ -142,7 +142,7 @@ struct ElfLinker::Relocation : private noncopyable {
unsigned offset = 0; unsigned offset = 0;
const char *type = nullptr; const char *type = nullptr;
const Symbol *value = nullptr; const Symbol *value = nullptr;
upx_uint64_t add; // used in .rela relocations upx_uint64_t add = 0; // used in .rela relocations
explicit Relocation(const Section *s, unsigned o, const char *t, const Symbol *v, explicit Relocation(const Section *s, unsigned o, const char *t, const Symbol *v,
upx_uint64_t a); upx_uint64_t a);
+5 -5
View File
@@ -82,10 +82,10 @@ static void pr_error(const char *iname, const char *msg, bool is_warning) noexce
bool c = acc_isatty(STDERR_FILENO) ? 1 : 0; bool c = acc_isatty(STDERR_FILENO) ? 1 : 0;
int fg = con_fg(stderr, FG_BRTRED); int fg = con_fg(stderr, FG_BRTRED);
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s: ", progname); upx_safe_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s: ", progname);
pr_print(c, buf); pr_print(c, buf);
//(void)con_fg(stderr,FG_RED); //(void)con_fg(stderr,FG_RED);
snprintf(buf, sizeof(buf), "%s: ", iname); upx_safe_snprintf(buf, sizeof(buf), "%s: ", iname);
pr_print(c, buf); pr_print(c, buf);
//(void)con_fg(stderr,FG_BRTRED); //(void)con_fg(stderr,FG_BRTRED);
pr_print(c, msg); pr_print(c, msg);
@@ -102,13 +102,13 @@ void printErr(const char *iname, const Throwable &e) noexcept {
char buf[1024]; char buf[1024];
size_t l; size_t l;
snprintf(buf, sizeof(buf), "%s", prettyName(typeid(e).name())); upx_safe_snprintf(buf, sizeof(buf), "%s", prettyName(typeid(e).name()));
l = strlen(buf); l = strlen(buf);
if (l < sizeof(buf) && e.getMsg()) if (l < sizeof(buf) && e.getMsg())
snprintf(buf + l, sizeof(buf) - l, ": %s", e.getMsg()); upx_safe_snprintf(buf + l, sizeof(buf) - l, ": %s", e.getMsg());
l = strlen(buf); l = strlen(buf);
if (l < sizeof(buf) && e.getErrno()) { if (l < sizeof(buf) && e.getErrno()) {
snprintf(buf + l, sizeof(buf) - l, ": %s", strerror(e.getErrno())); upx_safe_snprintf(buf + l, sizeof(buf) - l, ": %s", strerror(e.getErrno()));
#if 1 #if 1
// some compilers (e.g. Borland C++) put a trailing '\n' // some compilers (e.g. Borland C++) put a trailing '\n'
// into the strerror() result // into the strerror() result
+2 -2
View File
@@ -39,7 +39,7 @@ std::mutex opt_lock_mutex;
**************************************************************************/ **************************************************************************/
void Options::reset() noexcept { void Options::reset() noexcept {
#define opt ERROR_DO_NOT_USE_opt // protect against using the wrong variable #define opt ERROR_DO_NOT_USE_opt // self-protect against using the wrong variable
Options *const o = this; Options *const o = this;
mem_clear(o); mem_clear(o);
o->crp.reset(); o->crp.reset();
@@ -90,7 +90,7 @@ void Options::reset() noexcept {
**************************************************************************/ **************************************************************************/
TEST_CASE("Options::reset") { TEST_CASE("Options::reset") {
#define opt ERROR_DO_NOT_USE_opt // protect against using the wrong variable #define opt ERROR_DO_NOT_USE_opt // self-protect against using the wrong variable
COMPILE_TIME_ASSERT(std::is_standard_layout<Options>::value) COMPILE_TIME_ASSERT(std::is_standard_layout<Options>::value)
COMPILE_TIME_ASSERT(std::is_nothrow_default_constructible<Options>::value) COMPILE_TIME_ASSERT(std::is_nothrow_default_constructible<Options>::value)
COMPILE_TIME_ASSERT(std::is_trivially_copyable<Options>::value) COMPILE_TIME_ASSERT(std::is_trivially_copyable<Options>::value)
+1 -1
View File
@@ -31,7 +31,7 @@
// dos/com // dos/com
**************************************************************************/ **************************************************************************/
class PackCom : public Packer { class PackCom /*not_final*/ : public Packer {
typedef Packer super; typedef Packer super;
public: public:
+6 -6
View File
@@ -178,9 +178,9 @@ packed_struct(Sym) {
#define WANT_SYM_ENUM 1 #define WANT_SYM_ENUM 1
#include "p_elf_enum.h" #include "p_elf_enum.h"
static unsigned int get_st_bind(unsigned x) { return 0xf & (x >> 4); } static constexpr unsigned get_st_bind(unsigned x) noexcept { return 0xf & (x >> 4); }
static unsigned int get_st_type(unsigned x) { return 0xf & x; } static constexpr unsigned get_st_type(unsigned x) noexcept { return 0xf & x; }
static unsigned char make_st_info(unsigned bind, unsigned type) { static constexpr unsigned char make_st_info(unsigned bind, unsigned type) noexcept {
return (unsigned char) (((bind << 4) + (0xf & type)) & 0xff); return (unsigned char) (((bind << 4) + (0xf & type)) & 0xff);
} }
}; };
@@ -252,9 +252,9 @@ packed_struct(Sym) {
#define WANT_SYM_ENUM 1 #define WANT_SYM_ENUM 1
#include "p_elf_enum.h" #include "p_elf_enum.h"
static unsigned int get_st_bind(unsigned x) { return 0xf & (x >> 4); } static constexpr unsigned get_st_bind(unsigned x) noexcept { return 0xf & (x >> 4); }
static unsigned int get_st_type(unsigned x) { return 0xf & x; } static constexpr unsigned get_st_type(unsigned x) noexcept { return 0xf & x; }
static unsigned char make_st_info(unsigned bind, unsigned type) { static constexpr unsigned char make_st_info(unsigned bind, unsigned type) noexcept {
return (unsigned char) (((bind << 4) + (0xf & type)) & 0xff); return (unsigned char) (((bind << 4) + (0xf & type)) & 0xff);
} }
}; };
+2 -2
View File
@@ -36,7 +36,7 @@ class UiPacker;
class Filter; class Filter;
/************************************************************************* /*************************************************************************
// PackerBase: purely abstract minimal base class for all packers // PackerBase: abstract minimal base class for all packers
// //
// clients: PackMaster, UiPacker // clients: PackMaster, UiPacker
**************************************************************************/ **************************************************************************/
@@ -80,7 +80,7 @@ protected:
InputFile *const fi; // reference InputFile *const fi; // reference
union { // unnamed union union { // unnamed union
const upx_int64_t file_size; // must get set by constructor const upx_int64_t file_size; // must get set by constructor
const upx_uint64_t file_size_u; // (explicitly unsigned to avoid casts) const upx_uint64_t file_size_u; // (explicitly unsigned to avoid -Wsign-compare casts)
}; };
PackHeader ph; // must be filled by canUnpack(); also used by UiPacker PackHeader ph; // must be filled by canUnpack(); also used by UiPacker
}; };
+1 -1
View File
@@ -219,7 +219,7 @@ const char *Packer::getDecompressorSections() const {
"LZMA_ELF00,LZMA_DEC20,LZMA_DEC30"; "LZMA_ELF00,LZMA_DEC20,LZMA_DEC30";
// clang-format on // clang-format on
unsigned const method = ph_forced_method(ph.method); const unsigned method = ph_forced_method(ph.method);
if (method == M_NRV2B_LE32) if (method == M_NRV2B_LE32)
return opt->small ? nrv2b_le32_small : nrv2b_le32_fast; return opt->small ? nrv2b_le32_small : nrv2b_le32_fast;
if (method == M_NRV2D_LE32) if (method == M_NRV2D_LE32)
+3 -3
View File
@@ -49,7 +49,7 @@ void PackHeader::reset() noexcept {
// extremely simple checksum for the header itself (since version 10) // extremely simple checksum for the header itself (since version 10)
**************************************************************************/ **************************************************************************/
static byte get_packheader_checksum(SPAN_S(const byte) buf, int blen) { static upx_uint8_t get_packheader_checksum(SPAN_S(const byte) buf, int blen) {
assert(blen >= 4); assert(blen >= 4);
assert(get_le32(buf) == UPX_MAGIC_LE32); assert(get_le32(buf) == UPX_MAGIC_LE32);
buf += 4; buf += 4;
@@ -58,7 +58,7 @@ static byte get_packheader_checksum(SPAN_S(const byte) buf, int blen) {
while (blen-- > 0) while (blen-- > 0)
c += *buf++; c += *buf++;
c %= 251; c %= 251;
return (byte) c; return (upx_uint8_t) c;
} }
/************************************************************************* /*************************************************************************
@@ -108,7 +108,7 @@ void PackHeader::putPackHeader(SPAN_S(byte) p) const {
} }
int size = 0; int size = 0;
int old_chksum = 0; upx_uint8_t old_chksum = 0;
// the new variable length header // the new variable length header
if (format < 128) { // little endian if (format < 128) { // little endian
+1 -1
View File
@@ -2948,7 +2948,7 @@ void PeFile::unpack0(OutputFile *fo, const ht &ih, ht &oh, ord_mask_t ord_mask,
if (iobjs > 2) { if (iobjs > 2) {
// read the noncompressed section // read the noncompressed section
unsigned const size = isection[2].size; const unsigned size = isection[2].size;
ibuf.dealloc(); ibuf.dealloc();
ibuf.alloc(size + 1); ibuf.alloc(size + 1);
fi->seek(isection[2].rawdataptr, SEEK_SET); fi->seek(isection[2].rawdataptr, SEEK_SET);
+1
View File
@@ -360,6 +360,7 @@ private:
pointer ptr; pointer ptr;
reference operator[](std::ptrdiff_t) noexcept DELETED_FUNCTION; reference operator[](std::ptrdiff_t) noexcept DELETED_FUNCTION;
const_reference operator[](std::ptrdiff_t) const 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 UPX_CXX_DISABLE_NEW_DELETE_NO_VIRTUAL(OwningPointer) // UPX convention
}; };
// must overload mem_clear() // must overload mem_clear()
+41 -14
View File
@@ -90,7 +90,7 @@ void *MemBuffer::subref_impl(const char *errfmt, size_t skip, size_t take) {
// printf is using unsigned formatting // printf is using unsigned formatting
if (!errfmt || !errfmt[0]) if (!errfmt || !errfmt[0])
errfmt = "bad subref %#x %#x"; 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); throwCantPack(buf);
} }
return ptr + skip; 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 + 0, MAGIC2(p));
set_ne32(p + size_in_bytes + 4, stats.global_alloc_counter); 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 #if !defined(__SANITIZE_MEMORY__) && DEBUG
memset(ptr, 0xfb, size_in_bytes); memset(ptr, 0xfb, size_in_bytes);
(void) VALGRIND_MAKE_MEM_UNDEFINED(ptr, size_in_bytes); (void) VALGRIND_MAKE_MEM_UNDEFINED(ptr, size_in_bytes);
@@ -247,26 +247,33 @@ void MemBuffer::dealloc() noexcept {
**************************************************************************/ **************************************************************************/
TEST_CASE("MemBuffer core") { TEST_CASE("MemBuffer core") {
constexpr size_t N = 64;
MemBuffer mb; MemBuffer mb;
CHECK_THROWS(mb.checkState()); CHECK_THROWS(mb.checkState());
CHECK_THROWS(mb.alloc(0x30000000 + 1)); CHECK_THROWS(mb.alloc(0x30000000 + 1));
CHECK(raw_bytes(mb, 0) == nullptr); CHECK(raw_bytes(mb, 0) == nullptr);
CHECK_THROWS(raw_bytes(mb, 1)); CHECK_THROWS(raw_bytes(mb, 1));
mb.alloc(64); mb.alloc(N);
mb.checkState(); mb.checkState();
CHECK(raw_bytes(mb, 64) != nullptr); CHECK(mb.begin() == mb.cbegin());
CHECK(raw_bytes(mb, 64) == mb.getVoidPtr()); CHECK(mb.end() == mb.cend());
CHECK_THROWS(raw_bytes(mb, 65)); CHECK(mb.begin() == &mb[0]);
CHECK_NOTHROW(mb + 64); CHECK(mb.end() == &mb[0] + N);
CHECK_THROWS(mb + 65); 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 #if ALLOW_INT_PLUS_MEMBUFFER
CHECK_NOTHROW(64 + mb); CHECK_NOTHROW(N + mb);
CHECK_THROWS(65 + mb); CHECK_THROWS((N + 1) + mb);
#endif #endif
CHECK_NOTHROW(mb.subref("", 0, 64)); CHECK_NOTHROW(mb.subref("", 0, N));
CHECK_NOTHROW(mb.subref("", 64, 0)); CHECK_NOTHROW(mb.subref("", N, 0));
CHECK_THROWS(mb.subref("", 1, 64)); CHECK_THROWS(mb.subref("", 1, N));
CHECK_THROWS(mb.subref("", 64, 1)); CHECK_THROWS(mb.subref("", N, 1));
if (use_simple_mcheck()) { if (use_simple_mcheck()) {
byte *p = raw_bytes(mb, 0); byte *p = raw_bytes(mb, 0);
unsigned magic1 = get_ne32(p - 4); unsigned magic1 = get_ne32(p - 4);
@@ -314,6 +321,26 @@ TEST_CASE("MemBuffer unused") {
CHECK(mb.raw_size_in_bytes() == 0); 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") { TEST_CASE("MemBuffer::getSizeForCompression") {
CHECK_THROWS(MemBuffer::getSizeForCompression(0)); CHECK_THROWS(MemBuffer::getSizeForCompression(0));
CHECK_THROWS(MemBuffer::getSizeForDecompression(0)); CHECK_THROWS(MemBuffer::getSizeForDecompression(0));
+48 -20
View File
@@ -42,6 +42,11 @@ public:
typedef typename std::add_lvalue_reference<T>::type reference; typedef typename std::add_lvalue_reference<T>::type reference;
typedef typename std::add_pointer<T>::type pointer; typedef typename std::add_pointer<T>::type pointer;
typedef unsigned size_type; // limited by UPX_RSIZE_MAX 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: protected:
pointer ptr; pointer ptr;
@@ -57,9 +62,9 @@ public:
// array access // array access
reference operator[](ptrdiff_t i) const may_throw { reference operator[](ptrdiff_t i) const may_throw {
// TODO: &array[SIZE] == array + SIZE, this is legal; but element access is not // NOTE: &array[SIZE] is *not* legal for containers like std::vector and MemBuffer !
if very_unlikely (i < 0 || mem_size(sizeof(element_type), i) > size_in_bytes) if very_unlikely (i < 0 || mem_size(element_size, i) + element_size > size_in_bytes)
throwCantPack("MemBuffer invalid index %td (%u bytes)", i, size_in_bytes); throwCantPack("MemBuffer invalid array index %td (%u bytes)", i, size_in_bytes);
return ptr[i]; return ptr[i];
} }
// dereference // dereference
@@ -67,11 +72,33 @@ public:
// arrow operator // arrow operator
pointer operator->() const DELETED_FUNCTION; 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 // membuffer + n -> pointer
template <class U> template <class U>
typename std::enable_if<std::is_integral<U>::value, pointer>::type operator+(U n) const { 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 may_throw {
return raw_bytes(bytes) + n; // and check bytes size_t bytes = mem_size(element_size, n); // check mem_size
return raw_bytes(bytes) + n; // and check bytes
} }
private: private:
// membuffer - n -> pointer; not allowed - use raw_bytes() if needed // membuffer - n -> pointer; not allowed - use raw_bytes() if needed
@@ -83,7 +110,7 @@ public: // raw access
pointer raw_ptr() const noexcept { return ptr; } pointer raw_ptr() const noexcept { return ptr; }
size_type raw_size_in_bytes() const noexcept { return size_in_bytes; } 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 (bytes > 0) {
if very_unlikely (ptr == nullptr) if very_unlikely (ptr == nullptr)
throwCantPack("MemBuffer raw_bytes unexpected NULL ptr"); 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> { class MemBuffer final : public MemBufferBase<byte> {
public: public:
explicit inline MemBuffer() noexcept : MemBufferBase<byte>() {} explicit inline MemBuffer() noexcept : MemBufferBase<byte>() {}
explicit MemBuffer(upx_uint64_t bytes); explicit MemBuffer(upx_uint64_t bytes) may_throw;
~MemBuffer() noexcept; ~MemBuffer() noexcept;
static unsigned getSizeForCompression(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); static unsigned getSizeForDecompression(unsigned uncompressed_size, unsigned extra = 0)
may_throw;
void alloc(upx_uint64_t bytes); void alloc(upx_uint64_t bytes) may_throw;
void allocForCompression(unsigned uncompressed_size, unsigned extra = 0); void allocForCompression(unsigned uncompressed_size, unsigned extra = 0) may_throw;
void allocForDecompression(unsigned uncompressed_size, unsigned extra = 0); void allocForDecompression(unsigned uncompressed_size, unsigned extra = 0) may_throw;
void dealloc() noexcept; void dealloc() noexcept;
void checkState() const; void checkState() const may_throw;
unsigned getSize() const noexcept { return size_in_bytes; }
// explicit conversion // explicit conversion
void *getVoidPtr() noexcept { return (void *) ptr; } void *getVoidPtr() noexcept { return (void *) ptr; }
const void *getVoidPtr() const noexcept { return (const void *) ptr; } const void *getVoidPtr() const noexcept { return (const void *) ptr; }
unsigned getSize() const noexcept { return size_in_bytes; }
// util // util
void fill(unsigned off, unsigned len, int value); noinline void fill(unsigned off, unsigned len, int value) may_throw;
forceinline void clear(unsigned off, unsigned len) { fill(off, len, 0); } forceinline void clear(unsigned off, unsigned len) may_throw { fill(off, len, 0); }
forceinline void clear() { fill(0, size_in_bytes, 0); } forceinline void clear() may_throw { fill(0, size_in_bytes, 0); }
// If the entire range [skip, skip+take) is inside the buffer, // If the entire range [skip, skip+take) is inside the buffer,
// then return &ptr[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. // This is similar to BoundedPtr, except only checks once.
// skip == offset, take == size_in_bytes // 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); return (pointer) subref_impl(errfmt, skip, take);
} }
private: 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 // static debug stats
struct Stats { struct Stats {
+5 -5
View File
@@ -39,8 +39,8 @@
#include "../conf.h" #include "../conf.h"
/************************************************************************* /*************************************************************************
// assert sane memory buffer sizes to protect against integer overflows // upx_rsize_t and mem_size: assert sane memory buffer sizes to protect
// and malicious header fields // against integer overflows and malicious header fields
// see C 11 standard, Annex K // see C 11 standard, Annex K
**************************************************************************/ **************************************************************************/
@@ -253,7 +253,7 @@ TEST_CASE("ptr_check_no_overlap 3") {
// stdlib // 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 size_t bytes = mem_size(element_size, n); // assert size
void *p = malloc(bytes); void *p = malloc(bytes);
if (p != nullptr) if (p != nullptr)
@@ -262,7 +262,7 @@ void *upx_calloc(size_t n, size_t element_size) {
} }
// simple unoptimized memswap() // 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) { if (a != b && n != 0) {
byte *x = (byte *) a; byte *x = (byte *) a;
byte *y = (byte *) b; 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 // 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 #if defined(__clang__) && __clang_major__ < 15
// work around a clang < 15 ICE (Internal Compiler Error) // work around a clang < 15 ICE (Internal Compiler Error)
// @COMPILER_BUG @CLANG_BUG // @COMPILER_BUG @CLANG_BUG
+4 -4
View File
@@ -28,8 +28,8 @@
#pragma once #pragma once
/************************************************************************* /*************************************************************************
// assert sane memory buffer sizes to protect against integer overflows // upx_rsize_t and mem_size: assert sane memory buffer sizes to protect
// and malicious header fields // against integer overflows and malicious header fields
// see C 11 standard, Annex K // 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_standard_layout<T>::value)
COMPILE_TIME_ASSERT(std::is_trivially_copyable<T>::value) COMPILE_TIME_ASSERT(std::is_trivially_copyable<T>::value)
COMPILE_TIME_ASSERT(std::is_trivially_default_constructible<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)]; T *array = new T[size_t(n)];
#if !defined(__SANITIZE_MEMORY__) #if !defined(__SANITIZE_MEMORY__)
if (array != nullptr && bytes > 0) { 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_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 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); typedef void (*upx_sort_func_t)(void *array, size_t n, size_t element_size, upx_compare_func_t);
+2 -2
View File
@@ -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 // help constructor to distinguish between number of elements and bytes
struct XSpanCount final { struct XSpanCount final {
explicit forceinline_constexpr XSpanCount(size_t n) noexcept : count(n) {} explicit forceinline_constexpr XSpanCount(size_t n) noexcept : count(n) {}
size_t count; // public const size_t count; // public
}; };
struct XSpanSizeInBytes final { struct XSpanSizeInBytes final {
explicit forceinline_constexpr XSpanSizeInBytes(size_t bytes) noexcept : size_in_bytes(bytes) {} 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> template <class T>