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
Figure 1. Overview of Crustify. The assistant scopes the campaign with the user; a planner decomposes the target into modules and schedules their items into waves; binders and reviewers process each wave’s batches concurrently; all agents follow a common specification and use the same tools. Block diagram: the user exchanges goals and a brief with the assistant, which reads the CRUSTIFY.md settings and drives a planning stage (planner, module decomposition, scheduling) and per-module binding and review waves of concurrent binder and reviewer batches over the C repository, producing a safe Rust crate. All agents follow a shared specification and use ffibox, Wavefront, ffimap and the test tools.
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>
Table 1. ffibox primitives and the standard-library counterparts whose interfaces they mirror.
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
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.
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
Table 3. ffibox use in the bindings, over the C functions and structs of the wrapped library’s -sys crate.
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
Table 4. Line coverage per test suite at the campaign tips, over the bindings’ non-test sources (Rust) and over libgit2 and libavutil, the only ffmpeg library wrapped so far (C). All is the union of all suites.
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
Table 5. Review reports per category. UB (safe) and UB (unsafe) are reachable from the safe API and from the unsafe API within its contract.
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
Appendix
Table 6. Examples of C types and functions and the bindings that Crustify emits for them.
Figure 2. Cost of each wrapping (filled) and review (hollow) batch of both campaigns against what it wrapped or reviewed: the in-scope fields of its types, with batches of types without in-scope fields, i.e., opaque types and enums, in a separate column, and the C lines of code of its symbols. Both campaigns are pooled; color and marker give the model. The foundational batches, which bind allocation and string routines as lifecycle policies rather than API types and symbols, and one libgit2 review batch, the only one that ran on Opus 4.8, are omitted. All axes are logarithmic. Two log-log scatter plots of batch cost in dollars, for type batches against their in-scope fields and for symbol batches against their C lines of code. Type-batch cost barely grows with the number of fields, whereas symbol-batch cost grows with the C code wrapped; GPT-6.1 Sol batches lie lowest.
Figure 3. Cost of each wrapping (filled) and review (hollow) batch in campaign order: the x-axis runs over the campaign’s waves, labeled where each sub-campaign starts, the foundational void and string first, and where the per-module waves that followed them start ( modules ); the batches of one wave, which ran concurrently, are spread around its position, and those of its review wave half a wave later. Color and marker give the model; the libgit2 review batch that ran on Opus 4.8 is omitted. The y-axes are logarithmic. Four scatter plots of batch cost in dollars on a logarithmic axis against campaign order, for libgit2 and ffmpeg type and symbol batches, with ticks where each sub-campaign starts. Cost follows the sub-campaign and model rather than the position in the campaign.
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
Appendix
Table 7. Counts of ffimap and other scanners on our libgit2 bindings and git2-rs. scan denotes cargo-scan, and scan -e cargo-scan with macro expansion.
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
Appendix
Table 8. Wall time of each scanner, cold and warm, and peak memory of its warm runs.
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
Appendix
Table 9. Dependency graphs of both campaigns’ closures in implementation (impl) and public-signature (sig) mode.
We present Encapsulated Substitution and Agentic Refinement on a Live Scaffold for Safe C-to-Rust Translation, a two-phase pipeline for translating real-world C projects to safe Rust. Existing approaches either produce unsafe output without memory-safety guarantees or translate functions in isolation, failing to detect cross-unit type mismatches or handle unsafe constructs requiring whole-program reasoning. Furthermore, function-level LLM pipelines require coordinated caller updates when type signatures change, while project-scale systems often fail to produce compilable output under real-world dependency complexity. Encrust addresses these limitations by decoupling boundary adaptation from function logic via an Application Binary Interface (ABI)-preserving wrapper pattern and validating each intermediate state against the integrated codebase. Phase 1 (Encapsulated Substitution) translates each function using an ABI-preserving wrapper that splits it into two components: a caller-transparent shim retaining the original raw-pointer signature, and a safe inner function targeted by the LLM with a clean, scope-limited prompt. This enables independent per-function type changes with automatic rollback on failure, without coordinated caller updates. A deterministic, type-directed wrapper elimination pass then removes wrappers after successful translation. Phase 2 (Agentic Refinement) resolves unsafe constructs beyond per-function scope, including static mut globals, skipped wrapper pairs, and failed translations, using an LLM agent operating on the whole codebase under a baseline-aware verification gate. We evaluate Encrust on 7 GNU Coreutils programs and 8 libraries from the Laertes benchmark, showing substantial unsafe-construct reduction across all 15 programs while maintaining full test-vector correctness.
Hohyun Sim, Hyeonjoong Cho, Ali Shokri +2
Korea University, South Korea · University of Houston, United States · State University of New York Korea, South Korea +1
Translating C programs to safe Rust is challenging owing to significant differences in typing constraints, ownership, and borrowing rules. Interpreter programs are particularly important targets for such translation, as they often handle untrusted inputs and suffer from memory-related vulnerabilities. We present Reboot, a mostly-automatic technique that translates real-world interpreter programs from C to safe Rust. Using Reboot, we have translated six interpreters ranging from 6k to 23k lines of C code to safe Rust, with each translation requiring only 1 to 11 brief user interventions. All translations pass 100% of the provided test suites, and achieve 62%--92% pass rates on separately created validation tests that were never exposed to the system. A security case study on mujs shows that memory vulnerabilities such as heap buffer overflows and use-after-free present in C are eliminated in the safe Rust translation. Two ideas underpin Reboot. First, feature reduction decomposes the translation by program features, creating a sequence of milestones where each is a complete, testable program; the translation starts from the simplest version and incrementally restores features, with each milestone validated before proceeding. Second, a multi-agent architecture orchestrates inherently unreliable coding agents through automated validation and feedback, keeping long-running translation workflows on track with minimal human involvement. An ablation study confirms that feature reduction improves translation correctness compared to using multi-agent translation alone, with 6%--20% improvements in pass rates on validation test suites.
Bo Wang, Brandon Paulsen, Joey Dodds +3
National University of Singapore, Singapore · Amazon, USA
Translating C code into safe, idiomatic Rust is a longstanding software-engineering goal because it can eliminate entire classes of memory-safety vulnerabilities while preserving the functional behavior of legacy systems. Large language models (LLMs) have shown promise for this task but typically underperform when applied off-the-shelf, since general-purpose pretraining rarely emphasizes idiomatic Rust generation, cross-language semantic equivalence, or the ability to reason about and repair compiler/runtime feedback. In this report we describe a three-stage fine-tuning curriculum applied to Qwen3-27B that is designed to progressively specialize the model for the C-to-Rust (C2Rust) translation task: (1) continued pretraining on Rust-centric corpora to strengthen the model's prior over idiomatic Rust syntax and standard-library usage; (2) supervised fine-tuning (SFT) on the microsoft/Verus_Training_Data dataset to instill debugging and self-repair behavior over Rust code; and (3) task-specific SFT on paired C/Rust solutions derived from LeetCode problems to teach direct semantic translation. We evaluate the resulting model using the agentic, static-analysis-guided verification framework of SACTOR, which performs structure-aware, two-phase (unidiomatic to idiomatic) translation with foreign-function-interface (FFI)-based end-to-end (E2E) testing. We report success rate, idiomaticity (Clippy lint counts, unsafe-code fraction), and failure-mode analyses, and compare our fine-tuned model against baseline Qwen3-27B and other LLMs evaluated under the same framework.