I had some questions about FFI after watching ICFP 2018 video “Safely Mixing OCaml and Rust”.
Motivating example:
value pair(value a, value b) {
CAMLparam2(a, b); /*required*/
CAMLlocal1(r); /*required*/
r = caml_alloc(2,0);
Store_field(r,0,a);
Store_field(r,1,b);
CAMLreturn(r); /*required*/
}
The observation was that the C compiler does not tell you your code is incorrect if you leave out the /*required*/
lines. Another observation was that ctypes
does not handle object lifetime issues (although its cstubs
library does handle object layout issues).
So, could there be a C-like DSL tailored just for the OCaml/C FFI that, when the DSL is interpreted, outputs object-lifetime-safe C glue code? If the DSL were embedded in OCaml it could be checked against the six rules of the OCaml Manual’s “Living in harmony with the garbage collector” … perhaps through QuickCheck-like property testing, AFL fuzz testing, or even a rudimentary borrow checker.
Thoughts?