blob: ee2c725f0b13ba4e5a4c1565f7952bcd2ccf7041 [file] [log] [blame]
Kostya Serebryany5f799c72012-09-05 09:07:02 +00001// Test to make sure basic initialization order errors are caught.
2
Alexey Samsonovc1b3c522013-03-14 12:43:03 +00003// RUN: %clangxx_asan -m64 -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t
Alexey Samsonov566c0a12013-03-14 11:49:40 +00004// RUN: ASAN_OPTIONS=check_initialization_order=true %t 2>&1 \
Kostya Serebryany5f799c72012-09-05 09:07:02 +00005// RUN: | %symbolize | FileCheck %s
Alexey Samsonovc1b3c522013-03-14 12:43:03 +00006// RUN: %clangxx_asan -m32 -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t
Alexey Samsonov566c0a12013-03-14 11:49:40 +00007// RUN: ASAN_OPTIONS=check_initialization_order=true %t 2>&1 \
Kostya Serebryany5f799c72012-09-05 09:07:02 +00008// RUN: | %symbolize | FileCheck %s
9
10// Do not test with optimization -- the error may be optimized away.
11
Alexey Samsonov37642cf2013-05-21 10:11:17 +000012// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186
13// XFAIL: darwin
14
Alexey Samsonovd7963592012-09-07 09:24:29 +000015#include <cstdio>
16
17// The structure of the test is:
18// "x", "y", "z" are dynamically initialized globals.
19// Value of "x" depends on "y", value of "y" depends on "z".
20// "x" and "z" are defined in this TU, "y" is defined in another one.
21// Thus we shoud stably report initialization order fiasco independently of
22// the translation unit order.
23
24int initZ() {
25 return 5;
26}
27int z = initZ();
Kostya Serebryany5f799c72012-09-05 09:07:02 +000028
29// 'y' is a dynamically initialized global residing in a different TU. This
30// dynamic initializer will read the value of 'y' before main starts. The
31// result is undefined behavior, which should be caught by initialization order
32// checking.
Alexey Samsonovd7963592012-09-07 09:24:29 +000033extern int y;
Kostya Serebryany5f799c72012-09-05 09:07:02 +000034int __attribute__((noinline)) initX() {
35 return y + 1;
Kostya Serebryany69d8ede2012-10-15 13:04:58 +000036 // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
Kostya Serebryany5f799c72012-09-05 09:07:02 +000037 // CHECK: {{READ of size .* at 0x.* thread T0}}
Alexey Samsonovd7963592012-09-07 09:24:29 +000038 // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}}
Kostya Serebryany5f799c72012-09-05 09:07:02 +000039}
40
Kostya Serebryany5f799c72012-09-05 09:07:02 +000041// This initializer begins our initialization order problems.
42static int x = initX();
43
Kostya Serebryany5f799c72012-09-05 09:07:02 +000044int main() {
Alexey Samsonovd7963592012-09-07 09:24:29 +000045 // ASan should have caused an exit before main runs.
46 printf("PASS\n");
47 // CHECK-NOT: PASS
48 return 0;
Kostya Serebryany5f799c72012-09-05 09:07:02 +000049}