[MLton] Re: [MLton-user] ffi newbie (fwd)

Matthew Fluet fluet at tti-c.org
Thu Sep 6 20:07:46 PDT 2007


---------- Forwarded message ----------
Date: Thu, 6 Sep 2007 18:12:01 -0400
From: Sean McLaughlin <seanmcl at gmail.com>
Reply-To: seanmcl at cmu.edu
To: Matthew Fluet <fluet at tti-c.org>
Subject: Re: [MLton-user] ffi newbie

Hi Matthew,

   I've got the segfault in the program narrowed down to 4 small C and ML
files.
It is a very strange one.  In another part of the code I'm using the GLPK
linear programming C library.
To deallocate storage I'm using the F = MLton.Finalizable structure.  Oddly,
if
I call [F.new], I get the segfault, but not without it.

You can access and compile the offending program with

1) svn co [url deleted]

and running

2) Make

followed by

3) kepler self-test

This should do some tests and return normally.
If you uncomment the new call on line 16 (marked) in glpk.sml it segfaults
on my machine.
I find this very strange, and probably some kind of fluke, but it's a
possibility
that the Finalize module is messing things up.

Another thing is that maybe somehow the one function in glpk-stubs.c is not
right, but I've been using that module for months on LP problems and it's
working
fine.  It's only when I include it that the cfsqp module breaks... Weird!

As I said, you need -lglpk installed to compile and witness the fault.

I know you're busy, but if you get a chance to look at this I'd really
appreciate it.
Sorry for the horrendous amount of code for a test, but I can't figure out
how
to get it to happen otherwise.

Best,

Sean



On 9/5/07, Matthew Fluet <fluet at tti-c.org> wrote:
>
> On Wed, 5 Sep 2007, Sean McLaughlin wrote:
>>  I'm having trouble using the foreign function interface.  The
>> bug I have is strange, as I can only get it to occur in the context
>> of a larger program.
>
> If you have a large program that exhibits the problem, you can post it to
>    http://www.mlton.org/TemporaryUpload
>
>>  I implemented bindings for CFSQP in OCaml with little trouble,
>> and thus have confidence that in principle the SML-CFSQP connection
>> should work.  The problem I face with MLton is that the functions
>> need to take arrays of doubles.  Since these must be allocated
>> in the ML heap, I'm not sure how to create such arrays in C.
>> The solution I came up with is to allocate a global array for
>> the arguments in ML, and to set the values from C, finally calling
>> the SML function on the global array.
>
> Right, we currently do not have a way of registering roots/pointers with
> the garbage collector.  When you are in C, a pointer to an ML object isn't
> really valid after calling an ML exported function, since during the ML
> execution, the garbage collector may have moved the object.
>
> The global array and exporting a function to update the array seems
> reasonable, and should work with the FFI.
>
>>  I kept getting segfaults though, so I simplified the code until
>> it no longer does anything except modify that global argument array.
>> I figure it's simple enough now for someone to see my mistake.
>
> The code looks fine to me.  I was able to run the program without getting
> any segfaults, on both amd64-linux and x86-darwin.
>
>



More information about the MLton mailing list