tizoc / ocaml-interop

OCaml<->Rust FFI with an emphasis on safety.
MIT License
134 stars 21 forks source link

Use `dlopen2` crate to load OCaml runtime from Rust #57

Open yasuo-ozu opened 10 months ago

yasuo-ozu commented 10 months ago

(Sorry in advance if I misunderstood..)

Problem

When using Rust as host language and loading multiple OCaml custom binaries, conflicts occure in symbol names of OCaml's runtime.

Example setting

Using two wrapper crates a_rs and b_rs, which are wrapper crates of OCaml's library a and b. Link error occurs when trying to build a crate, which depends on both a_rs and b_rs.

Solution

The essential cause is we cannot support multiple OCaml runtimes in same Rust binary with current native (static or dynamic) loading mechanism. Using dlopen2 will solve it because it supports FFI of both native loading or dlopen mechanism. (I recommend dlopen strongly for Rust users who will create library crates (not binary crate)).

tizoc commented 10 months ago

Hi @yasuo-ozu. Why do the two wrappers link in the OCaml runtime? Isn't a better way of going around this issue to have a single OCaml runtime? So a_rs and b_rs will contain the OCaml code they wrap, but the OCaml runtime will only be included by c (the program using a_rs and b_rs)

yasuo-ozu commented 10 months ago

@tizoc Thanks for reply. I'm assuming that authors of a_rs and b_rs are different, and author of c doesn't aware that they wraps an OCaml runtime.

Additionaally, I am thinking of building an discrete runtime with ocamlc --make-runtime command and bundling with discrete Rust crate like caml_runtime_sys on which a_rs and b_rs depends, but it also problematic, becausr there are no warranty that OCaml nativa code of a_rs and b_rs are built with same OCaml toolchain.