44 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t2// `handle_sigbus=0` is required because when the rdar://problem/58789439 bug was3// 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 the5// symbolizer again.6// RUN: %env_tsan_opts=handle_sigbus=0,symbolize=1 %run %t 2>&1 | FileCheck %s7// RUN: %env_tsan_opts=handle_sigbus=0,symbolize=1 __check_mach_ports_lookup=some_value %run %t 2>&1 | FileCheck %s8#include <sanitizer/common_interface_defs.h>9#include <stdio.h>10#include <stdlib.h>11 12const char *kEnvName = "__UNLIKELY_ENV_VAR_NAME__";13 14int main() {15 if (getenv(kEnvName)) {16 fprintf(stderr, "Env var %s should not be set\n", kEnvName);17 abort();18 }19 20 // This will set an environment variable that isn't already in21 // the environment array. This will cause Darwin's Libc to22 // malloc() a new array.23 if (setenv(kEnvName, "some_value", /*overwrite=*/1)) {24 fprintf(stderr, "Failed to set %s \n", kEnvName);25 abort();26 }27 28 // rdar://problem/5878943929 // Now trigger symbolization. If symbolization tries to call30 // to `setenv` that adds a new environment variable, then Darwin31 // Libc will call `realloc()` and TSan's runtime will hit32 // an assertion failure because TSan's runtime uses a different33 // allocator during symbolization which leads to `realloc()` being34 // 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: DONE40 fprintf(stderr, "DONE\n");41 42 return 0;43}44