Skip to content

Do not initialize the allocator on free(nullptr). #74366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023

Conversation

eugenis
Copy link
Contributor

@eugenis eugenis commented Dec 4, 2023

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Dec 4, 2023

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

Author: Evgenii Stepanov (eugenis)

Changes

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 --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);

Copy link
Contributor

@cferris1000 cferris1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@ChiaHungDuan ChiaHungDuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. BTW, I think this change still makes sense even without the case mentioned in the description. I may revise the description a little bit to say some logic change and then mention the potential problem in bionic.

@eugenis eugenis merged commit 54c3095 into llvm:main Dec 4, 2023
@eugenis
Copy link
Contributor Author

eugenis commented Dec 4, 2023

Thanks! I tweaked the description as you suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants