1 // Checks that we do not print a faraway buffer overrun if we find a
3 // RUN: %clang_hwasan -O0 %s -o %t
4 // RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK
6 #include <sanitizer/hwasan_interface.h>
10 #define ALLOC_ATTEMPTS 256
12 char *Untag(void *x
) {
13 return (char *)__hwasan_tag_pointer(x
, 0);
16 void *FindMatch(void *ptrs
[ALLOC_ATTEMPTS
], void *value
) {
17 for (int i
= 0; i
< ALLOC_ATTEMPTS
; ++i
) {
20 int distance
= Untag(value
) - Untag(ptrs
[i
]);
21 // Leave at least one granule of gap to the allocation.
22 if (abs(distance
) < 1000 && abs(distance
) > 32)
28 int main(int argc
, char **argv
) {
29 __hwasan_enable_allocator_tagging();
30 void *ptrs
[ALLOC_ATTEMPTS
] = {};
31 // Find two allocations that are close enough so that they would be
32 // candidates as buffer overflows for each other.
35 for (int i
= 0; i
< ALLOC_ATTEMPTS
; ++i
) {
37 other
= FindMatch(ptrs
, one
);
43 fprintf(stderr
, "Could not find closeby allocations.\n");
46 __hwasan_tag_memory(Untag(one
), 3, 16);
47 __hwasan_tag_memory(Untag(other
), 3, 16);
48 // Tag potential adjaceant allocations with a mismatching tag, otherwise this
50 __hwasan_tag_memory(Untag(one
) + 16, 4, 16);
51 __hwasan_tag_memory(Untag(one
) - 16, 4, 16);
52 void *retagged_one
= __hwasan_tag_pointer(one
, 3);
54 volatile char *ptr
= (char *)retagged_one
;
58 // CHECK-NOT: Cause: heap-buffer-overflow
59 // CHECK: Cause: use-after-free
60 // CHECK-NOT: Cause: heap-buffer-overflow