public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9937] libphobos: Don't call free on the TLS array in the emutls destroy function.
@ 2022-04-26 13:33 Iain Buclaw
  0 siblings, 0 replies; only message in thread
From: Iain Buclaw @ 2022-04-26 13:33 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:c5d458ec228b46cf99fe384e0532c309bdfeb6bb

commit r11-9937-gc5d458ec228b46cf99fe384e0532c309bdfeb6bb
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Tue Apr 26 14:10:09 2022 +0100

    libphobos: Don't call free on the TLS array in the emutls destroy function.
    
    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.
    
    libphobos/ChangeLog:
    
            * libdruntime/gcc/emutls.d (emutlsDestroyThread): Clear the per-thread
            TLS array, don't call free().
    
    (cherry picked from commit 796b7cbac3d553a91d37c3961b9391fb7e19d0c6)

Diff:
---
 libphobos/libdruntime/gcc/emutls.d | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libphobos/libdruntime/gcc/emutls.d b/libphobos/libdruntime/gcc/emutls.d
index 462230508ab..ebeeb8deda3 100644
--- a/libphobos/libdruntime/gcc/emutls.d
+++ b/libphobos/libdruntime/gcc/emutls.d
@@ -222,9 +222,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
 {
@@ -236,7 +236,7 @@ extern (C) void emutlsDestroyThread(void* ptr) nothrow @nogc
             free(entry[-1]);
     }
 
-    free(arr);
+    arr.length = 0;
 }
 
 /*


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-26 13:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-26 13:33 [gcc r11-9937] libphobos: Don't call free on the TLS array in the emutls destroy function Iain Buclaw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).