https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90250

            Bug ID: 90250
           Summary: libphobos: segfault in runtime caused by unexpected GC
                    of TLS data.
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: d
          Assignee: ibuclaw at gdcproject dot org
          Reporter: ibuclaw at gdcproject dot org
  Target Milestone: ---

The crash is only observed on non Linux/glibc systems.

Reason being because glibc puts the TLS area for each new thread at the
beginning of the newly created stack. Due to the way we detect the stack
bottom, we hoover up the TLS data along with what we think the stack is.

This is of course a dirty implementation detail, but explains why things don't
crash on GNU/Linux the way they are.

---
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();
}

Reply via email to