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

Kernel: Make sure we can allocate kernel stack before creating thread

Wrap thread creation in a Thread::try_create() helper that first
allocates a kernel stack region. If that allocation fails, we propagate
an ENOMEM error to the caller.

This avoids the situation where a thread is half-constructed, without a
valid kernel stack, and avoids having to do messy cleanup in that case.
This commit is contained in:
Andreas Kling 2021-02-07 18:13:51 +01:00
parent 5c45b0d32d
commit b466ede1ea
4 changed files with 32 additions and 29 deletions

View file

@ -346,14 +346,11 @@ Process::Process(RefPtr<Thread>& first_thread, const String& name, uid_t uid, gi
first_thread = Thread::current()->clone(*this);
} else {
// NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
first_thread = adopt(*new Thread(*this));
auto thread_or_error = Thread::try_create(*this);
ASSERT(!thread_or_error.is_error());
first_thread = thread_or_error.release_value();
first_thread->detach();
}
if (first_thread && !first_thread->was_created()) {
// We couldn't entirely create or clone this thread, abort
first_thread = nullptr;
}
}
Process::~Process()
@ -810,12 +807,11 @@ RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_d
// FIXME: Do something with guard pages?
auto thread = adopt(*new Thread(*this));
if (!thread->was_created()) {
// Could not fully create this thread
auto thread_or_error = Thread::try_create(*this);
if (thread_or_error.is_error())
return {};
}
auto& thread = thread_or_error.value();
thread->set_name(name);
thread->set_affinity(affinity);
thread->set_priority(priority);