asan-switch.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. g++ asan-switch.cpp ../../objs/st/libst.a -fsanitize=address -fno-omit-frame-pointer -g -O0 -o asan-switch && ./asan-switch
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/resource.h>
  7. #include "../../objs/st/st.h"
  8. void* foo(void *args) {
  9. for (int i = 0; ; i++) {
  10. st_sleep(1);
  11. if (i && (i % 2) == 0) {
  12. char *p = new char[3];
  13. p[3] = 'H';
  14. }
  15. printf("#%d: main: working\n", i);
  16. }
  17. return NULL;
  18. }
  19. int main(int argc, char **argv) {
  20. register void* stack_top asm ("sp");
  21. struct rlimit limit;
  22. if (getrlimit (RLIMIT_STACK, &limit) == 0) {
  23. void* stack_bottom = (char*)stack_top - limit.rlim_cur;
  24. st_set_primordial_stack(stack_top, stack_bottom);
  25. }
  26. st_init();
  27. if (argc > 1) {
  28. // Directly call foo() to trigger ASAN, call the function in the primordial thread,
  29. // note that asan can not capther the stack of primordial thread.
  30. foo(NULL);
  31. } else {
  32. st_thread_create(foo, NULL, 0, 0);
  33. st_thread_exit(NULL);
  34. }
  35. return 0;
  36. }