6.4.3. Source Code Analysis for Rust

6.4.3.1. Introduction

The information in this chapter is only relevant to the Rust variant of the Axivion Suite.

AxivionRustFrontend can be used to generate an RFG from Rust source code. This enables Architecture Verification, Dead Code Analysis, Cycle Detection, and other RFG-based analyses.

In addition, the Rust RFG can be merged with a C/C++ RFG to enable cross-language analysis as described in Cross-Language Analysis Setup Guide.

For more details on supported features and Rust analysis capabilities, see Features.

6.4.3.2. Requirements

Analysis of Rust source code requires cargo (Rust’s package manager) to be installed.

The project under analysis must be a cargo project (i.e. have a cargo manifest file Cargo.toml in its root directory).

Note

Cargo requires network access to download project dependencies. If cargo cannot download the dependencies of the project being analyzed, the analysis may be incomplete.

6.4.3.3. Usage with axivion_ci

AxivionRustFrontend can be invoked as part of axivion_ci by enabling it under BuildSystemIntegration in the project configuration. The following sections detail this configuration using axivion_config.

Prerequisites

Before configuring AxivionRustFrontend for the first time, ensure the following global configuration properties are set:

  1. Set /Project/directory to the repository root.

  2. Set /Project/name to the name of the project.

  3. Set /Dashboard/dashboard_url to the URL where the Axivion Dashboard is hosted.

  4. Set the environment variable AXIVION_DATABASES_DIR to the correct location.

Setup of AxivionRustFrontend

  1. Create a new copy of the AxivionRustFrontend action.

  2. Set manifest_path to the path of the main Cargo.toml file for your Rust workspace or project.

  3. Configure the Rust toolchain settings:

  1. Set sysroot_path to the Rust toolchain directory:

  • Use discover (default) to automatically detect the sysroot using rustc --print sysroot

  • Use none to rely on environment variables (RUSTC or PATH)

  • Provide a specific path (e.g., $HOME/.rustup/toolchains/stable-x86_64-unknown-linux-gnu)

  1. Optionally set sysroot_src_path to the location of Rust standard library source code. If not specified, the tool will attempt to automatically locate or install the sources using rustup component add rust-src.

    Note

    If the project uses crates from the Rust standard library (e.g. std, core, alloc), source code for these libraries should be available, otherwise the analysis will be incomplete.

  1. Configure proc-macro settings:

  1. Set enable_proc_macros to true (default) to enable expansion of proc macros.

  2. Optionally set proc_macro_server_path to specify a custom rust-analyzer compatible proc-macro server. If not set, the tool will use the proc-macro server from the configured sysroot.

  1. Configure build settings:

  1. Set run_build_scripts to true (default) to execute build.rs scripts in the workspace.

  2. Set build_path to specify the build output directory (default: target).

  3. Use cargo_args to pass additional arguments to cargo commands.

  4. Use cargo_env to set additional environment variables for cargo execution.

  1. Configure feature settings:

  1. Set enable_all_features to true to enable all available features (overrides the features list).

  2. Use the features list to specify which cargo features to enable.

  1. Configure analyses and additional build steps, as necessary. Note that not every rule is applicable to Rust. See Features for more information.

6.4.3.4. Features

AxivionRustFrontend produces an RFG that the Axivion Suite uses to provide the following additional features:

  • Architecture Analysis

  • Cycle Detection (RFG-based)

  • Dead Code Detection (RFG-based)

  • Metrics (see Metrics)

  • Some MISRA rules for Rust

  • FFI Signature Checking

Entry Point Configuration for Dead Code Analysis

The following additional entry point configurations are supported. Entry point configuration allows addition of extra routines to the RFG Entries view. These entry points are used during Dead Code Analysis.

  • EntryPoints-EntriesByAttribute: Note: the entire attribute text is matched, including parameters. For example, to match all task` attributes, where the actual text may be #[task(priority = 1)], use a trailing wildcard: task*.

  • EntryPoints-EntriesByLinkname

  • EntryPoints-EntriesByName

  • EntryPoints-EntriesByPath

  • EntryPoints-EntriesByMacro

MISRA Rules for Rust

The Rust analysis supports the following MISRA-C 2012 directives adapted for Rust:

  • MisraC2012Directive-4.2: All usage of assembly language should be documented (detects asm!, global_asm!, etc.)

  • MisraC2012Directive-4.3: Assembly language shall be encapsulated and isolated

FFI Signature Checking

The Rust-CheckExternSignatures rule can be used with RFGs that contain merged C/C++ and Rust nodes to detect mismatches in Foreign Function Interface (FFI) signatures declared in Rust. This analysis can identify differences in number of parameters, type of parameters and return type between Rust FFI declarations and their C/C++ implementations.