1 // Tests use-after-scope detection and reporting.
2 // RUN: %clang_hwasan -O0 -g %s -o %t && not %run %t 2>&1 | FileCheck %s
3 // RUN: %clang_hwasan -O2 -g %s -o %t && not %run %t 2>&1 | FileCheck %s
4 // RUN: %clang_hwasan -O2 -g %s -DBUFFER_SIZE=1000000 -o %t && not %run %t 2>&1 | FileCheck %s
5 // RUN: %clang_hwasan -O2 -g %s -DBUFFER_SIZE=2000000 -o %t && not %run %t 2>&1 | FileCheck %s
6 // RUN: %clang_hwasan -g %s -o %t && not %env_hwasan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s --check-prefix=NOSYM
8 // RUN: %clang_hwasan -mllvm -hwasan-use-after-scope=false -g %s -o %t && %run %t 2>&1
10 // RUN: %clang_hwasan -g %s -o %t && not %run %t 2>&1 | FileCheck %s
12 // Run the same test as above, but using the __hwasan_add_frame_record libcall.
13 // The output should be the exact same.
14 // RUN: %clang_hwasan -mllvm -hwasan-record-stack-history=libcall -g %s -o %t && not %env_hwasan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s --check-prefix=NOSYM
16 // Stack histories currently are not recorded on x86.
17 // XFAIL: target=x86_64{{.*}}
20 #include <sanitizer/hwasan_interface.h>
24 # define BUFFER_SIZE 0x800
27 void USE(void *x
) { // pretend_to_do_something(void *x)
28 __asm__
__volatile__(""
34 __attribute__((noinline
)) void Unrelated1() {
38 __attribute__((noinline
)) void Unrelated2() {
42 __attribute__((noinline
)) void Unrelated3() {
47 __attribute__((noinline
)) char buggy() {
50 char zzz
[BUFFER_SIZE
] = {};
51 char yyy
[BUFFER_SIZE
] = {};
52 // With -hwasan-generate-tags-with-calls=false, stack tags can occasionally
53 // be zero, leading to a false negative
54 // (https://github.com/llvm/llvm-project/issues/69221). Work around it by
55 // using the neighboring variable, which is guaranteed by
56 // -hwasan-generate-tags-with-calls=false to have a different (hence
58 if (__hwasan_tag_pointer(zzz
, 0) == zzz
) {
59 assert(__hwasan_tag_pointer(yyy
, 0) != yyy
);
74 // CHECK: READ of size 1 at
75 // CHECK: #0 {{.*}} in buggy{{.*}}stack-uas.c:[[@LINE-10]]
76 // CHECK: Cause: stack tag-mismatch
77 // CHECK: is located in stack of thread
78 // CHECK: Potentially referenced stack objects:
79 // CHECK: Cause: use-after-scope
80 // CHECK-NEXT: 0x{{.*}} is located 0 bytes inside a {{.*}}-byte local variable {{zzz|yyy}} [0x{{.*}}) in buggy {{.*}}stack-uas.c:
81 // CHECK: Memory tags around the buggy address
83 // NOSYM: Previously allocated frames:
84 // NOSYM-NEXT: record_addr:0x{{.*}} record:0x{{.*}} ({{.*}}/stack-uas.c.tmp+0x{{.*}}){{$}}
85 // NOSYM-NEXT: record_addr:0x{{.*}} record:0x{{.*}} ({{.*}}/stack-uas.c.tmp+0x{{.*}}){{$}}
86 // NOSYM-NEXT: record_addr:0x{{.*}} record:0x{{.*}} ({{.*}}/stack-uas.c.tmp+0x{{.*}}){{$}}
87 // NOSYM-NEXT: record_addr:0x{{.*}} record:0x{{.*}} ({{.*}}/stack-uas.c.tmp+0x{{.*}}){{$}}
88 // NOSYM: Memory tags around the buggy address
90 // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in buggy