xref: /llvm-project/compiler-rt/test/hwasan/TestCases/report-unmapped.cpp (revision 71e5652f47b0d02a54aa9582319648bc4c23842c)
1 // RUN: %clangxx_hwasan %s -o %t && not %run %t 2>&1 | FileCheck %s
2 
3 #include <sanitizer/hwasan_interface.h>
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/mman.h>
8 #include <unistd.h>
9 
main(int argc,char ** argv)10 int main(int argc, char **argv) {
11   const size_t kPS = sysconf(_SC_PAGESIZE);
12 
13   void *r = nullptr;
14   int res = posix_memalign(&r, kPS, 2 * kPS);
15   if (res) {
16     perror("Failed to mmap: ");
17     abort();
18   }
19 
20   r = __hwasan_tag_pointer(r, 0);
21   __hwasan_tag_memory(r, 1,  2 * kPS);
22 
23   // Disable access to the page just after tag-mismatch report address.
24   res = mprotect((char*)r + kPS, kPS, PROT_NONE);
25   if (res) {
26     perror("Failed to mprotect: ");
27     abort();
28   }
29 
30   // Trigger tag-mismatch report.
31   return *((char*)r + kPS - 1);
32 }
33 
34 // CHECK: ERROR: HWAddressSanitizer: tag-mismatch on address
35 // CHECK: Memory tags around the buggy address
36 // CHECK: Tags for short granules around
37 
38 // Check that report is complete.
39 // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in main
40