xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/initialization-nobug.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // A collection of various initializers which shouldn't trip up initialization
2*7c3d14c8STreehugger Robot // order checking.  If successful, this will just return 0.
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-nobug-extra.cc -o %t
5*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=check_initialization_order=true %run %t 2>&1
6*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O1 %s %p/Helpers/initialization-nobug-extra.cc -o %t
7*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=check_initialization_order=true %run %t 2>&1
8*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O2 %s %p/Helpers/initialization-nobug-extra.cc -o %t
9*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=check_initialization_order=true %run %t 2>&1
10*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O3 %s %p/Helpers/initialization-nobug-extra.cc -o %t
11*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=check_initialization_order=true %run %t 2>&1
12*7c3d14c8STreehugger Robot 
13*7c3d14c8STreehugger Robot // Simple access:
14*7c3d14c8STreehugger Robot // Make sure that accessing a global in the same TU is safe
15*7c3d14c8STreehugger Robot 
16*7c3d14c8STreehugger Robot bool condition = true;
initializeSameTU()17*7c3d14c8STreehugger Robot int initializeSameTU() {
18*7c3d14c8STreehugger Robot   return condition ? 0x2a : 052;
19*7c3d14c8STreehugger Robot }
20*7c3d14c8STreehugger Robot int sameTU = initializeSameTU();
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot // Linker initialized:
23*7c3d14c8STreehugger Robot // Check that access to linker initialized globals originating from a different
24*7c3d14c8STreehugger Robot // TU's initializer is safe.
25*7c3d14c8STreehugger Robot 
26*7c3d14c8STreehugger Robot int A = (1 << 1) + (1 << 3) + (1 << 5), B;
getAB()27*7c3d14c8STreehugger Robot int getAB() {
28*7c3d14c8STreehugger Robot   return A * B;
29*7c3d14c8STreehugger Robot }
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot // Function local statics:
32*7c3d14c8STreehugger Robot // Check that access to function local statics originating from a different
33*7c3d14c8STreehugger Robot // TU's initializer is safe.
34*7c3d14c8STreehugger Robot 
countCalls()35*7c3d14c8STreehugger Robot int countCalls() {
36*7c3d14c8STreehugger Robot   static int calls;
37*7c3d14c8STreehugger Robot   return ++calls;
38*7c3d14c8STreehugger Robot }
39*7c3d14c8STreehugger Robot 
40*7c3d14c8STreehugger Robot // Trivial constructor, non-trivial destructor.
41*7c3d14c8STreehugger Robot struct StructWithDtor {
~StructWithDtorStructWithDtor42*7c3d14c8STreehugger Robot   ~StructWithDtor() { }
43*7c3d14c8STreehugger Robot   int value;
44*7c3d14c8STreehugger Robot };
45*7c3d14c8STreehugger Robot StructWithDtor struct_with_dtor;
getStructWithDtorValue()46*7c3d14c8STreehugger Robot int getStructWithDtorValue() { return struct_with_dtor.value; }
47*7c3d14c8STreehugger Robot 
main()48*7c3d14c8STreehugger Robot int main() { return 0; }
49