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
+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)))