From c67efba47618489ece93db2527622091a89f07f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20IRMAK?= Date: Wed, 9 Dec 2020 20:00:34 +0300 Subject: [PATCH] Allocate _tlsRanges in C heap Similar to the issue fixed here https://github.com/dlang/druntime/pull/1655#issuecomment-279223238 , 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. --- src/rt/sections_elf_shared.d | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/rt/sections_elf_shared.d b/src/rt/sections_elf_shared.d index 539da0b0c1..914df2d5b4 100644 --- a/src/rt/sections_elf_shared.d +++ b/src/rt/sections_elf_shared.d @@ -286,6 +286,7 @@ else void finiTLSRanges(Array!(void[])* rngs) nothrow @nogc { rngs.reset(); + .free(rngs); } void scanTLSRanges(Array!(void[])* rngs, scope ScanDG dg) nothrow @@ -369,7 +370,13 @@ else * Thread local array that contains TLS memory ranges for each * library initialized in this thread. */ - @property ref Array!(void[]) _tlsRanges() @nogc nothrow { static Array!(void[]) x; return x; } + @property ref Array!(void[]) _tlsRanges() @nogc nothrow { + static Array!(void[])* x = null; + if (x is null) + x = cast(Array!(void[])*).calloc(1, Array!(void[]).sizeof); + safeAssert(x !is null, "Failed to allocate TLS ranges"); + return *x; + } //Array!(void[]) _tlsRanges; enum _rtLoading = false;