Prechádzať zdrojové kódy

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.
Andreas Kling 6 rokov pred
rodič
commit
c5c4e54a67
2 zmenil súbory, kde vykonal 19 pridanie a 2 odobranie
  1. 18 1
      Kernel/Process.cpp
  2. 1 1
      Kernel/Thread.cpp

+ 18 - 1
Kernel/Process.cpp

@@ -278,7 +278,16 @@ int Process::do_exec(String path, Vector<String> arguments, Vector<String> 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<Thread*, 16> 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()

+ 1 - 1
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));