Targeted Static Analysis for OCaml C Stubs: eliminating gremlins from the code

Edwin Török
2023-07-28
Abstract:Migration to OCaml 5 requires updating a lot of C bindings due to the removal of naked pointer support. Writing OCaml user-defined primitives in C is a necessity, but is unsafe and error-prone. It does not benefit from either OCaml's or C's type checking, and existing C static analysers are not aware of the OCaml GC safety rules, and cannot infer them from existing macros alone.The alternative is automatically generating C stubs, which requires correctly managing value lifetimes. Having a static analyser for OCaml to C interfaces is useful outside the OCaml 5 porting effort too. After some motivating examples of real bugs in C bindings a static analyser is presented that finds these known classes of bugs. The tool works on the OCaml abstract parse and typed trees, and generates a header file and a caller model. Together with a simplified model of the OCaml runtime this is used as input to a static analysis framework, Goblint. An analysis is developed that tracks dereferences of OCaml values, and together with the existing framework reports incorrect dereferences. An example is shown how to extend the analysis to cover more safety properties. The tools and runtime models are generic and could be reused with other static analysis tools.
Programming Languages
What problem does this paper attempt to address?
The paper primarily addresses the issues of security and error detection in the interaction between OCaml and C, particularly in response to the new requirements for C bindings in OCaml version 5. Specifically, the paper attempts to solve the following key problems: 1. **C Binding Maintenance**: With the release of OCaml version 5, many existing C bindings need to be updated to accommodate changes in the new version, such as the removal of support for naked pointers. Manually reviewing and updating these bindings is both time-consuming and error-prone. 2. **Development of Static Analysis Tools**: The paper proposes a static analysis tool for the OCaml-to-C interface (named `lintcstubs`), designed to detect common errors in C bindings, such as mismatched parameter counts and accessing OCaml values without holding the runtime lock. 3. **Changes in Security Rules**: With the changes in security rules in OCaml 5 (e.g., the removal of support for naked pointers), existing C bindings need to be reviewed and updated to ensure they comply with the new security standards. 4. **Need for Automated Tools**: To review and update thousands of functions at scale, an automated tool is needed to assist in this process, reducing human errors. 5. **Modeling Security Properties**: The paper details how to use the static analysis framework Goblint to track the state of the runtime lock and ensure that unsafe behaviors, such as accessing OCaml values when the lock is released, do not occur. In summary, the paper aims to improve code security and simplify the migration of C bindings during the upgrade to OCaml version 5 by developing a static analysis tool specifically for OCaml C bindings.