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}}
6
7// SANITIZER_CAN_USE_PREINIT_ARRAY is undefined on android.
8// UNSUPPORTED: android
9
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.
14
15#include <sanitizer/tsan_interface.h>
16#include <stdio.h>
17
18#if BUILD_SO
19
20// "volatile" is needed to avoid compiler optimize-out constructors.
21volatile int counter = 0;
22volatile int lib_constructor_call = 0;
23volatile int tsan_init_call = 0;
24
25__attribute__ ((constructor))
26void LibConstructor() {
27 lib_constructor_call = ++counter;
28};
29
30#else // BUILD_SO
31
32extern int counter;
33extern int lib_constructor_call;
34extern int tsan_init_call;
35
36volatile int bin_constructor_call = 0;
37
38__attribute__ ((constructor))
39void BinConstructor() {
40 bin_constructor_call = ++counter;
41};
42
43namespace __tsan {
44
45void OnInitialize() {
46 tsan_init_call = ++counter;
47}
48
49}
50
51int main() {
52 // CHECK: TSAN_INIT 1
53 // CHECK: LIB_CONSTRUCTOR 2
54 // CHECK: BIN_CONSTRUCTOR 3
55 printf(format: "TSAN_INIT %d\n", tsan_init_call);
56 printf(format: "LIB_CONSTRUCTOR %d\n", lib_constructor_call);
57 printf(format: "BIN_CONSTRUCTOR %d\n", bin_constructor_call);
58 return 0;
59}
60
61#endif // BUILD_SO
62

source code of compiler-rt/test/tsan/Linux/check_preinit.cpp