Bug #9190 Got ERROR 2013 on CREATE FUNCTION
Submitted: 15 Mar 2005 10:48 Modified: 15 Mar 2005 11:52
Reporter: Marco Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: User-defined functions ( UDF ) Severity:S1 (Critical)
Version:4.1.10a, 4.0.24, 5.0.2 (All Standard) OS:Linux (Linux Fedora Core 3 x86)
Assigned to: CPU Architecture:Any

[15 Mar 2005 10:48] Marco
Description:
Hi,
I'm experimenting with the great MySQL feature: User Defined Function.

My MySQL distribution is the 4.1.10a Standard x86 RPM (download from MySQL site)
running on Fedora Core 3.

I've created my simple UDF function "hello" that when invoked produces ... guess what ...
an "Hello <arg>" message. :)

After compiling the source and registered the so file, I've open the mysql console and written the CREATE FUNCTION statement:

mysql> CREATE FUNCTION hello RETURNS STRING SONAME 'libhello.so';

but as result I've got:
ERROR 2013 (HY000): Lost connection to MySQL server during query

So I've downloaded the MySQL source and tried to compile the "udf_example.so" ... but nothing is changed ... I've got always the error 2013 :(((

Here below is the MySQL error log:

--- BEGIN MYSQL-ERRORLOG ---

mysqld: dynamic-link.h:62: elf_get_dynamic_info: Assertion `! "bad dynamic tag"' failed.
mysqld got signal 6;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
We will try our best to scrape up some info that will hopefully help diagnose
the problem, but since we have already crashed, something is definitely wrong
and this may fail.

key_buffer_size=8388600
read_buffer_size=131072
max_used_connections=1
max_connections=100
threads_connected=1
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_connections = 225791 K
bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

thd=0x891e1f0
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
Cannot determine thread, fp=0xbfe7e338, backtrace may not be correct.
Stack range sanity check OK, backtrace follows:
0x808d8d7
0x82e69f8
0x82f20d1
0x82e6d89
0x82f24f0
0x82eec89
0x8313a06
0x8314b65
0x8333557
0x831700a
0x83338fa
0x8317a41
0x831700a
0x8317d62
0x82e28c6
0x831700a
0x82e2bc6
0x82e28f5
0x811ca2b
0x80a0824
0x80a22d9
0x809c7c8
0x809c194
0x809b847
0x82e41ac
0x830da7a
New value of fp=(nil) failed sanity check, terminating stack trace!
Please read http://dev.mysql.com/doc/mysql/en/Using_stack_trace.html and follow instructions on how to resolve the stack trace. Resolved
stack trace is much more helpful in diagnosing the problem, so please do
resolve it
Trying to get some variables.
Some pointers may be invalid and cause the dump to abort...
thd->query at 0x89419c0 = create function do_system returns integer soname 'libhello.so'
thd->thread_id=1
The manual page at http://www.mysql.com/doc/en/Crashing.html contains
information that should help you find out what is causing the crash.

Number of processes running now: 0
050315 09:30:19 mysqld restarted
050315 9:30:19 [Warning] Asked for 196608 thread stack, but got 126976
050315 9:30:19 InnoDB: Started; log sequence number 0 4154912212
/usr/sbin/mysqld: ready for connections.
Version: '4.1.10a-standard' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Edition - Standard (GPL)

--- END MYSQL-ERRORLOG ---

Thank you

How to repeat:
Create the following C-file and name it as libhello.c:

--- BEGIN libhello.c ---

#ifdef __WIN__
typedef unsigned __int64 ulonglong;     /* Microsofts 64 bit types */
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/

#include <mysql.h>
#include <stdio.h>
#include <string.h>

#undef BEGIN_C_DECLS
#undef END_C_DECLS
#ifdef __cplusplus
# define BEGIN_C_DECLS extern "C" {
# define END_C_DECLS }
#else
# define BEGIN_C_DECLS /* empty */
# define END_C_DECLS /* empty */
#endif

#ifdef HAVE_DLOPEN

#define HELLO_STR "Hello "
#define HELLO_STRLEN 6

BEGIN_C_DECLS

my_bool hello_init( UDF_INIT* initid, UDF_ARGS* args, char* message );
void hello_deinit( UDF_INIT* initd );
char* hello( UDF_INIT* initid, UDF_ARGS* args, char* result, unsigned long* length, char* is_null, char* error );

my_bool hello_init( UDF_INIT* initid, UDF_ARGS* args, char* message )
{
        if ( args->arg_count != 1 || args->arg_type[0] != STRING_RESULT ) {
                strcpy( message, "Wrong arguments to hello; give a name" );
                return 1;
        }

        initid->max_length = HELLO_STRLEN + strlen( args->args[0] );

        return 0;
}

void hello_deinit( UDF_INIT* initid )
{
        /* empty */
}

char* hello( UDF_INIT* initid, UDF_ARGS* args, char* result, unsigned long* length, char* is_null, char* error )                                                {
        sprintf( result, "%s%s", HELLO_STR, args->args[0] );
        *length = strlen( result );

        return result;
}
              
END_C_DECLS     

--- END libhello.c ---

Compile it as (various CFLAGS and LDFLAGS come from mysql_config program):

$ cc -I/usr/include/mysql -mcpu=i486 -fno-strength-reduce -Wall   -c -o libhello.o libhello.c
$ cc -shared -L/usr/lib/mysql -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -lc -lnss_files -lnss_dns -lresolv -lc -lnss_files -lnss_dns -lresolv -o libhello.so libhello.o

Copy the so file to the system lib directory (e.g. /usr/lib) and register it (use the root user):

$ cp libhello.so /usr/lib/.
$ ldconfig -v

Enter to the MySQL console to create the function:
$ mysql -uroot -p

mysql> CREATE FUNCTION hello RETURNS STRING SONAME 'libhello.so';

Suggested fix:
No fix
[15 Mar 2005 11:28] Marco
This file contains the "libhello" code and can help you to reproduce the UDF problem

Attachment: libhello.tgz (application/x-gzip, text), 1.07 KiB.

[15 Mar 2005 11:52] MySQL Verification Team
I wasn't able to repeat the behavior reported with our udf_example.so

miguel@hegel:~/dbs/4.1$ bin/mysql -uroot  
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 4.1.11-debug-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE FUNCTION metaphon RETURNS STRING SONAME 'udf_example.so';
Query OK, 0 rows affected (0.01 sec)

mysql> select metaphon("miguel");
+--------------------+
| metaphon("miguel") |
+--------------------+
| MKL                |
+--------------------+
1 row in set (0.00 sec)

mysql> CREATE FUNCTION myfunc_double RETURNS REAL SONAME 'udf_example.so';
Query OK, 0 rows affected (0.00 sec)

mysql> select myfunc_double("miguel");
+-------------------------+
| myfunc_double("miguel") |
+-------------------------+
|                  107.17 |
+-------------------------+
1 row in set (0.01 sec)