Skip to content

Commit d87468e

Browse files
committed
[HWASAN] Add support for max_allocation_size_mb option
Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D143667
1 parent 5a6bc2d commit d87468e

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

compiler-rt/lib/hwasan/hwasan_allocator.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum {
4545

4646
// Initialized in HwasanAllocatorInit, an never changed.
4747
static ALIGNED(16) u8 tail_magic[kShadowAlignment - 1];
48+
static uptr max_malloc_size;
4849

4950
bool HwasanChunkView::IsAllocated() const {
5051
return metadata_ && metadata_->IsAllocated();
@@ -142,6 +143,12 @@ void HwasanAllocatorInit() {
142143
GetAliasRegionStart());
143144
for (uptr i = 0; i < sizeof(tail_magic); i++)
144145
tail_magic[i] = GetCurrentThread()->GenerateRandomTag();
146+
if (common_flags()->max_allocation_size_mb) {
147+
max_malloc_size = common_flags()->max_allocation_size_mb << 20;
148+
max_malloc_size = Min(max_malloc_size, kMaxAllowedMallocSize);
149+
} else {
150+
max_malloc_size = kMaxAllowedMallocSize;
151+
}
145152
}
146153

147154
void HwasanAllocatorLock() { allocator.ForceLock(); }
@@ -164,13 +171,13 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
164171
// Keep this consistent with LSAN and ASAN behavior.
165172
if (UNLIKELY(orig_size == 0))
166173
orig_size = 1;
167-
if (UNLIKELY(orig_size > kMaxAllowedMallocSize)) {
174+
if (UNLIKELY(orig_size > max_malloc_size)) {
168175
if (AllocatorMayReturnNull()) {
169176
Report("WARNING: HWAddressSanitizer failed to allocate 0x%zx bytes\n",
170177
orig_size);
171178
return nullptr;
172179
}
173-
ReportAllocationSizeTooBig(orig_size, kMaxAllowedMallocSize, stack);
180+
ReportAllocationSizeTooBig(orig_size, max_malloc_size, stack);
174181
}
175182
if (UNLIKELY(IsRssLimitExceeded())) {
176183
if (AllocatorMayReturnNull())

compiler-rt/test/lsan/TestCases/realloc_too_big.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// RUN: %clang_lsan %s -o %t
22
// RUN: %env_lsan_opts=allocator_may_return_null=1:max_allocation_size_mb=1:use_stacks=0 not %run %t 2>&1 | FileCheck %s
33

4-
// Fixme: remove once test passes with hwasan
5-
// UNSUPPORTED: hwasan
6-
74
/// Fails when only leak sanitizer is enabled
85
// UNSUPPORTED: arm-linux, armhf-linux
96

107
#include <assert.h>
118
#include <stdio.h>
129
#include <stdlib.h>
1310

14-
// CHECK: {{Leak|Address}}Sanitizer failed to allocate 0x100001 bytes
11+
// CHECK: {{.*}}Sanitizer failed to allocate 0x100001 bytes
1512

16-
// CHECK: {{Leak|Address}}Sanitizer: detected memory leaks
17-
// CHECK: {{Leak|Address}}Sanitizer: 9 byte(s) leaked in 1 allocation(s).
13+
// CHECK: {{.*}}Sanitizer: detected memory leaks
14+
// CHECK: {{.*}}Sanitizer: 9 byte(s) leaked in 1 allocation(s).
1815

1916
int main() {
2017
char *p = malloc(9);

0 commit comments

Comments
 (0)