1// Test to make sure basic initialization order errors are caught.
2// Check that on Linux initialization order bugs are caught
3// independently on order in which we list source files (if we specify
4// strict init-order checking).
5
6// RUN: %clangxx_asan -O0 %s %p/../Helpers/initialization-bug-extra.cc -o %t
7// RUN: ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true not %t 2>&1 \
8// RUN:    | FileCheck %s
9// RUN: %clangxx_asan -O0 %p/../Helpers/initialization-bug-extra.cc %s -o %t
10// RUN: ASAN_OPTIONS=check_initialization_order=true:strict_init_order=true not %t 2>&1 \
11// RUN:    | FileCheck %s
12
13// Do not test with optimization -- the error may be optimized away.
14
15#include <cstdio>
16
17// 'y' is a dynamically initialized global residing in a different TU.  This
18// dynamic initializer will read the value of 'y' before main starts.  The
19// result is undefined behavior, which should be caught by initialization order
20// checking.
21extern int y;
22int __attribute__((noinline)) initX() {
23  return y + 1;
24  // CHECK: {{AddressSanitizer: initialization-order-fiasco}}
25  // CHECK: {{READ of size .* at 0x.* thread T0}}
26  // CHECK: {{#0 0x.* in .*initX.* .*initialization-bug-any-order.cc:}}[[@LINE-3]]
27  // CHECK: {{0x.* is located 0 bytes inside of global variable .*y.*}}
28}
29
30// This initializer begins our initialization order problems.
31static int x = initX();
32
33int main() {
34  // ASan should have caused an exit before main runs.
35  printf("PASS\n");
36  // CHECK-NOT: PASS
37  return 0;
38}
39