View Issue Details

IDProjectCategoryView StatusLast Update
0000132LDMud 3.3Compilation, Installationpublic2005-05-15 13:04
Reporterpeng Assigned To 
PrioritynormalSeveritycrashReproducibilityalways
Status closedResolutionfixed 
Fixed in Version3.3 
Summary0000132: gnutls + MALLOC_TRACE impossible
DescriptionI've tried to do both (using smalloc and addional MALLOC_TRACE_LPC) and it crashes at initialization of the gnutls-libary.
I've lost the core, but there was a statement about freeing memory without some magic word or so.. It seems allocation inside the gnutls-libary is not compatible with MALLOC_TRACE
TagsNo tags attached.
Attached Files
backtrace_malloc_trace_gnutls.txt (1,884 bytes)   
#0  UNLINK_SMALL_FREE (block=0xffffffff) at smalloc.c:1055
1055        const word_t bsize = block[M_SIZE] & M_MASK;
(gdb) bt
#0  UNLINK_SMALL_FREE (block=0xffffffff) at smalloc.c:1055
#1  0x080f5081 in mem_alloc (size=96) at smalloc.c:1527
#2  0x080f6a58 in xalloc_traced (size=86, malloc_trace_file=0x81193f3 "strfuns.c", malloc_trace_line=123) at xalloc.c:516
#3  0x080e9f01 in strbuf_grow (buf=0xbfffe200, len=22) at strfuns.c:123
#4  0x080e8bac in strbuf_add (buf=0xbfffe200, text=0xbfffd150 "No program to trace.\n") at strfuns.c:152
#5  0x080e8cc1 in strbuf_addf (buf=0x18, format=0x18 <Address 0x18 out of bounds>) at strfuns.c:226
#6  0x0809886b in collect_trace (sbuf=0xbfffe200, rvec=0x0) at interpret.c:18142
#7  0x0809893c in dump_trace (how=1, rvec=0x18) at interpret.c:18435
#8  0x080dfd44 in fatal (fmt=0x811b02c "mem_free: block %p magic match failed: size %lu, expected %lx, found %lx\n")
    at simulate.c:621
#9  0x080f5275 in sfree (ptr=0xffffffff) at smalloc.c:1821
#10 0x080f52ac in mem_free (ptr=0x829fd18) at smalloc.c:1854
#11 0x080f768f in free (ptr=0x829fd2c) at xalloc.c:1226
#12 0x402658c4 in gcry_free () from /usr/lib/libgcrypt.so.11
#13 0x0829fd2c in ?? ()
#14 0x402b856c in ?? () from /usr/lib/libgcrypt.so.11
#15 0x0829f7d0 in ?? ()
#16 0x402a0913 in gcry_mpi_sub () from /usr/lib/libgcrypt.so.11
#17 0x4cd72d6d in ?? ()
#18 0x0829fdb8 in ?? ()
#19 0x0829f7d0 in ?? ()
#20 0x402a0b8d in gcry_mpi_test_bit () from /usr/lib/libgcrypt.so.11
#21 0x402a6edf in gcry_mpi_aprint () from /usr/lib/libgcrypt.so.11
#22 0x00000011 in ?? ()
#23 0x0829fe74 in ?? ()
#24 0x0829ff00 in ?? ()
#25 0x4cd72d6d in ?? ()
#26 0x4000a670 in _dl_rtld_di_serinfo () from /lib/ld-linux.so.2
#27 0x402a24bc in gcry_mpi_gcd () from /usr/lib/libgcrypt.so.11
#28 0x00000080 in ?? ()
#29 0x0829ff30 in ?? ()
#30 0x00000001 in ?? ()
#31 0x00000001 in ?? ()
#32 0x0829fbe4 in ?? ()
tls_malloc_trace.diff (2,095 bytes)   
Index: pkg-tls.c
===================================================================
--- pkg-tls.c   (Revision 149)
+++ pkg-tls.c   (Arbeitskopie)
@@ -189,6 +189,46 @@
     gnutls_dh_set_prime_bits( *session, DH_BITS);
 } /* initialize_tls_session() */
 
+/*-------------------------------------------------------------------------*/
+static void *
+tls_xalloc (size_t size)
+
+/* Wrapper function so that (gnu)tls will use our special allocator.
+ * Must use this construct since direct using is impossible due to
+ * the defines in xalloc.h.
+ */
+{
+    return xalloc(size);
+} /* tls_xalloc() */
+
+/*-------------------------------------------------------------------------*/
+static void *
+tls_rexalloc (void *old, size_t size)
+
+/* Wrapper function so that (gnu)tls will use our special allocator.
+ * It also takes care of the special behaviour of gnutls (realloc
+ * with NULL-pointer instead of using malloc.
+ */
+{
+  if (old == NULL)
+    return xalloc(size);
+  return rexalloc(old, size);
+} /* tls_rexalloc() */
+
+/*-------------------------------------------------------------------------*/
+static void
+tls_xfree (void *p)
+
+/* Wrapper function so that (gnu)tls will use our special allocator.
+ * It also takes care of the special behaviour of gnutls (freeing
+ *  NULL-pointers
+ */
+{
+  if (p == NULL)
+    return;
+  return xfree(p);
+} /* tls_free() */
+
 #endif /* SSL Package */ 
 
 /*-------------------------------------------------------------------------*/
@@ -341,6 +381,15 @@
     gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
 #endif
 
+   /* Use a special allocator since its impossible to do a sbrk_trace with
+    * the default one.
+    */
+    gnutls_global_set_mem_functions(tls_xalloc,
+                                    tls_xalloc,
+                                    NULL,
+                                    tls_rexalloc,
+                                    tls_xfree);
+
     gnutls_global_init();
 
     gnutls_certificate_allocate_credentials(&x509_cred);
tls_malloc_trace.diff (2,095 bytes)   

Activities

lars

2004-09-29 22:55

reporter   ~0000192

A core file would be very helpful as I can't reproduce the problem on my machine.

peng

2004-10-02 04:40

reporter   ~0000195

This is the backtrace, but I supose its not that useful. I've no idea about enabling full symbols in the gnutls and gcrypt-libaries.
It crashes at

    f = gnutls_certificate_set_x509_key_file(x509_cred, certfile, keyfile, GNUTLS_X509_FMT_PEM);

the gnutls_global_init works fine.
Versions: 3.3.605, gnutls-1.0.17, libgcrypt-1.1.94

peng

2004-12-05 04:53

reporter   ~0000234

Ok, I've found a solution. Crash is because some strange behaviour of the gnutls-lib: It tries to free Null-Pointers and it tries to reallocate NULL-Pointers instead of allocating new memory.
Patch applied (wrapper functions for the custom allocator-funtions that take care of this.

lars

2004-12-07 23:23

reporter   ~0000241

Fixed in 3.3.627, with a slight modification: I modified the central xfree() and rexalloc() functions to accept NULL pointers and do the right thing with them, in order to bring them in line with the C standard. But I also kept the wrapper in gnutls to keep the ability to tag gnutls' allocations properly.

Issue History

Date Modified Username Field Change
2004-09-27 23:18 peng New Issue
2004-09-29 22:55 lars Note Added: 0000192
2004-10-02 04:36 peng File Added: backtrace_malloc_trace_gnutls.txt
2004-10-02 04:40 peng Note Added: 0000195
2004-10-02 04:42 peng File Added: core-gnutls_malloc_trace.gz
2004-12-05 04:53 peng Note Added: 0000234
2004-12-05 04:54 peng File Added: tls_malloc_trace.diff
2004-12-07 23:23 lars Status new => resolved
2004-12-07 23:23 lars Fixed in Version => 3.3
2004-12-07 23:23 lars Resolution open => fixed
2004-12-07 23:23 lars Assigned To => lars
2004-12-07 23:23 lars Note Added: 0000241
2004-12-07 23:23 lars Assigned To lars =>
2005-05-15 13:04 lars Status resolved => closed