• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // REQUIRES: stable-runtime
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <sanitizer/hwasan_interface.h>
13 
14 #include "utils.h"
15 
main()16 int main() {
17   __hwasan_enable_allocator_tagging();
18   char * volatile x = (char*)malloc(10);
19   free(x);
20   __hwasan_disable_allocator_tagging();
21   untag_fprintf(stderr, ISREAD ? "Going to do a READ\n" : "Going to do a WRITE\n");
22   // CHECK: Going to do a [[TYPE:[A-Z]*]]
23   int r = 0;
24   if (ISREAD) r = x[5]; else x[5] = 42;  // should be on the same line.
25   // 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)
26   // CHECK: #0 {{.*}} in main {{.*}}use-after-free.c:[[@LINE-2]]
27   // Offset is 5 or 11 depending on left/right alignment.
28   // CHECK: is a small unallocated heap chunk; size: 32 offset: {{5|11}}
29   // CHECK: is located 5 bytes inside of 10-byte region
30   //
31   // CHECK: freed by thread {{.*}} here:
32   // CHECK: #0 {{.*}} in {{.*}}free{{.*}} {{.*}}hwasan_interceptors.cpp
33   // CHECK: #1 {{.*}} in main {{.*}}use-after-free.c:[[@LINE-14]]
34 
35   // CHECK: previously allocated here:
36   // CHECK: #0 {{.*}} in {{.*}}malloc{{.*}} {{.*}}hwasan_interceptors.cpp
37   // CHECK: #1 {{.*}} in main {{.*}}use-after-free.c:[[@LINE-19]]
38   // CHECK: Memory tags around the buggy address (one tag corresponds to 16 bytes):
39   // CHECK: =>{{.*}}[[MEM_TAG]]
40   // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in main
41   return r;
42 }
43