### Install Charon with Nix Source: https://github.com/aeneasverif/charon/blob/main/docs/installation.md Installs and runs Charon via Nix. Requires Nix to be installed on the system. ```bash nix run github:AeneasVerif/charon ``` -------------------------------- ### Build Charon from source Source: https://github.com/aeneasverif/charon/blob/main/docs/installation.md Builds Charon from source. Requires rustup and no pre-existing Rust or Cargo installations through other means. The resulting binary is placed in bin/charon. ```bash git clone https://github.com/AeneasVerif/charon.git cd charon make build-charon-rust ``` -------------------------------- ### Charon JSON output structure Source: https://github.com/aeneasverif/charon/blob/main/README.md Example of the JSON output produced by Charon, showing the top-level fields for crate name, type declarations, function declarations, global declarations, trait declarations, and trait implementations. ```json { "crate_name": "...", "type_decls": [ ... ], "fun_decls": [ ... ], "global_decls": [ ... ], "trait_decls": [ ... ], "trait_impls": [ ... ], } ``` -------------------------------- ### Recursive trait limitation example Source: https://github.com/aeneasverif/charon/blob/main/docs/transformations.md Illustrates a limitation of --remove-associated-types: recursive traits cannot always be transformed. The example shows how a new associated type FooTy_BarTy is added to supply the argument to Bar, because a type parameter cannot be added. ```rust trait Bar { type BarTy; } trait Foo { type FooTy: Foo + Bar; } // becomes: trait Bar {} trait Foo { type FooTy: Foo + Bar; // We need to supply an argument to `Bar` but we can't add a type parameter, so we add a // new associated type. type FooTy_BarTy; } ``` -------------------------------- ### Get Charon version with charon version Source: https://github.com/aeneasverif/charon/blob/main/docs/versioning.md Run `charon version` to display the current release version and the git commit hash of the source code used to build the binary. The output shows the semver version (e.g., 0.1.259) followed by the commit hash in parentheses. ```sh charon version # 0.1.259 (e4c4510d817097accb931fad0c14f357ce94066d) ``` -------------------------------- ### --remove-associated-types transformation example Source: https://github.com/aeneasverif/charon/blob/main/docs/transformations.md Shows the transformation applied by --remove-associated-types: the associated type Item is turned into a type parameter on the trait, and the function use_iterator gains an extra type parameter Clause0Item to supply the trait's argument. ```rust trait Iterator { type Item; fn next(&mut self) -> Option; } fn use_iterator(it: I) { ... } ``` ```rust trait Iterator { fn next(&mut self) -> Option; } fn use_iterator, Clause0Item>(it: I) { ... } ``` -------------------------------- ### Prepare kernel build directory and enable Rust Source: https://github.com/aeneasverif/charon/blob/main/docs/rust_for_linux.md Prepares a build directory and enables Rust support in the kernel configuration. It runs rustavailable, defconfig, enables the RUST config option, and runs olddefconfig. ```console mkdir -p ../linux-build export KERNEL_BUILD_DIR="$(realpath ../linux-build)" make LLVM=1 O="$KERNEL_BUILD_DIR" RUSTC="$CHARON_RUSTC" rustavailable make LLVM=1 O="$KERNEL_BUILD_DIR" RUSTC="$CHARON_RUSTC" defconfig scripts/config --file "$KERNEL_BUILD_DIR/.config" -e RUST make LLVM=1 O="$KERNEL_BUILD_DIR" RUSTC="$CHARON_RUSTC" olddefconfig ``` -------------------------------- ### Build kernel crate with Charon wrapper Source: https://github.com/aeneasverif/charon/blob/main/docs/rust_for_linux.md Builds the kernel crate using the wrapper script. The resulting llbc file is written to $CHARON_OUTPUT. ```console touch rust/kernel/lib.rs make LLVM=1 \ O="$KERNEL_BUILD_DIR" \ RUSTC="$KERNEL_BUILD_DIR/charon-rustc" \ rust/kernel.o ``` -------------------------------- ### Make wrapper executable Source: https://github.com/aeneasverif/charon/blob/main/docs/rust_for_linux.md Makes the wrapper script executable. ```console chmod +x "$KERNEL_BUILD_DIR/charon-rustc" ``` -------------------------------- ### Create Charon rustc wrapper script Source: https://github.com/aeneasverif/charon/blob/main/docs/rust_for_linux.md A wrapper script that acts like rustc but invokes Charon when compiling the kernel crate. It checks for the --crate-name kernel argument and then runs Charon with the specified options, otherwise it delegates to the original rustc. ```sh #!/usr/bin/env sh set -eu is_kernel=false previous= for argument in "$@"; do if [ "$previous" = "--crate-name" ] && [ "$argument" = "kernel" ]; then is_kernel=true break fi previous=$argument done if [ "$is_kernel" = true ]; then export CHARON_EMIT_ARTIFACTS=1 exec "$CHARON_BIN" rustc \ $CHARON_OPTIONS \ --sysroot default \ --dest-file "$CHARON_OUTPUT" \ -- "$@" else exec "$CHARON_RUSTC" "$@" fi ``` -------------------------------- ### Set Charon output and options Source: https://github.com/aeneasverif/charon/blob/main/docs/rust_for_linux.md Sets environment variables for the Charon output file and options. The options include --extract-opaque-bodies and --monomorphize. ```console export CHARON_OUTPUT="$PWD/kernel.llbc" export CHARON_OPTIONS="--extract-opaque-bodies --monomorphize" ``` -------------------------------- ### Locate Charon and its toolchain Source: https://github.com/aeneasverif/charon/blob/main/docs/rust_for_linux.md Finds the Charon binary and the rustc from its toolchain, storing them in environment variables for later use. ```console export CHARON_BIN="$(command -v charon)" export CHARON_RUSTC="$(charon toolchain-path)/bin/rustc" ``` -------------------------------- ### Inspect list_head layout with pretty-print Source: https://github.com/aeneasverif/charon/blob/main/docs/rust_for_linux.md Inspects the layout of Linux's C struct list_head by pretty-printing the llbc file and searching for its full name. ```console charon pretty-print --include-layouts "$CHARON_OUTPUT" > kernel.pretty rg -A24 \ 'Full name: bindings::bindings_raw::list_head$' \ kernel.pretty ``` -------------------------------- ### Charon thread-spawn violation error output Source: https://github.com/aeneasverif/charon/blob/main/charon/examples/detect_threads/output.txt Shows the error message and source location when Charon detects a thread spawn inside a request handler. The output includes the file path, line and column, and the offending code snippet. It ends with the total count of violations found. ```plaintext error: spawning threads from a request handler is forbidden --> examples/detect_threads/input.rs:4:27 | 4 | let _background_job = std::thread::spawn(move || { | ___________________________^ 5 | | consume(input); 6 | | }); | |______^ `input::handle_request` spawns a thread here Found 1 thread-spawn violation(s). ```