Thread overview
Runtime error when accessing static data in a DLL
Dec 24, 2009
Phil Deets
Dec 24, 2009
Richard Webb
Dec 24, 2009
Phil Deets
Dec 24, 2009
Phil Deets
Dec 27, 2009
Don
Dec 24, 2009
Phil Deets
December 24, 2009
I'm writing a DLL in D which will be loaded (not automatically, but by LoadLibrary) by a C application (the Lua interpreter). I'm having problems with globals and static data. Everything is fine if I only access stuff on the stack, but as soon as I access a global or a static class variable, either the program crashes or if I am still in DllMain, the LoadLibrary function will fail and GetLastError will indicate an error with the message "Invalid access to memory location.".

I'll work on reproducing it in a smaller scale, but does anyone know how to fix it?

In case it is helpful info, in DllMain under the DLL_PROCESS_ATTACH case, I am just calling Runtime.initialize; even though the sample calls gc_init, _minit, _moduleCtor, and runModuleUnitTests.
December 24, 2009
Sounds like you might be running into this: http://d.puremagic.com/issues/show_bug.cgi?id=3342
December 24, 2009
On Thu, 24 Dec 2009 12:49:42 -0500, Richard Webb <webby@beardmouse.org.uk> wrote:

> Sounds like you might be running into this:
> http://d.puremagic.com/issues/show_bug.cgi?id=3342

Thanks for the link. That is probably my problem since I'm running Windows XP.

Phil
December 24, 2009
On Thu, 24 Dec 2009 10:49:01 -0500, Phil Deets <pjdeets2@gmail.com> wrote:

> I'll work on reproducing it in a smaller scale

Reduced test case:

test.c (compiled with Visual C++ 2008 Express Edition)

// Adapted from sample code at http://msdn.microsoft.com/en-us/library/ms680582(VS.85).aspx
#include <windows.h>
#include <strsafe.h>

void main()
{
	if(!LoadLibraryA("d.dll"))
	{
		LPVOID lpMsgBuf;
		DWORD dw = GetLastError();
		FormatMessageA(
			FORMAT_MESSAGE_ALLOCATE_BUFFER |
			FORMAT_MESSAGE_FROM_SYSTEM |
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			dw,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPSTR) &lpMsgBuf,
			0, NULL );
		printf("LoadLibrary failed with error %d: %s\n", dw, lpMsgBuf);
		LocalFree(lpMsgBuf);
	}
}

dll.d:

import std.c.windows.windows;
int x;
extern (Windows) BOOL DllMain(HINSTANCE, ULONG, LPVOID) {
	x=0;
	return true;
}

dll.def:

LIBRARY         "d.dll"
EXETYPE         NT
SUBSYSTEM       WINDOWS
CODE            SHARED EXECUTE
DATA            WRITE

Compiled with:

dmd dll.d dll.def -ofd.dll

When I run the C app, I get the output "LoadLibrary failed with error 998: Invalid access to memory location."
December 24, 2009
On Thu, 24 Dec 2009 13:10:14 -0500, Phil Deets <pjdeets2@gmail.com> wrote:

> On Thu, 24 Dec 2009 12:49:42 -0500, Richard Webb <webby@beardmouse.org.uk> wrote:
>
>> Sounds like you might be running into this:
>> http://d.puremagic.com/issues/show_bug.cgi?id=3342
>
> Thanks for the link. That is probably my problem since I'm running Windows XP.
>
> Phil

I'm quite confident now that this is the issue. I added my vote to the bug. Does anybody know how much the runtime uses TLS? I'm thinking about the possibility of adding __gshared to all its static data.

An alternative workaround would be to install Vista or 7, but although I do have a legal Vista install disk, I would prefer an upgrade since I don't want to reinstall all my programs. Also, my computer barely meets Vista's minimum requirements.

I also might be able to rewrite lua.exe in D and preload my code in the executable. Most of Lua's functionality is in a library which would not need to be rewritten so this might be feasible.
December 27, 2009
Phil Deets wrote:
> On Thu, 24 Dec 2009 13:10:14 -0500, Phil Deets <pjdeets2@gmail.com> wrote:
> 
>> On Thu, 24 Dec 2009 12:49:42 -0500, Richard Webb <webby@beardmouse.org.uk> wrote:
>>
>>> Sounds like you might be running into this:
>>> http://d.puremagic.com/issues/show_bug.cgi?id=3342
>>
>> Thanks for the link. That is probably my problem since I'm running Windows XP.
>>
>> Phil
> 
> I'm quite confident now that this is the issue. I added my vote to the bug.

Yes, it's the most serious bug in Bugzilla. (It's quite appalling that such a terrible bug has existed in Windows for so long). I wish we had a solution to this. I guess we could replace all uses of implicit TLS with a Windows function call -- it'd be slow, but it ought to work.


 Does anybody know how much the runtime uses TLS? I'm thinking about
> the possibility of adding __gshared to all its static data.
> 
> An alternative workaround would be to install Vista or 7, but although I do have a legal Vista install disk, I would prefer an upgrade since I don't want to reinstall all my programs. Also, my computer barely meets Vista's minimum requirements.


> 
> I also might be able to rewrite lua.exe in D and preload my code in the executable. Most of Lua's functionality is in a library which would not need to be rewritten so this might be feasible.