Re: FFI callback is broken in last git version

Kostirya <[email protected]> Thu, 29 Sep 2016 18:17:46 +0300
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <CAG+QnTcTPgB70M0Wvc4vMKQ6Rknw8rd_i7vyJgpsvqbYNwV4qQ@mail.gmail.com>
I replaced it with the manual malloc, so it started working excellently.

It is like:

 val foo_ffi = _import "foo" reentrant: t -> unit;

 val malloc = (_import "malloc": Word.word -> t;) o Word.fromInt
 val free   = _import "free": t -> unit;

 fun foo () =
   let
     val r_mem  = malloc(4)
     val _ = setInt32(r_mem, 0, 0)
     val _ = foo_ffi(r_mem)
     val r = getInt32(r_mem, 0)
     val _ = free r_mem
   in
     r
   end



2016-09-29 17:19 GMT+03:00 Kostirya <[email protected]>:

> 2016-09-29 16:56 GMT+03:00 Matthew Fluet <[email protected]>:
> >
> > In your example, you _import "foo", but your C code defines "baz".
>
> It was a typo in the code example.
>
> >
> > But, other than that, I don't see anything wrong with the code, and
> > you should be able to pass an "int ref" to a C function, update it,
> > and then observe the update back in the SML code.
>
> That's right, it works in most calls, however it fails sometimes. The
> error only occurs on 64bit OS.
>
>
> >
> >
> > On Thu, Sep 29, 2016 at 9:41 AM, Kostirya <[email protected]> wrote:
> > > I found, thank you.
> > > But I do not know how to fix it.
> > > I'll try to describe on simplified example
> > >
> > > SML code:
> > >
> > >  val foo_ffi = _import "foo" reentrant: int ref -> unit;
> > >
> > >  fun foo () =
> > >    let
> > >      val r = ref 0
> > >      val _ = foo_ffi(r)
> > >    in
> > >      !r
> > >    end
> > >
> > > C code:
> > >
> > >  void baz (int * r) {
> > >          *r = 10; // There is Segmentation fault !!!
> > >          return;
> > >  }
> > >
> > > gdb:
> > > #4  0x00000000004212b4 in MLton_init (argc=<error reading variable:
> Cannot
> > > access memory at address 0x7ff2ce3ef174>,
> > >     argv=<error reading variable: Cannot access memory at address
> > > 0x7ff2ce3ef168>,
> > >     s=<error reading variable: Cannot access memory at address
> > > 0x7ff2ce3ef160>) at platform.c:20
> > > Backtrace stopped: previous frame inner to this frame (corrupt stack?)
> > >
> > > N.B. This only for 64 bit Linux. 32 bit - OK.
>
>

-- 
You received this message because you are subscribed to the Google Groups "MLton-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

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

_______________________________________________
MLton-user mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-user