[compiler-rt] 54c3095 - Do not initialize the allocator on free(nullptr). (#74366)

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 4 13:45:41 PST 2023


Author: Evgenii Stepanov
Date: 2023-12-04T13:45:37-08:00
New Revision: 54c30953b9374e3bcc1c79b53a231e4b53ceafed

URL: https://github.com/llvm/llvm-project/commit/54c30953b9374e3bcc1c79b53a231e4b53ceafed
DIFF: https://github.com/llvm/llvm-project/commit/54c30953b9374e3bcc1c79b53a231e4b53ceafed.diff

LOG: 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.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h
index 72e5caa026e4d..25ad11dbf7ee5 100644
--- a/compiler-rt/lib/scudo/standalone/combined.h
+++ b/compiler-rt/lib/scudo/standalone/combined.h
@@ -522,6 +522,9 @@ class Allocator {
 
   NOINLINE void deallocate(void *Ptr, Chunk::Origin Origin, uptr DeleteSize = 0,
                            UNUSED uptr Alignment = MinAlignment) {
+    if (UNLIKELY(!Ptr))
+      return;
+
     // For a deallocation, we only ensure minimal initialization, meaning thread
     // local data will be left uninitialized for now (when using ELF TLS). The
     // fallback cache will be used instead. This is a workaround for a situation
@@ -530,9 +533,6 @@ class Allocator {
     // being destroyed properly. Any other heap operation will do a full init.
     initThreadMaybe(/*MinimalInit=*/true);
 
-    if (UNLIKELY(!Ptr))
-      return;
-
 #ifdef GWP_ASAN_HOOKS
     if (UNLIKELY(GuardedAlloc.pointerIsMine(Ptr))) {
       GuardedAlloc.deallocate(Ptr);


        


More information about the llvm-commits mailing list