From 82e5994c8d72734be813db7ea56af7325b9c9f68 Mon Sep 17 00:00:00 2001 From: Leonard Chan Date: Wed, 26 Apr 2023 21:48:37 +0000 Subject: [PATCH] [hwasan] Do not memset allocation if it comes from the secondary allocator 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 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/hwasan/hwasan_allocator.cpp b/compiler-rt/lib/hwasan/hwasan_allocator.cpp index d3cb5c8..3bb7594 100644 --- a/compiler-rt/lib/hwasan/hwasan_allocator.cpp +++ b/compiler-rt/lib/hwasan/hwasan_allocator.cpp @@ -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); -- 2.7.4