Skip to content
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

[msan] Block signals during MsanThread::TSDDtor #98405

Merged
merged 6 commits into from
Jul 11, 2024

Conversation

thurstond
Copy link
Contributor

@thurstond thurstond commented Jul 10, 2024

MSan may segfault inside a signal handler, if MSan instrumentation is trying to access thread-local storage that has already been destroyed. This fixes the issue by blocking asychronous signals inside MsanThread::TSDDtor. This is based on an idea suggested by Paul Pluzhnikov (block async signals in MsanThread::Destroy()) and refined by Vitaly Buka.

Note: ed8565c changed *BlockSignals to only block asynchronous signals, despite the name.

MSan may segfault inside a signal handler, if MSan instrumentation is trying to access thread-local storage that has already
been destroyed. This fixes the issue by blocking asychronous signals
inside MsanThread::Destroy, as suggested by Paul Pluzhnikov.

Note: ed8565c changed *BlockSignals to
only block asynchronous signals, despite the name.
@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2024

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

Author: Thurston Dang (thurstond)

Changes

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

1 Files Affected:

  • (modified) compiler-rt/lib/msan/msan_thread.cpp (+4)
diff --git a/compiler-rt/lib/msan/msan_thread.cpp b/compiler-rt/lib/msan/msan_thread.cpp
index ff9b90bb81f0c..cc4dfe601ead6 100644
--- a/compiler-rt/lib/msan/msan_thread.cpp
+++ b/compiler-rt/lib/msan/msan_thread.cpp
@@ -3,6 +3,7 @@
 #include "msan_thread.h"
 #include "msan_interface_internal.h"
 
+#include "sanitizer_common/sanitizer_linux.h"
 #include "sanitizer_common/sanitizer_tls_get_addr.h"
 
 namespace __msan {
@@ -56,6 +57,9 @@ void MsanThread::TSDDtor(void *tsd) {
 }
 
 void MsanThread::Destroy() {
+#if SANITIZER_LINUX
+  ScopedBlockSignals block(nullptr);
+#endif
   malloc_storage().CommitBack();
   // We also clear the shadow on thread destruction because
   // some code may still be executing in later TSD destructors

@thurstond thurstond changed the title Msan signals [msan] Block signals during MsanThread::Destroy Jul 10, 2024
Copy link

github-actions bot commented Jul 10, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@vitalybuka vitalybuka left a comment

Choose a reason for hiding this comment

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

MsanThread is LINUX only any way

@vitalybuka
Copy link
Collaborator

I suggest to move blocker just before msan_current_thread = nullptr in MsanTSDDtor

@thurstond thurstond changed the title [msan] Block signals during MsanThread::Destroy [msan] Block signals during MsanThread::TSDDtor Jul 11, 2024
@thurstond thurstond requested a review from vitalybuka July 11, 2024 03:40
@thurstond thurstond merged commit fff8b32 into llvm:main Jul 11, 2024
6 checks passed
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
MSan may segfault inside a signal handler, if MSan instrumentation is
trying to access thread-local storage that has already been destroyed.
This fixes the issue by blocking asychronous signals inside
MsanThread::TSDDtor. This is based on an idea suggested by Paul
Pluzhnikov (block async signals in MsanThread::Destroy()) and refined by
Vitaly Buka.
    
Note: ed8565c changed *BlockSignals to
only block asynchronous signals, despite the name.
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.

3 participants