blob: 90e4db9aefcc3ebca01dddc46896dbcd21d2a2f6 [file] [log] [blame]
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +00001// Test to make sure basic initialization order errors are caught.
2
Alexey Samsonovf42e8602012-09-07 09:24:29 +00003// RUN: %clangxx_asan -m64 -O0 %s %p/Helpers/initialization-bug-extra2.cc\
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +00004// RUN: -mllvm -asan-initialization-order -o %t && %t 2>&1 \
5// RUN: | %symbolize | FileCheck %s
Alexey Samsonovf42e8602012-09-07 09:24:29 +00006// RUN: %clangxx_asan -m32 -O0 %s %p/Helpers/initialization-bug-extra2.cc\
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +00007// 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 Samsonovf42e8602012-09-07 09:24:29 +000012#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
21int initZ() {
22 return 5;
23}
24int z = initZ();
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000025
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 Samsonovf42e8602012-09-07 09:24:29 +000030extern int y;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000031int __attribute__((noinline)) initX() {
32 return y + 1;
Kostya Serebryany16205cd2012-10-15 13:04:58 +000033 // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000034 // CHECK: {{READ of size .* at 0x.* thread T0}}
Alexey Samsonovf42e8602012-09-07 09:24:29 +000035 // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}}
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000036}
37
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000038// This initializer begins our initialization order problems.
39static int x = initX();
40
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000041int main() {
Alexey Samsonovf42e8602012-09-07 09:24:29 +000042 // ASan should have caused an exit before main runs.
43 printf("PASS\n");
44 // CHECK-NOT: PASS
45 return 0;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000046}