blob: c9562a433094d9acce4e8106b16930e370c71b93 [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\
Alexey Samsonov963be1d2013-03-14 11:49:40 +00004// RUN: -fsanitize=init-order -o %t
5// RUN: ASAN_OPTIONS=check_initialization_order=true %t 2>&1 \
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +00006// RUN: | %symbolize | FileCheck %s
Alexey Samsonovf42e8602012-09-07 09:24:29 +00007// RUN: %clangxx_asan -m32 -O0 %s %p/Helpers/initialization-bug-extra2.cc\
Alexey Samsonov963be1d2013-03-14 11:49:40 +00008// RUN: -fsanitize=init-order -o %t
9// RUN: ASAN_OPTIONS=check_initialization_order=true %t 2>&1 \
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000010// RUN: | %symbolize | FileCheck %s
11
12// Do not test with optimization -- the error may be optimized away.
13
Alexey Samsonovf42e8602012-09-07 09:24:29 +000014#include <cstdio>
15
16// The structure of the test is:
17// "x", "y", "z" are dynamically initialized globals.
18// Value of "x" depends on "y", value of "y" depends on "z".
19// "x" and "z" are defined in this TU, "y" is defined in another one.
20// Thus we shoud stably report initialization order fiasco independently of
21// the translation unit order.
22
23int initZ() {
24 return 5;
25}
26int z = initZ();
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000027
28// 'y' is a dynamically initialized global residing in a different TU. This
29// dynamic initializer will read the value of 'y' before main starts. The
30// result is undefined behavior, which should be caught by initialization order
31// checking.
Alexey Samsonovf42e8602012-09-07 09:24:29 +000032extern int y;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000033int __attribute__((noinline)) initX() {
34 return y + 1;
Kostya Serebryany16205cd2012-10-15 13:04:58 +000035 // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000036 // CHECK: {{READ of size .* at 0x.* thread T0}}
Alexey Samsonovf42e8602012-09-07 09:24:29 +000037 // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}}
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000038}
39
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000040// This initializer begins our initialization order problems.
41static int x = initX();
42
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000043int main() {
Alexey Samsonovf42e8602012-09-07 09:24:29 +000044 // ASan should have caused an exit before main runs.
45 printf("PASS\n");
46 // CHECK-NOT: PASS
47 return 0;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000048}