-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[lsan] Process non-suspended threads #112807
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
vitalybuka
merged 7 commits into
main
from
users/vitalybuka/spr/lsan-process-non-suspended-threads
Oct 18, 2024
Merged
[lsan] Process non-suspended threads #112807
vitalybuka
merged 7 commits into
main
from
users/vitalybuka/spr/lsan-process-non-suspended-threads
Oct 18, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Vitaly Buka (vitalybuka) ChangesFor such threads we have no registers, so no exact To avoid crashes on unmapped memory, Full diff: https://github.com/llvm/llvm-project/pull/112807.diff 2 Files Affected:
diff --git a/compiler-rt/lib/lsan/lsan_common.cpp b/compiler-rt/lib/lsan/lsan_common.cpp
index 9aed36b96ce929..05f1edae0f05c1 100644
--- a/compiler-rt/lib/lsan/lsan_common.cpp
+++ b/compiler-rt/lib/lsan/lsan_common.cpp
@@ -293,6 +293,27 @@ struct DirectMemoryAccessor {
void Init(uptr begin, uptr end) {};
void *LoadPtr(uptr p) const { return *reinterpret_cast<void **>(p); }
};
+
+struct CopyLoader {
+ void Init(uptr begin, uptr end) {
+ buffer.clear();
+ buffer.resize(end - begin);
+ offset = reinterpret_cast<uptr>(buffer.data()) - begin;
+
+ // Need a partial data?
+ MemCpyAccessible(buffer.data(), reinterpret_cast<void *>(begin),
+ buffer.size());
+ };
+ void *LoadPtr(uptr p) const {
+ CHECK_LE(p + offset + sizeof(void *),
+ reinterpret_cast<uptr>(buffer.data() + buffer.size()));
+ return *reinterpret_cast<void **>(p + offset);
+ }
+
+ private:
+ uptr offset;
+ InternalMmapVector<char> buffer;
+};
} // namespace
// Scans the memory range, looking for byte patterns that point into allocator
@@ -535,6 +556,7 @@ static void ProcessThread(tid_t os_id, uptr sp,
static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
Frontier *frontier, tid_t caller_tid,
uptr caller_sp) {
+ InternalMmapVector<tid_t> done_threads;
InternalMmapVector<uptr> registers;
InternalMmapVector<Range> extra_ranges;
for (uptr i = 0; i < suspended_threads.ThreadCount(); i++) {
@@ -559,6 +581,24 @@ static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
DirectMemoryAccessor accessor;
ProcessThread(os_id, sp, registers, extra_ranges, frontier, accessor);
+ done_threads.push_back(os_id);
+ }
+
+ if (flags()->use_detached) {
+ CopyLoader accessor;
+ InternalMmapVector<tid_t> known_threads;
+ GetRunningThreadsLocked(&known_threads);
+ Sort(done_threads.data(), done_threads.size());
+ for (tid_t os_id : known_threads) {
+ registers.clear();
+ extra_ranges.clear();
+
+ uptr i = InternalLowerBound(done_threads, os_id);
+ if (i >= done_threads.size() || done_threads[i] != os_id) {
+ uptr sp = (os_id == caller_tid) ? caller_sp : 0;
+ ProcessThread(os_id, sp, registers, extra_ranges, frontier, accessor);
+ }
+ }
}
// Add pointers reachable from ThreadContexts
diff --git a/compiler-rt/lib/lsan/lsan_flags.inc b/compiler-rt/lib/lsan/lsan_flags.inc
index c97b021ba5c02f..09d759302fdd5d 100644
--- a/compiler-rt/lib/lsan/lsan_flags.inc
+++ b/compiler-rt/lib/lsan/lsan_flags.inc
@@ -41,6 +41,8 @@ LSAN_FLAG(bool, use_ld_allocations, true,
LSAN_FLAG(bool, use_unaligned, false, "Consider unaligned pointers valid.")
LSAN_FLAG(bool, use_poisoned, false,
"Consider pointers found in poisoned memory to be valid.")
+LSAN_FLAG(bool, use_detached, false,
+ "Scan threads even attaching to them failed.")
LSAN_FLAG(bool, log_pointers, false, "Debug logging")
LSAN_FLAG(bool, log_threads, false, "Debug logging")
LSAN_FLAG(int, tries, 1, "Debug option to repeat leak checking multiple times")
|
fmayer
reviewed
Oct 18, 2024
Created using spr 1.3.4 [skip ci]
fmayer
approved these changes
Oct 18, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For such threads we have no registers, so no exact
stack range, and no guaranties that stack is mapped
at all.
To avoid crashes on unmapped memory,
MemCpyAccessible
copies intersting range intotemporarily buffer, and we search for pointers there.