1 // RUN: %clang_tsan -fno-sanitize=thread -shared -fPIC -O1 -DBUILD_SO=1 %s -o \
3 // RUN: %clang_tsan -O1 %s %t.so -o %t && %run %t 2>&1 | FileCheck %s
4 // RUN: llvm-objdump -t %t | FileCheck %s --check-prefix=CHECK-DUMP
5 // CHECK-DUMP: {{[.]preinit_array.*__local_tsan_preinit}}
7 // SANITIZER_CAN_USE_PREINIT_ARRAY is undefined on android.
8 // UNSUPPORTED: android
10 // Test checks if __tsan_init is called from .preinit_array.
11 // Without initialization from .preinit_array, __tsan_init will be called from
12 // constructors of the binary which are called after constructors of shared
15 #include <sanitizer/tsan_interface.h>
20 // "volatile" is needed to avoid compiler optimize-out constructors.
21 volatile int counter
= 0;
22 volatile int lib_constructor_call
= 0;
23 volatile int tsan_init_call
= 0;
25 __attribute__ ((constructor
))
26 void LibConstructor() {
27 lib_constructor_call
= ++counter
;
33 extern int lib_constructor_call
;
34 extern int tsan_init_call
;
36 volatile int bin_constructor_call
= 0;
38 __attribute__ ((constructor
))
39 void BinConstructor() {
40 bin_constructor_call
= ++counter
;
46 tsan_init_call
= ++counter
;
53 // CHECK: LIB_CONSTRUCTOR 2
54 // CHECK: BIN_CONSTRUCTOR 3
55 printf("TSAN_INIT %d\n", tsan_init_call
);
56 printf("LIB_CONSTRUCTOR %d\n", lib_constructor_call
);
57 printf("BIN_CONSTRUCTOR %d\n", bin_constructor_call
);