Using Electric Fence (libefence) just for a shared library
In order to diagnose a tricky memory corruption bug (memory is getting randomly overwritten) I thought about utilizing Electric Fence + some custom mprotect calls to ensure that the corrupted data structures are only writable when I want them to be written to (and I immediately get a SIGSEGV when they are attempted to be written to).
Unfortunately, said code is a Ruby C Extension, which makes running it under libefence a performance nightmare as running the whole ruby interpreter under libefence using
export LD_PRELOAD=libefence.so.0.0
is horribly slow.
OTOH, linking the ruby extension with libefence directly (by passing -lefence to the linker) seems to have no effect causing it to run without libefence's instrumentation.
Is there a way to run only the memory allocations happening 开发者_如何学JAVAin a specific shared library through libefence and leaving other shared libs and the main process alone?
The simplest way would be to link the Ruby C extension with a static libefence library. There could still be problems though - what if something is malloc()ed in your extension but freed within ruby?
If you get a SIGSEGV straight away but want speed - you may want to use gdb. As soon as the SIGSEGV occurs the debugger will break.
Alternatively valgrind is good for finding memory problems but is also slow.
精彩评论