1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:08:13 +00:00

Kernel: Make sure we free the thread stack on thread exit

This adds two new arguments to the thread_exit system call which let
a thread unmap an arbitrary VM range on thread exit. LibPthread
uses this functionality to unmap the thread stack.

Fixes #7267.
This commit is contained in:
Gunnar Beutner 2021-05-28 11:20:22 +02:00 committed by Andreas Kling
parent 95c2166ca9
commit 42d667645d
4 changed files with 22 additions and 9 deletions

View file

@ -379,7 +379,7 @@ public:
KResultOr<int> sys$sched_setparam(pid_t pid, Userspace<const struct sched_param*>);
KResultOr<int> sys$sched_getparam(pid_t pid, Userspace<struct sched_param*>);
KResultOr<int> sys$create_thread(void* (*)(void*), Userspace<const Syscall::SC_create_thread_params*>);
[[noreturn]] void sys$exit_thread(Userspace<void*>);
[[noreturn]] void sys$exit_thread(Userspace<void*>, Userspace<void*>, size_t);
KResultOr<int> sys$join_thread(pid_t tid, Userspace<void**> exit_value);
KResultOr<int> sys$detach_thread(pid_t tid);
KResultOr<int> sys$set_thread_name(pid_t tid, Userspace<const char*> buffer, size_t buffer_size);

View file

@ -97,7 +97,7 @@ KResultOr<FlatPtr> handle(RegisterState& regs, FlatPtr function, FlatPtr arg1, F
process.sys$exit(arg1);
break;
case SC_exit_thread:
process.sys$exit_thread(arg1);
process.sys$exit_thread(arg1, arg2, arg3);
break;
default:
VERIFY_NOT_REACHED();

View file

@ -78,7 +78,7 @@ KResultOr<int> Process::sys$create_thread(void* (*entry)(void*), Userspace<const
return thread->tid().value();
}
void Process::sys$exit_thread(Userspace<void*> exit_value)
void Process::sys$exit_thread(Userspace<void*> exit_value, Userspace<void*> stack_location, size_t stack_size)
{
REQUIRE_PROMISE(thread);
@ -90,6 +90,12 @@ void Process::sys$exit_thread(Userspace<void*> exit_value)
auto current_thread = Thread::current();
PerformanceManager::add_thread_exit_event(*current_thread);
if (stack_location) {
auto unmap_result = space().unmap_mmap_range(VirtualAddress { stack_location }, stack_size);
if (unmap_result.is_error())
dbgln("Failed to unmap thread stack, terminating thread anyway. Error code: {}", unmap_result.error());
}
current_thread->exit(reinterpret_cast<void*>(exit_value.ptr()));
VERIFY_NOT_REACHED();
}