xref: /llvm-project/compiler-rt/test/hwasan/TestCases/Linux/syscalls.cpp (revision ae6db862a9ea0977e44127eb36e5d25e5673df04)
1 // RUN: %clangxx_hwasan -O0 %s -o %t && not %env_hwasan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s --implicit-check-not=RETURN_FROM_TEST
2 // RUN: %clangxx_hwasan -O3 %s -o %t && not %env_hwasan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s --implicit-check-not=RETURN_FROM_TEST
3 // RUN: %clangxx_hwasan -O0 %s -o %t && not %env_hwasan_opts=halt_on_error=0:symbolize=0 %run %t 2>&1 | FileCheck %s --implicit-check-not=RETURN_FROM_TEST --check-prefixes=CHECK,RECOVER
4 
5 // UNSUPPORTED: android
6 
7 #include <assert.h>
8 #include <errno.h>
9 #include <glob.h>
10 #include <malloc.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include <sanitizer/hwasan_interface.h>
15 #include <sanitizer/linux_syscall_hooks.h>
16 
17 /* Test the presence of __sanitizer_syscall_ in the tool runtime, and general
18    sanity of their behaviour. */
19 
main(int argc,char * argv[])20 int main(int argc, char *argv[]) {
21   // lit.cfg.py currently sets 'disable_allocator_tagging=1'
22   __hwasan_enable_allocator_tagging();
23 
24   char *buf = (char *)malloc(1000);
25   assert(buf != NULL);
26 
27   __sanitizer_syscall_pre_recvmsg(0, buf - 1, 0);
28   // CHECK: HWAddressSanitizer: tag-mismatch on address [[PTR:0x[a-f0-9]+]]
29   // CHECK: Cause: heap-buffer-overflow
30   // CHECK: [[PTR]] is located 1 bytes before a 1000-byte region
31 
32   free(buf);
33   fprintf(stderr, "RETURN_FROM_TEST\n");
34   // RECOVER: RETURN_FROM_TEST
35   return 0;
36 }
37