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