From b3877c16df3c621d1ce41994a784272e44a29de0 Mon Sep 17 00:00:00 2001 From: vhertz Date: Thu, 23 Jun 2022 11:41:19 +0000 Subject: [PATCH 1/2] Fix `QlLinuxThreadManagement.threads` to update appropriately --- qiling/os/linux/thread.py | 12 ++++++++++-- qiling/os/posix/syscall/unistd.py | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/qiling/os/linux/thread.py b/qiling/os/linux/thread.py index 1e2c5c1ba..3fe7c5c41 100644 --- a/qiling/os/linux/thread.py +++ b/qiling/os/linux/thread.py @@ -85,6 +85,8 @@ def __init__(self, ql: Qiling, start_address: int, exit_point: int, context = No if self._set_child_tid_address != None: self.ql.mem.write_ptr(self._set_child_tid_address, self.id, 4) + self.ql.os.thread_management.add_thread(self) + @property def ql(self): return self._ql @@ -551,10 +553,15 @@ def stop_thread(self, t): t.stop() if t in self.threads: self.threads.remove(t) + self.ql.log.debug(f"[Thread Manager] Thread IDs: { {t.id for t in self.threads} }") # Exit the world. if t == self.main_thread: self.stop() + def add_thread(self, t): + self.threads.add(t) + self.ql.log.debug(f"[Thread Manager] Thread IDs: { {t.id for t in self.threads} }") + def _clear_queued_msg(self): try: msg_before_main_thread = self.ql._msg_before_main_thread @@ -586,8 +593,9 @@ def _prepare_lib_patch(self): def stop(self): self.ql.log.debug("[Thread Manager] Stop the world.") self.ql.emu_stop() - for t in self.threads: - gevent.kill(t) + while len(self.threads) != 0: + t = self.threads.pop() + self.stop_thread(t) def run(self): previous_thread = self._prepare_lib_patch() diff --git a/qiling/os/posix/syscall/unistd.py b/qiling/os/posix/syscall/unistd.py index 046446f63..dd4a3ae4d 100644 --- a/qiling/os/posix/syscall/unistd.py +++ b/qiling/os/posix/syscall/unistd.py @@ -25,7 +25,7 @@ def ql_syscall_exit(ql: Qiling, code: int): if ql.multithread: def _sched_cb_exit(cur_thread): ql.log.debug(f"[Thread {cur_thread.get_id()}] Terminated") - cur_thread.stop() + ql.os.thread_management.stop_thread(cur_thread) cur_thread.exit_code = code td = ql.os.thread_management.cur_thread @@ -43,7 +43,7 @@ def ql_syscall_exit_group(ql: Qiling, code: int): if ql.multithread: def _sched_cb_exit(cur_thread): ql.log.debug(f"[Thread {cur_thread.get_id()}] Terminated") - cur_thread.stop() + ql.os.thread_management.stop_thread(cur_thread) cur_thread.exit_code = code td = ql.os.thread_management.cur_thread From eefacbebbd5537095bef89d7f6b54c634f584c27 Mon Sep 17 00:00:00 2001 From: vhertz Date: Thu, 23 Jun 2022 12:26:25 +0000 Subject: [PATCH 2/2] Add more one log for debugging --- qiling/os/linux/thread.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qiling/os/linux/thread.py b/qiling/os/linux/thread.py index 3fe7c5c41..194a5798d 100644 --- a/qiling/os/linux/thread.py +++ b/qiling/os/linux/thread.py @@ -595,6 +595,7 @@ def stop(self): self.ql.emu_stop() while len(self.threads) != 0: t = self.threads.pop() + self.ql.log.debug(f"[Thread Manager] Thread IDs: { {t.id for t in self.threads} }") self.stop_thread(t) def run(self):