[MLton] Re: MLton and shared libraries

Jens Axel Søgaard jensaxel@soegaard.net
Wed, 20 Apr 2005 19:49:17 +0200


Stephen Weeks wrote:

>>Then I spent some time foolishly editing include/c-main.h and
>>friends until I found out that build/bin/mlton uses the include
>>files in build/lib/include.
> 
> Yes, but those are copied from include by the "runtime" target in the
> main Makefile.  I wouldn't recommend editing files in build/lib/.

Thanks for the warning - I gather recompiling MLton would have removed
any changes behind my back.

>>Looking at GC_init and MLton_init didn't make it clear to me what I
>>kind of dummy arguments I need to use in init_function.
> 
> The ones you used don't seem obviously wrong to me.

Ok.

>>The catious reader might have noticed that the body of main
>>actually starts like this:
> 
> ...
> 
>>         if (gcState.isOriginal) {                                       \
>>                 real_Init();                                            \
>>                 PrepFarJump(mc, ml);                                    \
> 
> ...
> 
>>The dummy arguments I used, implies that the false branch is taken.
>>I figured that it should have been the true branch, so I removed
>>the if-statement, but this might be a mistake?
> 
> 
> No, you definitely want the isOriginal branch.
> 
> You could use gdb at this point to find out where the segfault is.

Here is what I can get out of GDB:

bash-2.05b# cat test.sml
val e = _export "foo": int -> int;
val _ = e (fn (i) => 42);

bash-2.05b# cat test.ss
(require (lib "foreign.ss"))
(unsafe!)
(define lib (ffi-lib "/usr/soegaard/ml/source/mlton-20041109/soegaard/test"))

bash-2.05b# gdb mzscheme
GNU gdb 5.2.1 (FreeBSD)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-unknown-freebsd"...
(gdb) (load "test.ss")
Undefined command: "".  Try "help".
(gdb) run
Starting program: /usr/soegaard/plt/bin/mzscheme
Welcome to MzScheme version 299.102, Copyright (c) 2004-2005 PLT Scheme, Inc.
 > (load "test.ss")
init_function> before real_Init
init_function> after real_Init
init_function> before trampoline
init_function> trampoline 1

Program received signal SIGSEGV, Segmentation fault.
Chunk0 () at test.1.c:5084
5084            S(Word32, 0) = 0;
(gdb) bt
#0  Chunk0 () at test.1.c:5084
(gdb)


Since S is defined as

#define S(ty, i) *(ty*)(StackTop + (i))

in c-chunk.h, this is merely a write to the first element
of the stack. Which shouldn't be able to fail unless the
GC hasn't been initialized - I think.

Apropos, should gcState.isOriginal be true after a call
to GC_init ?

Anyways, Chunk 0 is the only chunk in test.1.c and the
offending line is in the very end:

         G(Pointer, 97) = Word32_add (Frontier, 0x4);
         Frontier = Word32_add (Frontier, 0x8);
         O(Pointer, G(Pointer, 97), 0) = G(Pointer, 143);
         S(Word32, 0) = 1;
         Push (4);
         goto main_0;
L_1:
         S(Word32, 0) = 0;          ;;; <- HERE
         Push (4);
         FlushFrontier();
         FlushStackTop();
         GC_gc (GCState, 0x45C, 0x0, __FILE__, __LINE__);
         CacheFrontier();
         CacheStackTop();
L_0:
         Push (-4);
         goto L_4;
case 2:
         goto L_3;
case 1:
         Push (-4);
         MLton_bug (G(Pointer, 98));
case 0:
         goto L_0;
EndChunk


-- 
Jens Axel Søgaard