Skip to content

feat(ci): Add Miri to CI workflow to verify absence of Undefined Behavior #81

Description

@kalwalt

Summary

Add Miri as a GitHub Actions CI job to detect undefined behavior (UB) in the unsafe code paths at test-time.

Motivation

PureCV strives for a zero-unsafe policy, but a few unsafe blocks currently exist for performance-critical SIMD paths:

  • src/core/arithm.rs — pointer casts in binary_op! / binary_op_scalar! macros (transmuting dst chunks)
  • src/imgproc/derivatives.rsfrom_raw_parts / from_raw_parts_mut reinterpretation for f32 SIMD paths
  • src/core/tests.rs — raw pointer dereference in data_ptr / data_ptr_mut tests
    Requirements
    During development, a plan must be created identifying which code/tests to include and exclude from Miri runs:

Code to Include (run under Miri)

  • All unit tests in src/core/tests.rs (especially data_ptr and data_ptr_mut tests)
  • Arithmetic operations using unsafe transmutes (src/core/arithm.rs)
  • Derivative SIMD fast-path (src/imgproc/derivatives.rs)
  • src/core/structural.rs split/merge operations
  • All safe code paths (sequential/non-SIMD) — should pass trivially

Code to Exclude (skip under Miri)

  • Benchmarks (benches/) — too slow under Miri interpretation
  • WASM build (crates/wasm/) — Miri does not support wasm32 targets
  • pulp-based SIMD (#[cfg(feature = "simd")]) — Miri doesn't support most SIMD intrinsics; annotate with #[cfg_attr(miri, ignore)]
  • Rayon parallel tests (#[cfg(feature = "parallel")]) — Miri's thread support is limited; run without parallel feature or selectively ignore
  • External I/O or FFI — none expected, but exclude if added

Implementation Plan

  1. Add a new job miri to .github/workflows/ci.yml

  2. Use dtolnay/rust-toolchain@nightly with components: miri

  3. Run: cargo +nightly miri test --workspace (without parallel or simd features initially)

  4. Annotate incompatible tests with #[cfg_attr(miri, ignore)]

  5. Consider a second Miri run with --features simd once pulp gains Miri compatibility

  6. Document excluded tests and rationale in the workflow file

  7. Add a Badge in the readme file with all infos.

Proposed Workflow Addition

    name: Miri UB Check
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6
    - name: Install Rust nightly + Miri
      uses: dtolnay/rust-toolchain@nightly
      with:
        components: miri
    - name: Rust Cache
      uses: Swatinem/rust-cache@v2
    - name: Run Miri
      run: cargo +nightly miri test --workspace
      env:
        MIRIFLAGS: "-Zmiri-strict-provenance"

Acceptance Criteria

  • Miri CI job passes on the dev branch
  • All existing unsafe code is verified UB-free (or documented exceptions)
  • Tests incompatible with Miri are annotated with #[cfg_attr(miri, ignore)]
  • Plan document exists identifying included/excluded code with rationale

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Fields

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions