timescaledb/test/postgres-asan-instrumentation.patch
Alexander Kuzmenkov 936d751037 Add AddressSanitizer instrumentation for memory contexts
Use manual poison/unpoison at the existing Valgrind hooks, so that
AddressSanitizer sees palloc/pfree as well, not only the underlying
mallocs which are called much less often.

Fix some out-of-bound reads found with this instrumentation.
2023-05-16 21:32:52 +02:00

54 lines
1.7 KiB
Diff

diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 2c50575b37..11b6c688c7 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -3492,6 +4476,12 @@ check_stack_depth(void)
bool
stack_is_too_deep(void)
{
+ /*
+ * Pointer arithmetics to determine stack depth doesn't work under
+ * AddressSanitizer.
+ */
+ return false;
+
char stack_top_loc;
long stack_depth;
diff --git a/src/include/utils/memdebug.h b/src/include/utils/memdebug.h
index e88b4c6e8e..4ccbbf0146 100644
--- a/src/include/utils/memdebug.h
+++ b/src/include/utils/memdebug.h
@@ -19,6 +19,31 @@
#ifdef USE_VALGRIND
#include <valgrind/memcheck.h>
+
+#elif __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
+
+#include <sanitizer/asan_interface.h>
+
+#define VALGRIND_MAKE_MEM_DEFINED(addr, size) \
+ ASAN_UNPOISON_MEMORY_REGION(addr, size)
+
+#define VALGRIND_MAKE_MEM_NOACCESS(addr, size) \
+ ASAN_POISON_MEMORY_REGION(addr, size)
+
+#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size) \
+ ASAN_UNPOISON_MEMORY_REGION(addr, size)
+
+#define VALGRIND_MEMPOOL_ALLOC(context, addr, size) \
+ ASAN_UNPOISON_MEMORY_REGION(addr, size)
+
+#define VALGRIND_MEMPOOL_FREE(context, addr) \
+ ASAN_POISON_MEMORY_REGION(addr, 1 /* Length unknown, poison first byte. */)
+
+#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size) do {} while (0)
+#define VALGRIND_CREATE_MEMPOOL(context, redzones, zeroed) do {} while (0)
+#define VALGRIND_DESTROY_MEMPOOL(context) do {} while (0)
+#define VALGRIND_MEMPOOL_CHANGE(context, optr, nptr, size) do {} while (0)
+
#else
#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size) do {} while (0)
#define VALGRIND_CREATE_MEMPOOL(context, redzones, zeroed) do {} while (0)