Bug #3818 Using Embedded Server function my_server_init causes crash
Submitted: 18 May 2004 19:37 Modified: 26 May 2004 7:18
Reporter: David Woakes Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Embedded Library ( libmysqld ) Severity:S2 (Serious)
Version:4.0.18 OS:Windows (Windows 2000)
Assigned to: Alexey Botchkov CPU Architecture:Any

[18 May 2004 19:37] David Woakes
Description:
I'm working on a C# application and intending to use platform invoke to use the embedded server.  I found that the routine my_thr_init.c is included from the mysys library which is not compiled with USE_TLS.

This led a crash in the routine my_thread_init at line 169.

I noticed that libmysl has it's own identical copy of my_thr_init.c and that libmysql is compiled with USE_TLS.

How to repeat:
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;

namespace TestMySQL
{
  public class LibWrap 
  {
    [ SuppressUnmanagedCodeSecurity ]
    [ DllImport( "libmysqld.dll", 
        EntryPoint="mysql_server_init", 
        ExactSpelling=true, 
        CharSet=System.Runtime.InteropServices.CharSet.Ansi )]
    public static extern int ServerInit([ In ] int argc,
      [ In ] IntPtr[] args,
      [ In ] IntPtr[] groups);

    [ SuppressUnmanagedCodeSecurity ]
    [ DllImport( "libmysqld.dll", 
        EntryPoint="mysql_server_end", 
        ExactSpelling=true,
        CharSet=System.Runtime.InteropServices.CharSet.Ansi )]
    public static extern void ServerEnd();
  }
  /// <summary>
  /// Summary description for Class1.
  /// </summary>
  class Class1
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static IntPtr[] argv;
    static IntPtr[] groups;

    [STAThread]
    static void Main(string[] args)
    {
      argv = new IntPtr[7];
      argv[0] = StringToByteArray("dummy");
      argv[1] = StringToByteArray("--log-error=.\\mysql.err");
      argv[2] = StringToByteArray("--basedir=c:\\mysql\\");
      argv[3] = StringToByteArray("--datadir=c:\\mysql\\data\\");
      argv[4] = StringToByteArray("--skip-bdb");
      argv[5] = StringToByteArray("--skip-innodb");
      groups = new IntPtr[4];
      groups[0] = StringToByteArray("libmysqld_embServer");
      groups[1] = StringToByteArray("Embedded");
      groups[2] = StringToByteArray("Server");
      LibWrap.ServerInit(6, argv, groups);
      LibWrap.ServerEnd();
    }

    private static IntPtr StringToByteArray(string s)
    {
      IntPtr p = new IntPtr();
      byte[] b = new byte[s.Length+1];
      int i;
      for (i = 0; i < s.Length; i++)
        b[i] = (byte) s.ToCharArray()[i];
      b[s.Length] = 0;
      p = Marshal.AllocCoTaskMem(s.Length+1);
      Marshal.Copy(b,0,p,s.Length+1);
      return p;
    }
  }
}

Suggested fix:
I copied my_thr_init.c to the libmysqld project and added it to the project and built a new libmysqld dll.  This cured the problem.
[18 May 2004 22:17] MySQL Verification Team
Thank you for the bug report. I tested your sample code getting
the crash reported:

Unhandled Exception: System.NullReferenceException: 
Object reference not set to an instance of an object.
at testbug.LibWrap.ServerInit(Int32 argc, IntPtr[] args, IntPtr[] groups)
at testbug.Class1.Main(String[] args) in c:\temp\testbug\class1.cs:line 54

Added \libmysql\my_thr_init.c like you said but I got a compiler error
with the below line:

#if defined(__WIN__) && defined(EMBEDDED_LIBRARY)
 tmp->thread_self= (pthread_t)getpid();
#endif

my_thr_init.c(175) : error C2039:
'thread_self' : is not a member of 'st_my_thread_var'

I commented it just for test purposes and the libmysqld.dll
in fact not crash your application.
[18 May 2004 22:24] David Woakes
I had to make a couple of other changes apart from those I mentioned.  Not quite sure but somehow initially it did compile but later refused.  I changed the line you mentioned:

 tmp->thread_self= (pthread_t)getpid();

to

 tmp->pthread_self= (pthread_t)getpid();

And also copied the file mysys_priv.h into mylibsqld to get it recompiled.

When I do this I no error and can connect.  It was by adding traces to the routines in libmysqld I was able to find the error was caused by line 169 in my_thr_init.c.

Apologies for my incomplete report.  I find it odd that your traces didnt tell you where the error was occuring!  What did your traces tell you ?
[26 May 2004 7:18] Alexey Botchkov
bk commit - 4.0 tree (hf:1.1849) BUG#3818
[26 May 2004 7:18] Alexey Botchkov
Thank you for your bug report. This issue has already been fixed
in the latest released version of that product, which you can download at 
http://www.mysql.com/downloads/
[26 May 2004 7:31] David Woakes
I note that this issue has been closed with the comment that a fix is incorporated into the latest version of MySQL.  4.0.18 is the current production release of MySQL for windows and is where I found the bug.

Or does this mean a new release is about to be made ?