Add back missing TlsAlloc() call

Cosmopolitan Libc once called this important function although somewhere
along the way, possibly in a refactoring, it got removed and __tls_alloc
has always been zero ever since.
This commit is contained in:
Justine Tunney 2024-07-21 20:45:27 -07:00
parent e08a4cd99e
commit 5d2d9e9640
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
5 changed files with 12 additions and 9 deletions

View file

@ -65,6 +65,7 @@
#include "libc/thread/itimer.internal.h" #include "libc/thread/itimer.internal.h"
#include "libc/thread/posixthread.internal.h" #include "libc/thread/posixthread.internal.h"
#include "libc/thread/tls.h" #include "libc/thread/tls.h"
#include "libc/thread/tls2.internal.h"
#ifdef __x86_64__ #ifdef __x86_64__
extern long __klog_handle; extern long __klog_handle;
@ -112,7 +113,8 @@ static dontinline textwindows ssize_t ForkIo2(
ssize_t rc = ForkIo(h, buf, n, fn); ssize_t rc = ForkIo(h, buf, n, fn);
if (ischild) { if (ischild) {
// prevent crashes // prevent crashes
__tls_enabled_set(false); __threaded = false;
__tls_enabled = false;
__pid = __imp_GetCurrentProcessId(); __pid = __imp_GetCurrentProcessId();
__klog_handle = 0; __klog_handle = 0;
__maps.maps = 0; __maps.maps = 0;
@ -265,9 +267,8 @@ textwindows void WinMainForked(void) {
ReadOrDie(reader, __data_start, __data_end - __data_start); ReadOrDie(reader, __data_start, __data_end - __data_start);
ReadOrDie(reader, __bss_start, __bss_end - __bss_start); ReadOrDie(reader, __bss_start, __bss_end - __bss_start);
kStartTsc = savetsc; kStartTsc = savetsc;
__tls_enabled = false;
__threaded = false; __threaded = false;
__tls_index = 0;
__tls_enabled_set(false);
// fixup memory manager // fixup memory manager
__maps.free = 0; __maps.free = 0;
@ -456,9 +457,10 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
} else { } else {
rc = 0; rc = 0;
// re-apply code morphing for thread-local storage // re-apply code morphing for thread-local storage
__set_tls(tib); __tls_index = TlsAlloc();
__set_tls_win32(tib);
__morph_tls(); __morph_tls();
__tls_enabled_set(true); __tls_enabled = true;
// the child's pending signals is initially empty // the child's pending signals is initially empty
atomic_store_explicit(&__sig.pending, 0, memory_order_relaxed); atomic_store_explicit(&__sig.pending, 0, memory_order_relaxed);
atomic_store_explicit(&tib->tib_sigpending, 0, memory_order_relaxed); atomic_store_explicit(&tib->tib_sigpending, 0, memory_order_relaxed);

View file

@ -45,7 +45,7 @@ cosmo: push %rbp
#endif /* SYSDEBUG */ #endif /* SYSDEBUG */
#ifndef NOX87 #ifndef NOX87
// Windows always initializes FPU to douuble precision. // Windows always initializes FPU to double precision.
// WSL breaks Linux ABI by initializing FPU to double precision. // WSL breaks Linux ABI by initializing FPU to double precision.
// This code makes long double long again. // This code makes long double long again.
// //

View file

@ -251,6 +251,8 @@ textstartup void __enable_tls(void) {
atomic_store_explicit(&_pthread_static.ptid, tid, memory_order_release); atomic_store_explicit(&_pthread_static.ptid, tid, memory_order_release);
// ask the operating system to change the x86 segment register // ask the operating system to change the x86 segment register
if (IsWindows())
__tls_index = TlsAlloc();
__set_tls(tib); __set_tls(tib);
#ifdef __x86_64__ #ifdef __x86_64__

View file

@ -38,7 +38,7 @@ dontinstrument textstartup void __set_tls(struct CosmoTib *tib) {
#ifdef __x86_64__ #ifdef __x86_64__
// ask the operating system to change the x86 segment register // ask the operating system to change the x86 segment register
if (IsWindows()) { if (IsWindows()) {
asm("mov\t%1,%%gs:%0" : "=m"(*((long *)0x1480 + __tls_index)) : "r"(tib)); __set_tls_win32(tib);
} else if (IsLinux()) { } else if (IsLinux()) {
sys_set_tls(ARCH_SET_GS, tib); sys_set_tls(ARCH_SET_GS, tib);
} else if (IsFreebsd()) { } else if (IsFreebsd()) {

View file

@ -17,10 +17,9 @@ forceinline struct CosmoTib *__get_tls_privileged(void) {
__asm__("mov\t%%fs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory"); __asm__("mov\t%%fs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
} else { } else {
__asm__("mov\t%%gs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory"); __asm__("mov\t%%gs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
if (IsWindows()) { if (IsWindows())
tib = *(char **)(tib + 0x1480 + __tls_index * 8); tib = *(char **)(tib + 0x1480 + __tls_index * 8);
} }
}
return (struct CosmoTib *)tib; return (struct CosmoTib *)tib;
} }