Kernel: Make sure we unmap the TLS region when a thread exits

Previously the TLS region would get leaked which was noticible
when creating/destroying a lot of threads and then inspecting
the region map.
This commit is contained in:
Gunnar Beutner 2021-05-28 11:18:58 +02:00 committed by Andreas Kling
parent f63f471b87
commit b9d693665b
Notes: sideshowbarker 2024-07-19 01:59:31 +09:00
2 changed files with 10 additions and 0 deletions

View File

@ -266,6 +266,12 @@ void Thread::exit(void* exit_value)
set_should_die();
u32 unlock_count;
[[maybe_unused]] auto rc = unlock_process_if_locked(unlock_count);
if (m_thread_specific_range.has_value()) {
auto* region = process().space().find_region_from_range(m_thread_specific_range.value());
VERIFY(region);
if (!process().space().deallocate_region(*region))
dbgln("Failed to unmap TLS range, exiting thread anyway.");
}
die_if_needed();
}
@ -1021,6 +1027,8 @@ KResult Thread::make_thread_specific_region(Badge<Process>)
if (region_or_error.is_error())
return region_or_error.error();
m_thread_specific_range = range.value();
SmapDisabler disabler;
auto* thread_specific_data = (ThreadSpecificData*)region_or_error.value()->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment())).as_ptr();
auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment);

View File

@ -29,6 +29,7 @@
#include <Kernel/ThreadTracer.h>
#include <Kernel/TimerQueue.h>
#include <Kernel/UnixTypes.h>
#include <Kernel/VM/Range.h>
#include <LibC/fd_set.h>
#include <LibC/signal_numbers.h>
@ -1220,6 +1221,7 @@ private:
u32 m_kernel_stack_top { 0 };
OwnPtr<Region> m_kernel_stack_region;
VirtualAddress m_thread_specific_data;
Optional<Range> m_thread_specific_range;
Array<SignalActionData, NSIG> m_signal_action_data;
Blocker* m_blocker { nullptr };