1// RUN: %clang_hwasan -O0 -DISREAD=1 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK
2// RUN: %clang_hwasan -O1 -DISREAD=1 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK
3// RUN: %clang_hwasan -O2 -DISREAD=1 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK
4// RUN: %clang_hwasan -O3 -DISREAD=1 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK
5
6// RUN: %clang_hwasan -O0 -DISREAD=0 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <sanitizer/hwasan_interface.h>
11
12int main() {
13 __hwasan_enable_allocator_tagging();
14 char * volatile x = (char*)malloc(size: 10);
15 free(ptr: x);
16 __hwasan_disable_allocator_tagging();
17 fprintf(stderr, ISREAD ? "Going to do a READ\n" : "Going to do a WRITE\n");
18 // CHECK: Going to do a [[TYPE:[A-Z]*]]
19 int r = 0;
20 if (ISREAD) r = x[5]; else x[5] = 42; // should be on the same line.
21 // CHECK: ERROR: HWAddressSanitizer: tag-mismatch on address {{.*}} at pc {{[0x]+}}[[PC:.*]]
22 // CHECK: [[TYPE]] of size 1 at {{.*}} tags: [[PTR_TAG:[0-9a-f][0-9a-f]]]/[[MEM_TAG:[0-9a-f][0-9a-f]]] (ptr/mem)
23 // CHECK: #{{[0-9]}} {{[0-9]+}}{{.*}}[[PC]]
24 // If we instrument using calls (default on x86), main is not the top frame
25 // of the fault.
26 // CHECK: in main {{.*}}use-after-free.c:[[@LINE-6]]
27 // Offset is 5 or 11 depending on left/right alignment.
28 // CHECK: is a small unallocated heap chunk; size: {{16|32}} offset: {{5|11}}
29 // CHECK: Cause: use-after-free
30 // CHECK: is located 5 bytes inside a 10-byte region
31 //
32 // CHECK: freed by thread {{.*}} here:
33 // CHECK: #0 {{.*}} in {{.*}}free{{.*}} {{.*}}hwasan_allocation_functions.cpp
34 // CHECK: #1 {{.*}} in main {{.*}}use-after-free.c:[[@LINE-19]]
35
36 // CHECK: previously allocated by thread {{.*}} here:
37 // CHECK: #0 {{.*}} in {{.*}}malloc{{.*}} {{.*}}hwasan_allocation_functions.cpp
38 // CHECK: #1 {{.*}} in main {{.*}}use-after-free.c:[[@LINE-24]]
39 // CHECK: Memory tags around the buggy address (one tag corresponds to 16 bytes):
40 // CHECK: =>{{.*}}[[MEM_TAG]]
41 // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in main
42 return r;
43}
44

source code of compiler-rt/test/hwasan/TestCases/use-after-free.c