https://github.com/immunant/c2rust Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code + Explore + All features + Documentation + GitHub Skills + Blog * Solutions + By Plan + Enterprise + Teams + Compare all + By Solution + CI/CD & Automation + DevOps + DevSecOps + Case Studies + Customer Stories + Resources * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles + Repositories + Topics + Trending + Collections * Pricing [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this organization All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} immunant / c2rust Public * Notifications * Fork 159 * Star 2.8k Migrate C code to Rust c2rust.com/ License View license 2.8k stars 159 forks Star Notifications * Code * Issues 133 * Pull requests 24 * Discussions * Actions * Projects 2 * Wiki * Security * Insights More * Code * Issues * Pull requests * Discussions * Actions * Projects * Wiki * Security * Insights immunant/c2rust This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. master Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 82 branches 28 tags Code * Clone HTTPS GitHub CLI [https://github.com/i] Use Git or checkout with SVN using the web URL. [gh repo clone immuna] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @aneksteind aneksteind Merge pull request #654 from immunant/feat.pdg.addroflocal ... 26f3208 Oct 21, 2022 Merge pull request #654 from immunant/feat.pdg.addroflocal Add address-taken local support in dynamic analysis 26f3208 Git stats * 6,618 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows Remove the immunant/c2rust-testsuite checkout token in GitHub Actio... Jul 9, 2022 analysis add address-of-local support for dynamic analysis Oct 21, 2022 c2rust-analyze cargo clippy --fix on c2rust-analyze after #655. Oct 18, 2022 c2rust-asm-casts Added comment explaining why #[allow(clippy::zero_ptr)] was used fo... Jun 22, 2022 c2rust-ast-builder Fixed all clippy warnings (most were from clippy being updated, t... Aug 9, 2022 c2rust-ast-exporter Fixed a clippy::needless_bool from 12ff6ce. Oct 9, 2022 c2rust-ast-printer Revert "update dependency specifications" Aug 2, 2022 c2rust-bitfields-derive fix clippy warnings: len_zero Jun 21, 2022 c2rust-bitfields Removed some extern crates I missed. Jun 13, 2022 c2rust-build-paths upgrade print_bytes crate version Oct 17, 2022 c2rust-macros fix clippy warnings: single_match Jun 21, 2022 c2rust-refactor Removed inferred, previously turbofished generic types for None and... Jul 19, 2022 c2rust-transpile Print module names (Strings) using Display instead of Debug. Aug 26, 2022 c2rust Removed all camino dependencies as now it's unused. Aug 25, 2022 docker Merge branch 'master' into kkysen/rust-toolchain-toml Aug 9, 2022 docs Converted all rust-toolchain strings to rust-toolchain.toml. Aug 9, 2022 dynamic_instrumentation add address-of-local support for dynamic analysis Oct 21, 2022 examples Updated the rust-toolchain.toml in an example to match the root `ru... Aug 9, 2022 manual Converted all rust-toolchain files to the more expressive `rust-too... Aug 9, 2022 pdg add address-of-local support for dynamic analysis Oct 21, 2022 scripts Forgot to set INSTRUMENT_RUNTIME=bg in pdg.sh. Oct 20, 2022 tests Also check va_copy works with a member of a struct pointer. Aug 17, 2022 .gitignore Now CARGO_TARGET_DIR=instrument.target is set so that normal `cargo... Jul 28, 2022 .gitmodules updatexzoom dependency Feb 19, 2022 Cargo.lock add address-of-local support for dynamic analysis Oct 21, 2022 Cargo.toml Renamed rustc-private-link to the more generic c2rust-build-paths... Aug 24, 2022 LICENSE remove now-unused forked/vendored prettyplease May 10, 2022 README.md add python compiledb example Oct 4, 2022 azure-pipelines.yml Removed the redundant cargo fmt --check on Darwin since it'll be th... Oct 10, 2022 book.toml Replace homebrew include_md with mdbook feature Apr 15, 2022 rust-toolchain.toml Added miri to the needed components in rust-toolchain.toml now th... Oct 10, 2022 shell.nix Fix shell.nix for unstable Feb 20, 2021 View code [ ] C2Rust Documentation Installation Prerequisites Installing from crates.io Installing from Git Translating C to Rust Generating compile_commands.json Files ... with cmake ... with intercept-build ... with bear (Linux only) ... with compiledb Contact FAQ Acknowledgements and Licensing README.md C2Rust GitHub Actions Status Azure Build Status Latest Version Rustc Version C2Rust helps you migrate C99-compliant code to Rust. The translator (or transpiler), c2rust transpile, produces unsafe Rust code that closely mirrors the input C code. The primary goal of the translator is to preserve functionality; test suites should continue to pass after translation. Generating safe and idiomatic Rust code from C ultimately requires manual effort. We are currently working on analysis to automate some of the effort required to lift unsafe Rust into safe Rust types. This work is still in the early stages; please get in touch if you're interested! We previously maintained a scriptable refactoring tool, c2rust refactor, that reduces the tedium of refactoring, but this tool is now deprecated so that we can move forward with a recent Rust toolchain. Here's the big picture: C2Rust overview To learn more, check out our RustConf'18 talk on YouTube and try the C2Rust translator online at c2rust.com. Documentation To learn more about using and developing C2Rust, check out the manual . The manual is still a work-in-progress, so if you can't find something please let us know. Installation Prerequisites C2Rust requires LLVM 7 or later with its corresponding clang compiler and libraries. Python 3.6 or later, CMake 3.4.3 or later, and openssl (1.0) are also required. These prerequisites may be installed with the following commands, depending on your platform: * Ubuntu 18.04, Debian 10, and later: apt install build-essential llvm clang libclang-dev cmake libssl-dev pkg-config python3 Depending on the LLVM distribution, the llvm-dev package may also be required. For example, the official LLVM packages from apt.llvm.org require llvm-dev to be installed. * Arch Linux: pacman -S base-devel llvm clang cmake openssl python * NixOS / nix: nix-shell * OS X: XCode command-line tools and recent LLVM (we recommend the Homebrew version) are required. xcode-select --install brew install llvm python3 cmake openssl The C2Rust transpiler now builds using a stable Rust compiler. If you are developing other features, you may need to install the correct nightly compiler version. Installing from crates.io cargo install c2rust You can also set the LLVM version explicitly if you have multiple installed, like this, for example: LLVM_CONFIG_PATH=llvm-config-14 cargo install c2rust On OS X with Homebrew LLVM, you need to point the build system at the LLVM installation as follows: LLVM_CONFIG_PATH=/usr/local/opt/llvm/bin/llvm-config cargo install c2rust On Linux with Linuxbrew LLVM, you need to point the build system at the LLVM installation as follows: LLVM_CONFIG_PATH=/home/linuxbrew/.linuxbrew/opt/llvm/bin/llvm-config cargo install c2rust Note: adjust LLVM_CONFIG_PATH accordingly if Linuxbrew was installed to your home directory. On Gentoo, you need to point the build system to the location of libclang.so and llvm-config as follows: LLVM_CONFIG_PATH=/path/to/llvm-config LIBCLANG_PATH=/path/to/libclang.so cargo install c2rust If you have trouble with building and installing, or want to build from the latest master, the developer docs provide more details on the build system. Installing from Git If you'd like to check our recently developed features or you urgently require a bugfixed version of c2rust, you can install it directly from Git: cargo install --git https://github.com/immunant/c2rust.git c2rust Please note that the master branch is under constant development and you may experience issues or crashes. You should also set LLVM_CONFIG_PATH accordingly if required as described above. Translating C to Rust To translate C files specified in compile_commands.json (see below), run the c2rust tool with the transpile subcommand: c2rust transpile compile_commands.json (The c2rust refactor tool was also available for refactoring Rust code, see refactoring, but is now being replaced by a more robust way to refactor.) The translator requires the exact compiler commands used to build the C code. This information is provided via a compilation database file named compile_commands.json. (Read more about compilation databases here). Many build systems can automatically generate this file; we show a few examples below. Once you have a compile_commands.json file describing the C build, translate the C code to Rust with the following command: c2rust transpile path/to/compile_commands.json To generate a Cargo.toml template for a Rust library, add the -e option: c2rust transpile --emit-build-files path/to/compile_commands.json To generate a Cargo.toml template for a Rust binary, do this: c2rust transpile --binary myprog path/to/compile_commands.json Where --binary myprog tells the transpiler to use the main function from myprog.rs as the entry point for a binary. The translated Rust files will not depend directly on each other like normal Rust modules. They will export and import functions through the C API. These modules can be compiled together into a single static Rust library or binary. There are several known limitations in this translator. The translator will emit a warning and attempt to skip function definitions that cannot be translated. Generating compile_commands.json Files The compile_commands.json file can be automatically created using either cmake, intercept-build, or bear. It may be a good idea to remove optimizations (-OX) from the compilation database, as there are optimization builtins which we do not support translating. ... with cmake When creating the initial build directory with cmake, specify -DCMAKE_EXPORT_COMPILE_COMMANDS=1. This only works on projects configured to be built by cmake. This works on Linux and MacOS. cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ... ... with intercept-build intercept-build (part of the scan-build tool) is recommended for non-cmake projects. intercept-build is bundled with clang under tools /scan-build-py, but a standalone version can be easily installed via pip with: pip install scan-build Usage: intercept-build You can also use intercept-build to generate a compilation database for compiling a single C file. For example: intercept-build sh -c "cc program.c" ... with bear (Linux only) If you have bear installed, it can be used similarly to intercept-build: bear ... with compiledb The compiledb package can also be used for makefile projects if the other tools don't work. Install via pip with: pip install compiledb Usage: # After running ./autogen.sh && ./configure # etc. # Run compiledb make Contact To report issues with translation or refactoring, please use our Issue Tracker. To reach the development team, join our discord channel or email us at c2rust@immunant.com. FAQ I translated code on platform X, but it didn't work correctly on platform Y. We run the C preprocessor before translation to Rust. This specializes the code to the host platform. For this reason, we do not support cross compiling translated code at the moment. What platforms can C2Rust be run on? The translator and refactoring tool support both macOS and Linux. Other features, such as cross checking the functionality between C and Rust code, are currently limited to Linux hosts. Acknowledgements and Licensing This material is available under the BSD-3 style license as found in the LICENSE file. The C2Rust translator is inspired by Jamey Sharp's Corrode translator. We rely on Emscripten's Relooper algorithm to translate arbitrary C control flows. This material is based upon work supported by the United States Air Force and DARPA under Contract No. FA8750-15-C-0124. Any opinions, findings and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the United States Air Force and DARPA. Distribution Statement A, "Approved for Public Release, Distribution Unlimited." About Migrate C code to Rust c2rust.com/ Topics rust translation migration transpiler memory-safety security-hardening Resources Readme License View license Stars 2.8k stars Watchers 43 watching Forks 159 forks Releases 7 v0.16.0 - C2Rust builds with stable Rust Latest May 12, 2022 + 6 releases Packages 0 No packages published Used by 131 * @orangecms * @Playfloor * @nowknowing * @wolverian * @darahaas15 * @kjx98 * @x0f5c3 * @rcoder + 123 Contributors 46 * @ahomescu * @kkysen * @thedataking * @rinon * @TheDan64 * @glguy * @Saldivarcher * @harpocrates * @fw-immunant * @aneksteind * @spernsteiner + 35 contributors Languages * Rust 74.0% * HTML 11.0% * Python 7.2% * C++ 2.7% * C 2.1% * Lua 1.9% * Other 1.1% Footer (c) 2022 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.