diff --git a/compiler-rt/lib/hwasan/hwasan.cpp b/compiler-rt/lib/hwasan/hwasan.cpp --- a/compiler-rt/lib/hwasan/hwasan.cpp +++ b/compiler-rt/lib/hwasan/hwasan.cpp @@ -16,6 +16,7 @@ #include "hwasan_checks.h" #include "hwasan_dynamic_shadow.h" #include "hwasan_globals.h" +#include "hwasan_mapping.h" #include "hwasan_poisoning.h" #include "hwasan_report.h" #include "hwasan_thread.h" @@ -391,8 +392,15 @@ uptr shadow_last = MemToShadow(ptr_raw + sz - 1); Printf("HWASan shadow map for %zx .. %zx (pointer tag %x)\n", ptr_raw, ptr_raw + sz, GetTagFromPointer((uptr)p)); - for (uptr s = shadow_first; s <= shadow_last; ++s) - Printf(" %zx: %x\n", ShadowToMem(s), *(tag_t *)s); + for (uptr s = shadow_first; s <= shadow_last; ++s) { + tag_t mem_tag = *reinterpret_cast(s); + uptr granule_addr = ShadowToMem(s); + if (mem_tag && mem_tag < kShadowAlignment) + Printf(" %zx: %02x(%02x)\n", granule_addr, mem_tag, + *reinterpret_cast(granule_addr + kShadowAlignment - 1)); + else + Printf(" %zx: %02x\n", granule_addr, mem_tag); + } } sptr __hwasan_test_shadow(const void *p, uptr sz) { diff --git a/compiler-rt/test/hwasan/TestCases/hwasan-print-shadow.cpp b/compiler-rt/test/hwasan/TestCases/hwasan-print-shadow.cpp --- a/compiler-rt/test/hwasan/TestCases/hwasan-print-shadow.cpp +++ b/compiler-rt/test/hwasan/TestCases/hwasan-print-shadow.cpp @@ -8,26 +8,35 @@ #include int main() { - void *alloc = malloc(4096); + char *alloc = (char *)malloc(4096); + + // Simulate short granule tags. + alloc[15] = 0x00; + alloc[31] = 0xbb; + alloc[47] = 0xcc; + alloc[63] = 0xdd; + alloc[79] = 0xee; + alloc[95] = 0xff; // __hwasan_tag_memory expects untagged pointers. char *p = (char *)__hwasan_tag_pointer(alloc, 0); assert(p); + // Write tags to shadow. __hwasan_tag_memory(p, 1, 32); - __hwasan_tag_memory(p + 32, 3, 16); + __hwasan_tag_memory(p + 32, 16, 16); __hwasan_tag_memory(p + 48, 0, 32); __hwasan_tag_memory(p + 80, 4, 16); char *q = (char *)__hwasan_tag_pointer(p, 7); __hwasan_print_shadow(q + 5, 89 - 5); // CHECK: HWASan shadow map for {{.*}}5 .. {{.*}}9 (pointer tag 7) - // CHECK-NEXT: {{.*}}0: 1 - // CHECK-NEXT: {{.*}}0: 1 - // CHECK-NEXT: {{.*}}0: 3 - // CHECK-NEXT: {{.*}}0: 0 - // CHECK-NEXT: {{.*}}0: 0 - // CHECK-NEXT: {{.*}}0: 4 + // CHECK-NEXT: {{.*}}0: 01(00) + // CHECK-NEXT: {{.*}}0: 01(bb) + // CHECK-NEXT: {{.*}}0: 10 + // CHECK-NEXT: {{.*}}0: 00 + // CHECK-NEXT: {{.*}}0: 00 + // CHECK-NEXT: {{.*}}0: 04(ff) free(alloc); }