| 1 | // RUN: %clangxx_tsan -O1 %s -o %t |
| 2 | // `handle_sigbus=0` is required because when the rdar://problem/58789439 bug was |
| 3 | // present TSan's runtime could dereference bad memory leading to SIGBUS being raised. |
| 4 | // If the signal was caught TSan would deadlock because it would try to run the |
| 5 | // symbolizer again. |
| 6 | // RUN: %env_tsan_opts=handle_sigbus=0,symbolize=1 %run %t 2>&1 | FileCheck %s |
| 7 | // RUN: %env_tsan_opts=handle_sigbus=0,symbolize=1 __check_mach_ports_lookup=some_value %run %t 2>&1 | FileCheck %s |
| 8 | #include <sanitizer/common_interface_defs.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | const char *kEnvName = "__UNLIKELY_ENV_VAR_NAME__" ; |
| 13 | |
| 14 | int main() { |
| 15 | if (getenv(name: kEnvName)) { |
| 16 | fprintf(stderr, format: "Env var %s should not be set\n" , kEnvName); |
| 17 | abort(); |
| 18 | } |
| 19 | |
| 20 | // This will set an environment variable that isn't already in |
| 21 | // the environment array. This will cause Darwin's Libc to |
| 22 | // malloc() a new array. |
| 23 | if (setenv(name: kEnvName, value: "some_value" , /*overwrite=*/replace: 1)) { |
| 24 | fprintf(stderr, format: "Failed to set %s \n" , kEnvName); |
| 25 | abort(); |
| 26 | } |
| 27 | |
| 28 | // rdar://problem/58789439 |
| 29 | // Now trigger symbolization. If symbolization tries to call |
| 30 | // to `setenv` that adds a new environment variable, then Darwin |
| 31 | // Libc will call `realloc()` and TSan's runtime will hit |
| 32 | // an assertion failure because TSan's runtime uses a different |
| 33 | // allocator during symbolization which leads to `realloc()` being |
| 34 | // called on a pointer that the allocator didn't allocate. |
| 35 | // |
| 36 | // CHECK: #{{[0-9]}} main {{.*}}no_call_setenv_in_symbolize.cpp:[[@LINE+1]] |
| 37 | __sanitizer_print_stack_trace(); |
| 38 | |
| 39 | // CHECK: DONE |
| 40 | fprintf(stderr, format: "DONE\n" ); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |