blob: 3a05cc2889ea004e0c23d74a980c36d22c71ae79 [file] [log] [blame]
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +00001// Test to make sure basic initialization order errors are caught.
2
Alexey Samsonov889e3ab2013-06-07 09:38:55 +00003// RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t
Alexey Samsonov13f89cd2013-06-28 15:52:44 +00004// RUN: ASAN_OPTIONS=check_initialization_order=true %t 2>&1 | FileCheck %s
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +00005
6// Do not test with optimization -- the error may be optimized away.
7
Alexey Samsonov3677b182013-05-21 10:11:17 +00008// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186
9// XFAIL: darwin
10
Alexey Samsonovf42e8602012-09-07 09:24:29 +000011#include <cstdio>
12
13// The structure of the test is:
14// "x", "y", "z" are dynamically initialized globals.
15// Value of "x" depends on "y", value of "y" depends on "z".
16// "x" and "z" are defined in this TU, "y" is defined in another one.
17// Thus we shoud stably report initialization order fiasco independently of
18// the translation unit order.
19
20int initZ() {
21 return 5;
22}
23int z = initZ();
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000024
25// 'y' is a dynamically initialized global residing in a different TU. This
26// dynamic initializer will read the value of 'y' before main starts. The
27// result is undefined behavior, which should be caught by initialization order
28// checking.
Alexey Samsonovf42e8602012-09-07 09:24:29 +000029extern int y;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000030int __attribute__((noinline)) initX() {
31 return y + 1;
Kostya Serebryany16205cd2012-10-15 13:04:58 +000032 // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000033 // CHECK: {{READ of size .* at 0x.* thread T0}}
Alexey Samsonovf42e8602012-09-07 09:24:29 +000034 // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}}
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000035}
36
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000037// This initializer begins our initialization order problems.
38static int x = initX();
39
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000040int main() {
Alexey Samsonovf42e8602012-09-07 09:24:29 +000041 // ASan should have caused an exit before main runs.
42 printf("PASS\n");
43 // CHECK-NOT: PASS
44 return 0;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000045}