tree: 17cc2bad6c14230e9359349baf87c5245a78751b [search]
  1. src/
  2. tests/
  3. Cargo.toml
  4. README.md
tools/anneal-cpp/ffi-check/README.md

anneal-ffi-check

Fail-closed C/C++ to Rust foreign-function interface layout, calling-convention, ABI-passing, and unwind-safety checker in the tools/anneal-cpp workspace.

Usage

anneal-ffi-check --cpp-bundle BUNDLE.json \
  [--rust-ffi RUST_FFI.json] [--rust-type-sizes SIZES.txt] \
  [--map RUST=CPP]... [--format human|json] \
  [--emit-lean OUT.lean|-] [--lean-namespace NS] \
  [--emit-c-asserts OUT.h|-]

Exit codes: 0 when all checks pass, 1 on usage/IO/parse/schema errors, and 2 when one or more FFI parity violations are detected.

Inputs

  • --cpp-bundle: RawCppBundle (schema v1) JSON emitted by anneal-clang-dump.
  • --rust-ffi: compact schema_version: 1 JSON descriptor (for example, emitted by a compile-time const { size_of / align_of / offset_of! } probe) providing target_triple, pointer_width_bits, c_char_is_signed, optional c_long_bits, records, enums, and functions.
  • --rust-type-sizes: raw rustc -Z print-type-sizes output. When paired with --rust-ffi, every record, field offset/size, enum width, and rustc padding span is cross-verified against the compiler output. In standalone mode (without --rust-ffi), layout and padding checks run, while unattested facts (#[repr(C)], target policy, is_packed, and field types) fail closed.

Enforced Invariants

  • Aggregate geometry & padding: #[repr(C)], exact size_bytes, align_bytes, is_packed, is_union, field offset_bits, bitfield and unsupported-type rejection, and interval-based interior/tail padding coverage requiring MaybeUninit<u8> fields or bounded, non-overlapping unread_padding_ranges.
  • Scalar, pointer & enum parity: c_char signedness, bool vs u8 conflation, LP64/LLP64/ILP32 c_long widths (checked against lang_policy.c_long_bits when present), pointer const/nullability symmetry, bundle-attested enums[] width/signedness/discriminant parity, typed enum-vs-integer and cross-enum drift rejection (supporting both closed Rust enum, which requires attested discriminants parity against RawCppBundle.enums[].enumerators, and transparent c_enum bindings marked is_transparent_wrapper: true and cross-checked as Struct in --rust-type-sizes), rejection of C++ bool-backed enums (underlying_is_bool / c_spelling of "bool"/"_Bool"), and fail-closed rejection of bare Rust enum bindings over unrestricted C integers.
  • Calling convention, direction, ABI passing, unwind safety, and const-consistency: direction: import | export (default import), is_extern_c, exact linker symbol parity (catching __asm__ redirects), free C functions, is_variadic, fail-closed rejection of unprototyped C declarations (has_prototype: false / FunctionNoProtoType), checked layout presence for by-value records, System V AMD64 indirect/byval/sret classification (including transitively unaligned packed subobjects), extend_int sign/zero-extension parity, and rejection of polymorphic, non-trivially-destructible, non-trivially-copyable, or inherited C++ records passed by value.
    • For import: transitive in-TU call-graph unwind attestation (exceptions_enabled == false, is_definition == true, body_stmt present, and zero declaration-only, indirect, virtual, or unmodeled reachable callees for extern "C", or extern "C-unwind").
    • For export: requires a C/C++ header declaration (is_definition == false, has_prototype == true for C exports; omitted has_prototype is accepted only in C++ where declarations always carry prototypes), refuses C-variadic exports, and reverses the unwind check (extern "C" passes regardless of C/C++ exceptions_enabled because rustc guarantees abort-on-unwind; extern "C-unwind" exports are refused fail-closed). Enforces const-consistency (safety_pre_requires_write fails if the C/C++ prototype declares const) and emits non-fatal disclosure warnings for extra-validity-bearing export parameters (top-level closed enum, plus bool and non-null references at top level, inside by-value #[repr(C)] aggregates, and behind non-null references; closed enum in struct fields, pointees, and C-produced import returns remains fail-closed with EnumValidityUnverified) with an explicit caveat that non-flagged parameters still require baseline byte initialization.
  • Build-compiled C assertion header (--emit-c-asserts): writes a C11/C++17 header with _Static_assert checks on CHAR_BIT, pointer width, CHAR_MIN signedness, sizeof(long) * CHAR_BIT, record sizeof / _Alignof / offsetof, enum sizeof, and strict-prototype function-pointer witnesses (static R (*const anneal_proto_<sym>)(P...) = &<sym>;) for verified exports. Compile C targets with -Werror=strict-prototypes -Werror=incompatible-function-pointer-types (Clang) or -Werror=strict-prototypes -Werror=incompatible-pointer-types (GCC).
  • Lean parity export (--emit-lean): emits a Pass-1 Lean 4 module pairing Trusted.CppAst.CppRecordDef (failing closed unless fields[].is_const is present) with Anneal.RepAlgebra.Geometry and rfl-discharged layout parity theorems. When --emit-lean - or --emit-c-asserts - writes to stdout, the check report is written to stderr so stdout remains valid source.