diff options
| author | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 21:38:24 +0000 |
|---|---|---|
| committer | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 21:38:24 +0000 |
| commit | 77bb72741959a2b02921697755a51548e802770e (patch) | |
| tree | 4b3ab24c1007cbe4d617be0bba5c1751e97b90ed /runtime/src/pe_loader.cpp | |
| parent | 292c917822f0b475697b47e0e5ac576caf3ee2a9 (diff) | |
loader: the thread handles of the TLS swap are closed on every way out
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 8fca00668..f6b6673be 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -493,28 +493,39 @@ void install_on_existing_threads(Module& m) { | |||
| 493 | if (!query_thread_fn()) return; | 493 | if (!query_thread_fn()) return; |
| 494 | 494 | ||
| 495 | // Outside the TLS lock: none of this may wait for a thread that is inside its attach event. | 495 | // Outside the TLS lock: none of this may wait for a thread that is inside its attach event. |
| 496 | // The handles are closed on every way out, also when a vector below cannot grow. | ||
| 496 | struct Candidate { | 497 | struct Candidate { |
| 497 | DWORD id; | 498 | DWORD id = 0; |
| 498 | HANDLE handle; | 499 | HANDLE handle = nullptr; |
| 499 | }; | 500 | }; |
| 500 | std::vector<Candidate> candidates; | 501 | struct Candidates { |
| 501 | const HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); | 502 | std::vector<Candidate> list; |
| 502 | if (snapshot == INVALID_HANDLE_VALUE) return; | 503 | HANDLE snapshot = INVALID_HANDLE_VALUE; |
| 504 | ~Candidates() { | ||
| 505 | if (snapshot != INVALID_HANDLE_VALUE) CloseHandle(snapshot); | ||
| 506 | for (const Candidate& c : list) | ||
| 507 | if (c.handle) CloseHandle(c.handle); | ||
| 508 | } | ||
| 509 | } found; | ||
| 510 | std::vector<Candidate>& candidates = found.list; | ||
| 511 | // TH32CS_SNAPTHREAD lists the threads of every process; the loop keeps this one's. | ||
| 512 | found.snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); | ||
| 513 | if (found.snapshot == INVALID_HANDLE_VALUE) return; | ||
| 503 | THREADENTRY32 entry{}; | 514 | THREADENTRY32 entry{}; |
| 504 | entry.dwSize = sizeof entry; | 515 | entry.dwSize = sizeof entry; |
| 505 | const DWORD process = GetCurrentProcessId(), self = GetCurrentThreadId(); | 516 | const DWORD process = GetCurrentProcessId(), self = GetCurrentThreadId(); |
| 506 | for (BOOL more = Thread32First(snapshot, &entry); more; more = Thread32Next(snapshot, &entry)) { | 517 | for (BOOL more = Thread32First(found.snapshot, &entry); more; more = Thread32Next(found.snapshot, &entry)) { |
| 507 | if (entry.th32OwnerProcessID != process || entry.th32ThreadID == self) continue; | 518 | if (entry.th32OwnerProcessID != process || entry.th32ThreadID == self) continue; |
| 508 | if (HANDLE h = OpenThread(THREAD_QUERY_INFORMATION, FALSE, entry.th32ThreadID)) | 519 | candidates.push_back({entry.th32ThreadID, nullptr}); // the slot first, so the handle always has an owner |
| 509 | candidates.push_back({entry.th32ThreadID, h}); | 520 | candidates.back().handle = OpenThread(THREAD_QUERY_INFORMATION, FALSE, entry.th32ThreadID); |
| 510 | } | 521 | } |
| 511 | CloseHandle(snapshot); | ||
| 512 | 522 | ||
| 513 | { | 523 | { |
| 514 | ExclusiveTlsLock tls_lock; | 524 | ExclusiveTlsLock tls_lock; |
| 515 | ExistingThreadsLock lock; | 525 | ExistingThreadsLock lock; |
| 516 | ExistingThreads& state = existing_threads(); | 526 | ExistingThreads& state = existing_threads(); |
| 517 | for (const Candidate& c : candidates) { | 527 | for (const Candidate& c : candidates) { |
| 528 | if (!c.handle) continue; | ||
| 518 | if (std::find(state.ended.begin(), state.ended.end(), c.id) != state.ended.end()) continue; | 529 | if (std::find(state.ended.begin(), state.ended.end(), c.id) != state.ended.end()) continue; |
| 519 | void*** vector = running_thread_tls_vector(c.handle); | 530 | void*** vector = running_thread_tls_vector(c.handle); |
| 520 | void** slots = nullptr; | 531 | void** slots = nullptr; |
| @@ -564,7 +575,6 @@ void install_on_existing_threads(Module& m) { | |||
| 564 | state.blocks.push_back({c.id, m.tls_slot, &m, m.tls_index, vector, saved, block}); | 575 | state.blocks.push_back({c.id, m.tls_slot, &m, m.tls_index, vector, saved, block}); |
| 565 | } | 576 | } |
| 566 | } | 577 | } |
| 567 | for (const Candidate& c : candidates) CloseHandle(c.handle); | ||
| 568 | } | 578 | } |
| 569 | 579 | ||
| 570 | // ~Loader, with g_tls_lock held exclusively: the blocks load() put into other threads come out. A thread that has | 580 | // ~Loader, with g_tls_lock held exclusively: the blocks load() put into other threads come out. A thread that has |