[hwasan] Do not memset allocation if it comes from the secondary allocator
authorLeonard Chan <leonardchan@google.com>
Wed, 26 Apr 2023 21:48:37 +0000 (21:48 +0000)
committerLeonard Chan <leonardchan@google.com>
Wed, 26 Apr 2023 21:49:42 +0000 (21:49 +0000)
The secondary allocator calls mmap which should return zero-inited pages, so we
don't need to explicitly memset it with zeros. This is similar to what asan's
calloc does.

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

compiler-rt/lib/hwasan/hwasan_allocator.cpp

index d3cb5c8..3bb7594 100644 (file)
@@ -213,7 +213,10 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
     ReportOutOfMemory(size, stack);
   }
   if (zeroise) {
-    internal_memset(allocated, 0, size);
+    // The secondary allocator mmaps memory, which should be zero-inited so we
+    // don't need to explicitly clear it.
+    if (allocator.FromPrimary(allocated))
+      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);