blob: 1fd9187f0c61180ad25806a9aa6e7e8fb555ae18 [file] [view] [edit]
<!-- Copyright 2026 The Fuchsia Authors
Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
<LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
This file may not be copied, modified, or distributed except according to
those terms. -->
# `tools/anneal-cpp`: C/C++ Frontend, Differential Oracle, and Binary Tools
> [!NOTE]
> **Experimental / unbridged prototype:** The crates in `tools/anneal-cpp/` are
> excluded host tools outside Anneal's Rust verifier TCB; standalone C/C++ claim
> admission and correspondence bridges do not yet ship in `cargo-anneal`.
`tools/anneal-cpp/` is a standalone Cargo workspace ([`Cargo.toml`](Cargo.toml))
containing untrusted Rust tools for C11 and `-fno-exceptions` C++17/C++20
lowering, semantics and ISA differential testing, C/C++-to-Rust FFI parity
checking, and post-link ELF64 auditing. It pairs with the companion C++20 Clang
LibTooling extractor [`anneal-clang-dump`](../anneal-clang-dump/README.md),
which emits the `RawCppBundle` (schema v1) JSON consumed by `anneal-lower-cpp`,
`anneal-cpp-diff`, and `anneal-ffi-check`.
## Role and Trust Boundary
The root workspace ([`Cargo.toml`](../../Cargo.toml)) explicitly excludes
`tools/anneal-cpp` so `anneal-audit`'s Cargo boundary (`check_cargo_boundary`)
keeps every crate here in zone 2 (`tools/`), strictly outside `trusted/` and
`tests/` (see [`docs/trust-model.md`](../../docs/trust-model.md)). Every member
is `publish = false` and `build.rs`-free, and neither `trusted/` nor `tests/`
may depend on crates under `tools/`.
Tools in this workspace target companion Lean modules—including
[`Trusted.CppAst`](../../trusted/lean/Trusted/CppAst.lean),
[`Trusted.CppSemantics`](../../trusted/lean/Trusted/CppSemantics.lean),
[`Anneal.ElfX86`](../../theory/Anneal/ElfX86.lean), and
[`Anneal.ElfAArch64`](../../theory/Anneal/ElfAArch64.lean)—for lowering,
layout-parity export, and differential testing. Because `anneal-clang-dump` and
`anneal-lower-cpp` perform semantic gating and normalization outside `trusted/`,
and `TrustedBoundary` does not yet define an admission rule for `CapturedCpp`
modules on `main`, admitting standalone C/C++ verification claims will require
either TCB promotion or translation validation rather than trusting
excluded-tool output directly.
## Workspace Crates
`tools/anneal-cpp/Cargo.toml` defines six workspace members:
| Member | Package | Targets | Summary | Details |
| :--- | :--- | :--- | :--- | :--- |
| `lower-common` | `anneal-lower-common` | lib | Dependency-free Tarjan callee-first SCC ordering, 16-element balanced-tree Lean list chunking, and SHA-256 helpers shared by `anneal-lower-cpp`, `anneal-cpp-diff`, and `anneal-postlink`. | [`lower-common/README.md`](lower-common/README.md) |
| `lower-cpp` | `anneal-lower-cpp` | lib, bin | Pure-Rust, fail-closed lowerer from `anneal-clang-dump` `RawCppBundle` JSON to `CapturedCpp.lean` (`Trusted.CppAst.CppProgram`), enforcing language policy, ABI cross-checks, SSENF, scope lifetimes, and return-slot normalization. | [`lower-cpp/README.md`](lower-cpp/README.md) |
| `cpp-diff` | `anneal-cpp-diff` | lib, bin | `#![forbid(unsafe_code)]` C11/C++20 operational-semantics differential oracle comparing `Trusted.CppSemantics` against `clang`/`clang++` (UBSan+ASan) and optional `gcc`/`g++` (UBSan) over a built-in inline corpus. | [`cpp-diff/src/main.rs`](cpp-diff/src/main.rs) |
| `ffi-check` | `anneal-ffi-check` | lib, bin | Fail-closed C/C++ to Rust FFI layout, calling-convention, ABI-passing, `const`-consistency, and unwind-safety checker consuming `RawCppBundle` JSON with a Rust FFI descriptor and/or `rustc -Z print-type-sizes`; optionally emits C `_Static_assert` headers and Lean layout-parity modules. | [`ffi-check/README.md`](ffi-check/README.md) |
| `isa-oracle` | `anneal-isa-oracle` | lib, bin | Dual-ISA (`x86-64` and `AArch64`) differential decoder oracle comparing Anneal's Lean decoders (`Anneal.ElfX86` and `Anneal.ElfAArch64`) against `llvm-objdump` and GNU `objdump`, plus an `exec` subcommand replaying JSONL x86-64 execution traces against `Anneal.ElfX86.ScalarEval` across three coverage bands (`scalar`, `rep`, `sandbox`). | [`isa-oracle/src/main.rs`](isa-oracle/src/main.rs) |
| `postlink` | `anneal-postlink` | lib, bin | `#![forbid(unsafe_code)]` post-link ELF64 (`ET_EXEC` / `ET_DYN`) auditor for `x86-64` and `AArch64` checking `.text` byte fidelity outside allowlisted relocations, `.rodata` immutability, GOT/PLT binding, and binary hardening (`W^X`, `PT_GNU_STACK`, `PT_GNU_RELRO`, CET/BTI/PAC, `.stack_sizes`, syscall absence). | [`postlink/README.md`](postlink/README.md) |
### Differential Oracles Without Dedicated READMEs
- **`anneal-cpp-diff`** ([`cpp-diff/src/lib.rs`](cpp-diff/src/lib.rs),
[`cpp-diff/src/main.rs`](cpp-diff/src/main.rs)): runs the inline C11/C++20
corpus (`--list`, `--filter SUBSTR`, `--json`, `--json-out PATH`) through both
the Lean `Trusted.CppSemantics` leg (using `anneal-clang-dump` and `lean`,
overridable via `--clang-dump` / `$ANNEAL_CLANG_DUMP` and `--lean`) and native
compiler legs (`--cc`, `--cxx`, and optional `--gcc`/`--gxx`). Exits `0` when
all cases pass, `1` on differential failures, and `2` on CLI, environment, or
tool errors.
- **`anneal-isa-oracle`** ([`isa-oracle/src/lib.rs`](isa-oracle/src/lib.rs),
[`isa-oracle/src/exec.rs`](isa-oracle/src/exec.rs),
[`isa-oracle/src/main.rs`](isa-oracle/src/main.rs)): checks the built-in
dual-ISA adversarial corpus (`--adversarial`), raw hex bytes
(`--hex <BYTES> --arch <x86-64|aarch64>`), or an ELF64 section
(`--elf <PATH> [--section .text]`) against `llvm-objdump` and GNU `objdump`
(requiring `--lean-bin <PATH>` unless `--disasm-only` is passed; supports
`--lane <base|word-simd>`, `--olean-dir <DIR>`, `--require-reloc`, and
`--pretty`). The `exec` subcommand replays JSONL x86-64 execution traces
(`--corpus <PATH>`, or `--self-test` for the built-in corpus) against
`Anneal.ElfX86.ScalarEval` across the `scalar`, `rep`, and `sandbox` bands
(requiring `--lean-bin <PATH>`, and `--olean-dir <DIR>` when loading compiled
`.olean`s; supports `--band`, `--fuel`, `--batch-size`, `--strict-coverage`,
and `--pretty`). Decoder mode exits `0` when all cases pass, `1` on mismatches
or unexpected gaps, and `2` on CLI or tool errors; `exec` exits `0` when at
least one record is verified (`PASS` or `TRAP_PREDICTED`) with zero
mismatches, trap mismatches, fuel exhaustions, or cross-vendor defined defects
(and zero decode/step gaps when `--strict-coverage` is set), `1` otherwise,
and `2` on CLI or tool errors.
## Building, Linting, and Testing
CI runs the `tools-anneal-cpp` check declared in
[`ci/checks.toml`](../../ci/checks.toml) via
[`ci/scripts/run_anneal_cpp_check.sh`](../../ci/scripts/run_anneal_cpp_check.sh)
using the pinned `stable` toolchain from
[`ci/pins/rust-toolchains.toml`](../../ci/pins/rust-toolchains.toml):
```bash
cd tools/anneal-cpp
cargo "+${PIN_RUST_TOOLCHAINS__STABLE}" clippy --locked --offline \
--workspace --all-targets -- -D warnings
cargo "+${PIN_RUST_TOOLCHAINS__STABLE}" test --locked --offline --workspace
```
To invoke the CI wrapper directly from the repository root (it sets
`CARGO_NET_OFFLINE=true`, defaults `CARGO_TARGET_DIR` to
`${HOME:-/scratch}/anneal-cpp-target`, and enforces a 1800 s timeout):
```bash
PIN_RUST_TOOLCHAINS__STABLE=<stable> ./ci/scripts/run_anneal_cpp_check.sh
```
For local runs from `tools/anneal-cpp/` with your default Cargo toolchain (add
`--offline` once dependencies are cached):
```bash
cd tools/anneal-cpp
cargo clippy --locked --workspace --all-targets -- -D warnings
cargo test --locked --workspace
```
Workspace formatting is checked in CI by
[`ci/scripts/run_fmt_domain.sh`](../../ci/scripts/run_fmt_domain.sh) using the
pinned `nightly` toolchain from
[`ci/pins/rust-toolchains.toml`](../../ci/pins/rust-toolchains.toml):
```bash
cargo +"${PIN_RUST_TOOLCHAINS__NIGHTLY}" fmt \
--manifest-path tools/anneal-cpp/Cargo.toml --all --check
```
The workspace test suite runs entirely offline against checked-in JSON fixtures
(`lower-cpp/tests/fixtures/`) and inline corpora (`cpp-diff`, `isa-oracle`),
with no C++, LLVM, or Lean installation required. Live end-to-end runs of
`anneal-clang-dump`, `anneal-cpp-diff`, or `anneal-isa-oracle` additionally
require the external tools noted above (see
[`../anneal-clang-dump/README.md`](../anneal-clang-dump/README.md) for building
`anneal-clang-dump` against Clang/LLVM 22+).