#c2rust #refactoring #rust

nightly app crusts-sg

CRustS - From Unsafe C to safer Rust

2 releases

0.0.3 May 19, 2023
0.0.2 May 19, 2023

#311 in Programming languages

25 downloads per month

Apache-2.0

22KB
289 lines

CRustS - Transpiling Unsafe C code to Safer Rust

Michael Ling, Yijun Yu, Haitao Wu, Yuan Wang, James R. Cordy, Ahmed E. Hassan
Trustworthiness Software Engineering & Open Source Lab
Huawei Technologies, Inc.

As a safer alternative to C, Rust is a language for programming system software with a type-safe compiler to check its memory and concurrency safety. To facilitate a smooth transition from C to Rust in an existing project, and lay a solid foundation for an initial Rust re-implementation of existing functionalities in C, it would be helpful to have a source-to-source transpiler that can transform programs from C to Rust using program transformation technologies. However, existing C-to-Rust transformation tool sets have the drawback that they largely preserve the unsafe semantics of C, while rewriting them in Rust syntax. As such, the safety of the resulting Rust programs still depends primarily on the programmers, rather than on the Rust compiler. To gain more safety guarantees, in this demo, we present CRustS a systematic source-to-source transformation approach to increase the ratio of the code passing the safety checks of Rust compiler by relaxing the semantics-preserving constraints of the transformation. Our method uses 220 TXL source-to-source transformation rules, of which 198 are strictly semantics-preserving and 22 are semantics approximating, thus reducing the scope of unsafe expressions and exposing more opportunities for safe refactoring. Our method has been evaluated on both open-source and commercial projects. Our solution demonstrates significantly higher safe code ratios after the transformations, with function-level safe code ratios comparable to the average level of idiomatic Rust projects.

Compared to the Laertes[OOPSLA’21], with respect to their own benchmarks, the safe ratio obtained by CRustS is much higher.

Installation

# install c2rust
if [ $(uname -s) == "Darwin" ]; then
   git clone https://github.com/immunant/c2rust 
   cd c2rust
   scripts/provision_mac.sh
   cargo build --release
   cp target/release/c2rust $HOME/.cargo/bin
   cp target/release/c2rust-transpile $HOME/.cargo/bin
   cp target/release/c2rust-analyze $HOME/.cargo/bin
   cp target/release/c2rust-instrument $HOME/.cargo/bin
   cd -
   brew install bear
   export PATH=$HOME/.local/bin:$HOME/.cargo/bin:$PATH

   cargo install crusts

elif [ $(uname -s) == "Linux" ]; then
   apt install llvm cmake clang libclang-dev bear -y
   LLVM_LIB_DIR=/usr/lib/llvm-14/lib/ cargo install c2rust
   curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
   rustup override set nightly-2021-11-22-x86_64-unknown-linux-gnu
   rustup component add rustfmt --toolchain nightly-2021-11-22-x86_64-unknown-linux-gnu
   export PATH=$HOME/.local/bin:$HOME/.cargo/bin:$PATH

   cargo install crusts

else

   docker pull yijun/crusts

fi

Usage:

Run crusts in the folder where there is a Makefile, using

crusts [-v | -c2rust]

or

docker run -v $(pwd):/mnt -t yijun/crusts [-v | -c2rust]

As a result, Rust code will be generated from the C code:

src/*.rs -- contains transpiled and refactored Rust code from the C code;
Cargo.toml build.rs lib.rs -- contains the `cargo build` configurations;

Options

  • -v -- version information

  • -c2rust -- only run c2rust

  • -h -- show help

  • -txl -- implement customized txl rule

References

Update

  • comparing to Laertes
  • integrating with docker for Windows users
  • adding a switch -c2rust to turn off refactoring
  • adding a switch -v to show versioning
  • adding a switch -h to show help
  • adding a switch -txl to implement customized txl rule
  • bugfix: deref pointers, see test_unsafe
  • bugfix: printf patterns, see test_stdio

Dependencies

~13–29MB
~462K SLoC