blob: 1f843becaed0a4acb5cf6a6857045e8dd2351dc5 [file] [log] [blame]
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +00001// Test to make sure basic initialization order errors are caught.
2
Alexey Samsonov9e463ba2015-04-22 20:30:19 +00003// RUN: %clangxx_asan -O0 %s %p/Helpers/initialization-bug-extra2.cc -o %t-INIT-ORDER-EXE
Reid Kleckner85220d02015-08-12 23:50:12 +00004// RUN: %env_asan_opts=check_initialization_order=true not %run %t-INIT-ORDER-EXE 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
Ryan Govostesdc91fe52016-03-28 20:28:17 +00009// XFAIL: win32
Alexey Samsonov3677b182013-05-21 10:11:17 +000010
Ryan Govostese0f41da2016-03-30 22:21:58 +000011// The test is expected to fail on OS X Yosemite and older
12// UNSUPPORTED: osx-no-ld64-live_support
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).*}}
Alexey Samsonov9e463ba2015-04-22 20:30:19 +000038 // CHECK: registered at:
Alexey Samsonov26856432015-04-23 18:43:08 +000039 // CHECK: 0x{{.*}} in __asan_register_globals
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000040}
41
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000042// This initializer begins our initialization order problems.
43static int x = initX();
44
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000045int main() {
Alexey Samsonovf42e8602012-09-07 09:24:29 +000046 // ASan should have caused an exit before main runs.
47 printf("PASS\n");
48 // CHECK-NOT: PASS
49 return 0;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000050}