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