blob: 20fdbfe5f59dd866f367abb9c2e918ae1b40944f [file] [log] [blame]
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +00001// Test to make sure basic initialization order errors are caught.
2
Kuba Mracekd692ea12016-11-21 21:48:25 +00003// RUN: %clangxx_asan %macos_min_target_10_11 -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
Petr Hosekeb46c952018-08-09 02:16:18 +00009// XFAIL: windows-msvc
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
Kuba Mracekc0805982017-04-26 21:34:18 +000013// UNSUPPORTED: ios
Ryan Govostese0f41da2016-03-30 22:21:58 +000014
Alexey Samsonovf42e8602012-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 Serebryany4d45b9b2012-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 Samsonovf42e8602012-09-07 09:24:29 +000033extern int y;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000034int __attribute__((noinline)) initX() {
35 return y + 1;
Kostya Serebryany16205cd2012-10-15 13:04:58 +000036 // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000037 // CHECK: {{READ of size .* at 0x.* thread T0}}
Alexey Samsonovf42e8602012-09-07 09:24:29 +000038 // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}}
Alexey Samsonov9e463ba2015-04-22 20:30:19 +000039 // CHECK: registered at:
Alexey Samsonov26856432015-04-23 18:43:08 +000040 // CHECK: 0x{{.*}} in __asan_register_globals
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000041}
42
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000043// This initializer begins our initialization order problems.
44static int x = initX();
45
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000046int main() {
Alexey Samsonovf42e8602012-09-07 09:24:29 +000047 // ASan should have caused an exit before main runs.
48 printf("PASS\n");
49 // CHECK-NOT: PASS
50 return 0;
Kostya Serebryany4d45b9b2012-09-05 09:07:02 +000051}