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

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 4 12:49:40 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Evgenii Stepanov (eugenis)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/74366.diff


1 Files Affected:

- (modified) compiler-rt/lib/scudo/standalone/combined.h (+3-3) 


``````````diff
diff --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h
index 25c597199a658..79c3a36c742b4 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);

``````````

</details>


https://github.com/llvm/llvm-project/pull/74366


More information about the llvm-commits mailing list