[hwasan] Add malloc_fill_byte and free_fill_byte flags.
authorEvgeniy Stepanov <eugeni.stepanov@gmail.com>
Thu, 16 Aug 2018 20:13:09 +0000 (20:13 +0000)
committerEvgeniy Stepanov <eugeni.stepanov@gmail.com>
Thu, 16 Aug 2018 20:13:09 +0000 (20:13 +0000)
Reviewers: vitalybuka, kcc

Subscribers: kubamracek, llvm-commits

Differential Revision: https://reviews.llvm.org/D50753

llvm-svn: 339932

compiler-rt/lib/hwasan/hwasan_allocator.cc
compiler-rt/lib/hwasan/hwasan_flags.inc
compiler-rt/test/hwasan/TestCases/malloc_fill.cc [new file with mode: 0644]

index c2b9b0b..23e919e 100644 (file)
@@ -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,
index c457811..28122dd 100644 (file)
@@ -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.")
diff --git a/compiler-rt/test/hwasan/TestCases/malloc_fill.cc b/compiler-rt/test/hwasan/TestCases/malloc_fill.cc
new file mode 100644 (file)
index 0000000..b8513b7
--- /dev/null
@@ -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-