views:

148

answers:

3

I'm working on a multithreaded (pthread based) project. The project uses a library that I'm writing.

In order to check it I linked it with -lefence and it gave me SIGSEGV. After a lot of time spent in figuring out what's wrong, I finally decided to search the error on the library, even if it's functionality is extremely simple.

As test, I've tried to compile the test-unit linking with -lefence and I got SIGSEGV. The test is not messing with dynamically allocated memory, with the exception of code which, in different test units, succeded.

I noticed also that the -pthread flag sets the memory allocation as wrapped. gcc -dumpspecs | grep pthread produces the following:

%{posix:-D_POSIX_SOURCE} %{pthread:-D_REENTRANT}
%{pthread:-lpthread}    %{shared:-lc}    %{!shared:%{mieee-fp:-lieee} %{profile:-lc_p}%{!profile:-lc}}
 %{static: %{fmudflap|fmudflapth:  --wrap=malloc --wrap=free --wrap=calloc --wrap=realloc --wrap=mmap --wrap=munmap --wrap=alloca} %{fmudflapth: --wrap=pthread_create}} %{fmudflap|fmudflapth: --wrap=main}

Can it be the case that libefence is not supporting multithreading?

Edit: Basically I'm supposing that the electric fence library replaces the malloc/free calls of the regular libc. The fact that they have been wrapped with a thread-safe hood is useless, since they've wrapped again with non-reentrant stuff.

+2  A: 

In order to check it I linked it with -lefence and it gave me SIGSEGV. After a lot of time spent in figuring out what's wrong, I finally decided to search the error on the library, even if it's functionality is extremely simple.

I only know the concept of Electric fence, but you should be able to notice immediately where your buffer overflow occurs:

gcc ... -g ;
gdb a.out ;
run 
**Program received signal SIGSEGV, Segmentation fault.
address in function () from file.c**

and there is your overflow!

Kevin
Actually I did it, but is free() itself giving the error.
Dacav
I would say that it means you're trying to free a memory location which has not been allocated, are you sure about the pointer you're freeing ?
Kevin
@Kevin, negative: in this case I should get segmentation fault + lots of glib output even without electric fence, which is not happening.
Dacav
@Dacav; huh? You're expecting a segfault, yet you're asking why you're getting one? Or are you in fact NOT getting one, and expecting one?
roe
@roe: I'm just trying to spot possible bugs in memory management. My target is to verify absence of segmentation fault. Unfortunately I get segmentation fault, but in a place which has nothing to do with my memory management.
Dacav
+3  A: 

Have you tried valgrind?

jilles
I heard about valgrind, but I never tired it. I'll have a look. I'll give +1 for the advice. I'll accept your answer if valgrind will solve my dubts about efence.
Dacav
+1  A: 

Check put these two - it definitely did have a problem with phthread ( https://bugzilla.redhat.com/show_bug.cgi?id=54368 ) but recently someone claimed a fix ( http://www.mail-archive.com/[email protected]/msg280548.html ) Notice also the mention of another, delayed patch.

ZXX