Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/llm-fuzzer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ jobs:
# Add instrumentation to the Postgres memory contexts. For more details, see
# https://github.com/timescale/eng-database/wiki/Using-Address-Sanitizer#adding-more-instrumentation
PG_MAJOR=$(echo "${{ needs.config.outputs.pg_latest }}" | sed -e 's![.].*!!')
if [ ${PG_MAJOR} -lt 18 ]; then
if [ ${PG_MAJOR} -lt 16 ]; then
patch -F5 -p1 -d ~/$PG_SRC_DIR < test/postgres-asan-instrumentation-PG15.patch
elif [ ${PG_MAJOR} -lt 18 ]; then
patch -F5 -p1 -d ~/$PG_SRC_DIR < test/postgres-asan-instrumentation.patch
else
patch -F5 -p1 -d ~/$PG_SRC_DIR < test/postgres-asan-instrumentation-PG18GE.patch
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/sanitizer-build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ jobs:
# Add instrumentation to the Postgres memory contexts. For more details, see
# https://github.com/timescale/eng-database/wiki/Using-Address-Sanitizer#adding-more-instrumentation
PG_MAJOR=$(echo "${{ matrix.pg }}" | sed -e 's![.].*!!')
if [ ${PG_MAJOR} -lt 18 ]; then
if [ ${PG_MAJOR} -lt 16 ]; then
patch -F5 -p1 -d ~/$PG_SRC_DIR < test/postgres-asan-instrumentation-PG15.patch
elif [ ${PG_MAJOR} -lt 18 ]; then
patch -F5 -p1 -d ~/$PG_SRC_DIR < test/postgres-asan-instrumentation.patch
else
patch -F5 -p1 -d ~/$PG_SRC_DIR < test/postgres-asan-instrumentation-PG18GE.patch
Expand Down
68 changes: 68 additions & 0 deletions test/postgres-asan-instrumentation-PG15.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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)
diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -1267,11 +1267,9 @@
* Make sure not to mark too many bytes in case chunk->requested_size
* < size < oldchksize.
*/
-#ifdef USE_VALGRIND
if (Min(size, oldchksize) > chunk->requested_size)
VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + chunk->requested_size,
Min(size, oldchksize) - chunk->requested_size);
-#endif
#endif

chunk->requested_size = size;
72 changes: 71 additions & 1 deletion test/postgres-asan-instrumentation-PG18GE.patch
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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>
+
Expand Down Expand Up @@ -51,3 +51,73 @@ index e88b4c6e8e..4ccbbf0146 100644
#else
#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size) do {} while (0)
#define VALGRIND_CREATE_MEMPOOL(context, redzones, zeroed) do {} while (0)
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -1549,17 +1549,13 @@
void
pfree(void *pointer)
{
-#ifdef USE_VALGRIND
MemoryContextMethodID method = GetMemoryChunkMethodID(pointer);
MemoryContext context = GetMemoryChunkContext(pointer);
-#endif

MCXT_METHOD(pointer, free_p) (pointer);

-#ifdef USE_VALGRIND
if (method != MCTX_ALIGNED_REDIRECT_ID)
VALGRIND_MEMPOOL_FREE(context, pointer);
-#endif
}

/*
@@ -1569,12 +1565,8 @@
void *
repalloc(void *pointer, Size size)
{
-#ifdef USE_VALGRIND
MemoryContextMethodID method = GetMemoryChunkMethodID(pointer);
-#endif
-#if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
MemoryContext context = GetMemoryChunkContext(pointer);
-#endif
void *ret;

AssertNotInCriticalSection(context);
@@ -1597,10 +1589,8 @@
*/
ret = MCXT_METHOD(pointer, realloc) (pointer, size, 0);

-#ifdef USE_VALGRIND
if (method != MCTX_ALIGNED_REDIRECT_ID)
VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
-#endif

return ret;
}
@@ -1613,9 +1603,7 @@
void *
repalloc_extended(void *pointer, Size size, int flags)
{
-#if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
MemoryContext context = GetMemoryChunkContext(pointer);
-#endif
void *ret;

AssertNotInCriticalSection(context);
diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -1267,11 +1267,9 @@
* Make sure not to mark too many bytes in case chunk->requested_size
* < size < oldchksize.
*/
-#ifdef USE_VALGRIND
if (Min(size, oldchksize) > chunk->requested_size)
VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + chunk->requested_size,
Min(size, oldchksize) - chunk->requested_size);
-#endif
#endif

chunk->requested_size = size;
74 changes: 72 additions & 2 deletions test/postgres-asan-instrumentation.patch
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ index 2c50575b37..11b6c688c7 100644
+
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>
+
Expand Down Expand Up @@ -51,3 +51,73 @@ index e88b4c6e8e..4ccbbf0146 100644
#else
#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size) do {} while (0)
#define VALGRIND_CREATE_MEMPOOL(context, redzones, zeroed) do {} while (0)
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -1549,17 +1549,13 @@
void
pfree(void *pointer)
{
-#ifdef USE_VALGRIND
MemoryContextMethodID method = GetMemoryChunkMethodID(pointer);
MemoryContext context = GetMemoryChunkContext(pointer);
-#endif

MCXT_METHOD(pointer, free_p) (pointer);

-#ifdef USE_VALGRIND
if (method != MCTX_ALIGNED_REDIRECT_ID)
VALGRIND_MEMPOOL_FREE(context, pointer);
-#endif
}

/*
@@ -1569,12 +1565,8 @@
void *
repalloc(void *pointer, Size size)
{
-#ifdef USE_VALGRIND
MemoryContextMethodID method = GetMemoryChunkMethodID(pointer);
-#endif
-#if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
MemoryContext context = GetMemoryChunkContext(pointer);
-#endif
void *ret;

AssertNotInCriticalSection(context);
@@ -1597,10 +1589,8 @@
*/
ret = MCXT_METHOD(pointer, realloc) (pointer, size, 0);

-#ifdef USE_VALGRIND
if (method != MCTX_ALIGNED_REDIRECT_ID)
VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
-#endif

return ret;
}
@@ -1613,9 +1603,7 @@
void *
repalloc_extended(void *pointer, Size size, int flags)
{
-#if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
MemoryContext context = GetMemoryChunkContext(pointer);
-#endif
void *ret;

AssertNotInCriticalSection(context);
diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -1267,11 +1267,9 @@
* Make sure not to mark too many bytes in case chunk->requested_size
* < size < oldchksize.
*/
-#ifdef USE_VALGRIND
if (Min(size, oldchksize) > chunk->requested_size)
VALGRIND_MAKE_MEM_UNDEFINED((char *) pointer + chunk->requested_size,
Min(size, oldchksize) - chunk->requested_size);
-#endif
#endif

chunk->requested_size = size;
Loading