Skip to content
Zurück zum BlogFrom C to Rust: When Translation Is Proven
rust c research translation ffi

From C to Rust: When Translation Is Proven

17. Oktober 2026~4 min read· Tobias Weiss

Millions of lines of C code run in production systems — operating systems, drivers, crypto libraries. C is powerful but not memory-safe: use-after-free, buffer overflows, and double-frees are the most common security vulnerabilities of the past decades. Rust promises relief — but how do you translate millions of lines of legacy code without breaking it?

The dilemma of automated translation

Naive translation from C to Rust fails at a fundamental problem: C and Rust have different memory models. C allows arbitrary pointer arithmetic, manual memory management, and implicit conversions. Rust forbids all of this at compile time. A direct 1:1 translation produces either code that won't compile or code that uses unsafe so often that the security gain is zero.

// C: works, but unsafe
char* greet(const char* name) {
    char* buf = malloc(100);
    snprintf(buf, 100, "Hello, %s!", name);
    return buf;  // Caller must free() — if forgotten, memory leak
}
// Rust: safe, but different semantics
fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
    // No free() needed — ownership handles it
}

The translation works, but the semantics have changed: instead of a raw buffer the caller must free, there's a String object with automatic memory management. Is this the same program? Not exactly — but it is equivalent in the sense that the observable effects are identical.

Finding a Crab in the C: Comparative symbolic execution

The study "Finding a Crab in the C" (2026) solves exactly this problem. Its tool cozy uses comparative symbolic execution to prove the semantic equivalence between C code and its Rust translation — not guess, but prove.

The idea: cozy executes both versions — the original in C and the translation in Rust — in parallel and checks for every input path whether the observable effects are identical. If cozy finds no difference, the translation is considered assured — guaranteed equivalent.

Why 'assured' is better than 'tested'

Tests check a finite set of inputs. Symbolic execution checks all input paths the program can take. A test can miss a bug; comparative symbolic execution — when it completes — rules it out. That's the difference between "probably correct" and proven equivalent.

TransAgent: LLM-assisted translation with execution alignment

While cozy relies on formal equivalence, TransAgent (2024) takes a complementary approach: LLM-assisted translation with fine-grained execution alignment. The language model generates an initial translation, then both versions are executed and the runtime effects are compared. Deviations are fed back to the model, which corrects them in the next iteration.

This is the same principle as TreeCoder: constraint satisfaction during generation, not after the fact. The "constraints" here are the observable effects of the original program.

Why C-to-Rust is safer than keeping C

The study argues that translating to Rust is not just a language switch but a security repair. C code translated to Rust loses entire bug classes:

  • Use-after-free — impossible in Rust (ownership model)
  • Double-free — impossible in Rust (move semantics)
  • Buffer overflow — caught at compile time (bounds checking)
  • Data races — impossible in Rust (borrow checker)
// The same function in Rust — the bug class no longer exists
fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}
// No malloc, no free, no buffer, no overflow.
// The compiler proves: safe.

What this means for practice

Automated C-to-Rust translation is not yet perfect. cozy works on functions, not whole systems, and the translation requires manual review of the unsafe blocks needed for FFI boundaries. But the direction is clear:

  1. Legacy code doesn't have to stay unsafe forever. Tools like cozy make it possible to port C code to Rust incrementally — with proofs, not hope.
  2. FFI is the bridge, not the end. Rust can call C libraries via extern "C". You can rewrite safely and replace the old code gradually.
  3. Semantic equivalence is verifiable. Comparative symbolic execution makes "the same program" a provable property, not a guess.

The difference between translation and transformation

A translation preserves semantics. A transformation improves them. cozy does both: it translates C to Rust and transforms unsafe patterns into safe ones. The result is not the same program in a different language — it's a better program with proven equivalence.

Further reading

These articles complement the topic:

Dive into the course

Ready to try it yourself? Our Rust course teaches FFI, ownership, and unsafe code interactively — with playground, quizzes, and spaced repetition so it sticks.

Verwandte Artikel

This site uses essential cookies for Stripe payments. No tracking cookies.