Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime Error using Static Function Pointer (taking C structures with array fields as an argument) #507

Closed
aziem opened this issue Feb 28, 2017 · 3 comments

Comments

@aziem
Copy link

aziem commented Feb 28, 2017

I am getting a runtime error ("libffi does not support passing arrays") when trying to pass an Ocaml function to a C library, where the type of the function uses a structure containing an array field.

In more detail: I am trying to pass an OCaml function to a C library, where the function to be passed takes a struct with an array:

typedef struct { const void *data[2] ... } Blah

and the prototype of the callback in the C library is something like:

typedef enum VisitResult (*Visitor)(Blah, Blah, void*)

With stub generation I can create the OCaml type for the struct. In the OCaml I am creating a type for the function to be passed to the C library as follows:

let visitor_type = Ctypes.(blah @-> blah @-> (ptr void) @-> returning visit_result)

but when I try to do the coercion trick (mentioned in #364) on the above between funptr and static_funptr:

coerce (Foreign.funptr vistor_type) (static_funptr visitor_type) ocaml_callback_fn

I get a runtime exception being thrown:

Fatal error: exception Ctypes_static.Unsupported("libffi does not support passing arrays")

My bindings compile fine but at runtime I get the error message above: is there a way to fix this?

@aziem
Copy link
Author

aziem commented Mar 1, 2017

I fixed my issue by using the trick mentioned in #113 (direct link to the mentioned libffi workaround). Rather than wrapping my struct using:

let blah : blah structure typ = structure "Blah"
...
let data_field = field blah "data" (array 2 (ptr void)) 

instead doing:

let data_field = Array.init 2 (fun i -> F.field blah "data") (ptr void))

gets around the issue.

@yallop
Copy link
Owner

yallop commented Mar 1, 2017

let data_field = Array.init 2 (fun i -> F.field blah "data") (ptr void))`

I think that's likely to work. Whether you actually need to work around libffi's support for structs with array members depends on whether you actually need to pass OCaml functions to C. If all of your function pointers are created in C code then it'd be better to use static_funptr in place of funptr everywhere.

@yallop
Copy link
Owner

yallop commented Jul 14, 2017

I'm closing this, since it appears resolved.

@yallop yallop closed this as completed Jul 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants