diff --git a/include/memory_debugging.h b/include/memory_debugging.h index dc2d8c233b4..011ba19b843 100644 --- a/include/memory_debugging.h +++ b/include/memory_debugging.h @@ -31,7 +31,9 @@ such as Valgrind. */ +#if !defined(NDEBUG) || defined(HAVE_VALGRIND) #include +#endif #ifdef HAVE_VALGRIND #include @@ -42,11 +44,25 @@ #include #define MEM_UNDEFINED(a, len) VALGRIND_MAKE_MEM_UNDEFINED(a, len) -#define MEM_DEFINED_IF_ADDRESSABLE(a, len) \ - VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(a, len) #define MEM_NOACCESS(a, len) VALGRIND_MAKE_MEM_NOACCESS(a, len) #define MEM_CHECK_ADDRESSABLE(a, len) VALGRIND_CHECK_MEM_IS_ADDRESSABLE(a, len) +#elif defined(HAVE_ASAN) + +#include +#include + +#define MEM_MALLOCLIKE_BLOCK(p1, p2, p3, p4) ASAN_UNPOISON_MEMORY_REGION(p1, p2) +#define MEM_FREELIKE_BLOCK(p1, p2) ASAN_POISON_MEMORY_REGION(p1, p2) + +#define MEM_UNDEFINED(a, len) ASAN_UNPOISON_MEMORY_REGION(a, len) +#define MEM_NOACCESS(a, len) ASAN_POISON_MEMORY_REGION(a, len) + +// In the case of error, this will immediatelly terminate the process instead of +// printing an error and continuing, which is more common for ASan errors. +// Change it to log and continue if that becomes an issue. +#define MEM_CHECK_ADDRESSABLE(a, len) assert(!__asan_region_is_poisoned(a, len)) + #else /* HAVE_VALGRIND */ #define MEM_MALLOCLIKE_BLOCK(p1, p2, p3, p4) \ @@ -56,17 +72,17 @@ do { \ } while (0) #define MEM_UNDEFINED(a, len) ((void)0) -#define MEM_DEFINED_IF_ADDRESSABLE(a, len) ((void)0) #define MEM_NOACCESS(a, len) ((void)0) #define MEM_CHECK_ADDRESSABLE(a, len) ((void)0) #endif -#if !defined(NDEBUG) || defined(HAVE_VALGRIND) +#if !defined(NDEBUG) || defined(HAVE_VALGRIND) || defined(HAVE_ASAN) /** Put bad content in memory to be sure it will segfault if dereferenced. - With Valgrind, verify that memory is addressable, and mark it undefined. + With Valgrind and AddressSanitizer, verify that memory is addressable, and + mark it undefined. */ inline void TRASH(void *ptr, size_t length) { MEM_CHECK_ADDRESSABLE(ptr, length); diff --git a/sql/handler.cc b/sql/handler.cc index a42b4ef5ca9..32c599f9c3b 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -54,7 +54,7 @@ #include "my_pointer_arithmetic.h" #include "my_psi_config.h" #include "my_sqlcommand.h" -#include "my_sys.h" // MEM_DEFINED_IF_ADDRESSABLE() +#include "my_sys.h" #include "myisam.h" // TT_FOR_UPGRADE #include "mysql/binlog/event/binlog_event.h" #include "mysql/components/services/bits/psi_bits.h"