1// Test with "-O2" only to make sure inlining (leading to use-after-scope)
2// happens. "always_inline" is not enough, as Clang doesn't emit
3// llvm.lifetime intrinsics at -O0.
4//
5// RUN: %clangxx_asan -m64 -O2 -fsanitize=use-after-scope %s -o %t && \
6// RUN:     %t 2>&1 | %symbolize | FileCheck %s
7// RUN: %clangxx_asan -m32 -O2 -fsanitize=use-after-scope %s -o %t && \
8// RUN:     %t 2>&1 | %symbolize | FileCheck %s
9
10int *arr;
11
12__attribute__((always_inline))
13void inlined(int arg) {
14  int x[5];
15  for (int i = 0; i < arg; i++) x[i] = i;
16  arr = x;
17}
18
19int main(int argc, char *argv[]) {
20  inlined(argc);
21  return arr[argc - 1];  // BOOM
22  // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
23  // CHECK: READ of size 4 at 0x{{.*}} thread T0
24  // CHECK:   #0 0x{{.*}} in {{_?}}main
25  // CHECK:      {{.*}}use-after-scope-inlined.cc:[[@LINE-4]]
26  // CHECK: Address 0x{{.*}} is located at offset
27  // CHECK:      [[OFFSET:[^ ]*]] in frame <main> of T0{{.*}}:
28  // CHECK:   {{\[}}[[OFFSET]], {{.*}}) 'x.i'
29}
30