From 7a3a5d760ebcda9af957334966a0eb210d2439f5 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 12 Jul 2025 15:28:30 +0200 Subject: [PATCH 1/3] Fix thread deletion --- src/windows-emulator/syscalls/object.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/windows-emulator/syscalls/object.cpp b/src/windows-emulator/syscalls/object.cpp index cf8d9da7..81c71f4d 100644 --- a/src/windows-emulator/syscalls/object.cpp +++ b/src/windows-emulator/syscalls/object.cpp @@ -12,16 +12,6 @@ namespace syscalls return STATUS_SUCCESS; } - if (h.value.type == handle_types::thread) - { - const auto* t = c.proc.threads.get(h); - if (t && t->ref_count == 1) - { - // TODO: Better handle ref counting - return STATUS_SUCCESS; - } - } - auto* handle_store = c.proc.get_handle_store(h); if (handle_store && handle_store->erase(h)) { From ac2d34c143b289d09a8f6cbecf480db7302f7349 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 12 Jul 2025 15:39:53 +0200 Subject: [PATCH 2/3] Small cleanup --- src/windows-emulator/syscalls/thread.cpp | 27 ++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/windows-emulator/syscalls/thread.cpp b/src/windows-emulator/syscalls/thread.cpp index 39098480..6d631cf8 100644 --- a/src/windows-emulator/syscalls/thread.cpp +++ b/src/windows-emulator/syscalls/thread.cpp @@ -267,6 +267,21 @@ namespace syscalls return handle_NtOpenThreadToken(c, thread_handle, desired_access, open_as_self, token_handle); } + static void delete_thread_windows(const syscall_context& c, const uint32_t thread_id) + { + for (auto i = c.proc.windows.begin(); i != c.proc.windows.end();) + { + if (i->second.thread_id != thread_id) + { + ++i; + continue; + } + + i->second.ref_count = 1; + i = c.proc.windows.erase(i).first; + } + } + NTSTATUS handle_NtTerminateThread(const syscall_context& c, const handle thread_handle, const NTSTATUS exit_status) { auto* thread = !thread_handle.bits ? c.proc.active_thread : c.proc.threads.get(thread_handle); @@ -279,17 +294,7 @@ namespace syscalls thread->exit_status = exit_status; c.win_emu.callbacks.on_thread_terminated(thread_handle, *thread); - for (auto i = c.proc.windows.begin(); i != c.proc.windows.end();) - { - if (i->second.thread_id != thread->id) - { - ++i; - continue; - } - - i->second.ref_count = 1; - i = c.proc.windows.erase(i).first; - } + delete_thread_windows(c, thread->id); if (thread == c.proc.active_thread) { From d7a1a269b704152b9cf3c1e38735112f68b07612 Mon Sep 17 00:00:00 2001 From: Maurice Heumann Date: Sat, 12 Jul 2025 21:33:12 +0200 Subject: [PATCH 3/3] Setup thread before dispatching APC --- src/windows-emulator/windows_emulator.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index 44175c43..b384c576 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -107,6 +107,8 @@ namespace return; } + thread.setup_if_necessary(win_emu.emu(), win_emu.process); + win_emu.callbacks.on_generic_activity("APC Dispatch"); const auto next_apx = apcs.front();