src: improve memory sanitizer handling

This commit is contained in:
Markus F.X.J. Oberhumer
2023-09-26 15:15:55 +02:00
parent a24de15060
commit 39a6cc4b5f
5 changed files with 46 additions and 9 deletions
+10 -4
View File
@@ -34,6 +34,16 @@
#include "headers.h"
#include "version.h"
#if !defined(__has_attribute)
#define __has_attribute(x) 0
#endif
#if !defined(__has_builtin)
#define __has_builtin(x) 0
#endif
#if !defined(__has_feature)
#define __has_feature(x) 0
#endif
// reserve name "upx" for namespace
namespace upx {}
@@ -319,10 +329,6 @@ inline void NO_fprintf(FILE *, const char *, ...) noexcept attribute_format(2, 3
inline void NO_printf(const char *, ...) noexcept {}
inline void NO_fprintf(FILE *, const char *, ...) noexcept {}
#if !defined(__has_builtin)
# define __has_builtin(x) 0
#endif
#if __has_builtin(__builtin_memcpy_inline)
# define upx_memcpy_inline __builtin_memcpy_inline
#elif __has_builtin(__builtin_memcpy)
+14 -1
View File
@@ -109,6 +109,18 @@ static_assert(sizeof(void *) == 8);
#include <mutex>
#endif
// sanitizers
#if !defined(__SANITIZE_ADDRESS__) && defined(__has_feature)
#if __has_feature(address_sanitizer)
#define __SANITIZE_ADDRESS__ 1
#endif
#endif
#if !defined(__SANITIZE_MEMORY__) && defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define __SANITIZE_MEMORY__ 1
#endif
#endif
// UPX vendor git submodule headers
#include <doctest/doctest/parts/doctest_fwd.h>
#if WITH_BOOST_PFR
@@ -121,7 +133,8 @@ static_assert(sizeof(void *) == 8);
#ifndef WITH_VALGRIND
#define WITH_VALGRIND 1
#endif
#if defined(__SANITIZE_ADDRESS__) || defined(_WIN32) || !defined(__GNUC__)
#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_MEMORY__) || defined(_WIN32) || \
!defined(__GNUC__)
#undef WITH_VALGRIND
#endif
#if WITH_VALGRIND
+2 -2
View File
@@ -47,7 +47,7 @@ unsigned membuffer_get_size(MemBuffer &mb) noexcept { return mb.getSize(); }
// bool use_simple_mcheck()
**************************************************************************/
#if defined(__SANITIZE_ADDRESS__)
#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_MEMORY__)
static forceinline constexpr bool use_simple_mcheck() noexcept { return false; }
#elif (WITH_VALGRIND) && defined(RUNNING_ON_VALGRIND)
static bool use_simple_mcheck_flag;
@@ -190,7 +190,7 @@ void MemBuffer::alloc(upx_uint64_t bytes) {
set_ne32(p + size_in_bytes + 4, stats.global_alloc_counter);
}
ptr = (pointer) (void *) p;
#if DEBUG
#if !defined(__SANITIZE_MEMORY__) && DEBUG
memset(ptr, 0xfb, size_in_bytes);
(void) VALGRIND_MAKE_MEM_UNDEFINED(ptr, size_in_bytes);
#endif
+6
View File
@@ -70,12 +70,18 @@ inline void mem_size_assert(upx_uint64_t element_size, upx_uint64_t n) {
#if DEBUG
template <class T>
T *NewArray(upx_uint64_t n) {
COMPILE_TIME_ASSERT(std::is_standard_layout<T>::value)
COMPILE_TIME_ASSERT(std::is_trivially_copyable<T>::value)
COMPILE_TIME_ASSERT(std::is_trivially_default_constructible<T>::value)
size_t bytes = mem_size(sizeof(T), n); // assert size
T *array = new T[size_t(n)];
#if !defined(__SANITIZE_MEMORY__)
if (array != nullptr && bytes > 0) {
memset(array, 0xfb, bytes);
(void) VALGRIND_MAKE_MEM_UNDEFINED(array, bytes);
}
#endif
UNUSED(bytes);
return array;
}
#define New(type, n) (NewArray<type>((n)))