1// RUN: %clang_hwasan -O0 %s -DMALLOC -DFREE -o %t.mf
2// RUN: %env_hwasan_opts=tag_in_malloc=0,tag_in_free=1 not %run %t.mf 2>&1 | FileCheck %s --check-prefixes=FREE
3// RUN: %env_hwasan_opts=tag_in_malloc=1,tag_in_free=1 not %run %t.mf 2>&1 | FileCheck %s --check-prefixes=MALLOC
4// RUN: %env_hwasan_opts=tag_in_malloc=1,tag_in_free=0 not %run %t.mf 2>&1 | FileCheck %s --check-prefixes=MALLOC
5// RUN: %env_hwasan_opts=tag_in_malloc=0,tag_in_free=0 %run %t.mf 2>&1
6
7// RUN: %clang_hwasan -O0 %s -DFREE -o %t.f
8// RUN: %env_hwasan_opts=tag_in_malloc=0,tag_in_free=1 not %run %t.f 2>&1 | FileCheck %s --check-prefixes=FREE
9// RUN: %env_hwasan_opts=tag_in_malloc=1,tag_in_free=1 not %run %t.f 2>&1 | FileCheck %s --check-prefixes=FREE
10// RUN: %env_hwasan_opts=tag_in_malloc=1,tag_in_free=0 %run %t.f 2>&1
11// RUN: %env_hwasan_opts=tag_in_malloc=0,tag_in_free=0 %run %t.f 2>&1
12
13// RUN: %clang_hwasan -O0 %s -DMALLOC -o %t.m
14// RUN: %env_hwasan_opts=tag_in_malloc=0,tag_in_free=1 %run %t.m 2>&1
15// RUN: %env_hwasan_opts=tag_in_malloc=1,tag_in_free=1 not %run %t.m 2>&1 | FileCheck %s --check-prefixes=MALLOC
16// RUN: %env_hwasan_opts=tag_in_malloc=1,tag_in_free=0 not %run %t.m 2>&1 | FileCheck %s --check-prefixes=MALLOC
17// RUN: %env_hwasan_opts=tag_in_malloc=0,tag_in_free=0 %run %t.m 2>&1
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <sanitizer/hwasan_interface.h>
22
23int main() {
24 __hwasan_enable_allocator_tagging();
25 // Loop for a while to make sure that the memory for the test below is reused after an earlier free(),
26 // and is potentially tagged (when tag_in_free == 1).
27 for (int i = 0; i < 100; ++i) {
28 char * volatile p = (char*)malloc(size: 10);
29 free(ptr: p);
30 }
31
32 char * volatile p = (char*)malloc(size: 10);
33#ifdef MALLOC
34 // MALLOC: READ of size 1 at
35 // MALLOC: is located 6 bytes after a 10-byte region
36 // MALLOC: allocated by thread T0 here:
37 char volatile x = p[16];
38#endif
39 free(ptr: p);
40#ifdef FREE
41 // FREE: READ of size 1 at
42 // FREE: is located 0 bytes inside a 10-byte region
43 // FREE: freed by thread T0 here:
44 // FREE: previously allocated by thread T0 here:
45 char volatile y = p[0];
46#endif
47
48 __hwasan_disable_allocator_tagging();
49
50 return 0;
51}
52

source code of compiler-rt/test/hwasan/TestCases/tag_in_free.c