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
5 // REQUIRES: stable-runtime
7 #include <sanitizer/hwasan_interface.h>
11 #define ALLOC_ATTEMPTS 256
13 char *Untag(void *x
) {
14 return (char *)__hwasan_tag_pointer(x
, 0);
17 void *FindMatch(void *ptrs
[ALLOC_ATTEMPTS
], void *value
) {
18 for (int i
= 0; i
< ALLOC_ATTEMPTS
; ++i
) {
21 int distance
= Untag(value
) - Untag(ptrs
[i
]);
22 // Leave at least one granule of gap to the allocation.
23 if (abs(distance
) < 1000 && abs(distance
) > 32)
29 int main(int argc
, char **argv
) {
30 __hwasan_enable_allocator_tagging();
31 void *ptrs
[ALLOC_ATTEMPTS
] = {};
32 // Find two allocations that are close enough so that they would be
33 // candidates as buffer overflows for each other.
36 for (int i
= 0; i
< ALLOC_ATTEMPTS
; ++i
) {
38 other
= FindMatch(ptrs
, one
);
44 fprintf(stderr
, "Could not find closeby allocations.\n");
47 __hwasan_tag_memory(Untag(one
), 3, 16);
48 __hwasan_tag_memory(Untag(other
), 3, 16);
49 // Tag potential adjaceant allocations with a mismatching tag, otherwise this
51 __hwasan_tag_memory(Untag(one
) + 16, 4, 16);
52 __hwasan_tag_memory(Untag(one
) - 16, 4, 16);
53 void *retagged_one
= __hwasan_tag_pointer(one
, 3);
55 volatile char *ptr
= (char *)retagged_one
;
59 // CHECK-NOT: Cause: heap-buffer-overflow
60 // CHECK: Cause: use-after-free
61 // CHECK-NOT: Cause: heap-buffer-overflow