Kostya Serebryany | 4d45b9b | 2012-09-05 09:07:02 +0000 | [diff] [blame] | 1 | // Test to make sure basic initialization order errors are caught. |
| 2 | |
| 3 | // RUN: %clangxx_asan -m64 -O0 %s %p/Helpers/initialization-bug-extra.cc\ |
| 4 | // RUN: -mllvm -asan-initialization-order -o %t && %t 2>&1 \ |
| 5 | // RUN: | %symbolize | FileCheck %s |
| 6 | // RUN: %clangxx_asan -m32 -O0 %s %p/Helpers/initialization-bug-extra.cc\ |
| 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 | |
| 12 | extern int y; |
| 13 | |
| 14 | // 'y' is a dynamically initialized global residing in a different TU. This |
| 15 | // dynamic initializer will read the value of 'y' before main starts. The |
| 16 | // result is undefined behavior, which should be caught by initialization order |
| 17 | // checking. |
| 18 | int __attribute__((noinline)) initX() { |
| 19 | return y + 1; |
| 20 | // CHECK: {{AddressSanitizer initialization-order-fiasco}} |
| 21 | // CHECK: {{READ of size .* at 0x.* thread T0}} |
| 22 | // CHECK: {{#0 0x.* in .*initX.* .*initialization-bug.cc:19}} |
| 23 | // CHECK: {{0x.* is located 0 bytes inside of global variable .*y.*}} |
| 24 | } |
| 25 | |
| 26 | |
| 27 | // This initializer begins our initialization order problems. |
| 28 | static int x = initX(); |
| 29 | |
| 30 | // ASan should have caused an exit before main runs. |
| 31 | int main() { |
| 32 | return -1; |
| 33 | } |