xref: /llvm-project/compiler-rt/test/hwasan/TestCases/bcmp.cpp (revision ddf4a9ce636084b45b39634cabe4da25b73f0e0a)
1 // RUN: %clangxx_hwasan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2 // RUN: %clangxx_hwasan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
3 // RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
4 // RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
5 // REQUIRES: !android
6 
7 #include <assert.h>
8 #include <sanitizer/hwasan_interface.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 
13 __attribute__((no_sanitize("hwaddress"))) void
ForceCallInterceptor(void * p,const void * a,size_t size)14 ForceCallInterceptor(void *p, const void *a, size_t size) {
15   assert(bcmp(p, a, size) == 0);
16 }
17 
main(int argc,char ** argv)18 int main(int argc, char **argv) {
19   __hwasan_enable_allocator_tagging();
20   char a[] = {static_cast<char>(argc), 2, 3, 4};
21   int size = sizeof(a);
22   char *p = (char *)malloc(size);
23   memcpy(p, a, size);
24   free(p);
25   ForceCallInterceptor(p, a, size);
26   return 0;
27   // CHECK: HWAddressSanitizer: tag-mismatch on address
28   // CHECK: READ of size 4
29   // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-4]]
30   // CHECK: Cause: use-after-free
31   // CHECK: freed by thread
32   // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-8]]
33   // CHECK: previously allocated by thread
34   // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}bcmp.cpp:[[@LINE-12]]
35 }
36