Fix deleting threads (#410)

This commit is contained in:
Maurice Heumann
2025-07-12 21:48:41 +02:00
committed by GitHub
3 changed files with 18 additions and 21 deletions

View File

@@ -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))
{

View File

@@ -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)
{

View File

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