diff mbox series

[committed] libphobos: Don't call free on the TLS array in the emutls destroy function.

Message ID 20220426134719.2729944-1-ibuclaw@gdcproject.org
State New
Headers show
Series [committed] libphobos: Don't call free on the TLS array in the emutls destroy function. | expand

Commit Message

Iain Buclaw April 26, 2022, 1:47 p.m. UTC
Fixes a segfault seen on Darwin when a GC scan is ran after a thread has
been destroyed.  As the global emutlsArrays hash still has a reference
to the array itself, and tries to iterate all elements.

Setting the length to zero frees all allocated elements in the array,
and ensures that it is skipped when the _d_emutls_scan is called.

Bootstrapped and regression tested on x86_64-linux-gnu and
x86_64-apple-darwin20.  Committed to mainline and backported to the
gcc-9/10/11 release branches.

Regards,
Iain.

---
libphobos/ChangeLog:

	* libdruntime/gcc/emutls.d (emutlsDestroyThread): Clear the per-thread
	TLS array, don't call free().
---
 libphobos/libdruntime/gcc/emutls.d | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/libphobos/libdruntime/gcc/emutls.d b/libphobos/libdruntime/gcc/emutls.d
index 6d9fb309a30..ee3603206b6 100644
--- a/libphobos/libdruntime/gcc/emutls.d
+++ b/libphobos/libdruntime/gcc/emutls.d
@@ -223,9 +223,9 @@  void** emutlsAlloc(shared __emutls_object* obj) nothrow @nogc
 }
 
 /*
- * When a thread has finished, remove the TLS array from the GC
- * scan list emutlsArrays, free all allocated TLS variables and
- * finally free the array.
+ * When a thread has finished, free all allocated TLS variables and empty the
+ * array.  The pointer is not free'd as it is stil referenced by the GC scan
+ * list emutlsArrays, which gets destroyed when druntime is unloaded.
  */
 extern (C) void emutlsDestroyThread(void* ptr) nothrow @nogc
 {
@@ -237,7 +237,7 @@  extern (C) void emutlsDestroyThread(void* ptr) nothrow @nogc
             free(entry[-1]);
     }
 
-    free(arr);
+    arr.length = 0;
 }
 
 /*