src: more valgrind integration
This commit is contained in:
+11
-9
@@ -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);
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
|
||||
+11
-2
@@ -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
|
||||
|
||||
+15
-5
@@ -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: */
|
||||
|
||||
Reference in New Issue
Block a user