| // 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. | |
| //! Comprehensive positive and negative unit tests for `anneal-ffi-check`. | |
| use std::collections::BTreeMap; | |
| use crate::{ | |
| check_ffi, emit_lean_parity, parse_cpp_bundle, parse_print_type_sizes, | |
| parse_rust_descriptor_json, print_type_sizes::TypeSizeKind, verify_ffi_inputs, DiagnosticCode, | |
| PaddingKind, PaddingResolution, | |
| }; | |
| // Real `anneal-clang-dump` schema v1 JSON bundle modeled on `pos_struct_ptr`. | |
| const C11_PAIR_BUNDLE: &str = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "pos_struct_ptr.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "align_bytes": 4, | |
| "fields": [ | |
| { | |
| "bit_width": null, | |
| "is_const": false, | |
| "name": "lo", | |
| "offset_bits": 0, | |
| "ty": { | |
| "bits": 32, | |
| "is_char_like": false, | |
| "kind": "int", | |
| "signed": false | |
| } | |
| }, | |
| { | |
| "bit_width": null, | |
| "is_const": false, | |
| "name": "hi", | |
| "offset_bits": 32, | |
| "ty": { | |
| "bits": 16, | |
| "is_char_like": false, | |
| "kind": "int", | |
| "signed": false | |
| } | |
| } | |
| ], | |
| "id": "struct.pair", | |
| "is_union": false, | |
| "name": "pair", | |
| "size_bytes": 8 | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "abi_witness": { | |
| "cc": "c", | |
| "param_passing": [ | |
| { "kind": "direct_ptr" }, | |
| { "kind": "direct_ptr" } | |
| ], | |
| "return_passing": { "bits": 32, "kind": "direct_int" } | |
| }, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_extern_c": true, | |
| "is_variadic": false, | |
| "params": [ | |
| { | |
| "name": "dst", | |
| "ty": { | |
| "is_const": false, | |
| "is_restrict": false, | |
| "kind": "ptr", | |
| "may_alias": false, | |
| "pointee": { "id": "struct.pair", "kind": "record" } | |
| } | |
| }, | |
| { | |
| "name": "src", | |
| "ty": { | |
| "is_const": true, | |
| "is_restrict": false, | |
| "kind": "ptr", | |
| "may_alias": false, | |
| "pointee": { "id": "struct.pair", "kind": "record" } | |
| } | |
| } | |
| ], | |
| "return_ty": { | |
| "bits": 32, | |
| "is_char_like": false, | |
| "kind": "int", | |
| "signed": false | |
| }, | |
| "symbol": "mix", | |
| "unmangled_name": "mix" | |
| } | |
| ] | |
| }"#; | |
| // Verbatim `rustc -Z print-type-sizes` output captured from nightly rustc. | |
| const REAL_PRINT_TYPE_SIZES: &str = "\ | |
| print-type-size type: `Padded`: 12 bytes, alignment: 4 bytes | |
| print-type-size field `.tag`: 1 bytes | |
| print-type-size padding: 3 bytes | |
| print-type-size field `.val`: 4 bytes, alignment: 4 bytes | |
| print-type-size field `.tail`: 1 bytes | |
| print-type-size end padding: 3 bytes | |
| print-type-size type: `Packed`: 9 bytes, alignment: 1 bytes | |
| print-type-size field `.tag`: 1 bytes | |
| print-type-size field `.val`: 8 bytes | |
| print-type-size type: `ExplicitPad`: 8 bytes, alignment: 4 bytes | |
| print-type-size field `.tag`: 1 bytes | |
| print-type-size field `._pad0`: 3 bytes | |
| print-type-size field `.val`: 4 bytes | |
| print-type-size type: `U`: 8 bytes, alignment: 8 bytes | |
| print-type-size variant `U`: 8 bytes | |
| print-type-size field `.a`: 4 bytes | |
| print-type-size field `.b`: 8 bytes, offset: 0 bytes, alignment: 8 bytes | |
| print-type-size type: `SmallEnum`: 2 bytes, alignment: 2 bytes | |
| print-type-size discriminant: 2 bytes | |
| print-type-size variant `A`: 0 bytes | |
| print-type-size variant `B`: 0 bytes | |
| "; | |
| fn has_code(diags: &[crate::Diagnostic], code: DiagnosticCode) -> bool { | |
| diags.iter().any(|d| d.code == code) | |
| } | |
| #[test] | |
| fn positive_c11_pair_with_maybe_uninit_tail_padding_and_lean_export() { | |
| let rust_json = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "c_long_bits": 64, | |
| "records": [ | |
| { | |
| "rust_name": "Pair", | |
| "cpp_record": "struct.pair", | |
| "repr": "C", | |
| "is_packed": false, | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { | |
| "name": "lo", | |
| "offset_bytes": 0, | |
| "size_bytes": 4, | |
| "ty": { "kind": "int", "bits": 32, "signed": false } | |
| }, | |
| { | |
| "name": "hi", | |
| "offset_bytes": 4, | |
| "size_bytes": 2, | |
| "ty": { "kind": "int", "bits": 16, "signed": false } | |
| }, | |
| { | |
| "name": "_pad", | |
| "offset_bytes": 6, | |
| "size_bytes": 2, | |
| "ty": { "kind": "maybe_uninit_bytes", "len": 2 } | |
| } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "mix", | |
| "cpp_symbol": "mix", | |
| "abi": "C", | |
| "is_variadic": false, | |
| "return_ty": { "kind": "int", "bits": 32, "signed": false }, | |
| "params": [ | |
| { | |
| "name": "dst", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "pointee": { "kind": "record", "name": "Pair" } | |
| } | |
| }, | |
| { | |
| "name": "src", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": true, | |
| "pointee": { "kind": "record", "name": "Pair" } | |
| } | |
| } | |
| ], | |
| "abi_witness": { | |
| "return_passing": { "kind": "direct_int", "bits": 32 }, | |
| "param_passing": [ | |
| { "kind": "direct_ptr" }, | |
| { "kind": "direct_ptr" } | |
| ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let outcome = verify_ffi_inputs(C11_PAIR_BUNDLE, Some(rust_json), None, &BTreeMap::new()) | |
| .expect("valid inputs"); | |
| assert!(outcome.report.ok, "unexpected diagnostics: {:?}", outcome.report); | |
| assert_eq!(outcome.report.records_checked, 1); | |
| assert_eq!(outcome.report.functions_checked, 1); | |
| assert_eq!(outcome.report.padding_regions.len(), 1); | |
| assert_eq!(outcome.report.padding_regions[0].kind, PaddingKind::Tail); | |
| assert_eq!(outcome.report.padding_regions[0].start_byte, 6); | |
| assert_eq!(outcome.report.padding_regions[0].end_byte, 8); | |
| assert_eq!( | |
| outcome.report.padding_regions[0].resolution, | |
| PaddingResolution::CoveredByMaybeUninit | |
| ); | |
| let json_out = outcome.report.to_json_pretty().expect("json"); | |
| assert!(json_out.contains("\"ok\": true")); | |
| let human_out = outcome.report.format_human(); | |
| assert!(human_out.contains("anneal-ffi-check: PASS")); | |
| let lean = emit_lean_parity( | |
| "Anneal.FfiParity", | |
| &outcome.report.target_triple, | |
| &outcome.matched_records, | |
| ) | |
| .expect("lean emit"); | |
| assert!(lean.contains("public import Trusted.CppAst")); | |
| assert!(lean.contains("public import Anneal.RepAlgebra")); | |
| assert!(lean.contains("List.foldr")); | |
| assert!(lean.contains("def cppRecord_Pair : CppRecordDef")); | |
| assert!(lean.contains("isConst := false")); | |
| assert!(lean.contains("def rustGeom_Pair : Geometry Nat")); | |
| assert!(lean.contains("alignLog2 := 2")); | |
| assert!(lean.contains("theorem Pair_layout_parity :")); | |
| assert!(lean.contains("exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩")); | |
| } | |
| #[test] | |
| fn detects_exposed_interior_and_tail_padding_and_accepts_unread_ranges() { | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "padded.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Padded", | |
| "name": "Padded", | |
| "is_union": false, | |
| "size_bytes": 12, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { | |
| "name": "tag", | |
| "offset_bits": 0, | |
| "bit_width": null, | |
| "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } | |
| }, | |
| { | |
| "name": "val", | |
| "offset_bits": 32, | |
| "bit_width": null, | |
| "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } | |
| }, | |
| { | |
| "name": "tail", | |
| "offset_bits": 64, | |
| "bit_width": null, | |
| "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } | |
| } | |
| ] | |
| } | |
| ], | |
| "functions": [] | |
| }"#; | |
| // Missing padding coverage -> flags BOTH interior [1..4) and tail [9..12). | |
| let rust_exposed = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Padded", | |
| "cpp_record": "struct.Padded", | |
| "repr": "C", | |
| "size_bytes": 12, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "val", "offset_bytes": 4, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "tail", "offset_bytes": 8, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| let desc = parse_rust_descriptor_json(rust_exposed).unwrap(); | |
| let res = check_ffi(&bundle, &desc); | |
| assert!(!res.report.ok); | |
| assert!(has_code(&res.report.diagnostics, DiagnosticCode::InteriorPaddingExposed)); | |
| assert!(has_code(&res.report.diagnostics, DiagnosticCode::TailPaddingExposed)); | |
| // Declaring both padding ranges unread passes cleanly. | |
| let rust_unread = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Padded", | |
| "cpp_record": "struct.Padded", | |
| "repr": "C", | |
| "size_bytes": 12, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "val", "offset_bytes": 4, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "tail", "offset_bytes": 8, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } } | |
| ], | |
| "unread_padding_ranges": [ | |
| { "start_byte": 1, "end_byte": 4 }, | |
| { "start_byte": 9, "end_byte": 12 } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let desc_unread = parse_rust_descriptor_json(rust_unread).unwrap(); | |
| let res_ok = check_ffi(&bundle, &desc_unread); | |
| assert!(res_ok.report.ok, "diagnostics: {:?}", res_ok.report.diagnostics); | |
| assert_eq!(res_ok.report.padding_regions.len(), 2); | |
| assert_eq!(res_ok.report.padding_regions[0].resolution, PaddingResolution::DeclaredUnread); | |
| // Overlapping a MaybeUninit padding field with a live C value byte fails. | |
| let rust_overlap = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Padded", | |
| "cpp_record": "struct.Padded", | |
| "repr": "C", | |
| "size_bytes": 12, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "_bad_pad", "offset_bytes": 1, "size_bytes": 4, "ty": { "kind": "maybe_uninit_bytes", "len": 4 } }, | |
| { "name": "val", "offset_bytes": 4, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "tail", "offset_bytes": 8, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } } | |
| ], | |
| "unread_padding_ranges": [ | |
| { "start_byte": 9, "end_byte": 12 } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let desc_ov = parse_rust_descriptor_json(rust_overlap).unwrap(); | |
| let res_ov = check_ffi(&bundle, &desc_ov); | |
| assert!(has_code(&res_ov.report.diagnostics, DiagnosticCode::PaddingOverlapsValue)); | |
| } | |
| #[test] | |
| fn detects_geometry_packed_union_repr_offset_and_bitfield_mismatches() { | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "geom.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Packed", | |
| "name": "Packed", | |
| "is_union": false, | |
| "is_packed": true, | |
| "size_bytes": 9, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "tag", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } }, | |
| { "name": "val", "offset_bits": 8, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "union.U", | |
| "name": "U", | |
| "is_union": true, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Bits", | |
| "name": "Bits", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "flags", "offset_bits": 0, "bit_width": 3, "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [] | |
| }"#; | |
| // Positive: exact packed struct and union both pass. | |
| let rust_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Packed", | |
| "cpp_record": "struct.Packed", | |
| "repr": "C", | |
| "is_packed": true, | |
| "is_union": false, | |
| "size_bytes": 9, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "val", "offset_bytes": 1, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "U", | |
| "cpp_record": "union.U", | |
| "repr": "C", | |
| "is_packed": false, | |
| "is_union": true, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "b", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| let ok_res = check_ffi(&bundle, &parse_rust_descriptor_json(rust_ok).unwrap()); | |
| assert!(ok_res.report.ok, "unexpected: {:?}", ok_res.report.diagnostics); | |
| // Negative: wrong repr, unpacked, wrong size/align/offset, union mismatch, | |
| // and C bitfield. | |
| let rust_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "PackedWrong", | |
| "cpp_record": "struct.Packed", | |
| "repr": "Rust", | |
| "is_packed": false, | |
| "is_union": true, | |
| "size_bytes": 16, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "val", "offset_bytes": 8, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "Bits", | |
| "cpp_record": "struct.Bits", | |
| "repr": "C", | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "flags", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bad_res = check_ffi(&bundle, &parse_rust_descriptor_json(rust_bad).unwrap()); | |
| assert!(!bad_res.report.ok); | |
| for code in [ | |
| DiagnosticCode::ReprNotC, | |
| DiagnosticCode::UnionMismatch, | |
| DiagnosticCode::PackedMismatch, | |
| DiagnosticCode::SizeMismatch, | |
| DiagnosticCode::AlignMismatch, | |
| DiagnosticCode::FieldOffsetMismatch, | |
| DiagnosticCode::BitfieldUnsupported, | |
| ] { | |
| assert!( | |
| has_code(&bad_res.report.diagnostics, code), | |
| "missing expected {code:?} in {:?}", | |
| bad_res.report.diagnostics | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn detects_scalar_c_char_bool_u8_c_long_and_enum_mismatches() { | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "scalars.c", | |
| "target_triple": "aarch64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": false, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [], | |
| "enums": [ | |
| { "name": "Status", "bits": 32, "signed": false } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "check_scalars", | |
| "unmangled_name": "check_scalars", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "bool" }, | |
| "params": [ | |
| { "name": "ch", "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } }, | |
| { "name": "n", "ty": { "kind": "int", "bits": 64, "signed": true, "is_char_like": false } }, | |
| { "name": "st", "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "extend_int", "bits": 1, "sign_ext": false }, | |
| "param_passing": [ | |
| { "kind": "extend_int", "bits": 8, "sign_ext": false }, | |
| { "kind": "direct_int", "bits": 64 }, | |
| { "kind": "direct_int", "bits": 32 } | |
| ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| // Positive: matching unsigned c_char (aarch64), LP64 64-bit c_long, | |
| // 32-bit unsigned enum, and bool (`extend_int`, 1-bit zero-extended). | |
| let rust_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "aarch64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": false, | |
| "c_long_bits": 64, | |
| "enums": [ | |
| { | |
| "rust_name": "Status", | |
| "cpp_name": "enum Status", | |
| "repr_bits": 32, | |
| "repr_signed": false, | |
| "cpp_bits": 32, | |
| "cpp_signed": false | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "check_scalars", | |
| "cpp_symbol": "check_scalars", | |
| "abi": "C", | |
| "return_ty": { "kind": "bool" }, | |
| "params": [ | |
| { "name": "ch", "ty": { "kind": "c_char" } }, | |
| { "name": "n", "ty": { "kind": "c_long" } }, | |
| { "name": "st", "ty": { "kind": "int", "bits": 32, "signed": false } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| let ok = check_ffi(&bundle, &parse_rust_descriptor_json(rust_ok).unwrap()); | |
| assert!(ok.report.ok, "unexpected: {:?}", ok.report.diagnostics); | |
| assert_eq!(ok.report.enums_checked, 1); | |
| // Negative: signed c_char mismatch, u8 bound to C bool, LLP64 32-bit | |
| // c_long on LP64 target, 8-bit signed Rust enum vs 32-bit unsigned C, and | |
| // bare Rust enum bound directly to a C integer parameter. | |
| let rust_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "aarch64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "c_long_bits": 32, | |
| "enums": [ | |
| { | |
| "rust_name": "Status", | |
| "cpp_name": "enum Status", | |
| "repr_bits": 8, | |
| "repr_signed": true, | |
| "cpp_bits": 32, | |
| "cpp_signed": false | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "check_scalars", | |
| "cpp_symbol": "check_scalars", | |
| "abi": "C", | |
| "return_ty": { "kind": "int", "bits": 8, "signed": false }, | |
| "params": [ | |
| { "name": "ch", "ty": { "kind": "c_char" } }, | |
| { "name": "n", "ty": { "kind": "c_long" } }, | |
| { "name": "st", "ty": { "kind": "enum", "name": "Status", "bits": 8, "signed": true } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bad = check_ffi(&bundle, &parse_rust_descriptor_json(rust_bad).unwrap()); | |
| assert!(!bad.report.ok); | |
| for code in [ | |
| DiagnosticCode::CCharSignednessMismatch, | |
| DiagnosticCode::CLongWidthMismatch, | |
| DiagnosticCode::BoolU8Conflation, | |
| DiagnosticCode::EnumWidthMismatch, | |
| DiagnosticCode::EnumSignednessMismatch, | |
| DiagnosticCode::EnumValidityUnverified, | |
| ] { | |
| assert!( | |
| has_code(&bad.report.diagnostics, code), | |
| "missing expected {code:?} in {:?}", | |
| bad.report.diagnostics | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn detects_calling_convention_abi_sret_byval_and_extend_sign_mismatches() { | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "abi.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Big", | |
| "name": "Big", | |
| "is_union": false, | |
| "size_bytes": 24, | |
| "align_bytes": 8, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "offset_bits": 64, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "c", "offset_bits": 128, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "big_fn", | |
| "unmangled_name": "big_fn", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "record", "id": "struct.Big" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "record", "id": "struct.Big" } }, | |
| { "name": "s", "ty": { "kind": "int", "bits": 8, "signed": true, "is_char_like": false } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "indirect", "byval": false }, | |
| "param_passing": [ | |
| { "kind": "indirect", "byval": true }, | |
| { "kind": "extend_int", "bits": 8, "sign_ext": true } | |
| ] | |
| } | |
| }, | |
| { | |
| "symbol": "_Z10mangled_fni", | |
| "unmangled_name": "mangled_fn", | |
| "is_extern_c": false, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": true, | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "direct_int", "bits": 32 }, | |
| "param_passing": [ | |
| { "kind": "direct_int", "bits": 32 } | |
| ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| // Positive for `big_fn`: >16 B struct uses sret (`indirect`, `byval` | |
| // false) for return and `indirect` (`byval` true) for param. | |
| let rust_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Big", | |
| "cpp_record": "struct.Big", | |
| "repr": "C", | |
| "size_bytes": 24, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "b", "offset_bytes": 8, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "c", "offset_bytes": 16, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "big_fn", | |
| "cpp_symbol": "big_fn", | |
| "abi": "C", | |
| "is_variadic": false, | |
| "return_ty": { "kind": "record", "name": "Big" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "record", "name": "Big" } }, | |
| { "name": "s", "ty": { "kind": "int", "bits": 8, "signed": true } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| let ok = check_ffi(&bundle, &parse_rust_descriptor_json(rust_ok).unwrap()); | |
| assert!(ok.report.ok, "unexpected: {:?}", ok.report.diagnostics); | |
| // Negative: sret vs direct_int mismatch, extend_int sign_ext mismatch, | |
| // non-extern-"C" mangled callee, and variadic mismatch. | |
| let rust_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "functions": [ | |
| { | |
| "rust_name": "big_fn", | |
| "cpp_symbol": "big_fn", | |
| "abi": "C", | |
| "is_variadic": false, | |
| "return_ty": { "kind": "record", "name": "struct.Big" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "record", "name": "struct.Big" } }, | |
| { "name": "s", "ty": { "kind": "int", "bits": 8, "signed": true } } | |
| ], | |
| "abi_witness": { | |
| "return_passing": { "kind": "direct_int", "bits": 64 }, | |
| "param_passing": [ | |
| { "kind": "direct_int", "bits": 64 }, | |
| { "kind": "extend_int", "bits": 8, "sign_ext": false } | |
| ] | |
| } | |
| }, | |
| { | |
| "rust_name": "mangled_fn", | |
| "cpp_symbol": "mangled_fn", | |
| "abi": "C", | |
| "is_variadic": false, | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "int", "bits": 32, "signed": true } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bad = check_ffi(&bundle, &parse_rust_descriptor_json(rust_bad).unwrap()); | |
| assert!(!bad.report.ok); | |
| for code in [ | |
| DiagnosticCode::AbiPassingMismatch, | |
| DiagnosticCode::ExtendSignMismatch, | |
| DiagnosticCode::NotExternC, | |
| DiagnosticCode::VariadicMismatch, | |
| ] { | |
| assert!( | |
| has_code(&bad.report.diagnostics, code), | |
| "missing expected {code:?} in {:?}", | |
| bad.report.diagnostics | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn enforces_unwind_abi_and_cpp_object_model_boundary() { | |
| // Modeled on real `cxx_neg_exceptions`, `cxx_neg_virtual`, and `cxx_guard`. | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "cxx_boundary.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": true, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Poly", | |
| "name": "Poly", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "is_cxx_record": true, | |
| "is_polymorphic": true, | |
| "is_trivially_copyable": false, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [] | |
| }, | |
| { | |
| "id": "struct.Guard", | |
| "name": "Guard", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": false, | |
| "has_non_trivial_dtor": true, | |
| "dtor_symbol": "_ZN5GuardD1Ev", | |
| "bases": [], | |
| "fields": [ | |
| { | |
| "name": "p", | |
| "offset_bits": 0, | |
| "bit_width": null, | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "is_restrict": false, | |
| "may_alias": false, | |
| "pointee": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } | |
| } | |
| } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "may_throw", | |
| "unmangled_name": "may_throw", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false }, | |
| "params": [ | |
| { | |
| "name": "g", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "is_restrict": false, | |
| "may_alias": false, | |
| "pointee": { "kind": "record", "id": "struct.Guard" } | |
| } | |
| } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "direct_int", "bits": 32 }, | |
| "param_passing": [ { "kind": "direct_ptr" } ] | |
| } | |
| }, | |
| { | |
| "symbol": "take_guard_by_val", | |
| "unmangled_name": "take_guard_by_val", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { | |
| "name": "g", | |
| "dtor_symbol": "_ZN5GuardD1Ev", | |
| "ty": { "kind": "record", "id": "struct.Guard" } | |
| } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "indirect", "byval": false } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| // Positive: `extern "C-unwind"` with opaque pointer handle to `Guard`. | |
| let rust_safe = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "functions": [ | |
| { | |
| "rust_name": "may_throw", | |
| "cpp_symbol": "may_throw", | |
| "abi": "C-unwind", | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true }, | |
| "params": [ | |
| { | |
| "name": "g", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "pointee": { "kind": "opaque", "name": "Guard" } | |
| } | |
| } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let safe_res = check_ffi(&bundle, &parse_rust_descriptor_json(rust_safe).unwrap()); | |
| assert!(safe_res.report.ok, "unexpected: {:?}", safe_res.report.diagnostics); | |
| // Negative: `extern "C"` under `exceptions_enabled == true`, plus binding | |
| // polymorphic `Poly` and RAII `Guard` by value across `#[repr(C)]`. | |
| let rust_unsafe = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Poly", | |
| "cpp_record": "struct.Poly", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "may_throw", | |
| "cpp_symbol": "may_throw", | |
| "abi": "C", | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true }, | |
| "params": [ | |
| { | |
| "name": "g", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "pointee": { "kind": "opaque", "name": "Guard" } | |
| } | |
| } | |
| ] | |
| }, | |
| { | |
| "rust_name": "take_guard_by_val", | |
| "cpp_symbol": "take_guard_by_val", | |
| "abi": "C-unwind", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "g", "ty": { "kind": "record", "name": "struct.Guard" } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bad_res = check_ffi(&bundle, &parse_rust_descriptor_json(rust_unsafe).unwrap()); | |
| assert!(!bad_res.report.ok); | |
| for code in [ | |
| DiagnosticCode::UnwindAbiMismatch, | |
| DiagnosticCode::PolymorphicRecordByValue, | |
| DiagnosticCode::NonTrivialDtorByValue, | |
| DiagnosticCode::NonTriviallyCopyableByValue, | |
| ] { | |
| assert!( | |
| has_code(&bad_res.report.diagnostics, code), | |
| "missing expected {code:?} in {:?}", | |
| bad_res.report.diagnostics | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn parses_real_print_type_sizes_and_cross_checks_descriptors() { | |
| let parsed = parse_print_type_sizes(REAL_PRINT_TYPE_SIZES) | |
| .expect("real rustc -Z print-type-sizes output parses cleanly"); | |
| assert_eq!(parsed.len(), 5); | |
| assert_eq!(parsed[0].name, "Padded"); | |
| assert_eq!(parsed[0].kind, TypeSizeKind::Struct); | |
| assert_eq!(parsed[0].size_bytes, 12); | |
| assert_eq!(parsed[0].align_bytes, 4); | |
| assert_eq!(parsed[0].fields.len(), 3); | |
| assert_eq!(parsed[0].fields[1].offset_bytes, 4); | |
| assert_eq!(parsed[0].fields[2].offset_bytes, 8); | |
| assert_eq!(parsed[0].padding.len(), 2); | |
| assert_eq!(parsed[0].padding[0].kind, PaddingKind::Interior); | |
| assert_eq!(parsed[0].padding[0].start_byte, 1); | |
| assert_eq!(parsed[0].padding[0].end_byte, 4); | |
| assert_eq!(parsed[0].padding[1].kind, PaddingKind::Tail); | |
| assert_eq!(parsed[0].padding[1].start_byte, 9); | |
| assert_eq!(parsed[0].padding[1].end_byte, 12); | |
| assert_eq!(parsed[1].name, "Packed"); | |
| assert_eq!(parsed[1].size_bytes, 9); | |
| assert_eq!(parsed[1].align_bytes, 1); | |
| assert_eq!(parsed[3].name, "U"); | |
| assert_eq!(parsed[3].kind, TypeSizeKind::Union); | |
| assert_eq!(parsed[3].fields[1].offset_bytes, 0); | |
| assert_eq!(parsed[4].name, "SmallEnum"); | |
| assert_eq!(parsed[4].kind, TypeSizeKind::Enum); | |
| assert_eq!(parsed[4].discriminant_bytes, Some(2)); | |
| // Standalone `print-type-sizes` checks layout/padding AND fails closed on | |
| // unattested `#[repr(C)]`, target policy, packedness, and field types; | |
| // paired `--rust-ffi` + `--rust-type-sizes` passes cleanly. | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "pts.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Packed", | |
| "name": "Packed", | |
| "is_union": false, | |
| "is_packed": true, | |
| "size_bytes": 9, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "tag", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } }, | |
| { "name": "val", "offset_bits": 8, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "union.U", | |
| "name": "U", | |
| "is_union": true, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "enums": [ | |
| { "name": "SmallEnum", "bits": 16, "signed": false } | |
| ], | |
| "functions": [] | |
| }"#; | |
| let standalone = verify_ffi_inputs(cpp, None, Some(REAL_PRINT_TYPE_SIZES), &BTreeMap::new()) | |
| .expect("standalone print-type-sizes check"); | |
| assert!(!standalone.report.ok); | |
| assert_eq!(standalone.report.records_checked, 2); | |
| assert!(standalone.matched_records.is_empty()); | |
| assert!(has_code(&standalone.report.diagnostics, DiagnosticCode::ReprNotC)); | |
| assert!(has_code(&standalone.report.diagnostics, DiagnosticCode::MissingAbiField)); | |
| // Paired `--rust-ffi` + `--rust-type-sizes` passes when everything agrees. | |
| let exact_desc = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Packed", | |
| "cpp_record": "struct.Packed", | |
| "repr": "C", | |
| "is_packed": true, | |
| "size_bytes": 9, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "val", "offset_bytes": 1, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "U", | |
| "cpp_record": "union.U", | |
| "repr": "C", | |
| "is_packed": false, | |
| "is_union": true, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "b", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "enums": [ | |
| { | |
| "rust_name": "SmallEnum", | |
| "cpp_name": "SmallEnum", | |
| "repr_bits": 16, | |
| "repr_signed": false, | |
| "cpp_bits": 16, | |
| "cpp_signed": false | |
| } | |
| ] | |
| }"#; | |
| let paired_ok = | |
| verify_ffi_inputs(cpp, Some(exact_desc), Some(REAL_PRINT_TYPE_SIZES), &BTreeMap::new()) | |
| .expect("paired check runs"); | |
| assert!(paired_ok.report.ok, "unexpected: {:?}", paired_ok.report.diagnostics); | |
| // Cross-checking a tampered descriptor against `print-type-sizes` fails. | |
| let tampered = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Packed", | |
| "cpp_record": "struct.Packed", | |
| "repr": "C", | |
| "is_packed": true, | |
| "size_bytes": 16, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "val", "offset_bytes": 1, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let cross = | |
| verify_ffi_inputs(cpp, Some(tampered), Some(REAL_PRINT_TYPE_SIZES), &BTreeMap::new()) | |
| .expect("cross-check runs"); | |
| assert!(!cross.report.ok); | |
| assert!(cross.matched_records.is_empty()); | |
| assert!(has_code(&cross.report.diagnostics, DiagnosticCode::PrintTypeSizesMismatch)); | |
| } | |
| #[test] | |
| fn fails_closed_on_missing_or_malformed_schema_fields() { | |
| // Missing `sign_ext` on `extend_int`. | |
| let bad_abi = C11_PAIR_BUNDLE.replace( | |
| r#""return_passing": { "bits": 32, "kind": "direct_int" }"#, | |
| r#""return_passing": { "bits": 8, "kind": "extend_int" }"#, | |
| ); | |
| assert!(parse_cpp_bundle(&bad_abi).is_err()); | |
| // Wrong schema version. | |
| let bad_ver = C11_PAIR_BUNDLE.replace(r#""schema_version": 1"#, r#""schema_version": 99"#); | |
| assert!(parse_cpp_bundle(&bad_ver).is_err()); | |
| // Malformed `print-type-sizes` whose fields do not sum to total size. | |
| let bad_pts = "\ | |
| print-type-size type: `Broken`: 16 bytes, alignment: 4 bytes | |
| print-type-size field `.a`: 4 bytes | |
| "; | |
| assert!(parse_print_type_sizes(bad_pts).is_err()); | |
| // Invalid Lean namespace fails closed. | |
| assert!(emit_lean_parity("123Bad", "x86_64-unknown-linux-gnu", &[]).is_err()); | |
| } | |
| #[test] | |
| fn detects_target_pointer_bases_method_unknown_cc_and_llp64_long() { | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "edge.cc", | |
| "target_triple": "x86_64-pc-windows-msvc", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Base", | |
| "name": "Base", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "x", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Derived", | |
| "name": "Derived", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [ | |
| { "record_id": "struct.Base", "is_virtual": false, "offset_bytes": 0 } | |
| ], | |
| "fields": [ | |
| { "name": "y", "offset_bits": 32, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "method_like", | |
| "unmangled_name": "method_like", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "method_kind": "method", | |
| "is_virtual": false, | |
| "return_ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": false }, | |
| "params": [ | |
| { | |
| "name": "this", | |
| "is_this": true, | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "is_restrict": false, | |
| "may_alias": false, | |
| "pointee": { "kind": "record", "id": "struct.Base" } | |
| } | |
| } | |
| ], | |
| "abi_witness": { | |
| "cc": "unknown" | |
| } | |
| } | |
| ] | |
| }"#; | |
| let rust = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 32, | |
| "c_char_is_signed": true, | |
| "c_long_bits": 64, | |
| "records": [ | |
| { | |
| "rust_name": "Derived", | |
| "cpp_record": "struct.Derived", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "y", "offset_bytes": 4, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": true } } | |
| ], | |
| "unread_padding_ranges": [ | |
| { "start_byte": 0, "end_byte": 4 } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "method_like", | |
| "cpp_symbol": "method_like", | |
| "abi": "C", | |
| "return_ty": { "kind": "bool" }, | |
| "params": [ | |
| { | |
| "name": "this", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "pointee": { "kind": "opaque", "name": "Base" } | |
| } | |
| } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| let desc = parse_rust_descriptor_json(rust).unwrap(); | |
| let res = check_ffi(&bundle, &desc); | |
| assert!(!res.report.ok); | |
| for code in [ | |
| DiagnosticCode::TargetTripleMismatch, | |
| DiagnosticCode::PointerWidthMismatch, | |
| DiagnosticCode::CLongWidthMismatch, | |
| DiagnosticCode::CxxBasesUnsupported, | |
| DiagnosticCode::CxxMethodAsExternC, | |
| DiagnosticCode::CallingConvMismatch, | |
| DiagnosticCode::MissingAbiField, | |
| DiagnosticCode::BoolU8Conflation, | |
| ] { | |
| assert!( | |
| has_code(&res.report.diagnostics, code), | |
| "missing expected {code:?} in {:?}", | |
| res.report.diagnostics | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn rejects_unsupported_field_padding_skip_fake_padding_and_bad_unread_ranges() { | |
| // 1) C struct with unsupported (`double`) field and exposed padding fails | |
| // closed even when Rust fields omit `ty`. | |
| let cpp_unsup = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "unsup.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.HasFloat", | |
| "name": "HasFloat", | |
| "is_union": false, | |
| "size_bytes": 16, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "tag", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } }, | |
| { "name": "d", "offset_bits": 64, "bit_width": null, "ty": { "kind": "unsupported", "desc": "double" } } | |
| ] | |
| } | |
| ], | |
| "functions": [] | |
| }"#; | |
| let rust_no_ty = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "HasFloat", | |
| "cpp_record": "struct.HasFloat", | |
| "repr": "C", | |
| "size_bytes": 16, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1 }, | |
| { "name": "d", "offset_bytes": 8, "size_bytes": 8 } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp_unsup).unwrap(); | |
| let res = check_ffi(&bundle, &parse_rust_descriptor_json(rust_no_ty).unwrap()); | |
| assert!(!res.report.ok); | |
| assert!(has_code(&res.report.diagnostics, DiagnosticCode::UnsupportedType)); | |
| assert!(has_code(&res.report.diagnostics, DiagnosticCode::MissingAbiField)); | |
| // 2) `is_padding: true` with initialized `[u8; 3]` is rejected at parse | |
| // time. | |
| let fake_pad_json = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "BadPad", | |
| "cpp_record": "struct.pair", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "lo", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "hi", "offset_bytes": 4, "size_bytes": 2, "ty": { "kind": "int", "bits": 16, "signed": false } }, | |
| { "name": "_pad", "offset_bytes": 6, "size_bytes": 2, "is_padding": true, "ty": { "kind": "array", "elem": { "kind": "int", "bits": 8, "signed": false }, "len": 2 } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| assert!(parse_rust_descriptor_json(fake_pad_json).is_err()); | |
| // 3) Out-of-bounds (huge `end_byte`) and value-overlapping | |
| // `unread_padding_ranges` fail closed without OOM. | |
| let bad_unread = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Pair", | |
| "cpp_record": "struct.pair", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "lo", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "hi", "offset_bytes": 4, "size_bytes": 2, "ty": { "kind": "int", "bits": 16, "signed": false } } | |
| ], | |
| "unread_padding_ranges": [ | |
| { "start_byte": 0, "end_byte": 4 }, | |
| { "start_byte": 6, "end_byte": 999999999999 } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let pair_bundle = parse_cpp_bundle(C11_PAIR_BUNDLE).unwrap(); | |
| let res_unread = check_ffi(&pair_bundle, &parse_rust_descriptor_json(bad_unread).unwrap()); | |
| assert!(!res_unread.report.ok); | |
| assert!(has_code(&res_unread.report.diagnostics, DiagnosticCode::PaddingOverlapsValue)); | |
| assert!(has_code(&res_unread.report.diagnostics, DiagnosticCode::TailPaddingExposed)); | |
| } | |
| #[test] | |
| fn enforces_by_value_record_layout_presence_array_recursion_and_packed_sysv_abi() { | |
| // Packed 9 B struct (`tag: u8, val: u64`) has an unaligned field so SysV | |
| // AMD64 ABI §3.2.3p2 classifies it as MEMORY (`indirect`, `byval: true`). | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "byval.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Packed9", | |
| "name": "Packed9", | |
| "is_union": false, | |
| "is_packed": true, | |
| "size_bytes": 9, | |
| "align_bytes": 1, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "tag", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } }, | |
| { "name": "val", "offset_bits": 8, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Poly", | |
| "name": "Poly", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "is_cxx_record": true, | |
| "is_polymorphic": true, | |
| "is_trivially_copyable": false, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [] | |
| }, | |
| { | |
| "id": "struct.WrapPolyArr", | |
| "name": "WrapPolyArr", | |
| "is_union": false, | |
| "size_bytes": 16, | |
| "align_bytes": 8, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "arr", "offset_bits": 0, "bit_width": null, "ty": { "kind": "array", "elem": { "kind": "record", "id": "struct.Poly" }, "len": 2 } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "take_packed9", | |
| "unmangled_name": "take_packed9", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "p", "ty": { "kind": "record", "id": "struct.Packed9" } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "indirect", "byval": true } ] | |
| } | |
| }, | |
| { | |
| "symbol": "take_poly_arr", | |
| "unmangled_name": "take_poly_arr", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "w", "ty": { "kind": "record", "id": "struct.WrapPolyArr" } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "indirect", "byval": true } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| // Positive: packed 9 B struct with layout entry passes as MEMORY/byval. | |
| let rust_packed_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Packed9", | |
| "cpp_record": "struct.Packed9", | |
| "repr": "C", | |
| "is_packed": true, | |
| "size_bytes": 9, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "val", "offset_bytes": 1, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "take_packed9", | |
| "cpp_symbol": "take_packed9", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "p", "ty": { "kind": "record", "name": "struct.Packed9" } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let ok = check_ffi(&bundle, &parse_rust_descriptor_json(rust_packed_ok).unwrap()); | |
| assert!(ok.report.ok, "unexpected: {:?}", ok.report.diagnostics); | |
| // Negative: omitting `Packed9` from `rust_ffi.records` fails closed, and | |
| // nested `[Poly; 2]` inside `WrapPolyArr` is caught recursively. | |
| let rust_missing_and_arr = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [], | |
| "functions": [ | |
| { | |
| "rust_name": "take_packed9", | |
| "cpp_symbol": "take_packed9", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "p", "ty": { "kind": "record", "name": "struct.Packed9" } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "take_poly_arr", | |
| "cpp_symbol": "take_poly_arr", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "w", "ty": { "kind": "record", "name": "struct.WrapPolyArr" } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bad = check_ffi(&bundle, &parse_rust_descriptor_json(rust_missing_and_arr).unwrap()); | |
| assert!(!bad.report.ok); | |
| assert!(has_code(&bad.report.diagnostics, DiagnosticCode::MissingAbiField)); | |
| assert!(has_code(&bad.report.diagnostics, DiagnosticCode::PolymorphicRecordByValue)); | |
| // Non-SysV-x86_64 by-value record without explicit `abi_witness` fails | |
| // closed. | |
| let cpp_aarch64 = cpp.replace("x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"); | |
| let rust_aarch64 = | |
| rust_packed_ok.replace("x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"); | |
| let res_a64 = check_ffi( | |
| &parse_cpp_bundle(&cpp_aarch64).unwrap(), | |
| &parse_rust_descriptor_json(&rust_aarch64).unwrap(), | |
| ); | |
| assert!(!res_a64.report.ok); | |
| assert!(has_code(&res_a64.report.diagnostics, DiagnosticCode::MissingAbiField)); | |
| } | |
| #[test] | |
| fn hardens_lean_emitter_constness_injection_namespaces_and_dedup() { | |
| // 1) Missing `is_const` on a C++ field fails closed in `emit_lean_parity`. | |
| let no_const_bundle = C11_PAIR_BUNDLE.replacen("\"is_const\": false,\n", "", 2); | |
| let rust_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Pair", | |
| "cpp_record": "struct.pair", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "lo", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "hi", "offset_bytes": 4, "size_bytes": 2, "ty": { "kind": "int", "bits": 16, "signed": false } }, | |
| { "name": "_pad", "offset_bytes": 6, "size_bytes": 2, "ty": { "kind": "maybe_uninit_bytes", "len": 2 } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let outcome = check_ffi( | |
| &parse_cpp_bundle(&no_const_bundle).unwrap(), | |
| &parse_rust_descriptor_json(rust_ok).unwrap(), | |
| ); | |
| assert!(outcome.report.ok); | |
| assert!(emit_lean_parity( | |
| "Anneal.FfiParity", | |
| &outcome.report.target_triple, | |
| &outcome.matched_records, | |
| ) | |
| .is_err()); | |
| // 2) Forbidden root namespaces and Lean keywords are rejected. | |
| for bad_ns in ["Trusted", "Trusted.FfiParity", "Init.Ffi", "Lean.Ffi", "Anneal.theorem"] { | |
| assert!( | |
| emit_lean_parity(bad_ns, "x86_64-unknown-linux-gnu", &[]).is_err(), | |
| "expected namespace `{bad_ns}` to be rejected" | |
| ); | |
| } | |
| // 3) Newline / bidi injection in target triple fails closed, and `-/` in a | |
| // record id is neutralized in comments so it cannot close `/-- ... -/`. | |
| assert!(emit_lean_parity("Anneal.FfiParity", "x86_64\n#eval 1", &[]).is_err()); | |
| assert!(emit_lean_parity("Anneal.FfiParity", "x86_64\u{202E}bad", &[]).is_err()); | |
| let ok_outcome = check_ffi( | |
| &parse_cpp_bundle(C11_PAIR_BUNDLE).unwrap(), | |
| &parse_rust_descriptor_json(rust_ok).unwrap(), | |
| ); | |
| let mut pair_with_slash = ok_outcome.matched_records[0].clone(); | |
| pair_with_slash.cpp.id = "struct.Foo -/ #eval 1 /-".to_owned(); | |
| let mut pair_dup = ok_outcome.matched_records[0].clone(); | |
| pair_dup.rust.rust_name = "Pair".to_owned(); | |
| let lean = emit_lean_parity( | |
| "Anneal.FfiParity", | |
| "x86_64-unknown-linux-gnu", | |
| &[pair_with_slash, pair_dup], | |
| ) | |
| .expect("sanitized comment and dedup emit"); | |
| assert!(lean.contains("Captured C/C++ record `struct.Foo -_ _eval 1 _-`")); | |
| assert!(lean.lines().filter(|l| l.starts_with("/--")).all(|l| !l.contains("-/ #eval"))); | |
| assert!(lean.contains("def cppRecord_Pair :")); | |
| assert!(lean.contains("def cppRecord_Pair_2 :")); | |
| } | |
| #[test] | |
| fn classifies_generic_unions_and_single_variant_enums_and_rejects_bad_maps() { | |
| let tricky_pts = "\ | |
| print-type-size type: `my_mod::GenU<u32>`: 8 bytes, alignment: 8 bytes | |
| print-type-size variant `GenU`: 8 bytes | |
| print-type-size field `.a`: 4 bytes | |
| print-type-size field `.b`: 8 bytes, offset: 0 bytes, alignment: 8 bytes | |
| print-type-size type: `SingleVarTwo`: 8 bytes, alignment: 4 bytes | |
| print-type-size variant `SingleVarTwo`: 8 bytes | |
| print-type-size field `.a`: 4 bytes | |
| print-type-size field `.b`: 4 bytes | |
| print-type-size type: `OneFieldU`: 4 bytes, alignment: 4 bytes | |
| print-type-size variant `OneFieldU`: 4 bytes | |
| print-type-size field `.x`: 4 bytes | |
| "; | |
| let parsed = parse_print_type_sizes(tricky_pts).expect("parses cleanly"); | |
| assert_eq!(parsed[0].kind, TypeSizeKind::Union); | |
| assert_eq!(parsed[0].short_name(), "GenU<u32>"); | |
| assert_eq!(parsed[0].base_short_name(), "GenU"); | |
| assert_eq!(parsed[1].kind, TypeSizeKind::Enum); | |
| assert_eq!(parsed[2].kind, TypeSizeKind::SingleVariantOrUnion); | |
| // Empty print-type-sizes fails closed. | |
| assert!(parse_print_type_sizes("warning: unused\n").is_err()); | |
| // Unmatched `--map` entry fails closed, and `--map` with `--rust-ffi` fails | |
| // closed. | |
| let mut maps = BTreeMap::new(); | |
| maps.insert("NoSuchType".to_owned(), "struct.pair".to_owned()); | |
| assert!(verify_ffi_inputs(C11_PAIR_BUNDLE, None, Some(tricky_pts), &maps).is_err()); | |
| assert!(verify_ffi_inputs( | |
| C11_PAIR_BUNDLE, | |
| Some( | |
| r#"{"schema_version":1,"target_triple":"x86_64-unknown-linux-gnu","pointer_width_bits":64,"c_char_is_signed":true}"# | |
| ), | |
| None, | |
| &maps | |
| ) | |
| .is_err()); | |
| // Cross-check fails closed when a descriptor record is missing from | |
| // `print-type-sizes` output. | |
| let missing_in_pts = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Pair", | |
| "cpp_record": "struct.pair", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "lo", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "hi", "offset_bytes": 4, "size_bytes": 2, "ty": { "kind": "int", "bits": 16, "signed": false } }, | |
| { "name": "_pad", "offset_bytes": 6, "size_bytes": 2, "ty": { "kind": "maybe_uninit_bytes", "len": 2 } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let res = verify_ffi_inputs( | |
| C11_PAIR_BUNDLE, | |
| Some(missing_in_pts), | |
| Some(tricky_pts), | |
| &BTreeMap::new(), | |
| ) | |
| .expect("cross-check runs"); | |
| assert!(!res.report.ok); | |
| assert!(has_code(&res.report.diagnostics, DiagnosticCode::PrintTypeSizesMismatch)); | |
| } | |
| #[test] | |
| fn enforces_direct_int_width_nested_unaligned_subobjects_and_short_names() { | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "nested_abi.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.TwoU64", | |
| "name": "TwoU64", | |
| "is_union": false, | |
| "size_bytes": 16, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "offset_bits": 64, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.OneU32", | |
| "name": "OneU32", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "x", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.P4", | |
| "name": "P4", | |
| "is_union": false, | |
| "is_packed": true, | |
| "size_bytes": 4, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "x", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.OuterPacked", | |
| "name": "OuterPacked", | |
| "is_union": false, | |
| "is_packed": false, | |
| "size_bytes": 5, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "tag", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } }, | |
| { "name": "p", "offset_bits": 8, "bit_width": null, "ty": { "kind": "record", "id": "struct.P4" } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Packed2", | |
| "name": "Packed2", | |
| "is_union": false, | |
| "is_packed": true, | |
| "size_bytes": 2, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } }, | |
| { "name": "b", "offset_bits": 8, "bit_width": null, "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "bad_16b_direct64", | |
| "unmangled_name": "bad_16b_direct64", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "v", "ty": { "kind": "record", "id": "struct.TwoU64" } } ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_int", "bits": 64 } ] | |
| } | |
| }, | |
| { | |
| "symbol": "bad_4b_direct64", | |
| "unmangled_name": "bad_4b_direct64", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "v", "ty": { "kind": "record", "id": "struct.OneU32" } } ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_int", "bits": 64 } ] | |
| } | |
| }, | |
| { | |
| "symbol": "take_outer_packed", | |
| "unmangled_name": "take_outer_packed", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "o", "ty": { "kind": "record", "id": "struct.OuterPacked" } } ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "indirect", "byval": true } ] | |
| } | |
| }, | |
| { | |
| "symbol": "take_packed2", | |
| "unmangled_name": "take_packed2", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "p", "ty": { "kind": "record", "id": "struct.Packed2" } } ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_int", "bits": 16 } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| // Positive: nested unaligned `OuterPacked` passes via `indirect(byval)` and | |
| // short-named aligned `Packed2` passes via `direct_int(16)`. | |
| let rust_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "P4", | |
| "cpp_record": "struct.P4", | |
| "repr": "C", | |
| "is_packed": true, | |
| "size_bytes": 4, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "x", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "OuterPacked", | |
| "cpp_record": "OuterPacked", | |
| "repr": "C", | |
| "is_packed": false, | |
| "size_bytes": 5, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "tag", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "p", "offset_bytes": 1, "size_bytes": 4, "ty": { "kind": "record", "name": "P4" } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "Packed2", | |
| "cpp_record": "Packed2", | |
| "repr": "C", | |
| "is_packed": true, | |
| "size_bytes": 2, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } }, | |
| { "name": "b", "offset_bytes": 1, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "take_outer_packed", | |
| "cpp_symbol": "take_outer_packed", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "o", "ty": { "kind": "record", "name": "OuterPacked" } } ] | |
| }, | |
| { | |
| "rust_name": "take_packed2", | |
| "cpp_symbol": "take_packed2", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "p", "ty": { "kind": "record", "name": "Packed2" } } ] | |
| } | |
| ] | |
| }"#; | |
| let ok = check_ffi(&bundle, &parse_rust_descriptor_json(rust_ok).unwrap()); | |
| assert!(ok.report.ok, "unexpected: {:?}", ok.report.diagnostics); | |
| // Negative: `DirectInt(64)` rejected for both 16 B and 4 B structs. | |
| let rust_bad_widths = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "TwoU64", | |
| "cpp_record": "struct.TwoU64", | |
| "repr": "C", | |
| "size_bytes": 16, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "b", "offset_bytes": 8, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "OneU32", | |
| "cpp_record": "struct.OneU32", | |
| "repr": "C", | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "x", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "bad_16b_direct64", | |
| "cpp_symbol": "bad_16b_direct64", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "v", "ty": { "kind": "record", "name": "TwoU64" } } ] | |
| }, | |
| { | |
| "rust_name": "bad_4b_direct64", | |
| "cpp_symbol": "bad_4b_direct64", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "v", "ty": { "kind": "record", "name": "OneU32" } } ] | |
| } | |
| ] | |
| }"#; | |
| let bad = check_ffi(&bundle, &parse_rust_descriptor_json(rust_bad_widths).unwrap()); | |
| assert!(!bad.report.ok); | |
| let abi_errs = bad | |
| .report | |
| .diagnostics | |
| .iter() | |
| .filter(|d| d.code == DiagnosticCode::AbiPassingMismatch) | |
| .count(); | |
| assert_eq!(abi_errs, 2, "diagnostics: {:?}", bad.report.diagnostics); | |
| } | |
| #[test] | |
| fn rejects_transparent_nonnull_c_ptr_vacuous_enum_uefi_root_ns_and_omitted_field_size() { | |
| // 1) `repr: "transparent"`, `non_null: true` against nullable C pointer, | |
| // and standalone `enums[]` missing from `RawCppBundle.enums` all fail. | |
| let rust_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Pair", | |
| "cpp_record": "struct.pair", | |
| "repr": "transparent", | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "lo", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "hi", "offset_bytes": 4, "size_bytes": 2, "ty": { "kind": "int", "bits": 16, "signed": false } }, | |
| { "name": "_pad", "offset_bytes": 6, "size_bytes": 2, "ty": { "kind": "maybe_uninit_bytes", "len": 2 } } | |
| ] | |
| } | |
| ], | |
| "enums": [ | |
| { | |
| "rust_name": "UnattestedEnum", | |
| "cpp_name": "UnattestedEnum", | |
| "repr_bits": 8, | |
| "repr_signed": false, | |
| "cpp_bits": 8, | |
| "cpp_signed": false | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "mix", | |
| "cpp_symbol": "mix", | |
| "abi": "C", | |
| "return_ty": { "kind": "int", "bits": 32, "signed": false }, | |
| "params": [ | |
| { | |
| "name": "dst", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "non_null": true, | |
| "pointee": { "kind": "record", "name": "Pair" } | |
| } | |
| }, | |
| { | |
| "name": "src", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": true, | |
| "pointee": { "kind": "record", "name": "Pair" } | |
| } | |
| } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let res = check_ffi( | |
| &parse_cpp_bundle(C11_PAIR_BUNDLE).unwrap(), | |
| &parse_rust_descriptor_json(rust_bad).unwrap(), | |
| ); | |
| assert!(!res.report.ok); | |
| assert_eq!(res.report.enums_checked, 0); | |
| assert!(has_code(&res.report.diagnostics, DiagnosticCode::ReprNotC)); | |
| assert!(has_code(&res.report.diagnostics, DiagnosticCode::PointerMutOrNullMismatch)); | |
| assert!(has_code(&res.report.diagnostics, DiagnosticCode::MissingAbiField)); | |
| // 2) UEFI, Cygwin, and ILP32/x32 triples are not misclassified as LP64 | |
| // System V x86-64. | |
| assert_eq!(crate::check::inferred_target_c_long_bits("x86_64-unknown-uefi"), Some(32)); | |
| assert_eq!(crate::check::inferred_target_c_long_bits("aarch64-unknown-uefi"), Some(32)); | |
| assert_eq!(crate::check::inferred_target_c_long_bits("x86_64-unknown-linux-gnux32"), Some(32)); | |
| assert_eq!( | |
| crate::check::inferred_target_c_long_bits("aarch64-unknown-linux-gnu_ilp32"), | |
| Some(32) | |
| ); | |
| assert_eq!(crate::check::inferred_target_c_long_bits("x86_64-pc-cygwin"), None); | |
| // 3) `_root_.Trusted.*`, `_private.*`, bare `_`, and reserved kernel/repo | |
| // roots are rejected by `is_valid_lean_namespace`. | |
| for bad_ns in [ | |
| "_root_.Trusted.Injected", | |
| "_private.Foo", | |
| "_", | |
| "Proof.Parity", | |
| "Expected.Parity", | |
| "Captured.Parity", | |
| "SpecPreflight.Parity", | |
| "TrustedBoundary.Parity", | |
| "Nat.Parity", | |
| "Lake.Parity", | |
| ] { | |
| assert!( | |
| !crate::lean_emit::is_valid_lean_namespace(bad_ns), | |
| "expected `{bad_ns}` to be rejected" | |
| ); | |
| } | |
| // 4) `cross_check_print_type_sizes` catches omitted `size_bytes` via `ty` | |
| // and rejects non-byte-multiple `repr_bits` (e.g. 9). | |
| let pts_union_u16 = "\ | |
| print-type-size type: `U`: 8 bytes, alignment: 8 bytes | |
| print-type-size variant `U`: 8 bytes | |
| print-type-size field `.a`: 2 bytes | |
| print-type-size field `.b`: 8 bytes, offset: 0 bytes, alignment: 8 bytes | |
| print-type-size type: `OddEnum`: 1 bytes, alignment: 1 bytes | |
| print-type-size discriminant: 1 bytes | |
| print-type-size variant `A`: 0 bytes | |
| "; | |
| let stale_desc = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "U", | |
| "cpp_record": "union.U", | |
| "repr": "C", | |
| "is_union": true, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "b", "offset_bytes": 0, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "enums": [ | |
| { | |
| "rust_name": "OddEnum", | |
| "cpp_name": "OddEnum", | |
| "repr_bits": 9, | |
| "repr_signed": false | |
| } | |
| ] | |
| }"#; | |
| let desc = parse_rust_descriptor_json(stale_desc).unwrap(); | |
| let ts = parse_print_type_sizes(pts_union_u16).unwrap(); | |
| let cross_diags = crate::cross_check_print_type_sizes(&desc, &ts); | |
| let mismatches = | |
| cross_diags.iter().filter(|d| d.code == DiagnosticCode::PrintTypeSizesMismatch).count(); | |
| assert!(mismatches >= 2, "expected field-size and odd-enum mismatches: {cross_diags:?}"); | |
| } | |
| #[test] | |
| fn enforces_symmetric_ptr_constness_asm_redirect_and_decl_only_unwind() { | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "sym_and_decl.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [], | |
| "functions": [ | |
| { | |
| "symbol": "inc_ref", | |
| "unmangled_name": "inc_ref", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { | |
| "name": "x", | |
| "ty": { | |
| "kind": "ref", | |
| "is_const": false, | |
| "is_rvalue": false, | |
| "may_alias": false, | |
| "pointee": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } | |
| } | |
| }, | |
| { | |
| "name": "ro", | |
| "ty": { | |
| "kind": "ref", | |
| "is_const": true, | |
| "is_rvalue": false, | |
| "may_alias": false, | |
| "pointee": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } | |
| } | |
| } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_ptr" }, { "kind": "direct_ptr" } ] | |
| } | |
| }, | |
| { | |
| "symbol": "fill_and_pp", | |
| "unmangled_name": "fill_and_pp", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { | |
| "name": "p", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "is_restrict": false, | |
| "may_alias": false, | |
| "pointee": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } | |
| } | |
| }, | |
| { | |
| "name": "pp", | |
| "ty": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "is_restrict": false, | |
| "may_alias": false, | |
| "pointee": { | |
| "kind": "ptr", | |
| "is_const": false, | |
| "is_restrict": false, | |
| "may_alias": false, | |
| "pointee": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } | |
| } | |
| } | |
| } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_ptr" }, { "kind": "direct_ptr" } ] | |
| } | |
| }, | |
| { | |
| "symbol": "foo_v2", | |
| "unmangled_name": "foo", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "direct_int", "bits": 32 }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "ext_decl", | |
| "unmangled_name": "ext_decl", | |
| "is_extern_c": true, | |
| "is_definition": false, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| // Positive: symmetric constness, exact redirected linker symbol `foo_v2`, | |
| // and declaration-only `ext_decl` bound via `extern "C-unwind"`. | |
| let rust_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "functions": [ | |
| { | |
| "rust_name": "inc_ref", | |
| "cpp_symbol": "inc_ref", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "ptr", "is_const": false, "non_null": true, "pointee": { "kind": "int", "bits": 32, "signed": true } } }, | |
| { "name": "ro", "ty": { "kind": "ptr", "is_const": true, "non_null": true, "pointee": { "kind": "int", "bits": 32, "signed": true } } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "foo_v2", | |
| "cpp_symbol": "foo_v2", | |
| "abi": "C", | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true }, | |
| "params": [] | |
| }, | |
| { | |
| "rust_name": "ext_decl", | |
| "cpp_symbol": "ext_decl", | |
| "abi": "C-unwind", | |
| "return_ty": { "kind": "void" }, | |
| "params": [] | |
| } | |
| ] | |
| }"#; | |
| let ok = check_ffi(&bundle, &parse_rust_descriptor_json(rust_ok).unwrap()); | |
| assert!(ok.report.ok, "unexpected: {:?}", ok.report.diagnostics); | |
| // Negative: mutable C++ `int&` -> Rust `&i32`, mutable C `uint8_t*` -> | |
| // Rust `*const u8`, nested `int**` -> `*mut *const i32`, redirected | |
| // `__asm__("foo_v2")` bound as `foo`, and declaration-only `ext_decl` | |
| // bound as `extern "C"`. | |
| let rust_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "functions": [ | |
| { | |
| "rust_name": "inc_ref", | |
| "cpp_symbol": "inc_ref", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "ptr", "is_const": true, "non_null": true, "pointee": { "kind": "int", "bits": 32, "signed": true } } }, | |
| { "name": "ro", "ty": { "kind": "ptr", "is_const": true, "non_null": true, "pointee": { "kind": "int", "bits": 32, "signed": true } } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "fill_and_pp", | |
| "cpp_symbol": "fill_and_pp", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "p", "ty": { "kind": "ptr", "is_const": true, "non_null": false, "pointee": { "kind": "int", "bits": 8, "signed": false } } }, | |
| { "name": "pp", "ty": { "kind": "ptr", "is_const": false, "non_null": false, "pointee": { "kind": "ptr", "is_const": true, "non_null": false, "pointee": { "kind": "int", "bits": 32, "signed": true } } } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "foo", | |
| "cpp_symbol": "foo", | |
| "abi": "C", | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true }, | |
| "params": [] | |
| }, | |
| { | |
| "rust_name": "ext_decl", | |
| "cpp_symbol": "ext_decl", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [] | |
| } | |
| ] | |
| }"#; | |
| let bad = check_ffi(&bundle, &parse_rust_descriptor_json(rust_bad).unwrap()); | |
| assert!(!bad.report.ok); | |
| let ptr_errs = bad | |
| .report | |
| .diagnostics | |
| .iter() | |
| .filter(|d| d.code == DiagnosticCode::PointerMutOrNullMismatch) | |
| .count(); | |
| assert_eq!(ptr_errs, 3, "diagnostics: {:?}", bad.report.diagnostics); | |
| assert!(has_code(&bad.report.diagnostics, DiagnosticCode::MissingCppFunction)); | |
| assert!(has_code(&bad.report.diagnostics, DiagnosticCode::UnwindAbiMismatch)); | |
| } | |
| #[test] | |
| fn validates_union_padding_templated_short_names_and_unique_type_size_fields() { | |
| // 1) Union validation rejects `is_padding` fields, out-of-bounds | |
| // `unread_padding_ranges`, exposed union-wide tail padding, and non-byte | |
| // `offset_bits` in both `check_ffi` and `emit_lean_parity`. | |
| let cpp_union = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "union_pad.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "union.TailU", | |
| "name": "TailU", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 2, | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 16, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "offset_bits": 0, "bit_width": null, "ty": { "kind": "array", "elem": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true }, "len": 3 } } | |
| ] | |
| }, | |
| { | |
| "id": "union.OddOffU", | |
| "name": "OddOffU", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "x", "offset_bits": 4, "bit_width": null, "is_const": false, "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [] | |
| }"#; | |
| let u_bundle = parse_cpp_bundle(cpp_union).unwrap(); | |
| let rust_u_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "TailU", | |
| "cpp_record": "union.TailU", | |
| "repr": "C", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 2, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 2, "ty": { "kind": "int", "bits": 16, "signed": false } }, | |
| { "name": "b", "offset_bytes": 0, "size_bytes": 3, "ty": { "kind": "array", "elem": { "kind": "int", "bits": 8, "signed": false }, "len": 3 } }, | |
| { "name": "_bogus", "offset_bytes": 8, "size_bytes": 16, "is_padding": true, "ty": { "kind": "maybe_uninit_bytes", "len": 16 } } | |
| ], | |
| "unread_padding_ranges": [ | |
| { "start_byte": 0, "end_byte": 99 } | |
| ] | |
| }, | |
| { | |
| "rust_name": "OddOffU", | |
| "cpp_record": "union.OddOffU", | |
| "repr": "C", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "x", "offset_bits": 4, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let u_desc = parse_rust_descriptor_json(rust_u_bad).unwrap(); | |
| let u_res = check_ffi(&u_bundle, &u_desc); | |
| assert!(!u_res.report.ok); | |
| assert!(has_code(&u_res.report.diagnostics, DiagnosticCode::PaddingOverlapsValue)); | |
| assert!(has_code(&u_res.report.diagnostics, DiagnosticCode::TailPaddingExposed)); | |
| assert!(has_code(&u_res.report.diagnostics, DiagnosticCode::FieldOffsetMismatch)); | |
| // Direct Lean emitter rejects non-byte-aligned `offset_bits`. | |
| let bad_pair = | |
| crate::MatchedRecord { rust: u_desc.records[1].clone(), cpp: u_bundle.records[1].clone() }; | |
| assert!(emit_lean_parity("Anneal.FfiParity", "x86_64-unknown-linux-gnu", &[bad_pair]).is_err()); | |
| // Union with bounded unread range `[3..4)` for its tail padding passes. | |
| let rust_u_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "TailU", | |
| "cpp_record": "union.TailU", | |
| "repr": "C", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 2, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 2, "ty": { "kind": "int", "bits": 16, "signed": false } }, | |
| { "name": "b", "offset_bytes": 0, "size_bytes": 3, "ty": { "kind": "array", "elem": { "kind": "int", "bits": 8, "signed": false }, "len": 3 } } | |
| ], | |
| "unread_padding_ranges": [ | |
| { "start_byte": 3, "end_byte": 4 } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let u_ok = check_ffi(&u_bundle, &parse_rust_descriptor_json(rust_u_ok).unwrap()); | |
| assert!(u_ok.report.ok, "unexpected: {:?}", u_ok.report.diagnostics); | |
| // 2) Short `cpp_record` name resolves templated/namespaced C++ record IDs | |
| // consistently across `check_type_parity`, `resolve_rust_record`, and | |
| // paired `print-type-sizes` size derivation. | |
| let cpp_tmpl = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "tmpl.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.ns::Box<int>", | |
| "name": "Box", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "v", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Holder", | |
| "name": "Holder", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "b", "offset_bits": 0, "bit_width": null, "ty": { "kind": "record", "id": "struct.ns::Box<int>" } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "take_box", | |
| "unmangled_name": "take_box", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "b", "ty": { "kind": "record", "id": "struct.ns::Box<int>" } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_int", "bits": 32 } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let pts_tmpl = "\ | |
| print-type-size type: `RustBox`: 4 bytes, alignment: 4 bytes | |
| print-type-size field `.v`: 4 bytes | |
| print-type-size type: `Holder`: 4 bytes, alignment: 4 bytes | |
| print-type-size field `.b`: 4 bytes | |
| "; | |
| let rust_tmpl = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "RustBox", | |
| "cpp_record": "Box", | |
| "repr": "C", | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "v", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": true } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "Holder", | |
| "cpp_record": "Holder", | |
| "repr": "C", | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "b", "offset_bytes": 0, "ty": { "kind": "record", "name": "struct.ns::Box<int>" } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "take_box", | |
| "cpp_symbol": "take_box", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "b", "ty": { "kind": "record", "name": "RustBox" } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let tmpl_res = verify_ffi_inputs(cpp_tmpl, Some(rust_tmpl), Some(pts_tmpl), &BTreeMap::new()) | |
| .expect("templated short-name check runs"); | |
| assert!(tmpl_res.report.ok, "unexpected: {:?}", tmpl_res.report.diagnostics); | |
| // 3) Duplicate descriptor field names on a union are rejected so no rustc | |
| // field can be skipped during `print-type-sizes` cross-checking. | |
| let pts_dup_union = "\ | |
| print-type-size type: `DupU`: 8 bytes, alignment: 8 bytes | |
| print-type-size variant `DupU`: 8 bytes | |
| print-type-size field `.a`: 4 bytes | |
| print-type-size field `.b`: 2 bytes, offset: 0 bytes | |
| print-type-size field `.c`: 8 bytes, offset: 0 bytes, alignment: 8 bytes | |
| "; | |
| let dup_desc = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "DupU", | |
| "cpp_record": "union.DupU", | |
| "repr": "C", | |
| "is_union": true, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } }, | |
| { "name": "c", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let dup_diags = crate::cross_check_print_type_sizes( | |
| &parse_rust_descriptor_json(dup_desc).unwrap(), | |
| &parse_print_type_sizes(pts_dup_union).unwrap(), | |
| ); | |
| assert!( | |
| dup_diags.iter().any(|d| d.message.contains("duplicate descriptor field `a`")), | |
| "expected duplicate field diagnostic: {dup_diags:?}" | |
| ); | |
| assert!( | |
| dup_diags.iter().any(|d| d.message.contains("field `b` is missing")), | |
| "expected uncovered rustc field `b` diagnostic: {dup_diags:?}" | |
| ); | |
| } | |
| #[test] | |
| fn validates_opaque_pointees_duplicate_ids_int_widths_and_lossy_abi() { | |
| // 1) Opaque Rust handles allow `void*`, `Record*`, and `Record&`, but | |
| // reject `T**`, `T*&`, `int*`, and `volatile`/unsupported pointees. | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "opaque_and_widths.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Guard", | |
| "name": "Guard", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": false, | |
| "has_non_trivial_dtor": true, | |
| "dtor_symbol": "_ZN5GuardD1Ev", | |
| "bases": [], | |
| "fields": [] | |
| }, | |
| { | |
| "id": "struct.BitInt24", | |
| "name": "BitInt24", | |
| "is_union": false, | |
| "is_packed": true, | |
| "size_bytes": 6, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "pad", "offset_bits": 0, "bit_width": null, "ty": { "kind": "array", "elem": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true }, "len": 3 } }, | |
| { "name": "x", "offset_bits": 24, "bit_width": null, "ty": { "kind": "int", "bits": 24, "signed": false, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "opaque_ok", | |
| "unmangled_name": "opaque_ok", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "v", "ty": { "kind": "ptr", "is_const": false, "is_restrict": false, "may_alias": false, "pointee": { "kind": "void" } } }, | |
| { "name": "g", "ty": { "kind": "ptr", "is_const": false, "is_restrict": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Guard" } } }, | |
| { "name": "r", "ty": { "kind": "ref", "is_const": false, "is_rvalue": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Guard" } } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_ptr" }, { "kind": "direct_ptr" }, { "kind": "direct_ptr" } ] | |
| } | |
| }, | |
| { | |
| "symbol": "opaque_bad", | |
| "unmangled_name": "opaque_bad", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "pp", "ty": { "kind": "ptr", "is_const": false, "is_restrict": false, "may_alias": false, "pointee": { "kind": "ptr", "is_const": false, "is_restrict": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Guard" } } } }, | |
| { "name": "rp", "ty": { "kind": "ref", "is_const": false, "is_rvalue": false, "may_alias": false, "pointee": { "kind": "ptr", "is_const": false, "is_restrict": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Guard" } } } }, | |
| { "name": "ip", "ty": { "kind": "ptr", "is_const": false, "is_restrict": false, "may_alias": false, "pointee": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } }, | |
| { "name": "vp", "ty": { "kind": "ptr", "is_const": false, "is_restrict": false, "may_alias": false, "pointee": { "kind": "unsupported", "desc": "volatile Guard" } } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_ptr" }, { "kind": "direct_ptr" }, { "kind": "direct_ptr" }, { "kind": "direct_ptr" } ] | |
| } | |
| }, | |
| { | |
| "symbol": "odd_widths", | |
| "unmangled_name": "odd_widths", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "int", "bits": 0, "signed": false, "is_char_like": false }, | |
| "params": [ | |
| { "name": "a", "ty": { "kind": "int", "bits": 7, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "ty": { "kind": "int", "bits": 24, "signed": false, "is_char_like": false } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "direct_int", "bits": 0 }, | |
| "param_passing": [ { "kind": "direct_int", "bits": 7 }, { "kind": "direct_int", "bits": 24 } ] | |
| } | |
| }, | |
| { | |
| "symbol": "lossy_abi", | |
| "unmangled_name": "lossy_abi", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "y", "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ | |
| { "kind": "other", "desc": "coerce_and_expand" }, | |
| { "kind": "direct_other", "desc": "" } | |
| ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| let rust_opaque_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "functions": [ | |
| { | |
| "rust_name": "opaque_ok", | |
| "cpp_symbol": "opaque_ok", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "v", "ty": { "kind": "ptr", "is_const": false, "pointee": { "kind": "opaque", "name": "c_void" } } }, | |
| { "name": "g", "ty": { "kind": "ptr", "is_const": false, "pointee": { "kind": "opaque", "name": "Guard" } } }, | |
| { "name": "r", "ty": { "kind": "ptr", "is_const": false, "non_null": true, "pointee": { "kind": "opaque", "name": "Guard" } } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let ok = check_ffi(&bundle, &parse_rust_descriptor_json(rust_opaque_ok).unwrap()); | |
| assert!(ok.report.ok, "unexpected: {:?}", ok.report.diagnostics); | |
| let rust_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "BitInt24", | |
| "cpp_record": "struct.BitInt24", | |
| "repr": "C", | |
| "is_packed": true, | |
| "size_bytes": 6, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "pad", "offset_bytes": 0, "size_bytes": 3, "ty": { "kind": "array", "elem": { "kind": "int", "bits": 8, "signed": false }, "len": 3 } }, | |
| { "name": "x", "offset_bytes": 3, "size_bytes": 3, "ty": { "kind": "int", "bits": 24, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "opaque_bad", | |
| "cpp_symbol": "opaque_bad", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "pp", "ty": { "kind": "ptr", "is_const": false, "pointee": { "kind": "opaque", "name": "Guard" } } }, | |
| { "name": "rp", "ty": { "kind": "ptr", "is_const": false, "non_null": true, "pointee": { "kind": "opaque", "name": "Guard" } } }, | |
| { "name": "ip", "ty": { "kind": "ptr", "is_const": false, "pointee": { "kind": "opaque", "name": "OpaqueInt" } } }, | |
| { "name": "vp", "ty": { "kind": "ptr", "is_const": false, "pointee": { "kind": "opaque", "name": "Guard" } } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "odd_widths", | |
| "cpp_symbol": "odd_widths", | |
| "abi": "C", | |
| "return_ty": { "kind": "int", "bits": 0, "signed": false }, | |
| "params": [ | |
| { "name": "a", "ty": { "kind": "int", "bits": 7, "signed": false } }, | |
| { "name": "b", "ty": { "kind": "int", "bits": 24, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "lossy_abi", | |
| "cpp_symbol": "lossy_abi", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "y", "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ], | |
| "abi_witness": { | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ | |
| { "kind": "other", "desc": "coerce_and_expand" }, | |
| { "kind": "direct_other", "desc": "" } | |
| ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let bad = check_ffi(&bundle, &parse_rust_descriptor_json(rust_bad).unwrap()); | |
| assert!(!bad.report.ok); | |
| let opaque_type_errs = bad | |
| .report | |
| .diagnostics | |
| .iter() | |
| .filter(|d| { | |
| d.subject.starts_with("fn:opaque_bad") && d.code == DiagnosticCode::FieldTypeMismatch | |
| }) | |
| .count(); | |
| assert_eq!(opaque_type_errs, 3, "diagnostics: {:?}", bad.report.diagnostics); | |
| assert!(bad.report.diagnostics.iter().any( | |
| |d| d.subject.starts_with("fn:opaque_bad") && d.code == DiagnosticCode::UnsupportedType | |
| )); | |
| assert!(bad.report.diagnostics.iter().any( | |
| |d| d.subject.starts_with("fn:odd_widths") && d.code == DiagnosticCode::UnsupportedType | |
| )); | |
| assert!(bad | |
| .report | |
| .diagnostics | |
| .iter() | |
| .any(|d| d.subject.starts_with("record:BitInt24") | |
| && d.code == DiagnosticCode::UnsupportedType)); | |
| let lossy_errs = bad | |
| .report | |
| .diagnostics | |
| .iter() | |
| .filter(|d| { | |
| d.subject.starts_with("fn:lossy_abi") && d.code == DiagnosticCode::AbiPassingMismatch | |
| }) | |
| .count(); | |
| assert_eq!(lossy_errs, 2, "diagnostics: {:?}", bad.report.diagnostics); | |
| // 2) Duplicate record IDs and function symbols fail closed instead of | |
| // overwriting earlier entries. | |
| let dup_cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "dup.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Dup", | |
| "name": "Dup", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "x", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Dup", | |
| "name": "Dup", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "x", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": true, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "dup_fn", | |
| "unmangled_name": "dup_fn", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "direct_int", "bits": 32 }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "dup_fn", | |
| "unmangled_name": "dup_fn", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "int", "bits": 64, "signed": true, "is_char_like": false }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "direct_int", "bits": 64 }, | |
| "param_passing": [] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let dup_rust = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Dup", | |
| "cpp_record": "struct.Dup", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "x", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": true } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "dup_fn", | |
| "cpp_symbol": "dup_fn", | |
| "abi": "C", | |
| "return_ty": { "kind": "int", "bits": 64, "signed": true }, | |
| "params": [] | |
| } | |
| ] | |
| }"#; | |
| let dup_res = check_ffi( | |
| &parse_cpp_bundle(dup_cpp).unwrap(), | |
| &parse_rust_descriptor_json(dup_rust).unwrap(), | |
| ); | |
| assert!(!dup_res.report.ok); | |
| assert!(has_code(&dup_res.report.diagnostics, DiagnosticCode::AmbiguousCppRecord)); | |
| assert!(has_code(&dup_res.report.diagnostics, DiagnosticCode::MissingCppFunction)); | |
| } | |
| #[test] | |
| fn accepts_clang_sret_byval_true_and_parses_union_end_padding_and_zst_unions() { | |
| // 1) Clang's `getNaturalAlignIndirect` emits `indirect { byval: true }` for | |
| // trivially-copyable >16 B sret returns on SysV x86-64; inferred checking | |
| // accepts it for returns while still rejecting `byval: false` on params. | |
| let cpp_sret = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "sret.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Big", | |
| "name": "Big", | |
| "is_union": false, | |
| "size_bytes": 24, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "offset_bits": 64, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "c", "offset_bits": 128, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "make_big", | |
| "unmangled_name": "make_big", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "record", "id": "struct.Big" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "record", "id": "struct.Big" } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "indirect", "byval": true }, | |
| "param_passing": [ { "kind": "indirect", "byval": true } ] | |
| } | |
| }, | |
| { | |
| "symbol": "bad_param_byval", | |
| "unmangled_name": "bad_param_byval", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "record", "id": "struct.Big" } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "indirect", "byval": false } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let rust_sret_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Big", | |
| "cpp_record": "struct.Big", | |
| "repr": "C", | |
| "size_bytes": 24, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "b", "offset_bytes": 8, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "c", "offset_bytes": 16, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "make_big", | |
| "cpp_symbol": "make_big", | |
| "abi": "C", | |
| "return_ty": { "kind": "record", "name": "Big" }, | |
| "params": [ | |
| { "name": "x", "ty": { "kind": "record", "name": "Big" } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let sret_bundle = parse_cpp_bundle(cpp_sret).unwrap(); | |
| let sret_ok = check_ffi(&sret_bundle, &parse_rust_descriptor_json(rust_sret_ok).unwrap()); | |
| assert!(sret_ok.report.ok, "unexpected: {:?}", sret_ok.report.diagnostics); | |
| let rust_sret_bad_param = rust_sret_ok | |
| .replace(r#""rust_name": "make_big""#, r#""rust_name": "bad_param_byval""#) | |
| .replace(r#""cpp_symbol": "make_big""#, r#""cpp_symbol": "bad_param_byval""#) | |
| .replace( | |
| r#""return_ty": { "kind": "record", "name": "Big" }"#, | |
| r#""return_ty": { "kind": "void" }"#, | |
| ); | |
| let sret_bad = | |
| check_ffi(&sret_bundle, &parse_rust_descriptor_json(&rust_sret_bad_param).unwrap()); | |
| assert!(!sret_bad.report.ok); | |
| assert!(has_code(&sret_bad.report.diagnostics, DiagnosticCode::AbiPassingMismatch)); | |
| // 2) `parse_print_type_sizes` computes union `end padding:` from max | |
| // variant end (`[3..4)` for `{ a: u16, b: [u8; 3] }`), and classifies a | |
| // 2-field union starting with a ZST field as `SingleVariantOrUnion` rather | |
| // than `Enum`. | |
| let pts = "\ | |
| print-type-size type: `TailU`: 4 bytes, alignment: 2 bytes | |
| print-type-size variant `TailU`: 4 bytes | |
| print-type-size field `.a`: 2 bytes | |
| print-type-size field `.b`: 3 bytes, offset: 0 bytes | |
| print-type-size end padding: 1 bytes | |
| print-type-size type: `ZstU`: 4 bytes, alignment: 4 bytes | |
| print-type-size variant `ZstU`: 4 bytes | |
| print-type-size field `.a`: 0 bytes | |
| print-type-size field `.b`: 4 bytes | |
| "; | |
| let parsed = parse_print_type_sizes(pts).expect("parses cleanly"); | |
| assert_eq!(parsed[0].kind, TypeSizeKind::Union); | |
| assert_eq!(parsed[0].padding.len(), 1); | |
| assert_eq!(parsed[0].padding[0].kind, PaddingKind::Tail); | |
| assert_eq!(parsed[0].padding[0].start_byte, 3); | |
| assert_eq!(parsed[0].padding[0].end_byte, 4); | |
| assert_eq!(parsed[1].kind, TypeSizeKind::SingleVariantOrUnion); | |
| let desc_u = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "TailU", | |
| "cpp_record": "union.TailU", | |
| "repr": "C", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 2, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 2, "ty": { "kind": "int", "bits": 16, "signed": false } }, | |
| { "name": "b", "offset_bytes": 0, "size_bytes": 3, "ty": { "kind": "array", "elem": { "kind": "int", "bits": 8, "signed": false }, "len": 3 } } | |
| ], | |
| "unread_padding_ranges": [ | |
| { "start_byte": 3, "end_byte": 4 } | |
| ] | |
| }, | |
| { | |
| "rust_name": "ZstU", | |
| "cpp_record": "union.ZstU", | |
| "repr": "C", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 0, "ty": { "kind": "void" } }, | |
| { "name": "b", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let cross_diags = | |
| crate::cross_check_print_type_sizes(&parse_rust_descriptor_json(desc_u).unwrap(), &parsed); | |
| assert!(cross_diags.is_empty(), "unexpected cross-check diagnostics: {cross_diags:?}"); | |
| } | |
| #[test] | |
| fn rejects_nonstandard_c_long_bits_and_mutable_const_ref_records() { | |
| // 1) Non-inferred target triple (`x86_64-pc-cygwin`) rejects unsupported | |
| // `c_long_bits: 24` across `check_target_policy`, `check_type_parity`, and | |
| // `print-type-sizes` field-size derivation. | |
| let cpp_cygwin = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "clong.c", | |
| "target_triple": "x86_64-pc-cygwin", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [], | |
| "functions": [ | |
| { | |
| "symbol": "take_long24", | |
| "unmangled_name": "take_long24", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "n", "ty": { "kind": "int", "bits": 24, "signed": true, "is_char_like": false } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_int", "bits": 24 } ] | |
| } | |
| }, | |
| { | |
| "symbol": "take_long64", | |
| "unmangled_name": "take_long64", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "n", "ty": { "kind": "int", "bits": 64, "signed": true, "is_char_like": false } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_int", "bits": 64 } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let cyg_bundle = parse_cpp_bundle(cpp_cygwin).unwrap(); | |
| let rust_long24 = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-pc-cygwin", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "c_long_bits": 24, | |
| "functions": [ | |
| { | |
| "rust_name": "take_long24", | |
| "cpp_symbol": "take_long24", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "n", "ty": { "kind": "c_long" } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bad_long = check_ffi(&cyg_bundle, &parse_rust_descriptor_json(rust_long24).unwrap()); | |
| assert!(!bad_long.report.ok); | |
| assert!(has_code(&bad_long.report.diagnostics, DiagnosticCode::CLongWidthMismatch)); | |
| assert!(has_code(&bad_long.report.diagnostics, DiagnosticCode::UnsupportedType)); | |
| let pts_long = "\ | |
| print-type-size type: `HoldLong`: 3 bytes, alignment: 1 bytes | |
| print-type-size field `.n`: 3 bytes | |
| "; | |
| let desc_long24 = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-pc-cygwin", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "c_long_bits": 24, | |
| "records": [ | |
| { | |
| "rust_name": "HoldLong", | |
| "cpp_record": "struct.HoldLong", | |
| "repr": "C", | |
| "size_bytes": 3, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "n", "offset_bytes": 0, "ty": { "kind": "c_long" } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let pts_long_diags = crate::cross_check_print_type_sizes( | |
| &parse_rust_descriptor_json(desc_long24).unwrap(), | |
| &parse_print_type_sizes(pts_long).unwrap(), | |
| ); | |
| assert!(has_code(&pts_long_diags, DiagnosticCode::PrintTypeSizesMismatch)); | |
| let rust_long64 = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-pc-cygwin", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "c_long_bits": 64, | |
| "functions": [ | |
| { | |
| "rust_name": "take_long64", | |
| "cpp_symbol": "take_long64", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "n", "ty": { "kind": "c_long" } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let ok_long = check_ffi(&cyg_bundle, &parse_rust_descriptor_json(rust_long64).unwrap()); | |
| assert!(ok_long.report.ok, "unexpected: {:?}", ok_long.report.diagnostics); | |
| // 2) C++ `const T&` bound to Rust `&T` rejects records with `has_mutable` | |
| // set to true or omitted, while accepting `has_mutable: false`. | |
| let cpp_mut = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "mutable_ref.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Cached", | |
| "name": "Cached", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "has_mutable": true, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "value", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } }, | |
| { "name": "hits", "offset_bits": 32, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Unattested", | |
| "name": "Unattested", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "v", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Pure", | |
| "name": "Pure", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "has_mutable": false, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "v", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "read_cached", | |
| "unmangled_name": "read_cached", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "c", "ty": { "kind": "ref", "is_const": true, "is_rvalue": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Cached" } } }, | |
| { "name": "o", "ty": { "kind": "ref", "is_const": true, "is_rvalue": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Cached" } } }, | |
| { "name": "u", "ty": { "kind": "ref", "is_const": true, "is_rvalue": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Unattested" } } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_ptr" }, { "kind": "direct_ptr" }, { "kind": "direct_ptr" } ] | |
| } | |
| }, | |
| { | |
| "symbol": "read_pure", | |
| "unmangled_name": "read_pure", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "p", "ty": { "kind": "ref", "is_const": true, "is_rvalue": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Pure" } } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_ptr" } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let mut_bundle = parse_cpp_bundle(cpp_mut).unwrap(); | |
| let rust_mut_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Cached", | |
| "cpp_record": "struct.Cached", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "value", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": true } }, | |
| { "name": "hits", "offset_bytes": 4, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": true } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "Unattested", | |
| "cpp_record": "struct.Unattested", | |
| "repr": "C", | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "v", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": true } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "read_cached", | |
| "cpp_symbol": "read_cached", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "c", "ty": { "kind": "ptr", "is_const": true, "non_null": true, "pointee": { "kind": "record", "name": "Cached" } } }, | |
| { "name": "o", "ty": { "kind": "ptr", "is_const": true, "non_null": true, "pointee": { "kind": "opaque", "name": "Cached" } } }, | |
| { "name": "u", "ty": { "kind": "ptr", "is_const": true, "non_null": true, "pointee": { "kind": "record", "name": "Unattested" } } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let bad_mut = check_ffi(&mut_bundle, &parse_rust_descriptor_json(rust_mut_bad).unwrap()); | |
| assert!(!bad_mut.report.ok); | |
| let mut_ptr_errs = bad_mut | |
| .report | |
| .diagnostics | |
| .iter() | |
| .filter(|d| d.code == DiagnosticCode::PointerMutOrNullMismatch) | |
| .count(); | |
| assert_eq!(mut_ptr_errs, 2, "diagnostics: {:?}", bad_mut.report.diagnostics); | |
| assert!(has_code(&bad_mut.report.diagnostics, DiagnosticCode::MissingAbiField)); | |
| let rust_pure_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "Pure", | |
| "cpp_record": "struct.Pure", | |
| "repr": "C", | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "v", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": true } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "read_pure", | |
| "cpp_symbol": "read_pure", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "p", "ty": { "kind": "ptr", "is_const": true, "non_null": true, "pointee": { "kind": "record", "name": "Pure" } } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let ok_pure = check_ffi(&mut_bundle, &parse_rust_descriptor_json(rust_pure_ok).unwrap()); | |
| assert!(ok_pure.report.ok, "unexpected: {:?}", ok_pure.report.diagnostics); | |
| } | |
| #[test] | |
| fn validates_pointer_width_field_bounds_disjointness_align_and_base_mutability() { | |
| // 1) Unsupported `pointer_width_bits: 24` and triple-inconsistent | |
| // `x86_64` with 32-bit pointers both fail closed. | |
| let cpp_pw24 = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "pw.c", | |
| "target_triple": "custom-unknown-none", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 24, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.PtrWrap", | |
| "name": "PtrWrap", | |
| "is_union": false, | |
| "size_bytes": 3, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "p", "offset_bits": 0, "bit_width": null, "ty": { "kind": "ptr", "is_const": false, "is_restrict": false, "may_alias": false, "pointee": { "kind": "void" } } } | |
| ] | |
| } | |
| ], | |
| "functions": [] | |
| }"#; | |
| let rust_pw24 = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "custom-unknown-none", | |
| "pointer_width_bits": 24, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "PtrWrap", | |
| "cpp_record": "struct.PtrWrap", | |
| "repr": "C", | |
| "size_bytes": 3, | |
| "align_bytes": 1, | |
| "fields": [ | |
| { "name": "p", "offset_bytes": 0, "size_bytes": 3, "ty": { "kind": "ptr", "is_const": false, "pointee": { "kind": "opaque", "name": "c_void" } } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let res_pw24 = check_ffi( | |
| &parse_cpp_bundle(cpp_pw24).unwrap(), | |
| &parse_rust_descriptor_json(rust_pw24).unwrap(), | |
| ); | |
| assert!(!res_pw24.report.ok); | |
| assert!(has_code(&res_pw24.report.diagnostics, DiagnosticCode::PointerWidthMismatch)); | |
| assert!(has_code(&res_pw24.report.diagnostics, DiagnosticCode::UnsupportedType)); | |
| // 2) `x86_64-unknown-linux-gnu` with `pointer_width_bits: 32` is rejected | |
| // as inconsistent with the target triple and refuses 64-bit SysV inference. | |
| let cpp_x64_pw32 = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "x64_32.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 32, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.S4", | |
| "name": "S4", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "x", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": false, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "take_s4", | |
| "unmangled_name": "take_s4", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "s", "ty": { "kind": "record", "id": "struct.S4" } } ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_ptr" } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let rust_x64_pw32 = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 32, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "S4", | |
| "cpp_record": "struct.S4", | |
| "repr": "C", | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "x", "offset_bytes": 0, "size_bytes": 4, "ty": { "kind": "int", "bits": 32, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "rust_name": "take_s4", | |
| "cpp_symbol": "take_s4", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ { "name": "s", "ty": { "kind": "record", "name": "S4" } } ] | |
| } | |
| ] | |
| }"#; | |
| let res_x64_32 = check_ffi( | |
| &parse_cpp_bundle(cpp_x64_pw32).unwrap(), | |
| &parse_rust_descriptor_json(rust_x64_pw32).unwrap(), | |
| ); | |
| assert!(!res_x64_32.report.ok); | |
| assert!(has_code(&res_x64_32.report.diagnostics, DiagnosticCode::PointerWidthMismatch)); | |
| assert!(has_code(&res_x64_32.report.diagnostics, DiagnosticCode::MissingAbiField)); | |
| // 3) Field exceeding `size_bytes`, overlapping non-union struct fields, | |
| // non-power-of-two `align_bytes` (3), and `size_bytes % align_bytes != 0` | |
| // all fail closed. | |
| let cpp_bad_geom = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "geom_bounds.c", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": false, | |
| "lang_standard": "c11", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.OobField", | |
| "name": "OobField", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "big", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "union.OobUnion", | |
| "name": "OobUnion", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "big", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Overlap", | |
| "name": "Overlap", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "offset_bits": 40, "bit_width": null, "ty": { "kind": "int", "bits": 8, "signed": false, "is_char_like": true } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.BadAlign", | |
| "name": "BadAlign", | |
| "is_union": false, | |
| "size_bytes": 6, | |
| "align_bytes": 3, | |
| "fields": [] | |
| }, | |
| { | |
| "id": "struct.UnmultSize", | |
| "name": "UnmultSize", | |
| "is_union": false, | |
| "size_bytes": 6, | |
| "align_bytes": 4, | |
| "fields": [] | |
| } | |
| ], | |
| "functions": [] | |
| }"#; | |
| let rust_bad_geom = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "records": [ | |
| { | |
| "rust_name": "OobField", | |
| "cpp_record": "struct.OobField", | |
| "repr": "C", | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "big", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "OobUnion", | |
| "cpp_record": "union.OobUnion", | |
| "repr": "C", | |
| "is_union": true, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "big", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "Overlap", | |
| "cpp_record": "struct.Overlap", | |
| "repr": "C", | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "b", "offset_bytes": 5, "size_bytes": 1, "ty": { "kind": "int", "bits": 8, "signed": false } } | |
| ] | |
| }, | |
| { | |
| "rust_name": "BadAlign", | |
| "cpp_record": "struct.BadAlign", | |
| "repr": "C", | |
| "size_bytes": 6, | |
| "align_bytes": 3, | |
| "fields": [] | |
| }, | |
| { | |
| "rust_name": "UnmultSize", | |
| "cpp_record": "struct.UnmultSize", | |
| "repr": "C", | |
| "size_bytes": 6, | |
| "align_bytes": 4, | |
| "fields": [] | |
| } | |
| ] | |
| }"#; | |
| let res_geom = check_ffi( | |
| &parse_cpp_bundle(cpp_bad_geom).unwrap(), | |
| &parse_rust_descriptor_json(rust_bad_geom).unwrap(), | |
| ); | |
| assert!(!res_geom.report.ok); | |
| assert!(res_geom | |
| .report | |
| .diagnostics | |
| .iter() | |
| .any(|d| d.subject.contains("OobField") && d.code == DiagnosticCode::FieldSizeMismatch)); | |
| assert!(res_geom | |
| .report | |
| .diagnostics | |
| .iter() | |
| .any(|d| d.subject.contains("OobUnion") && d.code == DiagnosticCode::FieldSizeMismatch)); | |
| assert!(res_geom | |
| .report | |
| .diagnostics | |
| .iter() | |
| .any(|d| d.subject.contains("Overlap") && d.code == DiagnosticCode::FieldOffsetMismatch)); | |
| assert!(res_geom | |
| .report | |
| .diagnostics | |
| .iter() | |
| .any(|d| d.subject.contains("BadAlign") && d.code == DiagnosticCode::AlignMismatch)); | |
| assert!(res_geom | |
| .report | |
| .diagnostics | |
| .iter() | |
| .any(|d| d.subject.contains("UnmultSize") && d.code == DiagnosticCode::SizeMismatch)); | |
| // 4) `cpp_type_mutable_status` traverses `crec.bases` for `const Derived&` | |
| // bound to opaque `&OpaqueDerived`. | |
| let cpp_base_mut = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "base_mut.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.MutBase", | |
| "name": "MutBase", | |
| "is_union": false, | |
| "size_bytes": 4, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "has_mutable": true, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "hits", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| }, | |
| { | |
| "id": "struct.Derived", | |
| "name": "Derived", | |
| "is_union": false, | |
| "size_bytes": 8, | |
| "align_bytes": 4, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "has_mutable": false, | |
| "bases": [ | |
| { "record_id": "struct.MutBase", "is_virtual": false, "offset_bytes": 0 } | |
| ], | |
| "fields": [ | |
| { "name": "x", "offset_bits": 32, "bit_width": null, "ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "inspect_derived", | |
| "unmangled_name": "inspect_derived", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "d", "ty": { "kind": "ref", "is_const": true, "is_rvalue": false, "may_alias": false, "pointee": { "kind": "record", "id": "struct.Derived" } } } | |
| ], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [ { "kind": "direct_ptr" } ] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let rust_base_mut = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "functions": [ | |
| { | |
| "rust_name": "inspect_derived", | |
| "cpp_symbol": "inspect_derived", | |
| "abi": "C", | |
| "return_ty": { "kind": "void" }, | |
| "params": [ | |
| { "name": "d", "ty": { "kind": "ptr", "is_const": true, "non_null": true, "pointee": { "kind": "opaque", "name": "Derived" } } } | |
| ] | |
| } | |
| ] | |
| }"#; | |
| let res_base = check_ffi( | |
| &parse_cpp_bundle(cpp_base_mut).unwrap(), | |
| &parse_rust_descriptor_json(rust_base_mut).unwrap(), | |
| ); | |
| assert!(!res_base.report.ok); | |
| assert!(has_code(&res_base.report.diagnostics, DiagnosticCode::PointerMutOrNullMismatch)); | |
| } | |
| #[test] | |
| fn enforces_transitive_unwind_closure_and_explicit_sret_byval_parity() { | |
| // 1) A `-fno-exceptions` definition calling a declaration-only callee | |
| // (or indirect call, or missing `body_stmt`) fails closed for `extern "C"` | |
| // and passes for `extern "C-unwind"`, while an in-TU helper chain passes. | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "unwind_closure.cc", | |
| "target_triple": "aarch64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": false, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [ | |
| { | |
| "id": "struct.Big", | |
| "name": "Big", | |
| "is_union": false, | |
| "size_bytes": 24, | |
| "align_bytes": 8, | |
| "is_cxx_record": true, | |
| "is_polymorphic": false, | |
| "is_trivially_copyable": true, | |
| "has_non_trivial_dtor": false, | |
| "dtor_symbol": null, | |
| "has_mutable": false, | |
| "bases": [], | |
| "fields": [ | |
| { "name": "a", "offset_bits": 0, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "b", "offset_bits": 64, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } }, | |
| { "name": "c", "offset_bits": 128, "bit_width": null, "ty": { "kind": "int", "bits": 64, "signed": false, "is_char_like": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { | |
| "symbol": "__throw_out_of_range", | |
| "unmangled_name": "__throw_out_of_range", | |
| "is_extern_c": false, | |
| "is_definition": false, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "in_tu_leaf", | |
| "unmangled_name": "in_tu_leaf", | |
| "is_extern_c": false, | |
| "is_definition": true, | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "calls_decl_only", | |
| "unmangled_name": "calls_decl_only", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { | |
| "kind": "expr", | |
| "expr": { | |
| "kind": "call", | |
| "callee_symbol": "__throw_out_of_range", | |
| "is_indirect": false, | |
| "is_virtual": false, | |
| "args": [] | |
| } | |
| } | |
| ] | |
| }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "calls_indirect", | |
| "unmangled_name": "calls_indirect", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { | |
| "kind": "expr", | |
| "expr": { | |
| "kind": "call", | |
| "callee_symbol": null, | |
| "is_indirect": true, | |
| "is_virtual": false, | |
| "args": [] | |
| } | |
| } | |
| ] | |
| }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "missing_body", | |
| "unmangled_name": "missing_body", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "make_big_safe", | |
| "unmangled_name": "make_big_safe", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { | |
| "kind": "expr", | |
| "expr": { | |
| "kind": "call", | |
| "callee_symbol": "in_tu_leaf", | |
| "is_indirect": false, | |
| "is_virtual": false, | |
| "args": [] | |
| } | |
| } | |
| ] | |
| }, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "record", "id": "struct.Big" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "indirect", "byval": true }, | |
| "param_passing": [] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| // Negative: declaration-only callee, indirect call, and omitted `body_stmt` | |
| // all fail with `UnwindAbiMismatch` when bound as `extern "C"`. | |
| let rust_unwind_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "aarch64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": false, | |
| "functions": [ | |
| { "rust_name": "calls_decl_only", "cpp_symbol": "calls_decl_only", "abi": "C", "return_ty": { "kind": "void" }, "params": [] }, | |
| { "rust_name": "calls_indirect", "cpp_symbol": "calls_indirect", "abi": "C", "return_ty": { "kind": "void" }, "params": [] }, | |
| { "rust_name": "missing_body", "cpp_symbol": "missing_body", "abi": "C", "return_ty": { "kind": "void" }, "params": [] } | |
| ] | |
| }"#; | |
| let bad = check_ffi(&bundle, &parse_rust_descriptor_json(rust_unwind_bad).unwrap()); | |
| assert!(!bad.report.ok); | |
| let unwind_errs = bad | |
| .report | |
| .diagnostics | |
| .iter() | |
| .filter(|d| d.code == DiagnosticCode::UnwindAbiMismatch) | |
| .count(); | |
| assert_eq!(unwind_errs, 3, "diagnostics: {:?}", bad.report.diagnostics); | |
| // Positive: `extern "C-unwind"` passes for declaration-only callees, and | |
| // `make_big_safe` (in-TU leaf callee + explicit AArch64 sret `byval: false` | |
| // vs Clang `byval: true`) passes cleanly! | |
| let rust_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "aarch64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": false, | |
| "records": [ | |
| { | |
| "rust_name": "Big", | |
| "cpp_record": "struct.Big", | |
| "repr": "C", | |
| "size_bytes": 24, | |
| "align_bytes": 8, | |
| "fields": [ | |
| { "name": "a", "offset_bytes": 0, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "b", "offset_bytes": 8, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } }, | |
| { "name": "c", "offset_bytes": 16, "size_bytes": 8, "ty": { "kind": "int", "bits": 64, "signed": false } } | |
| ] | |
| } | |
| ], | |
| "functions": [ | |
| { "rust_name": "calls_decl_only", "cpp_symbol": "calls_decl_only", "abi": "C-unwind", "return_ty": { "kind": "void" }, "params": [] }, | |
| { | |
| "rust_name": "make_big_safe", | |
| "cpp_symbol": "make_big_safe", | |
| "abi": "C", | |
| "return_ty": { "kind": "record", "name": "Big" }, | |
| "params": [], | |
| "abi_witness": { | |
| "return_passing": { "kind": "indirect", "byval": false }, | |
| "param_passing": [] | |
| } | |
| } | |
| ] | |
| }"#; | |
| let ok = check_ffi(&bundle, &parse_rust_descriptor_json(rust_ok).unwrap()); | |
| assert!(ok.report.ok, "unexpected: {:?}", ok.report.diagnostics); | |
| } | |
| #[test] | |
| fn audits_ctor_inits_member_base_dtors_libbuiltins_and_trivial_constructs() { | |
| let cpp = r#"{ | |
| "schema_version": 1, | |
| "tu_main_file": "ctor_dtor_builtins.cc", | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "lang_policy": { | |
| "char_is_signed": true, | |
| "exceptions_enabled": false, | |
| "fast_math": false, | |
| "is_cplusplus": true, | |
| "lang_standard": "c++20", | |
| "pointer_width_bits": 64, | |
| "signed_overflow": "iso_ub", | |
| "strict_aliasing": true | |
| }, | |
| "records": [], | |
| "functions": [ | |
| { | |
| "symbol": "ext_g", | |
| "unmangled_name": "ext_g", | |
| "is_extern_c": false, | |
| "is_definition": false, | |
| "is_variadic": false, | |
| "return_ty": { "kind": "int", "bits": 32, "signed": true, "is_char_like": false }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "direct_int", "bits": 32 }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "_ZN6ExtMemD1Ev", | |
| "unmangled_name": "~ExtMem", | |
| "is_extern_c": false, | |
| "is_definition": false, | |
| "is_variadic": false, | |
| "method_kind": "dtor", | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "_ZN7ExtBaseD1Ev", | |
| "unmangled_name": "~ExtBase", | |
| "is_extern_c": false, | |
| "is_definition": false, | |
| "is_variadic": false, | |
| "method_kind": "dtor", | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "_ZN3FooC1Ev", | |
| "unmangled_name": "Foo", | |
| "is_extern_c": false, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "method_kind": "ctor", | |
| "ctor_inits": [ | |
| { | |
| "kind": "field", | |
| "record_id": "struct.Foo", | |
| "field_idx": 0, | |
| "init": { | |
| "kind": "call", | |
| "callee_symbol": "ext_g", | |
| "is_indirect": false, | |
| "is_virtual": false, | |
| "args": [] | |
| } | |
| } | |
| ], | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "_ZN8DelegCtorC1Ev", | |
| "unmangled_name": "DelegCtor", | |
| "is_extern_c": false, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "method_kind": "ctor", | |
| "ctor_inits": [ | |
| { "kind": "unsupported", "desc": "delegating constructor" } | |
| ], | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "_ZN7HasMemD1Ev", | |
| "unmangled_name": "~HasMem", | |
| "is_extern_c": false, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "method_kind": "dtor", | |
| "member_dtors": [ | |
| { "field_idx": 0, "record_id": "struct.HasMem", "dtor_symbol": "_ZN6ExtMemD1Ev" } | |
| ], | |
| "base_dtors": [], | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "_ZN7HasBaseD1Ev", | |
| "unmangled_name": "~HasBase", | |
| "is_extern_c": false, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "method_kind": "dtor", | |
| "member_dtors": [], | |
| "base_dtors": [ | |
| { "record_id": "struct.ExtBase", "offset_bytes": 0, "dtor_symbol": "_ZN7ExtBaseD1Ev" } | |
| ], | |
| "body_stmt": { "kind": "compound", "stmts": [] }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { | |
| "cc": "c", | |
| "return_passing": { "kind": "ignore" }, | |
| "param_passing": [] | |
| } | |
| }, | |
| { | |
| "symbol": "via_ctor_init", | |
| "unmangled_name": "via_ctor_init", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { | |
| "kind": "expr", | |
| "expr": { "kind": "construct", "ctor_symbol": "_ZN3FooC1Ev", "is_trivial": false, "args": [] } | |
| } | |
| ] | |
| }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { "cc": "c", "return_passing": { "kind": "ignore" }, "param_passing": [] } | |
| }, | |
| { | |
| "symbol": "via_deleg_ctor", | |
| "unmangled_name": "via_deleg_ctor", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { | |
| "kind": "expr", | |
| "expr": { "kind": "construct", "ctor_symbol": "_ZN8DelegCtorC1Ev", "is_trivial": false, "args": [] } | |
| } | |
| ] | |
| }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { "cc": "c", "return_passing": { "kind": "ignore" }, "param_passing": [] } | |
| }, | |
| { | |
| "symbol": "via_member_dtor", | |
| "unmangled_name": "via_member_dtor", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { "kind": "decl", "name": "x", "dtor_symbol": "_ZN7HasMemD1Ev" } | |
| ] | |
| }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { "cc": "c", "return_passing": { "kind": "ignore" }, "param_passing": [] } | |
| }, | |
| { | |
| "symbol": "via_base_dtor", | |
| "unmangled_name": "via_base_dtor", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { "kind": "decl", "name": "x", "dtor_symbol": "_ZN7HasBaseD1Ev" } | |
| ] | |
| }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { "cc": "c", "return_passing": { "kind": "ignore" }, "param_passing": [] } | |
| }, | |
| { | |
| "symbol": "via_libbuiltin_longjmp", | |
| "unmangled_name": "via_libbuiltin_longjmp", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { | |
| "kind": "expr", | |
| "expr": { | |
| "kind": "call", | |
| "callee_symbol": "longjmp", | |
| "is_builtin": true, | |
| "is_indirect": false, | |
| "args": [] | |
| } | |
| } | |
| ] | |
| }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { "cc": "c", "return_passing": { "kind": "ignore" }, "param_passing": [] } | |
| }, | |
| { | |
| "symbol": "via_libbuiltin_new", | |
| "unmangled_name": "via_libbuiltin_new", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { | |
| "kind": "expr", | |
| "expr": { | |
| "kind": "call", | |
| "callee_symbol": "_Znwm", | |
| "is_builtin": true, | |
| "is_indirect": false, | |
| "args": [] | |
| } | |
| } | |
| ] | |
| }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { "cc": "c", "return_passing": { "kind": "ignore" }, "param_passing": [] } | |
| }, | |
| { | |
| "symbol": "via_trivial_ctor_and_expect", | |
| "unmangled_name": "via_trivial_ctor_and_expect", | |
| "is_extern_c": true, | |
| "is_definition": true, | |
| "is_variadic": false, | |
| "body_stmt": { | |
| "kind": "compound", | |
| "stmts": [ | |
| { | |
| "kind": "expr", | |
| "expr": { | |
| "kind": "construct", | |
| "ctor_symbol": "_ZN3PodC1ERKS_", | |
| "is_trivial": true, | |
| "args": [] | |
| } | |
| }, | |
| { | |
| "kind": "expr", | |
| "expr": { | |
| "kind": "call", | |
| "callee_symbol": "__builtin_expect", | |
| "is_builtin": true, | |
| "is_indirect": false, | |
| "args": [] | |
| } | |
| } | |
| ] | |
| }, | |
| "return_ty": { "kind": "void" }, | |
| "params": [], | |
| "abi_witness": { "cc": "c", "return_passing": { "kind": "ignore" }, "param_passing": [] } | |
| } | |
| ] | |
| }"#; | |
| let bundle = parse_cpp_bundle(cpp).unwrap(); | |
| // Negative: all 6 bad paths fail with `UnwindAbiMismatch`. | |
| let rust_bad = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "functions": [ | |
| { "rust_name": "via_ctor_init", "cpp_symbol": "via_ctor_init", "abi": "C", "return_ty": { "kind": "void" }, "params": [] }, | |
| { "rust_name": "via_deleg_ctor", "cpp_symbol": "via_deleg_ctor", "abi": "C", "return_ty": { "kind": "void" }, "params": [] }, | |
| { "rust_name": "via_member_dtor", "cpp_symbol": "via_member_dtor", "abi": "C", "return_ty": { "kind": "void" }, "params": [] }, | |
| { "rust_name": "via_base_dtor", "cpp_symbol": "via_base_dtor", "abi": "C", "return_ty": { "kind": "void" }, "params": [] }, | |
| { "rust_name": "via_libbuiltin_longjmp", "cpp_symbol": "via_libbuiltin_longjmp", "abi": "C", "return_ty": { "kind": "void" }, "params": [] }, | |
| { "rust_name": "via_libbuiltin_new", "cpp_symbol": "via_libbuiltin_new", "abi": "C", "return_ty": { "kind": "void" }, "params": [] } | |
| ] | |
| }"#; | |
| let bad = check_ffi(&bundle, &parse_rust_descriptor_json(rust_bad).unwrap()); | |
| assert!(!bad.report.ok); | |
| let errs = bad | |
| .report | |
| .diagnostics | |
| .iter() | |
| .filter(|d| d.code == DiagnosticCode::UnwindAbiMismatch) | |
| .count(); | |
| assert_eq!(errs, 6, "diagnostics: {:?}", bad.report.diagnostics); | |
| // Positive: trivial `construct` (`is_trivial: true`) + `__builtin_expect` | |
| // pass cleanly under `extern "C"`. | |
| let rust_ok = r#"{ | |
| "schema_version": 1, | |
| "target_triple": "x86_64-unknown-linux-gnu", | |
| "pointer_width_bits": 64, | |
| "c_char_is_signed": true, | |
| "functions": [ | |
| { "rust_name": "via_trivial_ctor_and_expect", "cpp_symbol": "via_trivial_ctor_and_expect", "abi": "C", "return_ty": { "kind": "void" }, "params": [] } | |
| ] | |
| }"#; | |
| let ok = check_ffi(&bundle, &parse_rust_descriptor_json(rust_ok).unwrap()); | |
| assert!(ok.report.ok, "unexpected: {:?}", ok.report.diagnostics); | |
| } |