Issue 5457 - [GC] DLL startup code is out of order; gc proxy not set properly
Summary: [GC] DLL startup code is out of order; gc proxy not set properly
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: x86 Windows
: P3 normal
Assignee: Sean Kelly
URL:
Keywords: dll
Depends on:
Blocks:
 
Reported: 2011-01-16 22:08 UTC by Walter Bright
Modified: 2024-12-07 13:31 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Walter Bright 2011-01-16 22:08:28 UTC
DLLs link with gcstub.obj, this is because DLLs share the gc with the caller's gc, instead of having a separate gc that fights the caller's. The general idea is that upon initialization the DLL sets "proxy" to point to the caller's gc, and then all gc calls are routed through the proxy.

First, in our DLL's DllMain(), we call:

        case DLL_PROCESS_ATTACH:
            dll_process_attach(hInstance);

In dll_process_attach(), druntime calls:

    Runtime.initialize()

which calls:

    rt_init(null)

which calls:

   gc_init();
   initStaticDataGC();

which calls:

    gcstub.gc.gc_addRange()

which looks like:

extern (C) void gc_addRange( void* p, size_t sz )
{
    if( proxy is null )
    {
        Range* r = cast(Range*) realloc( ranges,
                                         (nranges+1) * ranges[0].sizeof );
        if( r is null )
            onOutOfMemoryError();
        r[nranges].pos = p;
        r[nranges].len = sz;
        ranges = r;
        ++nranges;
        return;
    }
    return proxy.gc_addRange( p, sz );
}

Note that proxy is null. It is supposed to be initialized by rt_loadLibrary(). But, sadly, it is too late since dll_process_attach() gets called first by LoadLibrary()!

This bug makes D DLLs loaded dynamically from a D program unusable.
Comment 1 dlangBugzillaToGithub 2024-12-07 13:31:12 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17112

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB