# CAMLroot: revisiting the OCaml FFI

**Authors:** Fr\'ed\'eric Bour

arXiv: 1812.04905 · 2019-04-29

## TL;DR

This paper revisits the OCaml FFI, proposing safer and simpler approaches to interface with C code by adding indirection and introducing a region-based lifetime tracking system, balancing safety and performance.

## Contribution

It introduces two methods—API indirection with optional runtime checks and a region-based lifetime system—to improve safety and usability of OCaml's C FFI.

## Key findings

- Safer API with optional runtime assertions
- Region-based lifetime tracking simplifies C code
- Trade-off between performance and safety achieved

## Abstract

The OCaml language comes with a facility for interfacing with C code -- the Foreign Function Interface or FFI. The primitives for working with the OCaml runtime -- and, in particular, with the garbage collector (GC) -- strive for a minimal overhead: they avoid unnecessary work and allow for calls to C code to be very cheap. But they are also hard to use properly. Satisfying the GC invariants leads to counter-intuitive C code and there are hardly any safety checks to warn the developer. In this work, we explore two complementary approaches to mitigate these issues. First, simply adding an indirection to the API manipulating OCaml values let us write safer code amenable to optional runtime tests that assert proper use of the API. Second, a notion of region for tracking lifetimes of OCaml values on C side let us trade some performance for simpler code.

## Full text

_Full body text omitted from this summary view._ Fetch the complete paper as Markdown: https://tomesphere.com/paper/1812.04905/full.md

## Figures

2 figures with captions in the complete paper: https://tomesphere.com/paper/1812.04905/full.md

## References

7 references — full list in the complete paper: https://tomesphere.com/paper/1812.04905/full.md

---
Source: https://tomesphere.com/paper/1812.04905