forked from OSchip/llvm-project
[hwasan] Add malloc_fill_byte and free_fill_byte flags.
Reviewers: vitalybuka, kcc Subscribers: kubamracek, llvm-commits Differential Revision: https://reviews.llvm.org/D50753 llvm-svn: 339932
This commit is contained in:
parent
bb1aede865
commit
fa9f78553a
|
@ -156,8 +156,12 @@ static void *HwasanAllocate(StackTrace *stack, uptr size, uptr alignment,
|
|||
meta->state = CHUNK_ALLOCATED;
|
||||
meta->requested_size = size;
|
||||
meta->alloc_context_id = StackDepotPut(*stack);
|
||||
if (zeroise)
|
||||
if (zeroise) {
|
||||
internal_memset(allocated, 0, size);
|
||||
} else if (flags()->max_malloc_fill_size > 0) {
|
||||
uptr fill_size = Min(size, (uptr)flags()->max_malloc_fill_size);
|
||||
internal_memset(allocated, flags()->malloc_fill_byte, fill_size);
|
||||
}
|
||||
|
||||
void *user_ptr = allocated;
|
||||
if (flags()->tag_in_malloc &&
|
||||
|
@ -182,6 +186,10 @@ void HwasanDeallocate(StackTrace *stack, void *user_ptr) {
|
|||
// This memory will not be reused by anyone else, so we are free to keep it
|
||||
// poisoned.
|
||||
HwasanThread *t = GetCurrentThread();
|
||||
if (flags()->max_free_fill_size > 0) {
|
||||
uptr fill_size = Min(size, (uptr)flags()->max_free_fill_size);
|
||||
internal_memset(p, flags()->free_fill_byte, fill_size);
|
||||
}
|
||||
if (flags()->tag_in_free &&
|
||||
atomic_load_relaxed(&hwasan_allocator_tagging_enabled))
|
||||
TagMemoryAligned((uptr)p, size,
|
||||
|
|
|
@ -31,3 +31,16 @@ HWASAN_FLAG(bool, disable_allocator_tagging, false, "")
|
|||
// If false, use simple increment of a thread local counter to generate new
|
||||
// tags.
|
||||
HWASAN_FLAG(bool, random_tags, true, "")
|
||||
|
||||
HWASAN_FLAG(
|
||||
int, max_malloc_fill_size, 0x1000, // By default, fill only the first 4K.
|
||||
"HWASan allocator flag. max_malloc_fill_size is the maximal amount of "
|
||||
"bytes that will be filled with malloc_fill_byte on malloc.")
|
||||
HWASAN_FLAG(
|
||||
int, max_free_fill_size, 0,
|
||||
"HWASan allocator flag. max_free_fill_size is the maximal amount of "
|
||||
"bytes that will be filled with free_fill_byte during free.")
|
||||
HWASAN_FLAG(int, malloc_fill_byte, 0xbe,
|
||||
"Value used to fill the newly allocated memory.")
|
||||
HWASAN_FLAG(int, free_fill_byte, 0x55,
|
||||
"Value used to fill deallocated memory.")
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
// Check that we fill malloc-ed memory correctly.
|
||||
// RUN: %clangxx_hwasan %s -o %t
|
||||
// RUN: %run %t | FileCheck %s
|
||||
// RUN: %env_hwasan_opts=max_malloc_fill_size=10:malloc_fill_byte=8 %run %t | FileCheck %s --check-prefix=CHECK-10-8
|
||||
// RUN: %env_hwasan_opts=max_malloc_fill_size=20:malloc_fill_byte=171 %run %t | FileCheck %s --check-prefix=CHECK-20-ab
|
||||
|
||||
#include <stdio.h>
|
||||
int main(int argc, char **argv) {
|
||||
// With asan allocator this makes sure we get memory from mmap.
|
||||
static const int kSize = 1 << 25;
|
||||
unsigned char *x = new unsigned char[kSize];
|
||||
printf("-");
|
||||
for (int i = 0; i <= 32; i++) {
|
||||
printf("%02x", x[i]);
|
||||
}
|
||||
printf("-\n");
|
||||
delete [] x;
|
||||
}
|
||||
|
||||
// CHECK: -bebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebebe-
|
||||
// CHECK-10-8: -080808080808080808080000000000000000000000000000000000000000000000-
|
||||
// CHECK-20-ab: -abababababababababababababababababababab00000000000000000000000000-
|
Loading…
Reference in New Issue