libphobos: Fix segfault in runtime caused by unexpected GC of TLS data.
authorIain Buclaw <ibuclaw@gdcproject.org>
Thu, 25 Apr 2019 15:31:35 +0000 (15:31 +0000)
committerIain Buclaw <ibuclaw@gcc.gnu.org>
Thu, 25 Apr 2019 15:31:35 +0000 (15:31 +0000)
libphobos/ChangeLog:

2019-04-25  Iain Buclaw  <ibuclaw@gdcproject.org>

PR d/90250
* libdruntime/gcc/sections/elf_shared.d (initTLSRanges): Populate
_tlsRanges in every startup thread.
* testsuite/libphobos.thread/thread.exp: Load libphobos-dg.exp.
* testsuite/libphobos.thread/tlsgc_sections.d: New test.

From-SVN: r270576

libphobos/ChangeLog
libphobos/libdruntime/gcc/sections/elf_shared.d
libphobos/testsuite/libphobos.thread/thread.exp
libphobos/testsuite/libphobos.thread/tlsgc_sections.d [new file with mode: 0644]

index f1b76bb88d2146160341bcde09524b742e8b141c..0d937e02bfce9eb4ac9cecfd857ed3411988de56 100644 (file)
@@ -1,3 +1,11 @@
+2019-04-25  Iain Buclaw  <ibuclaw@gdcproject.org>
+
+       PR d/90250
+       * libdruntime/gcc/sections/elf_shared.d (initTLSRanges): Populate
+       _tlsRanges in every startup thread.
+       * testsuite/libphobos.thread/thread.exp: Load libphobos-dg.exp.
+       * testsuite/libphobos.thread/tlsgc_sections.d: New test.
+
 2019-04-25  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
 
        * m4/druntime/cpu.m4 (DRUNTIME_CPU_SOURCES): Quote brackets.
index 3a2c85cba6454b37acde109643a860406bb0955c..1eafecdd32276134b12dab91918cdf2f0784f522 100644 (file)
@@ -308,7 +308,13 @@ else
      */
     Array!(void[])* initTLSRanges() nothrow @nogc
     {
-        return &_tlsRanges();
+        auto rngs = &_tlsRanges();
+        if (rngs.empty)
+        {
+            foreach (ref pdso; _loadedDSOs)
+                rngs.insertBack(pdso.tlsRange());
+        }
+        return rngs;
     }
 
     void finiTLSRanges(Array!(void[])* rngs) nothrow @nogc
index d35df567ab389367c46da0ccca7a1ecae8bb3330..3e760d3e3701e81cf4f85b51e6f1fb4ab5e34dc5 100644 (file)
@@ -14,6 +14,8 @@
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 
+load_lib libphobos-dg.exp
+
 # Initialize dg.
 dg-init
 
diff --git a/libphobos/testsuite/libphobos.thread/tlsgc_sections.d b/libphobos/testsuite/libphobos.thread/tlsgc_sections.d
new file mode 100644 (file)
index 0000000..1421d92
--- /dev/null
@@ -0,0 +1,39 @@
+final class Class
+{
+    // This gets triggered although the instance always stays referenced.
+    ~this()
+    {
+        import core.stdc.stdlib;
+        abort();
+    }
+}
+
+Class obj;
+
+static this()
+{
+    obj = new Class;
+}
+
+static ~this()
+{
+    // Free without destruction to avoid triggering abort()
+    import core.memory;
+    GC.free(cast(void*)obj);
+}
+
+void doit()
+{
+    foreach (i; 0 .. 10_000)
+        new ubyte[](100_000);
+}
+
+void main()
+{
+    import core.thread;
+    auto t = new Thread(&doit);
+    t.start();
+
+    // This triggers the GC that frees the still referenced Class instance.
+    doit();
+}