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
13ForceCallInterceptor(void *p, const void *a, size_t size) {
14 assert(memcmp(p, a, size) == 0);
15}
16
17int 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: size);
22 memcpy(dest: p, src: a, n: size);
23 free(ptr: 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

source code of compiler-rt/test/hwasan/TestCases/memcmp.cpp