Skip to content

Commit 5d2d9e9

Browse files
committed
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.
1 parent e08a4cd commit 5d2d9e9

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

libc/proc/fork-nt.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "libc/thread/itimer.internal.h"
6666
#include "libc/thread/posixthread.internal.h"
6767
#include "libc/thread/tls.h"
68+
#include "libc/thread/tls2.internal.h"
6869
#ifdef __x86_64__
6970

7071
extern long __klog_handle;
@@ -112,7 +113,8 @@ static dontinline textwindows ssize_t ForkIo2(
112113
ssize_t rc = ForkIo(h, buf, n, fn);
113114
if (ischild) {
114115
// prevent crashes
115-
__tls_enabled_set(false);
116+
__threaded = false;
117+
__tls_enabled = false;
116118
__pid = __imp_GetCurrentProcessId();
117119
__klog_handle = 0;
118120
__maps.maps = 0;
@@ -265,9 +267,8 @@ textwindows void WinMainForked(void) {
265267
ReadOrDie(reader, __data_start, __data_end - __data_start);
266268
ReadOrDie(reader, __bss_start, __bss_end - __bss_start);
267269
kStartTsc = savetsc;
270+
__tls_enabled = false;
268271
__threaded = false;
269-
__tls_index = 0;
270-
__tls_enabled_set(false);
271272

272273
// fixup memory manager
273274
__maps.free = 0;
@@ -456,9 +457,10 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
456457
} else {
457458
rc = 0;
458459
// re-apply code morphing for thread-local storage
459-
__set_tls(tib);
460+
__tls_index = TlsAlloc();
461+
__set_tls_win32(tib);
460462
__morph_tls();
461-
__tls_enabled_set(true);
463+
__tls_enabled = true;
462464
// the child's pending signals is initially empty
463465
atomic_store_explicit(&__sig.pending, 0, memory_order_relaxed);
464466
atomic_store_explicit(&tib->tib_sigpending, 0, memory_order_relaxed);

libc/runtime/cosmo.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ cosmo: push %rbp
4545
#endif /* SYSDEBUG */
4646

4747
#ifndef NOX87
48-
// Windows always initializes FPU to douuble precision.
48+
// Windows always initializes FPU to double precision.
4949
// WSL breaks Linux ABI by initializing FPU to double precision.
5050
// This code makes long double long again.
5151
//

libc/runtime/enable_tls.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ textstartup void __enable_tls(void) {
251251
atomic_store_explicit(&_pthread_static.ptid, tid, memory_order_release);
252252

253253
// ask the operating system to change the x86 segment register
254+
if (IsWindows())
255+
__tls_index = TlsAlloc();
254256
__set_tls(tib);
255257

256258
#ifdef __x86_64__

libc/runtime/set_tls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dontinstrument textstartup void __set_tls(struct CosmoTib *tib) {
3838
#ifdef __x86_64__
3939
// ask the operating system to change the x86 segment register
4040
if (IsWindows()) {
41-
asm("mov\t%1,%%gs:%0" : "=m"(*((long *)0x1480 + __tls_index)) : "r"(tib));
41+
__set_tls_win32(tib);
4242
} else if (IsLinux()) {
4343
sys_set_tls(ARCH_SET_GS, tib);
4444
} else if (IsFreebsd()) {

libc/thread/tls2.internal.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ forceinline struct CosmoTib *__get_tls_privileged(void) {
1717
__asm__("mov\t%%fs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
1818
} else {
1919
__asm__("mov\t%%gs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
20-
if (IsWindows()) {
20+
if (IsWindows())
2121
tib = *(char **)(tib + 0x1480 + __tls_index * 8);
22-
}
2322
}
2423
return (struct CosmoTib *)tib;
2524
}

0 commit comments

Comments
 (0)