Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 1 | // Test to make sure basic initialization order errors are caught. |
| 2 | |
Alexey Samsonov | 889e3ab | 2013-06-07 09:38:55 +0000 | [diff] [blame^] | 3 | // RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t |
| 4 | // RUN: ASAN_OPTIONS=check_initialization_order=true %t 2>&1 | %symbolize | 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 |
| 9 | // XFAIL: darwin |
| 10 | |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame] | 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(); |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 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. |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame] | 29 | extern int y; |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 30 | int __attribute__((noinline)) initX() { |
| 31 | return y + 1; |
Kostya Serebryany | 16205cd | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 32 | // CHECK: {{AddressSanitizer: initialization-order-fiasco}} |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 33 | // CHECK: {{READ of size .* at 0x.* thread T0}} |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame] | 34 | // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}} |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 37 | // This initializer begins our initialization order problems. |
| 38 | static int x = initX(); |
| 39 | |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 40 | int main() { |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame] | 41 | // ASan should have caused an exit before main runs. |
| 42 | printf("PASS\n"); |
| 43 | // CHECK-NOT: PASS |
| 44 | return 0; |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 45 | } |