From c5c4e54a67bc97d80616710b4d57dd34ee54a774 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 23 Apr 2019 22:17:01 +0200 Subject: [PATCH] Kernel: Process destruction should destroy all child threads. We were only destroying the main thread when a process died, leaving any secondary threads around. They couldn't run, but because they were still in the global thread list, strange things could happen since they had some now-stale pointers to their old process. --- Kernel/Process.cpp | 19 ++++++++++++++++++- Kernel/Thread.cpp | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index ca05b11741..bdfa930d99 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -278,7 +278,16 @@ int Process::do_exec(String path, Vector arguments, Vector envir dbgprintf("%s(%d) do_exec: thread_count() = %d\n", m_name.characters(), m_pid, thread_count()); // FIXME(Thread): Kill any threads the moment we commit to the exec(). - ASSERT(thread_count() == 1); + if (thread_count() != 1) { + dbgprintf("Gonna die because I have many threads! These are the threads:\n"); + for_each_thread([] (Thread& thread) { + dbgprintf("Thread{%p}: TID=%d, PID=%d\n", &thread, thread.tid(), thread.pid()); + return IterationDecision::Continue; + }); + ASSERT(thread_count() == 1); + ASSERT_NOT_REACHED(); + } + auto parts = path.split('/'); if (parts.is_empty()) @@ -605,6 +614,14 @@ Process::~Process() dbgprintf("~Process{%p} name=%s pid=%d, m_fds=%d\n", this, m_name.characters(), pid(), m_fds.size()); delete m_main_thread; m_main_thread = nullptr; + + Vector my_threads; + for_each_thread([&my_threads] (auto& thread) { + my_threads.append(&thread); + return IterationDecision::Continue; + }); + for (auto* thread : my_threads) + delete thread; } void Process::dump_regions() diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 69cc363722..6919749135 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -12,7 +12,7 @@ Thread::Thread(Process& process) : m_process(process) , m_tid(process.m_next_tid++) { - dbgprintf("Thread: New thread TID=%u in %s(%u)\n", m_tid, process.name().characters(), process.pid()); + dbgprintf("Thread{%p}: New thread TID=%u in %s(%u)\n", this, m_tid, process.name().characters(), process.pid()); set_default_signal_dispositions(); m_fpu_state = (FPUState*)kmalloc_aligned(sizeof(FPUState), 16); memset(&m_tss, 0, sizeof(m_tss));