[compiler-rt] d87468e - [HWASAN] Add support for max_allocation_size_mb option
Kirill Stoimenov via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 9 12:25:34 PST 2023
Author: Kirill Stoimenov
Date: 2023-02-09T20:24:47Z
New Revision: d87468e56c221555be7a1c1550036af5b7034896
URL: https://github.com/llvm/llvm-project/commit/d87468e56c221555be7a1c1550036af5b7034896
DIFF: https://github.com/llvm/llvm-project/commit/d87468e56c221555be7a1c1550036af5b7034896.diff
LOG: [HWASAN] Add support for max_allocation_size_mb option
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D143667
Added:
Modified:
compiler-rt/lib/hwasan/hwasan_allocator.cpp
compiler-rt/test/lsan/TestCases/realloc_too_big.c
Removed:
################################################################################
diff --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp
index b5bd6529dfb23..d096a8faa2c7e 100644
--- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp
@@ -45,6 +45,7 @@ enum {
// Initialized in HwasanAllocatorInit, an never changed.
static ALIGNED(16) u8 tail_magic[kShadowAlignment - 1];
+static uptr max_malloc_size;
bool HwasanChunkView::IsAllocated() const {
return metadata_ && metadata_->IsAllocated();
@@ -142,6 +143,12 @@ void HwasanAllocatorInit() {
GetAliasRegionStart());
for (uptr i = 0; i < sizeof(tail_magic); i++)
tail_magic[i] = GetCurrentThread()->GenerateRandomTag();
+ if (common_flags()->max_allocation_size_mb) {
+ max_malloc_size = common_flags()->max_allocation_size_mb << 20;
+ max_malloc_size = Min(max_malloc_size, kMaxAllowedMallocSize);
+ } else {
+ max_malloc_size = kMaxAllowedMallocSize;
+ }
}
void HwasanAllocatorLock() { allocator.ForceLock(); }
@@ -164,13 +171,13 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
// Keep this consistent with LSAN and ASAN behavior.
if (UNLIKELY(orig_size == 0))
orig_size = 1;
- if (UNLIKELY(orig_size > kMaxAllowedMallocSize)) {
+ if (UNLIKELY(orig_size > max_malloc_size)) {
if (AllocatorMayReturnNull()) {
Report("WARNING: HWAddressSanitizer failed to allocate 0x%zx bytes\n",
orig_size);
return nullptr;
}
- ReportAllocationSizeTooBig(orig_size, kMaxAllowedMallocSize, stack);
+ ReportAllocationSizeTooBig(orig_size, max_malloc_size, stack);
}
if (UNLIKELY(IsRssLimitExceeded())) {
if (AllocatorMayReturnNull())
diff --git a/compiler-rt/test/lsan/TestCases/realloc_too_big.c b/compiler-rt/test/lsan/TestCases/realloc_too_big.c
index 4f98309fc225e..1a6569b083dcd 100644
--- a/compiler-rt/test/lsan/TestCases/realloc_too_big.c
+++ b/compiler-rt/test/lsan/TestCases/realloc_too_big.c
@@ -1,9 +1,6 @@
// RUN: %clang_lsan %s -o %t
// RUN: %env_lsan_opts=allocator_may_return_null=1:max_allocation_size_mb=1:use_stacks=0 not %run %t 2>&1 | FileCheck %s
-// Fixme: remove once test passes with hwasan
-// UNSUPPORTED: hwasan
-
/// Fails when only leak sanitizer is enabled
// UNSUPPORTED: arm-linux, armhf-linux
@@ -11,10 +8,10 @@
#include <stdio.h>
#include <stdlib.h>
-// CHECK: {{Leak|Address}}Sanitizer failed to allocate 0x100001 bytes
+// CHECK: {{.*}}Sanitizer failed to allocate 0x100001 bytes
-// CHECK: {{Leak|Address}}Sanitizer: detected memory leaks
-// CHECK: {{Leak|Address}}Sanitizer: 9 byte(s) leaked in 1 allocation(s).
+// CHECK: {{.*}}Sanitizer: detected memory leaks
+// CHECK: {{.*}}Sanitizer: 9 byte(s) leaked in 1 allocation(s).
int main() {
char *p = malloc(9);
More information about the llvm-commits
mailing list