-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ASAN: Support coroutine context switching and stack tracing (#4153)
For coroutine, we should use `__sanitizer_start_switch_fiber` which similar to`VALGRIND_STACK_REGISTER`, see google/sanitizers#189 (comment) for details. If not fix this, asan will output warning: ``` ==72269==WARNING: ASan is ignoring requested __asan_handle_no_return: stack type: default top: 0x00016f638000; bottom 0x000106bec000; size: 0x000068a4c000 (1755627520) False positive error reports may follow For details see google/sanitizers#189 ``` It will cause asan failed to get the stack, see `research/st/asan-switch.cpp` for example: ``` ==71611==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x000103600733 at pc 0x0001009d3d7c bp 0x000100b4bd40 sp 0x000100b4bd38 WRITE of size 1 at 0x000103600733 thread T0 #0 0x1009d3d78 in foo(void*) asan-switch.cpp:13 ``` After fix this issue, it should provide the full stack when crashing: ``` ==73437==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x000103300733 at pc 0x000100693d7c bp 0x00016f76f550 sp 0x00016f76f548 WRITE of size 1 at 0x000103300733 thread T0 #0 0x100693d78 in foo(void*) asan-switch.cpp:13 #1 0x100693df4 in main asan-switch.cpp:23 #2 0x195aa20dc (<unknown module>) ``` For primordial coroutine, if not set the stack by `st_set_primordial_stack`, then the stack is NULL and asan can't get the stack tracing. Note that it's optional and only make it fail to display the stack information, no other errors. --- Co-authored-by: john <hondaxiao@tencent.com>
- Loading branch information
1 parent
55610cf
commit 8f48a0e
Showing
15 changed files
with
236 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ huge-threads | |
exceptions | ||
hello | ||
pthreads | ||
asan-switch | ||
hello-thread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
g++ asan-switch.cpp ../../objs/st/libst.a -fsanitize=address -fno-omit-frame-pointer -g -O0 -o asan-switch && ./asan-switch | ||
*/ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/resource.h> | ||
#include "../../objs/st/st.h" | ||
|
||
void* foo(void *args) { | ||
for (int i = 0; ; i++) { | ||
st_sleep(1); | ||
if (i && (i % 2) == 0) { | ||
char *p = new char[3]; | ||
p[3] = 'H'; | ||
} | ||
printf("#%d: main: working\n", i); | ||
} | ||
return NULL; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
register void* stack_top asm ("sp"); | ||
struct rlimit limit; | ||
if (getrlimit (RLIMIT_STACK, &limit) == 0) { | ||
void* stack_bottom = (char*)stack_top - limit.rlim_cur; | ||
st_set_primordial_stack(stack_top, stack_bottom); | ||
} | ||
|
||
st_init(); | ||
if (argc > 1) { | ||
// Directly call foo() to trigger ASAN, call the function in the primordial thread, | ||
// note that asan can not capther the stack of primordial thread. | ||
foo(NULL); | ||
} else { | ||
st_thread_create(foo, NULL, 0, 0); | ||
st_thread_exit(NULL); | ||
} | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
g++ hello-thread.cpp ../../objs/st/libst.a -g -O0 -o hello-thread && ./hello-thread | ||
*/ | ||
#include <stdio.h> | ||
#include "../../objs/st/st.h" | ||
|
||
void* foo(void *args) { | ||
for (int i = 0; ; i++) { | ||
st_sleep(1); | ||
printf("#%d: main: working\n", i); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
int main() { | ||
st_init(); | ||
st_thread_create(foo, NULL, 0, 0); | ||
st_thread_exit(NULL); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <sys/resource.h> | ||
#include <stdio.h> | ||
int main (void) | ||
{ | ||
struct rlimit limit; | ||
|
||
getrlimit (RLIMIT_STACK, &limit); | ||
printf ("\nStack Limit = %ld and %ld max\n", limit.rlim_cur, limit.rlim_max); | ||
} |
Oops, something went wrong.