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