1// RUN: %clang_hwasan %s -o %t && not %env_hwasan_opts=verbose_threads=1 %run %t 2>&1 | FileCheck %s
2
3#include <pthread.h>
4#include <stdlib.h>
5#include <stdio.h>
6
7#include <sanitizer/hwasan_interface.h>
8
9void *BoringThread(void *arg) {
10 char * volatile x = (char*)malloc(size: 10);
11 x[5] = 0;
12 free(ptr: x);
13 return NULL;
14}
15
16// CHECK: Creating : T0
17// CHECK: Creating : T1
18// CHECK: Destroying: T1
19// CHECK: Creating : T1100
20// CHECK: Destroying: T1100
21// CHECK: Creating : T1101
22
23void *UAFThread(void *arg) {
24 char * volatile x = (char*)malloc(size: 10);
25 fprintf(stderr, format: "ZZZ %p\n", x);
26 free(ptr: x);
27 x[5] = 42;
28 // CHECK: ERROR: HWAddressSanitizer: tag-mismatch on address
29 // CHECK: WRITE of size 1
30 // CHECK: many-threads-uaf.c:[[@LINE-3]]
31 // CHECK: Thread: T1101
32 return NULL;
33}
34
35int main() {
36 __hwasan_enable_allocator_tagging();
37 pthread_t t;
38 for (int i = 0; i < 1100; i++) {
39 pthread_create(newthread: &t, NULL, start_routine: BoringThread, NULL);
40 pthread_join(th: t, NULL);
41 }
42 pthread_create(newthread: &t, NULL, start_routine: UAFThread, NULL);
43 pthread_join(th: t, NULL);
44}
45

source code of compiler-rt/test/hwasan/TestCases/many-threads-uaf.c