35 lines · cpp
1// RUN: %clangxx_hwasan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s2// RUN: %clangxx_hwasan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s3// RUN: %clangxx_hwasan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s4// RUN: %clangxx_hwasan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s5 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"))) void13ForceCallInterceptor(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);22 memcpy(p, a, size);23 free(p);24 ForceCallInterceptor(p, a, size);25 return 0;26 // CHECK: HWAddressSanitizer: tag-mismatch on address27 // CHECK: READ of size 428 // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-4]]29 // CHECK: Cause: use-after-free30 // CHECK: freed by thread31 // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-8]]32 // CHECK: previously allocated by thread33 // CHECK: #{{[[:digit:]]+}} 0x{{[[:xdigit:]]+}} in main {{.*}}memcmp.cpp:[[@LINE-12]]34}35