Crustify: Repository-Level Generation of Safe Rust Bindings with LLM Agents and Coverage-Guided Testing
Organizations: University of California, Berkeley, USA · Intel Corporation, USA
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.
Figures & tables
| Challenge | ffibox primitive | Standard counterpart |
|---|---|---|
| C1 | Foo (layout newtype) | — |
| C2 | CBox<Foo, P> | Box<T> |
| C2 | CArc<Foo, P> | Arc<T> |
| C2 | CVal<Foo, P> | T (by value) |
| C3 | FooRef<’a> / FooMut<’a> | &T / &mut T |
| C4 | CGuard / FooLocked<’a> | MutexGuard<T> |
| libgit2 | ffmpeg | ||||
| Crustify | git2-rs | Crustify | ffmpeg-the-third | ffmpeg-next | |
| API coverage (%) | 41.9 | 65.1 | 42.8 | 18.7 | 17.6 |
| Tests | 2,064 | 238 | 2,694 | 47 | 48 |
| Rust line coverage (%) | 98.92 | 60.59 | 99.07 | 11.34 | 18.73 |
| C line coverage (%) | 46.78 | 45.12 | 67.20 | 6.77 | 10.08 |
| Public functions | 2,663 | 2,719 | 2,637 | 2,222 | 1,955 |
| libgit2 | ffmpeg | |
| C functions wrapped / declared | 435 / 501 | 503 / 528 |
| Used C structs with a layout type | 68 / 73 | 42 / 43 |
| C fields with an accessor | 212 | 192 |
| accessors, on shared / exclusive handles | 213 / 172 | 201 / 217 |
| Public types on ffibox (%) | 66.8 | 66.2 |
| Layout types: define_ctype! / by hand | 40 / 34 | 58 / 0 |
| libgit2 | ffmpeg | |||||
|---|---|---|---|---|---|---|
| Suite | Tests | Rust (%) | C (%) | Tests | Rust (%) | C (%) |
| Safe UB | 688 | 79.28 | 28.10 | 821 | 75.04 | 53.10 |
| Unsafe UB | 100 | 12.81 | 22.12 | 182 | 28.16 | 26.64 |
| Equivalence | 489 | 75.93 | 43.47 | 723 | 80.38 | 66.67 |
| Unit | 787 | 87.34 | 32.03 | 968 | 84.45 | 41.20 |
| All | 2,064 | 98.92 | 45.95 | 2,694 | 99.07 | 67.20 |
| Category | libgit2 | ffmpeg |
|---|---|---|
| UB (safe) | 6 | 4 |
| UB (unsafe) | 2 | 9 |
| Residual unsafe or raw types | 7 | 8 |
| Safety obligations | 11 | 8 |
| Equivalence | 6 | 4 |
| Other a | 146 | 105 |
Appendix figures & tables6 assets
Supplementary material from the paper’s appendix.
Appendix
| C item | Rust binding | ffibox primitive |
| AVBPrint | BPrintBox , BPrintOnBuffer<’b> | CBox with two policies |
| git_object | ObjectArc<’r> | CArc , reference-counted by C |
| av_file_map | FileMap | CVec , length passed to its policy |
| git_checkout_options | CheckoutOptions<’o> | layout with a hand-written lifetime |
| git_commit | CommitRef<’a, ’r> | handle with two lifetimes |
| git_strarray (result) | StrArrayVal | CVal , disposed on drop |
| Metric | Tool | Crustify | git2-rs |
|---|---|---|---|
| unsafe fn | ffimap / geiger | 166 / 156 | 124 / 123 |
| unsafe impl | ffimap / geiger / scan | 324 / 266 / 266 | 8 / 8 / 8 |
| unsafe expressions | geiger | 6,448 | 3,336 |
| unsafe blocks | ffimap | 2,097 | 1,004 |
| Lines in unsafe blocks | ffimap | 2,911 | 3,497 |
| Blocks with several unsafe operations | Clippy | 0 | 400 |
| Cold / warm (s) | Peak memory (MB) | |||
| Tool | Crustify | git2-rs | Crustify | git2-rs |
| ffimap | 37.2 / 12.9 | 18.0 / 5.1 | 358 | 245 |
| cargo-geiger | 12.2 / 11.4 | 12.4 / 12.2 | 981 | 1,530 |
| cargo-scan | 38.2 / 26.8 | 69.0 / 25.2 | 1,018 | 1,181 |
| cargo-scan -e | 46.2 / 34.6 | 99.6 / 55.9 | 1,017 | 1,175 |
| Clippy | 9.8 / 2.6 | 9.5 / 0.45 | 250 | 25 |
| libgit2 impl | libgit2 sig | ffmpeg impl | ffmpeg sig | |
| Nodes | 7,545 | 7,526 | 5,777 | 5,651 |
| Edges | 49,528 | 7,436 | 21,259 | 3,400 |
| Non-trivial SCCs | 22 | 7 | 19 | 4 |
| Largest SCC (nodes) | 135 | 8 | 21 | 5 |
| Nodes in cycles | 3.35% | 0.39% | 1.47% | 0.19% |
| Cut edges | 126 | 22 | 43 | 6 |
Code Generation
Code generation studies methods for turning natural-language, visual, or formal specifications into executable programs and code edits.
See all 335 papers