On Jan 10, 2007, at 1:39 PM, MenTaLguY wrote:

> It's not quite as shiny as dmalloc, but have you tried electric fence
> (libefence)?  On Linux, at least, you can use it via LD_PRELOAD,  
> without
> recompiling Ruby.

I haven't tried electric fence, but thanks for mentioning it.

I've given up trying to build Ruby with dmalloc support now that I've  
learned that MacOS X has built-in support for dmalloc-like memory  
debugging.  These debugging features are available automatically in  
the standard malloc routines, and I don't have to do anything special  
in the build process of Ruby.  For the curious, more information is  
available in Apple's Tech Note TN2124:

   http://developer.apple.com/technotes/tn2004/tn2124.html#SECMALLOC

> Also, do you see the memory corruption under both MacOS X and FreeBSD?

Yes, there seems to be memory corruption under both.  Here's what I  
get under FreeBSD 6.1-STABLE-200607.  I have two processes  
communicating over SSL that abort in two different ways.  The first  
dies with 'rb_gc_mark(): unknown data type', which causes the second  
to crash at exit (after I hit ^C) with '[BUG] Segmentation  
fault' (I've included the actual messages below).  I don't get these  
crashes if I run my program without fastthread (but my program can't  
run for long periods of time without fastthread because of a serious  
memory leak, so I can't say with 100% certainty that these crashes  
don't happen without fastthread).

If I immediately interrupt (^C) the server process before the client  
has connected to it, then I don't get '[BUG] Segmentation fault'.   
Threads are already running at this point (and require 'fastthread'  
has executed), but perhaps mutex operations haven't been done yet.   
Now, once a client connects, and some communication takes place  
(definitely causing mutexes/fastthread to be used), I get a segfault  
if I interrupt the server process.  Here's the transcript:

$ ~/ruby-1.8.5-p12/bin/ruby g.rb
Waiting for clients on port 8742...
^C./globalserver.rb:62:in `join': Interrupt
         from ./globalserver.rb:62:in `join'
         from g.rb:10
$ ~/ruby-1.8.5-p12/bin/ruby g.rb
Waiting for clients on port 8742...
accepted connection from 192.172.226.88
GlobalSpaceDemux: got hello from $Id: globalmux.rb,v 1.43 2006/12/13  
20:51:49 youngh Exp $, protocol 1
Waiting for clients on port 8742...
^C./globalserver.rb:62:in `join': Interrupt
         from ./globalserver.rb:62:in `join'
         from g.rb:10
./globalserver.rb:62: [BUG] Segmentation fault
ruby 1.8.5 (2006-12-25) [i386-freebsd6.1]

Abort trap: 6 (core dumped)

---------------------------------
### building ruby:
export CFLAGS=-g  # prevent building with -O2
./configure --prefix=/home/youngh/ruby-1.8.5-p12 --enable-pthread

### building fastthread-0.6.1 with '~/ruby-1.8.5-p12/bin/ruby setup.rb':
gcc -I. -I/home/youngh/ruby-1.8.5-p12/lib/ruby/1.8/i386-freebsd6.1 -I/ 
home/young
h/ruby-1.8.5-p12/lib/ruby/1.8/i386-freebsd6.1 -I/home/youngh/ruby/ 
fastthread-0.6
.1/ext/fastthread  -fPIC -g -c fastthread.c
gcc -shared -Wl,-soname,fastthread.so -L'/home/youngh/ruby-1.8.5-p12/ 
lib' -Wl,-R
'/home/youngh/ruby-1.8.5-p12/lib' -o fastthread.so fastthread.o  - 
lpthread -lcry
pt -lm   -lc

### incidentally, I get the same/similar problems if I build ruby  
without --enable-pthread

### for both crashes, the stack is corrupted--notice how rb_bug() is  
at frame #97
### the stack doesn't get corrupted this badly or at all with MacOS X  
(running on PowerPC)

$ gdb -c ruby.core
(gdb) file /home/youngh/ruby-1.8.5-p12/bin/ruby
Reading symbols from /home/youngh/ruby-1.8.5-p12/bin/ruby...done.
(gdb) bt
#0  0x2814a537 in ?? ()
#1  0x28137f71 in ?? ()
#2  0x00000000 in ?? ()
#3  0x00000004 in ?? ()
#4  0x00000006 in ?? ()
#5  0x00000005 in ?? ()
#6  0x28127c00 in ?? ()
#7  0x28127500 in ?? ()
#8  0x28127600 in ?? ()
#9  0x28127700 in ?? ()
#10 0x28127800 in ?? ()
#11 0x28127900 in ?? ()
#12 0x2810256a in ?? ()
#13 0x28127b00 in ?? ()
#14 0x28127c00 in ?? ()
#15 0x00000020 in ?? ()
#16 0x00000000 in ?? ()
#17 0x00000000 in ?? ()
#18 0x00000000 in ?? ()
#19 0x00000000 in ?? ()
#20 0x00000000 in ?? ()
#21 0x00000000 in ?? ()
#22 0x0000000d in ?? ()
#23 0x0000000d in ?? ()
#24 0x28142819 in ?? ()
#25 0x2814d4b4 in ?? ()
#26 0x083b6400 in ?? ()
#27 0xbfbfd9d4 in ?? ()
...
#90 0x00000258 in ?? ()
#91 0x083aefd0 in ?? ()
#92 0x00000001 in ?? ()
#93 0x2814d4b4 in ?? ()
#94 0xbfbfe230 in ?? ()
#95 0x00000002 in ?? ()
#96 0xbfbfded8 in ?? ()
#97 0x080de0be in rb_bug (fmt=0x8116000 "@?%(\025?\233???\020\b")
     at error.c:214
Previous frame inner to this frame (corrupt stack?)
(gdb)

-----------------------
### crash in the client process:
### the location given varies per run--this isn't a YAML bug
/home/youngh/ruby-1.8.5-p12/lib/ruby/1.8/yaml/rubytypes.rb:360: [BUG]  
rb_gc_mark(): unknown data type 0x20(0x83dffb0) non object
ruby 1.8.5 (2006-12-25) [i386-freebsd6.1]

Abort trap: 6 (core dumped)


  --Young