Skip to content

Commit 54c3095

Browse files
authored
Do not initialize the allocator on free(nullptr). (#74366)
free(nullptr) is guaranteed by ISO and POSIX to be a no-op, we should not pay for the overhead of maybeInit() in this case. Additionally, Bionic calls free(nullptr) before the allocator settings are finalized. Scudo should not run allocator initialization at that time. Doing so causes various bad things to happen, like mapping primary regions with the wrong PROT_MTE setting.
1 parent e77bfaa commit 54c3095

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

compiler-rt/lib/scudo/standalone/combined.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ class Allocator {
522522

523523
NOINLINE void deallocate(void *Ptr, Chunk::Origin Origin, uptr DeleteSize = 0,
524524
UNUSED uptr Alignment = MinAlignment) {
525+
if (UNLIKELY(!Ptr))
526+
return;
527+
525528
// For a deallocation, we only ensure minimal initialization, meaning thread
526529
// local data will be left uninitialized for now (when using ELF TLS). The
527530
// fallback cache will be used instead. This is a workaround for a situation
@@ -530,9 +533,6 @@ class Allocator {
530533
// being destroyed properly. Any other heap operation will do a full init.
531534
initThreadMaybe(/*MinimalInit=*/true);
532535

533-
if (UNLIKELY(!Ptr))
534-
return;
535-
536536
#ifdef GWP_ASAN_HOOKS
537537
if (UNLIKELY(GuardedAlloc.pointerIsMine(Ptr))) {
538538
GuardedAlloc.deallocate(Ptr);

0 commit comments

Comments
 (0)