We believe that software should be both fast and safe. Languages like C and C++ deliver blazingly fast performance but are pretty much impossible to use safely. Software mitigations built into the operating system make exploitation of buggy code much harder but attackers are amazing at finding ways to bypass new defenses. What we really need is a way to eliminate the source of memory corruption bugs. Rust delivers on speed and interoperability while making memory safety the default. We see it as a practical fit for incrementally moving systems software in a safer direction; and we’re not the only ones.

To make that safer world a reality, we need a way to migrate existing software into more developer-friendly and secure languages. While many great projects such as Servo, Redox, and ripgrep are implementing new software in Rust, manual re-implementation efforts are costly and don’t scale well. This is where C2Rust enters the picture. Our goal is to build useful tools that make it easier to take existing C code and get up and running with Rust. We aim to automate much of the translation and rewriting process so that migrating legacy systems is practical and scalable with minimal manual effort.

We’re excited to announce that a milestone long in the making is finally here! You can now install C2Rust from crates.io with a simple cargo install on Linux and OS X. We’ve been hard at work improving C2Rust, so go install and give it a spin! You can find the necessary prerequisites in the C2Rust README.

cargo +nightly-2019-06-22 install c2rust

Important! C2Rust depends on specific Rust nightly Rust version, so you’ll need to specify the correct version when installing. Check https://crates.io/crates/c2rust for the latest required Rust nightly version.

Trying out C2Rust

Here’s a quick example of how to use the C2Rust translator on a simple buffer library. C2Rust reliably translates far larger projects but that’s a topic for a another blog post.

If you haven’t already, install C2Rust:

cargo +nightly-2019-06-22 install c2rust

Then check out the clibs buffer library:

git clone https://github.com/clibs/buffer.git
cd buffer

Next, we need to generate a compile_commands.json file describing the build. We recommend intercept-build for simplicity but many other tools do the same thing.

pip install scan-build
intercept-build make

Now we’re ready to translate the code!

c2rust transpile --emit-build-files --binary test compile_commands.json

Here’s what the flags do:

  • --binary test tells the transpiler that we want to build a binary with the main function from the test module (test.c)
  • --emit-build-files will produce cargo build files for the project. Now we can build and run the Rust project with:
cargo +nightly build
cargo +nightly run

Translated programs require nightly Rust for several reasons, including support for defining C variadic functions in Rust. This is gated by the c_variadic feature. When C-compatible variadic functions and a few other features are stabilized, translated projects won’t require nightly Rust. If your translated code doesn’t use any of the unstable features, you won’t need nightly after translation.

Getting in touch

Hit a roadblock? Is it one of our known limitations? If not, jump onto our discord channel to tell us about it or send a mail to [email protected]. We’d love to hear from you.