Home | History | Annotate | Download | only in Linux
      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.
      4 
      5 // RUN: %clangxx_asan -m64 -O0 %s %p/../Helpers/initialization-bug-extra.cc\
      6 // RUN:   -mllvm -asan-initialization-order -o %t && %t 2>&1 \
      7 // RUN:    | %symbolize | FileCheck %s
      8 // RUN: %clangxx_asan -m64 -O0 %p/../Helpers/initialization-bug-extra.cc %s\
      9 // RUN:   -mllvm -asan-initialization-order -o %t && %t 2>&1 \
     10 // RUN:    | %symbolize | FileCheck %s
     11 
     12 // Do not test with optimization -- the error may be optimized away.
     13 
     14 #include <cstdio>
     15 
     16 // 'y' is a dynamically initialized global residing in a different TU.  This
     17 // dynamic initializer will read the value of 'y' before main starts.  The
     18 // result is undefined behavior, which should be caught by initialization order
     19 // checking.
     20 extern int y;
     21 int __attribute__((noinline)) initX() {
     22   return y + 1;
     23   // CHECK: {{AddressSanitizer initialization-order-fiasco}}
     24   // CHECK: {{READ of size .* at 0x.* thread T0}}
     25   // CHECK: {{#0 0x.* in .*initX.* .*initialization-bug-any-order.cc:22}}
     26   // CHECK: {{0x.* is located 0 bytes inside of global variable .*y.*}}
     27 }
     28 
     29 // This initializer begins our initialization order problems.
     30 static int x = initX();
     31 
     32 int main() {
     33   // ASan should have caused an exit before main runs.
     34   printf("PASS\n");
     35   // CHECK-NOT: PASS
     36   return 0;
     37 }
     38