blob: 0f23709b27440a4f74907141d7ad6a380fca30a1 [file]
// 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.
//! `anneal-ffi-check`: fail-closed C/C++ <-> Rust FFI layout, calling
//! convention, ABI passing, and unwind-safety checker.
//!
//! Consumes a `RawCppBundle` (schema v1, emitted by `anneal-clang-dump`)
//! paired with:
//! - a compact Rust FFI JSON descriptor (as emitted by a compile-time Rust
//! `const { size_of / align_of / offset_of! }` probe),
//! - both `--rust-ffi` and raw `rustc -Z print-type-sizes` output
//! (cross-verifying the descriptor against compiler-emitted layouts), or
//! - standalone `rustc -Z print-type-sizes` output (diagnostic layout/padding
//! audit that fails closed on unattested `#[repr(C)]`, target policy,
//! packedness, and field types).
//!
//! Optionally exports a Pass-1 Lean 4 structural parity module pairing
//! `Trusted.CppAst.CppRecordDef` with `Anneal.RepAlgebra.Geometry`.
pub mod c_asserts;
pub mod check;
pub mod cpp_bundle;
pub mod lean_emit;
pub mod print_type_sizes;
pub mod report;
pub mod rust_desc;
#[cfg(test)]
mod export_tests;
#[cfg(test)]
mod tests;
use std::collections::BTreeMap;
pub use c_asserts::emit_c_asserts;
pub use check::{
check_ffi, CheckOutcome, MatchedEnum, MatchedExport, MatchedRecord, VerifiedTargetPolicy,
};
pub use cpp_bundle::{parse_cpp_bundle, CppBundle};
pub use lean_emit::emit_lean_parity;
pub use print_type_sizes::{
cross_check_print_type_sizes, cross_check_print_type_sizes_with_bundle,
descriptor_from_print_type_sizes, parse_print_type_sizes, TypeSizeRecord,
};
pub use report::{
CheckReport, Diagnostic, DiagnosticCategory, DiagnosticCode, PaddingKind, PaddingRegionReport,
PaddingResolution, ParseError, Warning, WarningCode,
};
pub use rust_desc::{parse_rust_descriptor_json, FfiDirection, RustFfiDescriptor};
/// High-level verification entry point combining a `RawCppBundle` JSON string
/// with an optional Rust FFI JSON descriptor and optional
/// `rustc -Z print-type-sizes` text.
///
/// At least one of `rust_ffi_json` or `rust_type_sizes` must be provided.
/// Passing non-empty `explicit_maps` alongside `rust_ffi_json` fails closed.
pub fn verify_ffi_inputs(
cpp_bundle_json: &str,
rust_ffi_json: Option<&str>,
rust_type_sizes: Option<&str>,
explicit_maps: &BTreeMap<String, String>,
) -> Result<CheckOutcome, ParseError> {
if rust_ffi_json.is_some() && !explicit_maps.is_empty() {
return Err(ParseError::Schema(
"--map cannot be combined with --rust-ffi; set `cpp_record` \
directly in the Rust FFI JSON descriptor"
.to_owned(),
));
}
let bundle = parse_cpp_bundle(cpp_bundle_json)?;
let ts_records = match rust_type_sizes {
Some(text) => Some(parse_print_type_sizes(text)?),
None => None,
};
let (desc, cross_diags) = match (rust_ffi_json, &ts_records) {
(Some(json), Some(ts)) => {
let desc = parse_rust_descriptor_json(json)?;
let diags = cross_check_print_type_sizes_with_bundle(&desc, ts, Some(&bundle));
(desc, diags)
}
(Some(json), None) => {
let desc = parse_rust_descriptor_json(json)?;
(desc, Vec::new())
}
(None, Some(ts)) => {
let desc = descriptor_from_print_type_sizes(&bundle, ts, explicit_maps)?;
(desc, Vec::new())
}
(None, None) => {
return Err(ParseError::Schema(
"at least one of --rust-ffi or --rust-type-sizes is required".to_owned(),
));
}
};
let mut outcome = check_ffi(&bundle, &desc);
if !cross_diags.is_empty() {
outcome.report.diagnostics.extend(cross_diags);
outcome.report.ok = false;
outcome.matched_records.clear();
outcome.matched_enums.clear();
outcome.matched_exports.clear();
}
Ok(outcome)
}