Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / Linux / check_preinit.cpp
blobb5f63d3d4b9e361307924245d5b13a00de3e34c3
1 // RUN: %clang_tsan -fno-sanitize=thread -shared -fPIC -O1 -DBUILD_SO=1 %s -o \
2 // RUN: %t.so && \
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
13 // library.
15 #include <sanitizer/tsan_interface.h>
16 #include <stdio.h>
18 #if BUILD_SO
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;
30 #else // BUILD_SO
32 extern int 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;
43 namespace __tsan {
45 void OnInitialize() {
46 tsan_init_call = ++counter;
51 int main() {
52 // CHECK: TSAN_INIT 1
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);
58 return 0;
61 #endif // BUILD_SO