Skip to content

Commit 433b2ea

Browse files
committed
[hwasan] Always untag short granule in shadow.
Fixes a regression when the allocator is disabled, and a dirty allocation is re-used. This only occurs when the allocator is disabled, so a test-only fix, but still necessary. Reviewed By: eugenis Differential Revision: https://reviews.llvm.org/D108650
1 parent d2bb6d5 commit 433b2ea

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

compiler-rt/lib/hwasan/hwasan_allocator.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
162162
internal_memset(allocated, flags()->malloc_fill_byte, fill_size);
163163
}
164164
if (size != orig_size) {
165-
internal_memcpy(reinterpret_cast<u8 *>(allocated) + orig_size, tail_magic,
166-
size - orig_size - 1);
165+
u8 *tail = reinterpret_cast<u8 *>(allocated) + orig_size;
166+
uptr tail_length = size - orig_size;
167+
internal_memcpy(tail, tail_magic, tail_length - 1);
168+
// Short granule is excluded from magic tail, so we explicitly untag.
169+
tail[tail_length - 1] = 0;
167170
}
168171

169172
void *user_ptr = allocated;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clangxx_hwasan %s -o %t && %run %t 2>&1
2+
3+
#include <sanitizer/hwasan_interface.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
// Regression test for https://reviews.llvm.org/D107938#2961070, where, on
9+
// reusing an allocation, we forgot to reset the short granule tag if the
10+
// allocator was disabled. This lead to a false positive magic-string mismatch.
11+
12+
int main() {
13+
void *p = malloc(16);
14+
memset(p, 0xff, 16);
15+
free(p);
16+
17+
// Relies on the LRU cache immediately recycling the allocation above.
18+
p = malloc(8);
19+
free(p); // Regression was here, in the magic-string check in the runtime.
20+
return 0;
21+
}

0 commit comments

Comments
 (0)