xref: /llvm-project/compiler-rt/test/hwasan/TestCases/memset-recover.cpp (revision ae6db862a9ea0977e44127eb36e5d25e5673df04)
1 // RUN: %clangxx_hwasan %s -o %t
2 // RUN: %env_hwasan_opts=halt_on_error=0 not %run %t 2>&1 | FileCheck %s --implicit-check-not=RETURN_FROM_TEST --check-prefixes=CHECK,RECOVER
3 // RUN: %env_hwasan_opts=halt_on_error=1 not %run %t 2>&1 | FileCheck %s --implicit-check-not=RETURN_FROM_TEST
4 
5 #include <sanitizer/hwasan_interface.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 
11 __attribute__((no_sanitize("hwaddress"))) void
ForceCallInterceptor(void * p,int c,size_t size)12 ForceCallInterceptor(void *p, int c, size_t size) {
13   memset(p, c, size) == nullptr;
14 }
15 
main(int argc,char ** argv)16 int main(int argc, char **argv) {
17   __hwasan_enable_allocator_tagging();
18   char a[] = {static_cast<char>(argc), 2, 3, 4};
19   int size = sizeof(a);
20   char *volatile p = (char *)malloc(size);
21   void *volatile p2 = p;
22   for (int i = 0; p2 == p; p2 = __hwasan_tag_pointer(p, ++i)) {
23   }
24   ForceCallInterceptor(p2, 0, size);
25   free(p);
26   fprintf(stderr, "RETURN_FROM_TEST\n");
27   return 0;
28   // CHECK: HWAddressSanitizer: tag-mismatch on address
29   // CHECK: WRITE of size 4
30   // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memset-recover.cpp:[[@LINE-28]]
31   // RECOVER: RETURN_FROM_TEST
32 }
33