Bug 3342 – TLS on dynamically loaded DLLs fails on Windows earlier than Vista
Status
RESOLVED
Resolution
FIXED
Severity
blocker
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2009-09-24T23:02:00Z
Last change time
2015-06-09T01:28:30Z
Keywords
wrong-code
Assigned to
nobody
Creator
bugzilla
Comments
Comment #0 by bugzilla — 2009-09-24T23:02:40Z
A full description of the problem: http://www.nynaeve.net/?p=187
The symptom of the problem is erratic memory corruption.
The only known workaround at the moment is to not use TLS on dynamically loaded DLLs for machines running Windows XP or earlier. Use __gshared instead.
Comment #1 by clugdbug — 2009-10-26T02:18:58Z
Marking as blocker, since this prevents me from using D2 professionally. It's currently not possible to avoid using TLS (even if you completely avoid Phobos), since druntime uses it.
Note that this problem only applies to _implicit_ TLS. Explicit TLS, using TlsGetValue() and TlsSetValue(), still works.
Comment #2 by r.sagitario — 2010-03-06T08:42:18Z
I've posted a patch for multi-threading support for DLLs here in bug #3885. This includes a fix for implicite TLS on XP.
It needs to access some global data in ntdll.dll, and the way to get there is not really obvious, but seems to be valid for all the ntdll-versions I have found on my system: SP2, one inbetween and SP3.
Comment #3 by bugzilla — 2010-03-09T23:11:09Z
I've been looking at the patch, it's an impressive piece of work!
But I'd like it to detect the Windows version, and not do anything if it is Vista or newer, as those systems don't have the TLS bug. If we don't do this check, since the patch relies on undocumented ntdll internals, D apps are vulnerable to breaking with Windows updates.
Also, skywing has some code to deal with this at http://www.nynaeve.net/Code/VistaImplicitTls.cpp
Can you check it to see if anything was missed?
Thanks!
Comment #4 by r.sagitario — 2010-03-10T01:32:28Z
There is a detection whether _tls_index is set and the tls-data-array contains a valid pointer (thinking about it the latter might be flawed), so this should cover any situation where the OS did not setup TLS correctly (when loaded at process init time, TLS is correct on XP, too).
Do you think, that an explicite version check is better? What about applications that are configured to pretend running under a different windows version?
As much as I can see, the referenced code contains two things not in the patch:
- it keeps track of used tls-indexes in a bitmap, but XP does not have this. That's why the patch prevents unloading of the DLL.
- it uses ExReleaseRundownProtection before accessing some TEB data, but this seems to be an (undocumented) kernel function, and I think we should be safe with our operations as the nt-loader holds a lock that disallows reentrence into DllMain