1*673dc3d4SNico Weber // Test to make sure basic initialization order errors are caught.
2*673dc3d4SNico Weber // Check that on Linux initialization order bugs are caught
3*673dc3d4SNico Weber // independently on order in which we list source files (if we specify
4*673dc3d4SNico Weber // strict init-order checking).
5*673dc3d4SNico Weber 
6*673dc3d4SNico Weber // RUN: %clangxx_asan -O0 %s %p/../Helpers/initialization-bug-extra.cpp -o %t
7*673dc3d4SNico Weber // RUN: %env_asan_opts=strict_init_order=true not %run %t 2>&1 | FileCheck %s
8*673dc3d4SNico Weber // RUN: %clangxx_asan -O0 %p/../Helpers/initialization-bug-extra.cpp %s -o %t
9*673dc3d4SNico Weber // RUN: %env_asan_opts=strict_init_order=true not %run %t 2>&1 | FileCheck %s
10*673dc3d4SNico Weber 
11*673dc3d4SNico Weber // Do not test with optimization -- the error may be optimized away.
12*673dc3d4SNico Weber 
13*673dc3d4SNico Weber #include <cstdio>
14*673dc3d4SNico Weber 
15*673dc3d4SNico Weber // 'y' is a dynamically initialized global residing in a different TU.  This
16*673dc3d4SNico Weber // dynamic initializer will read the value of 'y' before main starts.  The
17*673dc3d4SNico Weber // result is undefined behavior, which should be caught by initialization order
18*673dc3d4SNico Weber // checking.
19*673dc3d4SNico Weber extern int y;
initX()20*673dc3d4SNico Weber int __attribute__((noinline)) initX() {
21*673dc3d4SNico Weber   return y + 1;
22*673dc3d4SNico Weber   // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
23*673dc3d4SNico Weber   // CHECK: {{READ of size .* at 0x.* thread T0}}
24*673dc3d4SNico Weber   // CHECK: {{#0 0x.* in .*initX.* .*initialization-bug-any-order.cpp:}}[[@LINE-3]]
25*673dc3d4SNico Weber   // CHECK: {{0x.* is located 0 bytes inside of global variable .*y.*}}
26*673dc3d4SNico Weber }
27*673dc3d4SNico Weber 
28*673dc3d4SNico Weber // This initializer begins our initialization order problems.
29*673dc3d4SNico Weber static int x = initX();
30*673dc3d4SNico Weber 
main()31*673dc3d4SNico Weber int main() {
32*673dc3d4SNico Weber   // ASan should have caused an exit before main runs.
33*673dc3d4SNico Weber   printf("PASS\n");
34*673dc3d4SNico Weber   // CHECK-NOT: PASS
35*673dc3d4SNico Weber   return 0;
36*673dc3d4SNico Weber }
37