1// Test to make sure basic initialization order errors are caught.
2
3// RUN: %clangxx_asan %min_macos_deployment_target=10.11 -O0 %s %p/Helpers/initialization-bug-extra2.cpp -o %t-INIT-ORDER-EXE
4// RUN: %env_asan_opts=check_initialization_order=true not %run %t-INIT-ORDER-EXE 2>&1 | FileCheck %s
5
6// Do not test with optimization -- the error may be optimized away.
7
8// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186
9// XFAIL: target={{.*windows-msvc.*}}
10
11#include "defines.h"
12#include <cstdio>
13
14// The structure of the test is:
15// "x", "y", "z" are dynamically initialized globals.
16// Value of "x" depends on "y", value of "y" depends on "z".
17// "x" and "z" are defined in this TU, "y" is defined in another one.
18// Thus we should stably report initialization order fiasco independently of
19// the translation unit order.
20
21int initZ() {
22 return 5;
23}
24int z = initZ();
25
26// 'y' is a dynamically initialized global residing in a different TU. This
27// dynamic initializer will read the value of 'y' before main starts. The
28// result is undefined behavior, which should be caught by initialization order
29// checking.
30extern int y;
31int ATTRIBUTE_NOINLINE initX() {
32 return y + 1;
33 // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
34 // CHECK: {{READ of size .* at 0x.* thread T0}}
35 // CHECK: {{0x.* is located 0 bytes inside of global variable .*(y|z).*}}
36 // CHECK: registered at:
37 // CHECK: 0x{{.*}} in __asan_register_globals
38}
39
40// This initializer begins our initialization order problems.
41static int x = initX();
42
43int main() {
44 // ASan should have caused an exit before main runs.
45 printf("PASS\n");
46 // CHECK-NOT: PASS
47 return 0;
48}
49

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of compiler-rt/test/asan/TestCases/initialization-bug.cpp