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