xref: /aosp_15_r20/external/compiler-rt/test/tsan/Linux/check_preinit.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clang_tsan -fno-sanitize=thread -shared -fPIC -O1 -DBUILD_SO=1 %s -o \
2*7c3d14c8STreehugger Robot // RUN:  %t.so && \
3*7c3d14c8STreehugger Robot // RUN:   %clang_tsan -O1 %s %t.so -o %t && %run %t 2>&1 | FileCheck %s
4*7c3d14c8STreehugger Robot // RUN: llvm-objdump -t %t | FileCheck %s --check-prefix=CHECK-DUMP
5*7c3d14c8STreehugger Robot // CHECK-DUMP:  {{[.]preinit_array.*__local_tsan_preinit}}
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot // SANITIZER_CAN_USE_PREINIT_ARRAY is undefined on android.
8*7c3d14c8STreehugger Robot // UNSUPPORTED: android
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot // Test checks if __tsan_init is called from .preinit_array.
11*7c3d14c8STreehugger Robot // Without initialization from .preinit_array, __tsan_init will be called from
12*7c3d14c8STreehugger Robot // constructors of the binary which are called after constructors of shared
13*7c3d14c8STreehugger Robot // library.
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #include <stdio.h>
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #if BUILD_SO
18*7c3d14c8STreehugger Robot 
19*7c3d14c8STreehugger Robot // "volatile" is needed to avoid compiler optimize-out constructors.
20*7c3d14c8STreehugger Robot volatile int counter = 0;
21*7c3d14c8STreehugger Robot volatile int lib_constructor_call = 0;
22*7c3d14c8STreehugger Robot volatile int tsan_init_call = 0;
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot __attribute__ ((constructor))
LibConstructor()25*7c3d14c8STreehugger Robot void LibConstructor() {
26*7c3d14c8STreehugger Robot   lib_constructor_call = ++counter;
27*7c3d14c8STreehugger Robot };
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot #else  // BUILD_SO
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot extern int counter;
32*7c3d14c8STreehugger Robot extern int lib_constructor_call;
33*7c3d14c8STreehugger Robot extern int tsan_init_call;
34*7c3d14c8STreehugger Robot 
35*7c3d14c8STreehugger Robot volatile int bin_constructor_call = 0;
36*7c3d14c8STreehugger Robot 
37*7c3d14c8STreehugger Robot __attribute__ ((constructor))
BinConstructor()38*7c3d14c8STreehugger Robot void BinConstructor() {
39*7c3d14c8STreehugger Robot   bin_constructor_call = ++counter;
40*7c3d14c8STreehugger Robot };
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot namespace __tsan {
43*7c3d14c8STreehugger Robot 
OnInitialize()44*7c3d14c8STreehugger Robot void OnInitialize() {
45*7c3d14c8STreehugger Robot   tsan_init_call = ++counter;
46*7c3d14c8STreehugger Robot }
47*7c3d14c8STreehugger Robot 
48*7c3d14c8STreehugger Robot }
49*7c3d14c8STreehugger Robot 
main()50*7c3d14c8STreehugger Robot int main() {
51*7c3d14c8STreehugger Robot   // CHECK: TSAN_INIT 1
52*7c3d14c8STreehugger Robot   // CHECK: LIB_CONSTRUCTOR 2
53*7c3d14c8STreehugger Robot   // CHECK: BIN_CONSTRUCTOR 3
54*7c3d14c8STreehugger Robot   printf("TSAN_INIT %d\n", tsan_init_call);
55*7c3d14c8STreehugger Robot   printf("LIB_CONSTRUCTOR %d\n", lib_constructor_call);
56*7c3d14c8STreehugger Robot   printf("BIN_CONSTRUCTOR %d\n", bin_constructor_call);
57*7c3d14c8STreehugger Robot   return 0;
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot 
60*7c3d14c8STreehugger Robot #endif  // BUILD_SO
61