1
0
mirror of https://github.com/upx/upx.git synced 2025-08-07 22:46:51 +08:00

src: more valgrind integration

This commit is contained in:
Markus F.X.J. Oberhumer
2023-01-26 08:50:30 +01:00
parent a094df7b55
commit 8a1bd67a20
5 changed files with 46 additions and 24 deletions

View File

@ -353,15 +353,15 @@ jobs:
- { zig_target: x86_64-macos.13-none }
- { zig_target: x86_64-windows-gnu }
env:
# 2023-01-24
ZIG_DIST_VERSION: 0.11.0-dev.1436+59d9afcb5
# 2023-01-26
ZIG_DIST_VERSION: 0.11.0-dev.1460+e675af069
# for zig-cc wrapper scripts (see below):
ZIG_CPPFLAGS: -DUPX_DOCTEST_CONFIG_MULTITHREADING
ZIG_FLAGS: ${{ matrix.zig_flags }}
ZIG_PIC: ${{ matrix.zig_pic }}
ZIG_TARGET: ${{ matrix.zig_target }}
steps:
- name: 'Install Alpine packages'
- name: 'Install Alpine Linux container packages'
if: ${{ job.container }}
shell: sh
run: |

View File

@ -8,7 +8,7 @@
# mkdir -p build/release
# cd build/release
# cmake ../..
# cmake --build . --parallel # (or just use "make -j" instead)
# make -j (or use "cmake --build . --parallel")
CMAKE = cmake
UPX_CMAKE_BUILD_FLAGS += --parallel
@ -46,6 +46,8 @@ release: build/release
.NOTPARALLEL: # because the actual builds use "cmake --parallel"
.SUFFIXES:
# END of Makefile; extra stuff follows
#***********************************************************************
# extra builds: some pre-defined build configurations
#***********************************************************************
@ -147,12 +149,12 @@ build/extra/cross-darwin-x86_64/%: export CC = clang -target x86_64-apple-darwi
build/extra/cross-darwin-x86_64/%: export CXX = clang++ -target x86_64-apple-darwin
#***********************************************************************
# advanced: generic eXtra target
# advanced: generic extra target
#***********************************************************************
# usage:
# make UPX_XTARGET=mytarget CC="my-cc -flags" CXX="my-cxx -flags"
# make UPX_XTARGET=mytarget CC="my-cc -flags" CXX="my-cxx -flags" xtarget/debug
# make UPX_XTARGET=my-target CC="my-cc -flags" CXX="my-cxx -flags"
# make UPX_XTARGET=my-target CC="my-cc -flags" CXX="my-cxx -flags" xtarget/debug
ifneq ($(UPX_XTARGET),)
ifneq ($(CC),)
@ -168,7 +170,6 @@ xtarget/debug: build/xtarget/$(UPX_XTARGET)/debug
xtarget/release: build/xtarget/$(UPX_XTARGET)/release
# set new default
.DEFAULT_GOAL = xtarget/release
##$(eval .DEFAULT_GOAL = build/xtarget/$(UPX_XTARGET)/release)
endif
endif

View File

@ -33,26 +33,28 @@
**************************************************************************/
TEST_CASE("raw_bytes ptr") {
upx_uint32_t *ptr = nullptr;
upx_uint16_t *ptr = nullptr;
CHECK_NOTHROW(raw_bytes(ptr, 0));
CHECK_THROWS(raw_bytes(ptr, 1));
CHECK_THROWS(raw_index_bytes(ptr, 0, 0));
CHECK_THROWS(raw_index_bytes(ptr, 1, 0));
CHECK_THROWS(raw_index_bytes(ptr, 0, 1));
upx_uint32_t buf[4];
upx_uint16_t buf[4];
ptr = buf;
CHECK(ptr_udiff_bytes(raw_index_bytes(ptr, 1, 1), ptr) == 4u);
CHECK(ptr_udiff_bytes(raw_index_bytes(ptr, 1, 1), ptr) == 2u);
CHECK(ptr_udiff_bytes(raw_index_bytes(ptr, 4, 0), ptr) == 8u);
}
TEST_CASE("raw_bytes bounded array") {
upx_uint32_t buf[4];
CHECK_NOTHROW(raw_bytes(buf, 16));
CHECK_THROWS(raw_bytes(buf, 17));
upx_uint16_t buf[4];
CHECK_NOTHROW(raw_bytes(buf, 8));
CHECK_THROWS(raw_bytes(buf, 9));
CHECK_NOTHROW(raw_index_bytes(buf, 4, 0));
CHECK_THROWS(raw_index_bytes(buf, 4, 1));
CHECK_NOTHROW(raw_index_bytes(buf, 3, 4));
CHECK_THROWS(raw_index_bytes(buf, 3, 5));
CHECK(ptr_udiff_bytes(raw_index_bytes(buf, 1, 1), buf) == 4u);
CHECK_NOTHROW(raw_index_bytes(buf, 3, 2));
CHECK_THROWS(raw_index_bytes(buf, 3, 3));
CHECK(ptr_udiff_bytes(raw_index_bytes(buf, 1, 1), buf) == 2u);
CHECK(ptr_udiff_bytes(raw_index_bytes(buf, 4, 0), buf) == 8u);
}
/*************************************************************************

View File

@ -139,9 +139,12 @@ ACC_COMPILE_TIME_ASSERT_HEADER((char)(-1) == 255) // -funsigned-char
# include <rangeless/include/fn.hpp>
#endif
#ifndef WITH_VALGRIND
#define WITH_VALGRIND 1
# define WITH_VALGRIND 1
#endif
#if (WITH_VALGRIND) && defined(__GNUC__) && !defined(__SANITIZE_ADDRESS__)
#if defined(__SANITIZE_ADDRESS__) || defined(_WIN32) || !defined(__GNUC__)
# undef WITH_VALGRIND
#endif
#if (WITH_VALGRIND)
# include <valgrind/include/valgrind/memcheck.h>
#endif
@ -318,6 +321,12 @@ typedef upx_int64_t upx_off_t;
#define CLANG_FORMAT_DUMMY_STATEMENT /*empty*/
// malloc debuggers
#if !defined(VALGRIND_CHECK_MEM_IS_ADDRESSABLE)
# define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(addr,len) 0
#endif
#if !defined(VALGRIND_CHECK_MEM_IS_DEFINED)
# define VALGRIND_CHECK_MEM_IS_DEFINED(addr,len) 0
#endif
#if !defined(VALGRIND_MAKE_MEM_DEFINED)
# define VALGRIND_MAKE_MEM_DEFINED(addr,len) 0
#endif

View File

@ -172,8 +172,12 @@ 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 very_unlikely (size_in_bytes > 0 && ptr == nullptr)
throwInternalError("raw_bytes unexpected NULL ptr");
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;
}
@ -183,23 +187,29 @@ 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");
(void) mem_size(sizeof(T), index, size_in_bytes); // assert size
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) {
if very_unlikely (size_in_bytes > mem_size(sizeof(T), N))
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) {
return raw_bytes(a, mem_size(sizeof(T), index, size_in_bytes)) + index;
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: */