Bin_prot in mirage unikernels?

So the problem is not precisely bigstringaf but “the required layout for a library to be compatible with MirageOS”. Generally, an usual library in OCaml can be used with MirageOS only if it does not depends on unix.cmxa.

dune comes with a mechanism, forbidden_libraries to ensure that you don’t depend on unix.

C stubs and MirageOS

However, for a library which has some C stubs, the requirement is a bit more complex. In fact, MirageOS can compile your librairies with something else than the usual standard C library. If you want to make a the spt/hvt unikernel (Solo5/ocaml-freestanding), your C stubs must be compiled with some extra flags and some specific headers.

The usual layout in such case (which appears for bigstringaf, digestif or mirage-crypto) is to provide into the distribution cross-compiled C artifacts for MirageOS. The example of bigstringaf is better than others because the library is small.

Finally, the library provides:

  • libbigstringaf_stubs_freestanding.a to be linked with a Solo5 unikernel
  • libbigstringaf_stubs_xen.a to be linked with a Xen unikernel

Both are compiled with specific rules:

  • lib/freestanding/dune
    It provides bigstringaf.freestanding which is a compilation of the copied bigstringaf_stubs.c with specific flags. Currently these flags can be get by pkg-config
  • lib/xen/dune
    It’s the same than freestanding but with Xen’s flags

Then, the library must provides the place where are located these specific C libraries. For that, we use the META file and we add these informations:

xen_linkopts = "-l:xen/libbigstringaf_xen_stubs.a"
freestanding_linkopts = "-l:freestanding/libbigstringaf_freestanding_stubs.a"

With all of that, you are able to use these C stubs with MirageOS! It’s a bit complex than usual but we must respect that to be able to correctly link and use your C stubs in any proposed targets by MirageOS.

The same layout exists for digestif, mirage-crypto, cstruct, bigarray-overlap or any libraries largely used by MirageOS which contains C stubs. To come back to your initial case, bin_prot seems to provide the Xen target (eg. xen directory) and it follows the same pattern:

  • a sub-library bin_prot.xen
  • a call to pkg-config to get extra flags
  • a modification of the META file

So, as far as I can tell, bin_prot is compatible with MirageOS only for Xen targets. However, it should be easy to extend it to Solo5 unikernel (and, again, follow the same layout).

Bigstringaf

bigstringaf was ported to MirageOS mostly to facilitate the port of httpaf to MirageOS (as one of its dependencies). It is about an issue from the OCaml community to have a decent API to use a bigstring (or more specially, a (char, unsigned_int8_elt, c_layout) Array1.t.

Some others projects like it exist:

  • bigstring
  • bigstringaf
  • ocplib-endian
  • base_bigstring

And an issue exist to integrate such API into the standard library: https://github.com/ocaml/ocaml/issues/7772

Then, about the question if we should use Buffer or Bigstringaf, I think it depends on your use-case. As explained Hannes Mehnert interview about MirageOS and OCaml by Evrone, Bigarray has some advantages and some inconveniences.

1 Like