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