• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Test to make sure basic initialization order errors are caught.
2 
3 // RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t-INIT-ORDER-EXE
4 // RUN: %env_asan_opts=check_initialization_order=true not %run %t-INIT-ORDER-EXE 2>&1 | FileCheck %s
5 
6 // Do not test with optimization -- the error may be optimized away.
7 
8 // FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186
9 // XFAIL: darwin,win32
10 
11 // The test is expected to fail on OS X Yosemite and older
12 // UNSUPPORTED: osx-no-ld64-live_support
13 
14 #include <cstdio>
15 
16 // The structure of the test is:
17 // "x", "y", "z" are dynamically initialized globals.
18 // Value of "x" depends on "y", value of "y" depends on "z".
19 // "x" and "z" are defined in this TU, "y" is defined in another one.
20 // Thus we shoud stably report initialization order fiasco independently of
21 // the translation unit order.
22 
initZ()23 int initZ() {
24   return 5;
25 }
26 int z = initZ();
27 
28 // 'y' is a dynamically initialized global residing in a different TU.  This
29 // dynamic initializer will read the value of 'y' before main starts.  The
30 // result is undefined behavior, which should be caught by initialization order
31 // checking.
32 extern int y;
initX()33 int __attribute__((noinline)) initX() {
34   return y + 1;
35   // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
36   // CHECK: {{READ of size .* at 0x.* thread T0}}
37   // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}}
38   // CHECK: registered at:
39   // CHECK: 0x{{.*}} in __asan_register_globals
40 }
41 
42 // This initializer begins our initialization order problems.
43 static int x = initX();
44 
main()45 int main() {
46   // ASan should have caused an exit before main runs.
47   printf("PASS\n");
48   // CHECK-NOT: PASS
49   return 0;
50 }
51