1// RUN: %clang_hwasan %s -o %t && %run %t
2
3#include <assert.h>
4#include <sanitizer/hwasan_interface.h>
5#include <stdio.h>
6#include <stdlib.h>
7
8int main() {
9 __hwasan_enable_allocator_tagging();
10 for (int sz = 0; sz < 64; ++sz) {
11 fprintf(stderr, format: "sz: %d\n", sz);
12 char *x = (char *)malloc(size: sz);
13 do {
14 // Empty range is always OK.
15 for (int b = -16; b < sz + 32; ++b)
16 assert(__hwasan_test_shadow(x + b, 0) == -1);
17
18 int real_sz = sz ? sz : 1;
19 // Unlucky case when we cant distinguish between tag and short granule size.
20 if (__hwasan_tag_pointer(p: x, tag: real_sz % 16) == x)
21 break;
22
23 // Underflow - the first byte is bad.
24 for (int b = -16; b < 0; ++b)
25 assert(__hwasan_test_shadow(x + b, real_sz) == 0);
26
27 // Inbound ranges.
28 for (int b = 0; b < real_sz; ++b)
29 for (int e = b; e <= real_sz; ++e)
30 assert(__hwasan_test_shadow(x + b, e - b) == -1);
31
32 // Overflow - the first byte after the buffer is bad.
33 for (int b = 0; b <= real_sz; ++b)
34 for (int e = real_sz + 1; e <= real_sz + 64; ++e)
35 assert(__hwasan_test_shadow(x + b, e - b) == (real_sz - b));
36
37 } while (0);
38 free(ptr: x);
39 }
40 return 0;
41}
42

source code of compiler-rt/test/hwasan/TestCases/test_shadow.c