summaryrefslogtreecommitdiff
path: root/packaging/0002-Fix-asan-false-positive-errors-15563.patch
blob: 05b649c55f11d0e5866bdde7cba675d153066f2e (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
From 99bc9451f0dff634101734c53358f2bea88b2d83 Mon Sep 17 00:00:00 2001
From: Konstantin Baladurin <k.baladurin@partner.samsung.com>
Date: Fri, 26 Jan 2018 01:19:19 +0300
Subject: [PATCH 2/4] Fix asan false-positive errors: (#15563)

- Call __asan_handle_no_return in RtlRestoreContext if it doesn't return
  and in ThrowExceptionFromContextInternal function;

- Increase alternate signal stack size and use it also for asan.
---
 CMakeLists.txt                           |  1 +
 enablesanitizers.sh                      |  7 ++++---
 src/pal/src/arch/amd64/context2.S        | 13 ++++++++++++-
 src/pal/src/arch/amd64/exceptionhelper.S |  6 ++++++
 src/pal/src/arch/arm/context2.S          | 13 ++++++++++++-
 src/pal/src/arch/arm/exceptionhelper.S   |  6 ++++++
 src/pal/src/arch/arm64/context2.S        | 11 +++++++++++
 src/pal/src/arch/arm64/exceptionhelper.S |  6 ++++++
 src/pal/src/arch/i386/context2.S         |  5 ++++-
 src/pal/src/arch/i386/exceptionhelper.S  |  6 ++++++
 src/pal/src/exception/signal.cpp         |  5 +++++
 11 files changed, 73 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 45dd65b..1a3ad16 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -408,6 +408,7 @@ elseif (CLR_CMAKE_PLATFORM_UNIX)
       if (${__ASAN_POS} GREATER -1)
         set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
         set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
+        add_definitions(-DHAS_ASAN)
         message("Address Sanitizer (asan) enabled")
       endif ()
       if (${__UBSAN_POS} GREATER -1)
diff --git a/enablesanitizers.sh b/enablesanitizers.sh
index 2937b0b..aedb95d 100755
--- a/enablesanitizers.sh
+++ b/enablesanitizers.sh
@@ -83,8 +83,9 @@ else
         unset DEBUG_SANITIZERS
         echo "Setting DEBUG_SANITIZERS="
     else
-        # for now, specify alloc_dealloc_mismatch=0 as there are too many error reports that are not an issue
-        ASAN_OPTIONS="symbolize=1 alloc_dealloc_mismatch=0"
+        # for now, specify alloc_dealloc_mismatch=0 as there are too many error reports that are not an issue.
+        # Also specify use_sigaltstack=0 as coreclr uses own alternate stack for signal handlers
+        ASAN_OPTIONS="symbolize=1 alloc_dealloc_mismatch=0 use_sigaltstack=0"
         # when Clang 3.8 available, add: suppressions=$(readlink -f sanitizersuppressions.txt)
         UBSAN_OPTIONS="print_stacktrace=1"
 
@@ -132,4 +133,4 @@ else
     unset __EnableLSan
     unset __TurnOff
     unset __Options
-fi
\ No newline at end of file
+fi
diff --git a/src/pal/src/arch/amd64/context2.S b/src/pal/src/arch/amd64/context2.S
index 0e93e81..46c941f 100644
--- a/src/pal/src/arch/amd64/context2.S
+++ b/src/pal/src/arch/amd64/context2.S
@@ -104,7 +104,18 @@ LEAF_END RtlCaptureContext, _TEXT
 LEAF_ENTRY RtlRestoreContext, _TEXT
     push_nonvol_reg rbp
     alloc_stack (IRetFrameLengthAligned)
-    
+
+#ifdef HAS_ASAN
+    test    BYTE PTR [rdi + CONTEXT_ContextFlags], CONTEXT_CONTROL
+    je      LOCAL_LABEL(Restore_CONTEXT_DEBUG_REGISTERS)
+
+    push_nonvol_reg rdi
+    push_nonvol_reg rsi
+    call    EXTERNAL_C_FUNC(__asan_handle_no_return)
+    pop_nonvol_reg rsi
+    pop_nonvol_reg rdi
+LOCAL_LABEL(Restore_CONTEXT_DEBUG_REGISTERS):
+#endif
     test    BYTE PTR [rdi + CONTEXT_ContextFlags], CONTEXT_DEBUG_REGISTERS
     je      LOCAL_LABEL(Done_Restore_CONTEXT_DEBUG_REGISTERS)
     mov     rdx, [rdi + CONTEXT_Dr0]
diff --git a/src/pal/src/arch/amd64/exceptionhelper.S b/src/pal/src/arch/amd64/exceptionhelper.S
index b7b34ac..72a1393 100644
--- a/src/pal/src/arch/amd64/exceptionhelper.S
+++ b/src/pal/src/arch/amd64/exceptionhelper.S
@@ -14,6 +14,12 @@
 // Then it uses the ThrowExceptionHelper to throw the passed in exception from that context.
 // EXTERN_C void ThrowExceptionFromContextInternal(CONTEXT* context, PAL_SEHException* ex);
 LEAF_ENTRY ThrowExceptionFromContextInternal, _TEXT
+#ifdef HAS_ASAN
+        // Need to call __asan_handle_no_return explicitly here because we re-intialize RSP before
+        // throwing exception in ThrowExceptionHelper
+        call    EXTERNAL_C_FUNC(__asan_handle_no_return)
+#endif
+
         // Save the RBP to the stack so that the unwind can work at the instruction after
         // loading the RBP from the context, but before loading the RSP from the context.
         push_nonvol_reg rbp
diff --git a/src/pal/src/arch/arm/context2.S b/src/pal/src/arch/arm/context2.S
index 61e9ab8..42f50c9 100644
--- a/src/pal/src/arch/arm/context2.S
+++ b/src/pal/src/arch/arm/context2.S
@@ -112,7 +112,18 @@ LEAF_END RtlCaptureContext, _TEXT
 //
 LEAF_ENTRY RtlRestoreContext, _TEXT
     END_PROLOGUE
-    
+
+#ifdef HAS_ASAN
+    ldr r2, [r0, #(CONTEXT_ContextFlags)]
+    tst r2, #(CONTEXT_CONTROL)
+    beq     LOCAL_LABEL(Restore_CONTEXT_FLOATING_POINT)
+
+    push {r0, r1}
+    bl EXTERNAL_C_FUNC(__asan_handle_no_return)
+    pop {r0, r1}
+
+LOCAL_LABEL(Restore_CONTEXT_FLOATING_POINT):
+#endif
     ldr r2, [r0, #(CONTEXT_ContextFlags)]
     tst r2, #(CONTEXT_FLOATING_POINT)
     
diff --git a/src/pal/src/arch/arm/exceptionhelper.S b/src/pal/src/arch/arm/exceptionhelper.S
index 76cdcba..dad48de 100644
--- a/src/pal/src/arch/arm/exceptionhelper.S
+++ b/src/pal/src/arch/arm/exceptionhelper.S
@@ -11,6 +11,12 @@
 // EXTERN_C void ThrowExceptionFromContextInternal(CONTEXT* context, PAL_SEHException* ex);
 LEAF_ENTRY ThrowExceptionFromContextInternal, _TEXT
     // Ported from src/pal/src/arch/amd64/exceptionhelper.S
+#ifdef HAS_ASAN
+    // Need to call __asan_handle_no_return explicitly here because we re-intialize SP before
+    // throwing exception in ThrowExceptionHelper
+    bl  EXTERNAL_C_FUNC(__asan_handle_no_return)
+#endif
+
     push_nonvol_reg {r7} /* FP. x64-RBP */
 
     ldr	r4,	[r0, #(CONTEXT_R4)]
diff --git a/src/pal/src/arch/arm64/context2.S b/src/pal/src/arch/arm64/context2.S
index e62a9ac..ac3661a 100644
--- a/src/pal/src/arch/arm64/context2.S
+++ b/src/pal/src/arch/arm64/context2.S
@@ -133,6 +133,17 @@ LEAF_END RtlCaptureContext, _TEXT
 //  x1: Exception*
 //
 LEAF_ENTRY RtlRestoreContext, _TEXT
+
+#ifdef HAS_ASAN
+    ldr w17, [x0, #(CONTEXT_ContextFlags)]
+    tbz w17, #CONTEXT_CONTROL_BIT, LOCAL_LABEL(Restore_CONTEXT_FLOATING_POINT)
+
+    stp x0, x1, [sp]
+    bl EXTERNAL_C_FUNC(__asan_handle_no_return)
+    ldp x0, x1, [sp]
+
+LOCAL_LABEL(Restore_CONTEXT_FLOATING_POINT):
+#endif
     // aarch64 specifies:
     //   IP0 and IP1, the Intra-Procedure Call temporary registers,
     //   are available for use by e.g. veneers or branch islands during a procedure call.
diff --git a/src/pal/src/arch/arm64/exceptionhelper.S b/src/pal/src/arch/arm64/exceptionhelper.S
index 480846e..7deeee6 100644
--- a/src/pal/src/arch/arm64/exceptionhelper.S
+++ b/src/pal/src/arch/arm64/exceptionhelper.S
@@ -12,6 +12,12 @@
 // Then it uses the ThrowExceptionHelper to throw the passed in exception from that context.
 // EXTERN_C void ThrowExceptionFromContextInternal(CONTEXT* context, PAL_SEHException* ex);
 LEAF_ENTRY ThrowExceptionFromContextInternal, _TEXT
+#ifdef HAS_ASAN
+        // Need to call __asan_handle_no_return explicitly here because we re-intialize SP before
+        // throwing exception in ThrowExceptionHelper
+        bl  EXTERNAL_C_FUNC(__asan_handle_no_return)
+#endif
+
     // Save the FP & LR to the stack so that the unwind can work at the instruction after
     // loading the FP from the context, but before loading the SP from the context.
     stp fp, lr,  [sp, -16]!
diff --git a/src/pal/src/arch/i386/context2.S b/src/pal/src/arch/i386/context2.S
index cf7581d..8c5db20 100644
--- a/src/pal/src/arch/i386/context2.S
+++ b/src/pal/src/arch/i386/context2.S
@@ -94,8 +94,11 @@ LEAF_ENTRY RtlCaptureContext, _TEXT
 LEAF_END RtlCaptureContext, _TEXT
 
 LEAF_ENTRY RtlRestoreContext, _TEXT
-    mov   eax, [esp + 4]
 
+#ifdef HAS_ASAN
+    call    EXTERNAL_C_FUNC(__asan_handle_no_return)
+#endif
+    mov     eax, [esp + 4]
     test    BYTE PTR [eax + CONTEXT_ContextFlags], CONTEXT_FLOATING_POINT
     je      LOCAL_LABEL(Done_Restore_CONTEXT_FLOATING_POINT)
     frstor  [eax + CONTEXT_FloatSave]
diff --git a/src/pal/src/arch/i386/exceptionhelper.S b/src/pal/src/arch/i386/exceptionhelper.S
index b9ceffc..609efcf 100644
--- a/src/pal/src/arch/i386/exceptionhelper.S
+++ b/src/pal/src/arch/i386/exceptionhelper.S
@@ -18,6 +18,12 @@
 //////////////////////////////////////////////////////////////////////////
 
 LEAF_ENTRY ThrowExceptionFromContextInternal, _TEXT
+#ifdef HAS_ASAN
+        // Need to call __asan_handle_no_return explicitly here because we re-intialize ESP before
+        // throwing exception in ThrowExceptionHelper
+        call  EXTERNAL_C_FUNC(__asan_handle_no_return)
+#endif
+
         push  ebp
         mov   ecx, [esp + 12] // ecx: PAL_SEHException * (first argument for ThrowExceptionHelper)
         mov   eax, [esp + 8]  // ebx: CONTEXT *
diff --git a/src/pal/src/exception/signal.cpp b/src/pal/src/exception/signal.cpp
index 10eecf7..f795b81 100644
--- a/src/pal/src/exception/signal.cpp
+++ b/src/pal/src/exception/signal.cpp
@@ -153,6 +153,11 @@ BOOL EnsureSignalAlternateStack()
         // We include the size of the SignalHandlerWorkerReturnPoint in the alternate stack size since the 
         // context contained in it is large and the SIGSTKSZ was not sufficient on ARM64 during testing.
         int altStackSize = SIGSTKSZ + ALIGN_UP(sizeof(SignalHandlerWorkerReturnPoint), 16) + VIRTUAL_PAGE_SIZE;
+#ifdef HAS_ASAN
+        // Asan also uses alternate stack so we increase its size on the SIGSTKSZ * 4 that enough for asan
+        // (see kAltStackSize in compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cc)
+        altStackSize += SIGSTKSZ * 4;
+#endif
         void* altStack;
         int st = posix_memalign(&altStack, VIRTUAL_PAGE_SIZE, altStackSize);
         if (st == 0)
-- 
2.7.4