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 | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame^] | 3 | // RUN: %clangxx_asan -m64 -O0 %s %p/Helpers/initialization-bug-extra2.cc\ |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 4 | // RUN: -mllvm -asan-initialization-order -o %t && %t 2>&1 \ |
| 5 | // RUN: | %symbolize | FileCheck %s |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame^] | 6 | // RUN: %clangxx_asan -m32 -O0 %s %p/Helpers/initialization-bug-extra2.cc\ |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 7 | // RUN: -mllvm -asan-initialization-order -o %t && %t 2>&1 \ |
| 8 | // RUN: | %symbolize | FileCheck %s |
| 9 | |
| 10 | // Do not test with optimization -- the error may be optimized away. |
| 11 | |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame^] | 12 | #include <cstdio> |
| 13 | |
| 14 | // The structure of the test is: |
| 15 | // "x", "y", "z" are dynamically initialized globals. |
| 16 | // Value of "x" depends on "y", value of "y" depends on "z". |
| 17 | // "x" and "z" are defined in this TU, "y" is defined in another one. |
| 18 | // Thus we shoud stably report initialization order fiasco independently of |
| 19 | // the translation unit order. |
| 20 | |
| 21 | int initZ() { |
| 22 | return 5; |
| 23 | } |
| 24 | int z = initZ(); |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 25 | |
| 26 | // 'y' is a dynamically initialized global residing in a different TU. This |
| 27 | // dynamic initializer will read the value of 'y' before main starts. The |
| 28 | // result is undefined behavior, which should be caught by initialization order |
| 29 | // checking. |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame^] | 30 | extern int y; |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 31 | int __attribute__((noinline)) initX() { |
| 32 | return y + 1; |
| 33 | // CHECK: {{AddressSanitizer initialization-order-fiasco}} |
| 34 | // CHECK: {{READ of size .* at 0x.* thread T0}} |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame^] | 35 | // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}} |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 38 | // This initializer begins our initialization order problems. |
| 39 | static int x = initX(); |
| 40 | |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 41 | int main() { |
Alexey Samsonov | f42e860 | 2012-09-07 09:24:29 +0000 | [diff] [blame^] | 42 | // ASan should have caused an exit before main runs. |
| 43 | printf("PASS\n"); |
| 44 | // CHECK-NOT: PASS |
| 45 | return 0; |
Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 46 | } |