xref: /llvm-project/compiler-rt/test/asan/TestCases/asan_update_allocation.cpp (revision 091706269cea817f62447afa83a8d34284916679)
1 // RUN: %clangxx_asan -O0 %s --std=c++14 -o %t
2 
3 // RUN: not %run %t 10 0 2>&1 | FileCheck %s --check-prefixes=CHECK,T0
4 // RUN: not %run %t 10000000 0 2>&1 | FileCheck %s --check-prefixes=CHECK,T0
5 
6 // RUN: not %run %t 10 1 2>&1 | FileCheck %s --check-prefixes=CHECK,T1
7 // RUN: not %run %t 10000000 1 2>&1 | FileCheck %s --check-prefixes=CHECK,T1
8 
9 // REQUIRES: stable-runtime
10 
11 #include <sanitizer/asan_interface.h>
12 #include <stdlib.h>
13 #include <thread>
14 
UPDATE(void * p)15 void UPDATE(void *p) {
16   __asan_update_allocation_context(p);
17 }
18 
main(int argc,char * argv[])19 int main(int argc, char *argv[]) {
20   char *x = (char *)malloc(atoi(argv[1]) * sizeof(char));
21   if (atoi(argv[2]))
22     std::thread([&]() { UPDATE(x); }).join();
23   else
24     UPDATE(x);
25   free(x);
26   return x[5];
27   // CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
28   // CHECK: READ of size 1 at {{.*}} thread T0
29   // T0: allocated by thread T0 here
30   // T1: allocated by thread T1 here
31   // CHECK: UPDATE
32 }
33