-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathpostgres-asan-instrumentation.patch
More file actions
123 lines (113 loc) · 3.55 KB
/
Copy pathpostgres-asan-instrumentation.patch
File metadata and controls
123 lines (113 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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/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;