# Crustify: Repository-Level Generation of Safe Rust Bindings with LLM Agents and Coverage-Guided Testing

Crustify wraps C libraries in safe Rust bindings using LLM agents and coverage-guided tests

Marius Momeu, Marius Arvinte, Cory Cornelius, Koushik Sen · arXiv · 2026-11-7

- **Problem** Hand-written safe Rust bindings over C are tedious and error-prone, because Rust's compiler does not check the unsafe code inside them. A single mistake can cause undefined behavior, and no shared primitives exist for C pointers.
- **Method** Crustify is an agentic co-translator that binds C repositories in dependency-ordered waves. Agents derive each pointer's ownership, check bindings with equivalence and sanitizer tests, and a reviewer audits each wave.
- **Results** On libgit2, Crustify wrapped 41.9% of the public API in about 51.9 hours with one developer in the loop. Its tests reached 98.92% line coverage of the Rust bindings.

## Why safe C bindings keep breaking soundness

A safe binding has to meet two demands at once. It must be sound, so no caller who writes only safe code can cause undefined behavior (UB). It must also be ergonomic, so consumers never handle raw pointers or release C objects by hand. The Rust compiler does not verify soundness at the FFI boundary, so the implementer carries the whole proof.

Most such bindings are still written by hand, and no widely adopted set of primitives exists for C pointers in safe Rust.

The core friction is that Rust's smart pointers assume ownership and aliasing rules that C objects don't follow. A Box adopted from C is released by Rust's allocator rather than the library's own destructor. The same Box hands out &T and &mut T references that the C side may still alias, and the compiler cannot enforce that. Arc keeps its count in a header it allocates itself, while many C objects count references through their own library routines.

Replacing these references with lifetime-bound handles doesn't work, because Deref and DerefMut must return a plain reference. So the problem is a mismatch between Rust's ownership model and C's lifecycle conventions, not one isolated bug pattern.

Testing tools cover only part of this gap. Miri detects UB in Rust code, but it doesn't check the foreign code that it calls.

## A pointer layer that never forms a reference into C memory

Owners that mirror Box, Arc, and Vec hand out handles instead, so the C-specific parts stay inside the binding. Consumers then use the familiar Rust idioms, while the C-specific parts stay inside the binding.

Each owner takes a policy that names the release routine and, where they exist, the allocate and clone routines. Because the routines attach to the policy rather than to the type, one type can carry several policies, each a pair of constructor and destructor. Table 1 lists each ffibox primitive beside the standard-library type whose interface it mirrors.

![Table 1: ffiboxprimitives and the standard-library counterparts whose interfaces they mirror](https://refivu.com/papers/0a929076-bec9-4572-90ad-a8b133795903/figures/figure-a79a5930fbd4a5f304eeb7472c5edc51.png?thumb=512)

**Table 1** .ffiboxprimitives and the standard-library counterparts whose interfaces they mirror.

[Original paper](https://arxiv.org/abs/2611.10394v1) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) · Marius Momeu, Marius Arvinte, Cory Cornelius, Koushik Sen · Crustify: Repository-Level Generation of Safe Rust Bindings with LLM Agents and Coverage-Guided Testing · Original paper figure · Extracted the figure area from page 8 of the original PDF.

For borrowed access, every layout type defines FooRef and FooMut handles, each one pointer wide and tied to the memory it points to. A native &mut would assert uniqueness that C doesn't respect, which is UB.

CCell ties a layout type to its C type and its handles, so generic code reaches C objects only through handles.

## How a campaign orders, binds and reviews a C repository

A campaign first decides the order, because a binding can use a type's safe wrapper only after that type is wrapped. C hides many of these dependencies behind casts between struct types and type-erased void \* slots, which project-level translators tend to miss. It breaks cycles and places the items into waves of mutually independent, size-bounded batches, and it does no data-flow analysis itself.

Binders wrap and test each batch, and reviewers audit each landed wave. Batches within a wave run concurrently as isolated sessions in separate worktrees.

Before wrapping, each batch derives the ownership and lifecycle properties of its items from the code paths that store, transfer, clone or free each pointer. Where one pointer has several semantics, the batch forks it into one Rust form per semantics. For example, ffmpeg's av\_dict\_set takes ownership of its strings only when the caller sets a flag.

Each form fixes ownership, mutability, nullability and cardinality in its type, so the compiler checks every use.

Each batch writes equivalence tests that run the C implementation and the Rust API on equivalent inputs. These tests run under Miri, AddressSanitizer, UndefinedBehaviorSanitizer, ThreadSanitizer and BorrowSanitizer, and each instrument checks a different obligation. Line coverage is measured on both sides of the boundary, and tests are added for reachable paths that remain uncovered.

Each binding wave is followed by a review wave that finishes before any dependent wave starts. The reviewers look for undefined behavior, equivalence drift and coverage gaps, and Miri runs over the whole workspace in each review wave. The authors review between waves because a flawed binding would otherwise force breaking changes in every binding that depends on it.

## Coverage and defects on libgit2 and ffmpeg

Crustify's two campaigns ran over libgit2 and ffmpeg, whose public APIs have 165 and 210 types and 981 and 1,128 symbols. Crustify wrapped 41.9% of the libgit2 API and 42.8% of the ffmpeg API. The campaigns ran for about 51.9 and 58.9 hours, with one developer checking in between sub-campaigns. They cost $2,308 and $2,120 in tokens, about 5.1 and 4.5 billion of them.

The tests cover 98.92% of the libgit2 Rust bindings and 99.07% of the ffmpeg ones. Against these, git2-rs reached 60.59% Rust line coverage, and the ffmpeg community crates scored at most 18.73%. The authors measured every crate with the same coverage method over the same C, so the numbers share one protocol. Table 2 lists the Rust and C line coverage of each crate side by side.

On the C side, the tests covered 46.78% of libgit2 with its bundled xdiff and llhttp, compared to 45.12% for git2-rs. The ffmpeg C figure of 67.20% covers libavutil alone, the only ffmpeg library wrapped so far. So that figure doesn't describe all of ffmpeg, and the 45.95% libgit2 figure uses libgit2 alone.

Review agents found and fixed 8 defects in the libgit2 bindings and 13 in the ffmpeg bindings. Each of these defects caused UB reachable from Rust, and a reproducer confirmed each one. The paper treats these counts as a lower bound, because review is not exhaustive.

## How much unsafe surface remains for consumers

Crustify's bindings keep unsafe code off the interface that consumers use, and Table 2 places them beside git2-rs, ffmpeg-the-third, and ffmpeg-next. On the interface consumers are expected to use, 1.6% and 0.4% of the public functions are unsafe for libgit2 and ffmpeg. That count leaves out the seams and the methods of ffibox's traits that declare unsafe.

Counted over every public function, the libgit2 bindings declare 3.8% unsafe, against 4.1% for git2-rs. The ffmpeg bindings declare 2.4% unsafe, against 3.7% for ffmpeg-the-third and 5.7% for ffmpeg-next.

![Table 2: Generated versus community-maintained bindings. Baseline API coverage is an upper bound that counts every C item the crate’s source names, enums exclu…](https://refivu.com/papers/0a929076-bec9-4572-90ad-a8b133795903/figures/figure-c6df0c05e9c421a38f12cf540045efa3.png?thumb=512)

**Table 2** . Generated versus community-maintained bindings. Baseline API coverage is an upper bound that counts every C item the crate’s source names, enums excluded as for ours; ours counts wrapped items only. libgit2’s C line coverage is over libgit2 with its bundled xdiff and llhttp, which both crates compile, against 45.95% over libgit2 alone (Table 4). Public functions include the methods of trait implementations.

[Original paper](https://arxiv.org/abs/2611.10394v1) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

Only 0.1% and 0.6% of the public functions have a raw pointer in their signature. For ffmpeg, 14 of those raw-pointer positions store or return opaque user data. The bindings keep that user data visible on purpose, since a bare pointer would otherwise reach the interface without ffibox's primitives. Two libgit2 functions take a raw pointer to a C type the bindings don't wrap yet.

Among the community ffmpeg-next functions, safe constructors such as codec::video::RateIter::new dereference the pointer they receive.

Drop implementations written by hand release C objects in 28.3%, 8.5%, and 10.6% of the community crates' public types. The authors' bindings do this in 5 public libgit2 types, which is 1.3% of them. In ffmpeg, 3 public types, or 0.9%, implement Drop by hand. Those hand-written Drop types are guards over C state, Rust storage that C reads, and collections that CVec cannot represent.

Fewer consumer-facing types release C memory themselves, so consumers don't need to manage C objects by hand.

Behind this interface, unsafe blocks account for 16.6% of the non-test code in each set of bindings. The bindings wrap 86.8% and 95.3% of the C functions that libgit2-sys and libavutil-sys declare. The unsafe surface therefore covers only what the bindings wrap, not the whole C API.

## Limitations of the evidence for Crustify's bindings

The ffimap counts are a lower bound, because ffimap is a linter and not a verifier.

Sanitizers and Miri detect undefined behavior only on the paths that the tests execute. A clean run is therefore bounded by the test coverage that the campaigns measure, which is 98.92% and 99.07% of the Rust bindings. Repeated runs may produce different bindings, tests, and review findings, so the authors report one campaign per target rather than averages.

Review finds defects but doesn't show their absence, so the 8 and 13 defects reported are a lower bound.

The authors didn't run a campaign without the developer, so they don't measure how much their checks contributed. The authors stopped using the GPT models because their safety guardrails kept interrupting agents that wrote tests and reproducers for memory-safety violations.

The evaluation covers only libavutil of ffmpeg, the only ffmpeg library wrapped so far. C++ targets and incremental porting remain future work, and ffibox doesn't yet represent vectors whose elements own memory.

## Paper info

- **Authors**: Marius Momeu, Marius Arvinte, Cory Cornelius, Koushik Sen
- **Venue**: arXiv · Preprint
- **First published**: 2026-11-7
- **Added**: 2026-11-10
- **Updated**: 2026-11-7
- **Version**: v1
- **arXiv**: 2611.10394
- **Category**: cs.PL · cs.AI · cs.CR · cs.SE
- **Original paper terms**: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
- **What we show**: Paper info, commentary and permitted figures from the original paper

[Original paper ↗](https://arxiv.org/abs/2611.10394v1) · [PDF ↗](https://arxiv.org/pdf/2611.10394v1)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Abstract

Wrapping C code in a safe Rust interface lets Rust applications use it without writing unsafe code, until the code itself is migrated to Rust. Such bindings are tedious and error-prone to write, however: their internals use unsafe code that the Rust compiler does not check, so a single mistake can introduce undefined behavior (UB), and they have so far been written mostly by hand. We present Crustify, an agentic co-translator that generates safe Rust bindings for repository-level C libraries. Crustify schedules a target's types and symbols into dependency-ordered waves of independent batches that agents bind concurrently. Its agents derive the ownership and lifecycle contract of every pointer and type they wrap, check each binding with coverage-guided equivalence and UB tests under sanitizers and Miri, and review every wave. They build on three tools that we contribute: (a) ffibox, Rust primitives for C/Rust interop that mirror Box, Arc, and Vec for C objects without ever forming a reference to memory that C may alias; (b) Wavefront, a neuro-symbolic static analyzer that orders a target's items by their dependencies, including those that casts and type-erased pointers hide; and (c) ffimap, a rustc-based analysis that maps the unsafe surface of bindings. On libgit2 and ffmpeg, with one developer in the loop, Crustify generated safe bindings for 41.9% and 42.8% of their public APIs in about 51.9 and 58.9 hours. On the interface consumers are expected to use, only 1.6% and 0.4% of the bindings' public functions are unsafe, and only 0.1% and 0.6% have a raw pointer in their signature. Their tests cover 98.92% and 99.07% of the Rust code and 45.95% and 67.20% of libgit2 and of ffmpeg's libavutil, and the reviewers fixed 8 and 13 defects that caused UB reachable from Rust. To our knowledge, this is the first work to automate the generation of safe Rust bindings for C libraries.

## Quote from the paper

Marius Momeu, Marius Arvinte, Cory Cornelius, Koushik Sen (2026). Crustify: Repository-Level Generation of Safe Rust Bindings with LLM Agents and Coverage-Guided Testing. arXiv:2611.10394v1. https://arxiv.org/abs/2611.10394v1

**BibTeX**

```
@misc{momeu2026crustify,
  title = {{Crustify: Repository-Level Generation of Safe Rust Bindings with LLM Agents and Coverage-Guided Testing}},
  author = {Marius Momeu and Marius Arvinte and Cory Cornelius and Koushik Sen},
  year = {2026},
  eprint = {2611.10394},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.10394v1}
}
```

## Other paper commentary

- [Reflexion: Language Agents with Verbal Reinforcement Learning](https://refivu.com/en/papers/a572f458-b121-4d38-bfc9-7a77309a3aba-reflexion-language-agents-with-verbal-reinforcement-learning.md)
- [A Generalist Agent](https://refivu.com/en/papers/890475cb-e797-43df-8de3-0d0eb9ed882e-a-generalist-agent.md)
- [Toward Agentic Robotics: A Large-Scale Study of Harness Design for Robot-Use Agents](https://refivu.com/en/papers/14d1c215-9799-4493-b5f1-c86c787dcba4-toward-agentic-robotics-a-large-scale-study-of.md)
- [VeriLadder: A Multi-Agent Specification Refinement System for Tool-Verified Verilog Datasets](https://refivu.com/en/papers/f3a3ce4f-fd41-462e-8bb7-f3e5263ba827-veriladder-a-multi-agent-specification-refinement-system-for.md)
- [AutoPILOT: A Visual Agent Harness for Real-Time Gameplay](https://refivu.com/en/papers/610b7c6f-2c3b-4308-bb50-b139b48fceb7-autopilot-a-visual-agent-harness-for-real-time.md)
- [LesionAgent: Training-Free Agentic Tumor Tracking and Response Assessment in Longitudinal CT](https://refivu.com/en/papers/aad1fd25-45a9-4d0f-bd6b-86f8fce045bc-lesionagent-training-free-agentic-tumor-tracking-and.md)
- [Do large language models rank moral values? A transitivity test of moral competence](https://refivu.com/en/papers/29f72fc7-e4da-43f9-b5ac-537ccc003552-do-large-language-models-rank-moral-values-a.md)
- [ScreenProof: Where Is This Screen Wrong? Finding UI Faults Without a Reference](https://refivu.com/en/papers/0dcfcae6-d73a-4ed2-99f5-ffc88e29026b-screenproof-where-is-this-screen-wrong-finding-ui.md)
- [BO-Arena: An Evolving Benchmark for High-Dimensional Bayesian Optimisation](https://refivu.com/en/papers/ac0c04c7-34c7-4776-83da-4025fd35cf2c-bo-arena-an-evolving-benchmark-for-high-dimensional.md)
- [All Quiet on the Neural Front: Language Models Do Not Report Injected Pain States](https://refivu.com/en/papers/04b68ddc-9ed8-4013-9cd0-43c33d59ed84-all-quiet-on-the-neural-front-language-models.md)
- [Dataset Collections: Challenges of Large-Scale Data Aggregation in 3D Medical Image Datasets](https://refivu.com/en/papers/5550635b-b1eb-4824-80c9-e57e8f847e54-dataset-collections-challenges-of-large-scale-data.md)
- [Probably Safe Sim-to-Real Transfer with Lifetime Validity](https://refivu.com/en/papers/0b314123-d9b1-4121-af5f-f57316cf9b93-probably-safe-sim-to-real-transfer-with-lifetime.md)

## Terms in this paper

- [AddressSanitizer](https://refivu.com/en/terms/7770c196364d-addresssanitizer)
- [Wavefront](https://refivu.com/en/terms/0711b922b4b9-wavefront)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/0a929076-bec9-4572-90ad-a8b133795903-crustify-repository-level-generation-of-safe-rust-bindings)
