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

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