Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 1 | // Test to make sure basic initialization order errors are caught. |
| 2 | |
Alexey Samsonov | c1b3c52 | 2013-03-14 12:43:03 +0000 | [diff] [blame] | 3 | // RUN: %clangxx_asan -m64 -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t |
Alexey Samsonov | 566c0a1 | 2013-03-14 11:49:40 +0000 | [diff] [blame] | 4 | // RUN: ASAN_OPTIONS=check_initialization_order=true %t 2>&1 \ |
Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 5 | // RUN: | %symbolize | FileCheck %s |
Alexey Samsonov | c1b3c52 | 2013-03-14 12:43:03 +0000 | [diff] [blame] | 6 | // RUN: %clangxx_asan -m32 -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t |
Alexey Samsonov | 566c0a1 | 2013-03-14 11:49:40 +0000 | [diff] [blame] | 7 | // RUN: ASAN_OPTIONS=check_initialization_order=true %t 2>&1 \ |
Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 8 | // RUN: | %symbolize | FileCheck %s |
| 9 | |
| 10 | // Do not test with optimization -- the error may be optimized away. |
| 11 | |
Alexey Samsonov | 37642cf | 2013-05-21 10:11:17 +0000 | [diff] [blame^] | 12 | // FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186 |
| 13 | // XFAIL: darwin |
| 14 | |
Alexey Samsonov | d796359 | 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 | 5f799c7 | 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 | d796359 | 2012-09-07 09:24:29 +0000 | [diff] [blame] | 33 | extern int y; |
Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 34 | int __attribute__((noinline)) initX() { |
| 35 | return y + 1; |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 36 | // CHECK: {{AddressSanitizer: initialization-order-fiasco}} |
Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 37 | // CHECK: {{READ of size .* at 0x.* thread T0}} |
Alexey Samsonov | d796359 | 2012-09-07 09:24:29 +0000 | [diff] [blame] | 38 | // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}} |
Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 41 | // This initializer begins our initialization order problems. |
| 42 | static int x = initX(); |
| 43 | |
Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 44 | int main() { |
Alexey Samsonov | d796359 | 2012-09-07 09:24:29 +0000 | [diff] [blame] | 45 | // ASan should have caused an exit before main runs. |
| 46 | printf("PASS\n"); |
| 47 | // CHECK-NOT: PASS |
| 48 | return 0; |
Kostya Serebryany | 5f799c7 | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 49 | } |