Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // Test to make sure basic initialization order errors are caught. |
| 2 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 3 | // RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t-INIT-ORDER-EXE |
| 4 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:check_initialization_order=true not %run %t-INIT-ORDER-EXE 2>&1 | FileCheck %s |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 |
| 10 | |
| 11 | #include <cstdio> |
| 12 | |
| 13 | // The structure of the test is: |
| 14 | // "x", "y", "z" are dynamically initialized globals. |
| 15 | // Value of "x" depends on "y", value of "y" depends on "z". |
| 16 | // "x" and "z" are defined in this TU, "y" is defined in another one. |
| 17 | // Thus we shoud stably report initialization order fiasco independently of |
| 18 | // the translation unit order. |
| 19 | |
| 20 | int initZ() { |
| 21 | return 5; |
| 22 | } |
| 23 | int z = initZ(); |
| 24 | |
| 25 | // 'y' is a dynamically initialized global residing in a different TU. This |
| 26 | // dynamic initializer will read the value of 'y' before main starts. The |
| 27 | // result is undefined behavior, which should be caught by initialization order |
| 28 | // checking. |
| 29 | extern int y; |
| 30 | int __attribute__((noinline)) initX() { |
| 31 | return y + 1; |
| 32 | // CHECK: {{AddressSanitizer: initialization-order-fiasco}} |
| 33 | // CHECK: {{READ of size .* at 0x.* thread T0}} |
| 34 | // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}} |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 35 | // CHECK: registered at: |
| 36 | // CHECK: 0x{{.*}} in __asan_register_globals |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // This initializer begins our initialization order problems. |
| 40 | static int x = initX(); |
| 41 | |
| 42 | int main() { |
| 43 | // ASan should have caused an exit before main runs. |
| 44 | printf("PASS\n"); |
| 45 | // CHECK-NOT: PASS |
| 46 | return 0; |
| 47 | } |