Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 8ba7ecb

Browse files
omerfirmakdlang-bot
authored andcommitted
Allocate _tlsRanges in C heap
Similar to the issue fixed here #1655 (comment) , static version of the sections_elf_shared accesses TLS of a dead thread. The simplest fix is to allocate _tlsRanges somewhere that would persist outside of the thread scope, for example the C heap. This was encountered while porting ldc 1.24.0 to Alpine Linux which uses Musl. https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/14364 But this issue should be affecting other libc implementations as well.
1 parent 1e64c52 commit 8ba7ecb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/rt/sections_elf_shared.d

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ else
286286
void finiTLSRanges(Array!(void[])* rngs) nothrow @nogc
287287
{
288288
rngs.reset();
289+
.free(rngs);
289290
}
290291

291292
void scanTLSRanges(Array!(void[])* rngs, scope ScanDG dg) nothrow
@@ -369,7 +370,13 @@ else
369370
* Thread local array that contains TLS memory ranges for each
370371
* library initialized in this thread.
371372
*/
372-
@property ref Array!(void[]) _tlsRanges() @nogc nothrow { static Array!(void[]) x; return x; }
373+
@property ref Array!(void[]) _tlsRanges() @nogc nothrow {
374+
static Array!(void[])* x = null;
375+
if (x is null)
376+
x = cast(Array!(void[])*).calloc(1, Array!(void[]).sizeof);
377+
safeAssert(x !is null, "Failed to allocate TLS ranges");
378+
return *x;
379+
}
373380
//Array!(void[]) _tlsRanges;
374381

375382
enum _rtLoading = false;

0 commit comments

Comments
 (0)