[fpc-devel] FPC shared lib called from C shared lib called from C main program crashes

Lukas Zeller luz_info at synthesis.ch
Thu Sep 17 17:52:24 CEST 2009


Hi Jonas,

A quick followup:

In the meantime I tested the supersimple test program that reliable  
crashes on 10.6 also on a 10.5 installation.
Now here it behaves correctly (?!)

HOWEVER, with the original, big program the problem also shows on 10.5.
As I discovered another side effect (garbled function result, see  
readme.txt below) on 10.6, I assume there IS something within FPC that  
can cause internal memory damage - and it's a matter of luck if that  
leads to a full crash or not.

As the examples are so small, I post them here for further discussion:


--- hellolib.pas  
--------------------------------------------------------------------------

library hellolib;

function ua:integer; cdecl;
begin
     writeln('using standard output from pascal lib');
     ua := 42;
end;

exports ua;

begin
     writeln('Comment out this, and the writeln above in function ua  
will crash');
end.

--- hello.c  
-------------------------------------------------------------------------------

#include <stdio.h>

extern int ua();

int main()
{
     printf ("hello world\n");
     printf ("universal answer = %d\n", ua());
     return 0;
}

--- readme.txt  
----------------------------------------------------------------------------

Example program to show that when using Stdout (Write, WriteLn) from  
within a FPC
shared library which is called from a plain C main program, the  
program crashes
(exit code 103), UNLESS a Write/WriteLn of at least one character is  
done
in the FPC library's initialisation block (begin … end.)

The issue was directly reproducible on Mac OS 10.6 with this small  
test program,
but not on 10.5. We however had the problem on 10.5 as well with our  
real (much bigger)
program.
Looks like memory corruption of some sort.


Compile the example as is:
--------------------------

# - make the lib (no_dead_strip_inits_and_terms to make it run on Snow  
Leopard)
fpc -CD hellolib.pas -k"-no_dead_strip_inits_and_terms"

# - make the main
gcc -arch i386 -L`pwd` -lhellolib hello.c -o hello


Run the example with a WriteLn() in the library init code
---------------------------------------------------------

./hello

# Output should be:
Comment out this, and the writeln above in function ua will crash
hello world
using standard output from pascal lib
universal answer = 42

# Output in Snow Leopard is (weird function result, stdout from the  
library is not visible at all)
hello world
universal answer = 65578


Run the example with COMMENTED OUT WriteLn() (i.e. EMPTY library init  
code)
---------------------------------------------------------------------------

./hello

# Program does not output anything, simply exits

echo $?
103

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




Lukas



More information about the fpc-devel mailing list