From acce1ba942c443d88a56f505d80b34c7e4699944 Mon Sep 17 00:00:00 2001 From: Chris Alef Date: Sat, 3 Oct 2026 13:38:03 -0700 Subject: [PATCH] Keep Kani's machine model through the link, and make it right for Apple arm64 link_goto_binary passed kani_lib.c to goto-cc as C source. goto-cc compiles a C source with its own host configuration, and the resulting __CPROVER_architecture_* symbols replaced the machine model Kani wrote into the symbol table. CBMC reads those symbols back when it adds its C library, so the library was interpreted with goto-cc's configuration rather than Kani's. On aarch64 Linux that changed wchar_t_is_unsigned from Kani's 1 (correct: wchar_t is unsigned int there) to 0. When goto-cc links goto binaries the first input's symbols win, so kani_lib.c is now compiled on its own and its object linked after the Rust inputs. The compile is the one that already ran inside the link, so the cost is one more goto-cc process per harness. A --c-lib given as C source still overwrites the model; that is left as it is and commented. Keeping the model exposes a value the overwrite used to hide: the aarch64 model set char_is_unsigned for every OS, but Apple's arm64 ABI makes char signed, and Rust's c_char is i8 on aarch64-apple-darwin. goto-cc's host configuration happened to be right, so this change alone would have moved Apple Silicon runs onto the wrong value. The model now splits char on the OS, as it already does for long double and wchar_t. The new script-based test linked_machine_model compares every __CPROVER_architecture_* symbol in each harness's .symtab.out with the linked .out, with no host values hard-coded. Co-Authored-By: Claude Opus 5.5 --- .../compiler_interface.rs | 4 +- kani-driver/src/call_goto_cc.rs | 17 +++-- .../linked_machine_model/config.yml | 4 ++ .../linked_machine_model.expected | 2 + .../linked_machine_model.rs | 10 +++ .../linked_machine_model.sh | 67 +++++++++++++++++++ 6 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 tests/script-based-pre/linked_machine_model/config.yml create mode 100644 tests/script-based-pre/linked_machine_model/linked_machine_model.expected create mode 100644 tests/script-based-pre/linked_machine_model/linked_machine_model.rs create mode 100755 tests/script-based-pre/linked_machine_model/linked_machine_model.sh diff --git a/kani-compiler/src/codegen_cprover_gotoc/compiler_interface.rs b/kani-compiler/src/codegen_cprover_gotoc/compiler_interface.rs index 9c65098f..28347a55 100644 --- a/kani-compiler/src/codegen_cprover_gotoc/compiler_interface.rs +++ b/kani-compiler/src/codegen_cprover_gotoc/compiler_interface.rs @@ -873,7 +873,9 @@ fn new_machine_model(sess: &Session) -> MachineModel { } Arch::AArch64 => { let bool_width = 8; - let char_is_unsigned = true; + // Apple's arm64 ABI makes `char` signed, unlike AAPCS64 and Linux. + // https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms + let char_is_unsigned = matches!(os, Os::Linux); let char_width = 8; let double_width = 64; let float_width = 32; diff --git a/kani-driver/src/call_goto_cc.rs b/kani-driver/src/call_goto_cc.rs index b906c256..2ed1bd90 100644 --- a/kani-driver/src/call_goto_cc.rs +++ b/kani-driver/src/call_goto_cc.rs @@ -12,13 +12,22 @@ impl KaniSession { /// Given a set of goto binaries (`inputs`), produce `output` by linking everything /// together (including essential libraries). The result is generic over all proof harnesses. pub fn link_goto_binary(&self, inputs: &[PathBuf], output: &Path) -> Result<()> { + // Compile `kani_lib.c` on its own and link the object after the Rust inputs. When goto-cc + // links goto binaries, the first input's `__CPROVER_architecture_*` symbols win, so the + // machine model Kani wrote survives. A C source in the link command instead is compiled + // with goto-cc's own (host) configuration, which then overwrites that model, and CBMC + // loads its C library for the host rather than for the model Kani wrote. + let kani_lib = output.with_extension("kani_lib.o"); + let mut compile = Command::new("goto-cc"); + compile.arg("-c").arg(&self.kani_lib_c).arg("-o").arg(&kani_lib); + self.run_suppress(compile)?; + self.record_temporary_file(&kani_lib); + let mut args: Vec = Vec::new(); args.extend(inputs.iter().map(|x| x.clone().into_os_string())); + // A `--c-lib` given as C source still overwrites the model, for the reason above. args.extend(self.args.c_lib.iter().map(|x| x.clone().into_os_string())); - - // TODO think about this: kani_lib_c is just an empty c file. Maybe we could just - // create such an empty file ourselves instead of having to look up this path. - args.push(self.kani_lib_c.clone().into_os_string()); + args.push(kani_lib.into_os_string()); args.push("-o".into()); args.push(output.to_owned().into_os_string()); diff --git a/tests/script-based-pre/linked_machine_model/config.yml b/tests/script-based-pre/linked_machine_model/config.yml new file mode 100644 index 00000000..ef5bc7ce --- /dev/null +++ b/tests/script-based-pre/linked_machine_model/config.yml @@ -0,0 +1,4 @@ +# Copyright Kani Contributors +# SPDX-License-Identifier: Apache-2.0 OR MIT +script: linked_machine_model.sh +expected: linked_machine_model.expected diff --git a/tests/script-based-pre/linked_machine_model/linked_machine_model.expected b/tests/script-based-pre/linked_machine_model/linked_machine_model.expected new file mode 100644 index 00000000..479b2683 --- /dev/null +++ b/tests/script-based-pre/linked_machine_model/linked_machine_model.expected @@ -0,0 +1,2 @@ +VERIFICATION:- SUCCESSFUL +the linked goto binary keeps the machine model Kani wrote diff --git a/tests/script-based-pre/linked_machine_model/linked_machine_model.rs b/tests/script-based-pre/linked_machine_model/linked_machine_model.rs new file mode 100644 index 00000000..656a1d64 --- /dev/null +++ b/tests/script-based-pre/linked_machine_model/linked_machine_model.rs @@ -0,0 +1,10 @@ +// Copyright Kani Contributors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//! Any harness will do: the test inspects the goto binaries Kani writes for it, not its verdict. + +#[kani::proof] +fn check_linked_machine_model() { + let x: u8 = kani::any(); + assert!(u16::from(x) <= 255); +} diff --git a/tests/script-based-pre/linked_machine_model/linked_machine_model.sh b/tests/script-based-pre/linked_machine_model/linked_machine_model.sh new file mode 100755 index 00000000..e760a11d --- /dev/null +++ b/tests/script-based-pre/linked_machine_model/linked_machine_model.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# Copyright Kani Contributors +# SPDX-License-Identifier: Apache-2.0 OR MIT + +# Check that linking does not change the machine model Kani wrote. kani-compiler writes the +# target's model into each harness's symbol table as `__CPROVER_architecture_*` symbols, and CBMC +# reads those symbols from the linked binary to decide how to interpret C (`kani_lib.c`, its own C +# library, any `--c-lib`). If `goto-cc` compiles C source in the link step, its host configuration +# replaces those symbols. Every field Kani wrote must therefore reach the linked `.out` unchanged. +# No values are hard-coded, so the test runs on every host; it can only fail on a host whose +# `goto-cc` configuration differs from Kani's model for that target. +set -eu + +tmp_dir=$(mktemp -d) +trap 'rm -rf "${tmp_dir}"' EXIT +cp linked_machine_model.rs "${tmp_dir}" +cd "${tmp_dir}" + +kani linked_machine_model.rs --keep-temps + +# Print one `name value` line per `__CPROVER_architecture_*` symbol of a goto binary, with the +# `(__CPROVER_integer)` cast the link adds removed so that values compare as written. +machine_model() { + goto-instrument --show-symbol-table "$1" | awk ' + /^Symbol\.+: __CPROVER_architecture_/ { name = $2; next } + /^Symbol/ { name = "" } + /^Value\.+:/ && name != "" { + value = $0 + sub(/^Value\.+: /, "", value) + sub(/^\(__CPROVER_integer\)/, "", value) + print name, value + name = "" + }' | sort +} + +harnesses=0 +for symtab in *.symtab.out; do + linked="${symtab%.symtab.out}.out" + if [ ! -e "${linked}" ]; then + echo "error: no linked binary \`${linked}\` for \`${symtab}\`" + exit 1 + fi + machine_model "${symtab}" > written.txt + machine_model "${linked}" > linked.txt + if [ ! -s written.txt ]; then + echo "error: \`${symtab}\` has no __CPROVER_architecture_* symbols" + exit 1 + fi + # Every line Kani wrote must appear, unchanged, in the linked binary. The link may add fields + # Kani does not write (`os`, for one), so only `written.txt` lines are required. + missing=$(comm -23 written.txt linked.txt) + if [ -n "${missing}" ]; then + echo "error: linking \`${symtab}\` changed the machine model Kani wrote" + echo "written by Kani, then missing or changed in \`${linked}\`:" + echo "${missing}" + echo "the linked binary has:" + grep -F -f <(echo "${missing}" | cut -d' ' -f1 | sed 's/$/ /') linked.txt || true + exit 1 + fi + harnesses=$((harnesses + 1)) +done + +if [ "${harnesses}" -eq 0 ]; then + echo "error: no \`.symtab.out\` files; did --keep-temps stop keeping them?" + exit 1 +fi +echo "the linked goto binary keeps the machine model Kani wrote" -- 2.55.0