Bug #28358 libmysql.dll cannot be dynamically loaded on Windows
Submitted: 10 May 2007 16:20 Modified: 27 May 2007 16:30
Reporter: Tom Donovan (Candidate Quality Contributor) Email Updates:
Status: Closed Impact on me:
Category:MySQL Server: Packaging Severity:S2 (Serious)
Version:5.0.41, 5.1 OS:Microsoft Windows
Assigned to: Daniel Fischer CPU Architecture:Any

[10 May 2007 16:20] Tom Donovan
Starting with MySQL Community Server version 5.0.41, libmysql.dll cannot be dynamically loaded on Windows.

This is a common requirement.  For example, Apache mod_php will usually need to load libmysql.dll.

The symbol USE_TLS is no longer defined in the Windows build.  Windows is expected to allocate the storage for THR_KEY_mysys in mysys/my_thr_init.c using the pthread_key macro.

This violates the Visual C++ Rules and Limitations for TLS
"If a DLL declares any nonlocal data or object as __declspec( thread ), it can cause a protection fault if dynamically loaded."

How to repeat:
Use Windows LoadLibrary() to load libmysql.dll, or (in Apache) use the LoadFile "{path}/libmysql.dll" directive.

Access violation - code c0000005 (first chance)
008cbd5d 8b3481          mov     esi,dword ptr [ecx+eax*4] ds:0023:00000000=????????

Stack trace:
libmysql!my_thread_init+0xd [c:\work\mysql-5.0.41\mysys\my_thr_init.c @ 265]
libmysql!my_thread_global_init+0xa6 [c:\work\mysql-5.0.41\mysys\my_thr_init.c @ 154]
libmysql!my_init+0x34 [c:\work\mysql-5.0.41\mysys\my_init.c @ 87]
libmysql!libmysql_init+0x18 [c:\work\mysql-5.0.41\libmysql\dll.c @ 41]
libmysql!LibMain+0x36 [c:\work\mysql-5.0.41\libmysql\dll.c @ 63]
libmysql!__DllMainCRTStartup+0x6c [f:\sp\vctools\crt_bld\self_x86\crt\src\dllcrt0.c @ 365]
libmysql!_DllMainCRTStartup+0x1d [f:\sp\vctools\crt_bld\self_x86\crt\src\dllcrt0.c @ 328]
WARNING: Stack unwind information not available. Following frames may be wrong.

Suggested fix:
Defining the symbol USE_TLS allows libmysql.dll to be dynamically loaded.
This symbol was defined for Windows prior to version 5.0.41.
[10 May 2007 18:42] MySQL Verification Team
Thank you for the bug report.

"The symbol USE_TLS is no longer defined in the Windows build."

How you verified that because looking our download page the source package
for Windows is still the version 5.0.37 and looking the project solution
from the current source tree I have;

/O2 /Ob1 /I "." /I "..\include" /I "../zlib" /I "../extra/yassl/include" /D "DBUG_OFF" /D "NDEBUG" /D "USE_TLS" /D "MYSQL_CLIENT" /D "_WINDOWS" /D "_VC80_UPGRADE=0x0710" /D "_WINDLL" /GF /FD /EHsc /MT /Gy /Fp".\release_obj/libmysql.pch" /Fo".\release_obj/" /Fd".\release_obj/" /W3 /nologo /c /errorReport:prompt

Thanks in advance.
[10 May 2007 18:56] Tom Donovan
I built MySQL 5.0.41 from the source at http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.41.tar.gz/from/pick
using VC8 SP1, Win Platform SDK 2003 R2, & CMake 2.4.

Yes - the symbol was defined 5.0.37 (and 5.0.40), but it is not defined in 5.0.41.

More important, the pre-built Windows 32-bit binaries from http://dev.mysql.com/downloads/mysql/5.0.html
fail to dynamically load on Win2k & WinXP.
[10 May 2007 19:24] MySQL Verification Team
Thank you foe the feedback. I created the project solution with:
[miguel@light mysql-5.0]$ scripts/make_win_src_distribution.
So I assume you used a procedure using CMake?
[10 May 2007 20:06] Tom Donovan
I built using:


      (edited the file .\win\configure.data 
       and fixed the unclosed double-quotes)


Then I used the MySQL.sln solution file.  
I made sure the path to cmake.exe and bison.exe was in my "build exe dirs".

*  Config 'Release' or 'RelWithDebInfo' gives the same results as the pre-built binaries.

*  If I define USE_TLS and rebuild, libmysql.dll can be dynamically loaded.

Here's a short stand-alone test program.  Give the .dll filename as an argument.

#include <windows.h>
main(int argc, char **argv)
{   HMODULE hndl;
    hndl = LoadLibraryEx(argv[1],NULL,0);
    if (!hndl)  printf("System Error %d\n", GetLastError());
    else        printf("Loaded OK, handle = 0x%lx\n", hndl);

C:\MySQL> CL test.c

C:\MySQL> test.exe libmsql.dll
System Error 998

Invalid access to memory location.
[11 May 2007 2:29] Scott MacVicar
I just spent the last few hours trying to work out why I couldn't load the latest PHP connectors I just built. Dropping in an older libmysql.dll resolves the issue.

I can confirm the symptoms but haven't got any spare time to look at the cause.
[11 May 2007 12:38] Jean-Yves Délèze
I had exactly the same problem with libmysql.dll (v5.0.41) from the pre-built Windows 32-bit binary.

When I build the dll using VC++ 2005 Express Edition, Windows Platform SDK 2003 R2, CMake 2.4.6 and bison 2.1, the problem is the same. 

Like Tom, the dll can be dynamically loaded if I define USE_TLS before building.
[11 May 2007 13:02] Sveta Smirnova
Thank you for the report.

Verified as described in last comment: CMake doesn't set USE_TLS
[14 May 2007 11:24] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:


ChangeSet@1.2484, 2007-05-14 13:20:18+02:00, df@pippilotta.erinye.com +1 -0
  bug#28358 libmysql.dll cannot be dynamically loaded on Windows
[24 May 2007 15:14] Bugs System
Pushed into 5.1.19-beta
[24 May 2007 19:31] Bugs System
Pushed into 5.0.44
[25 May 2007 11:42] Joerg Bruehe
Included in the 5.1.19 build, and in the main tree for next 5.0 (= 5.0.44).
[27 May 2007 16:30] Paul Dubois
Noted in 5.0.44, 5.1.19 changelogs.

libmysql.dll could not be dynamically loaded on Windows.
[5 Jul 2007 12:17] MySQL Verification Team
Bug: http://bugs.mysql.com/bug.php?id=29566 was marked as duplicate of
this one.