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