Weird shared lib function recursion problem

Weird shared lib function recursion problem

Post by Nathan Anderso » Wed, 29 Jul 1998 04:00:00



OK, I'm stumped, folks.

Just for the record: Linux kernel 2.0.33, ELF, libc 5.4.33

I'm working on a project where I just took a chunk of code that was
working PERFECTLY and stuck it in a shared library that is dynamically
loaded at runtime with dlopen( ).  The one function that resides in this
shared library is recursive; that is, it calls itself under certain
conditions (of course, you already knew that ;-)).

The odd problem that I am having is, the first time that the function's
logic leads it to calling itself, my application SEGFAULTs!  It
SEGFAULTs even before it hits the first line of code within the
function.  Of course, the first time that the function is called from an
_outside_ source it runs fine.  But when it calls _itself_ for the first
time is when it dies.

Here's the stumper: I found out what the cause of the problem is.  The
one condition that will reproduce the problem every time is this: one of
the arguments accepted by the recursive function within the shared
library is a pointer to a function, and the function to which the
pointer belongs accepts arguments.  If the function to which the pointer
belongs has an empty argument list, or if a pointer to a function is
_not_ accepted by the function residing in the shared library, no
SEGFAULT occurs.

(Heh, what a mouthfull.)

The function pointed to doesn't even have to be called by the function
in the shared library; it just has to be passed to another call to the
function made by the function.

To demonstrate the problem, I've included below a simple little program
which reproduces the problem.  To compile, use the following:

gcc -shared [-g] -o foo.so -fPIC foo.c
gcc -rdynamic [-g] -o main main.c -ldl

...or just use the script included in the attached tarball (770 bytes).

Anyone got an explanation for this, or perhaps even a _solution_?

Really, _ANY_ suggestion will be _greatly_ appreciated! :-)

---- (sources top) ----

/* foo.c : top */

#include <stdio.h>

int foo( int, int ( * )( int, char *[ ] ) );

int foo( int num, int ( * func )( int argc,
                                  char * argv[ ] ) )
{
   unsigned char * bar;

   bar = ( unsigned char * ) malloc( sizeof( char ) * 256 );

   printf( "foo\n" );

   if( num == 1 )
      return( 0 );

   foo( num - 1, func );

   free( bar );
   return( 0 );

Quote:}

/* foo.c : end */

-----------------------

/* main.c : top */

#include <dlfcn.h>
#include <stdio.h>

int ( * foo )( int, int ( * )( int, char *[ ] ) );

int main( int argc, char * argv[ ] )
{
   void * handle;

   if( ( ( handle = dlopen( "./foo.so", RTLD_NOW ) ) == NULL ) ||
       ( ( foo = dlsym( handle, "foo" ) )            == NULL ) )
   {
      printf( "%s\n", dlerror( ) );
      exit( 1 );
   }

   ( * foo )( 10, &main );

   dlclose( handle );
   return( 0 );

Quote:}

/* main.c : end */

---- (sources end) ----

TTFN,

--
+-----+--------------------------------------------------+-----+

|     |         -== NUMERUS UNUS DOLATOR SUM ==-         |     |
|     +--------------------------------------------------+     |
|                                                              |
| +----------------------------------------------------------+ |
| | UNITY(tm): Just Think(tm)                                | |
| | http://www.blarg.net/~aerie/Unity/                       | |
| |                                                          | |
| | CommonTone(tm): Simple WWW Database System               | |
| | http://commontone.home.ml.org/                           | |
| |                                                          | |
| | Webmaster, 4-H Computers of Snohomish County, WA         | |
| | http://4hcomp-sno-wa.home.ml.org/                        | |
+-+----------------------------------------------------------+-+

  foo.tar.gz
1K Download
 
 
 

1. Help - shared libs lib****.so.4 lib***.so.5

Can anyone advise me ??

I have recently set up a Linux-FT system - runnung well and v. stable -
installing applications and utilities many will not run as they appear
to be missing lib* files. eg. Mosaic asks for libc.so.4 libm.so.4, I
have libc.so.5 and libm.so.5 , so I need both sets of 'lib' files??
What is the best solution?

thanks

Chris

2. Stop iptables drop messages to console

3. weird behaviour: gcc-2.2.2 and shared libs

4. Q: Re: Q: Mitsumi Quad-Speed.

5. How to call same function in many shared libs?

6. samba + remote windows printing?

7. Profiling shared lib functions

8. Is there a utility to convert an ASCII string in a binary file?

9. Tricky question about '_init' function of a shared lib

10. Adding a function is an existing shared lib?

11. pointer function in shared lib

12. Tricky question about '_init' function of a shared lib

13. How to step into a shared lib function?