authorgravatar for bblack@wikimedia.orgBrandon Black <bblack@wikimedia.org> 2026-03-30 06:56:14-05:00
committergravatar for bblack@wikimedia.orgBrandon Black <bblack@wikimedia.org> 2026-03-31 14:47:44-05:00
log614cd66e7e9b7323dd67bf208fda9abf296721b4
treec25eef638f0e9035405e8042435bed7b114939b0
parentbb9f97e0856dd5c0dcc0c71633b7c15b0587b68b

LinuxThreadImpl: clear tidptr during detached exit

Fixes: #31714

1 files changed, 5 insertions(+), 0 deletions(-)

lib/std/Thread.zig+5
......@@ -1147,6 +1147,11 @@ const LinuxThreadImpl = struct {
11471147 /// Ported over from musl libc's pthread detached implementation:
11481148 /// https://github.com/ifduyue/musl/search?q=__unmapself
11491149 fn freeAndExit(self: *ThreadCompletion) noreturn {
1150 // If we do not reset the child_tidptr to null here, the kernel would later write the
1151 // value zero to that address, which is inside the block we're unmapping below, after
1152 // our thread exits. This can sometimes corrupt memory in other mmap blocks from
1153 // unrelated concurrent threads.
1154 _ = linux.set_tid_address(null);
11501155 // If a signal were delivered between SYS_munmap and SYS_exit, any installed signal
11511156 // handler would immediately segfault due to the stack being unmapped. To avoid this,
11521157 // we need to mask all signals before entering the inline asm.