Skip to content

Report variadic functions with a non-C ABI as unsupported - #4836

Merged
tautschnig merged 7 commits into
model-checking:mainfrom
feliperodri:fix-4817-variadic-non-c-abi
Sep 30, 2026
Merged

tautschnig merged 7 commits into
model-checking:mainfrom
feliperodri:fix-4817-variadic-non-c-abi

Conversation

@feliperodri

@feliperodri feliperodri commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

kani autoharness on a crate containing unsafe extern "sysv64" fn(_: ...) aborts with an assertion from inside the compiler itself (#4817):

assertion failed: !self.c_variadic || matches!(self.conv, CanonAbi::C)

rustc_public only models C-variadics whose calling convention canonicalizes to C, so asking any other variadic for its stable ABI takes the whole compilation down. Two things about the report are worth correcting: it isn't autoharness-specific — a hand-written harness calling such a function ICEs identically, autoharness just makes every function reachable — and it isn't variadics in general: extern "C" variadics work today and keep working.

So Kani now checks the internal ABI before asking for the stable one, and treats the unsupported shape as an unsupported construct: the function is declared from its signature with a stub body, and calls to it (or foreign declarations of it) report "Variadic function with a non-C calling convention is not currently supported by Kani". A harness that reaches one fails with that message; harnesses that don't still verify.

Why the internal ABI and not the extern "sysv64" string: the mapping from extern string to calling convention is target-dependent (extern "system" and extern "cdecl" canonicalize differently per platform, see rustc_target/src/spec/abi_map.rs), so a string check would be wrong on some targets.

Autoharness also reports these functions better now. It already skipped them, but blamed the wrong thing — "Missing Arbitrary implementation for argument(s) _: std::ffi::VaList<'_>", which reads as though an Arbitrary implementation would unblock them. It wouldn't; Kani cannot model the call at all. C-variadics keep the VaList reason, which is their real blocker: Kani can call one (tests/kani/FunctionCall/Variadic), it just cannot generate the argument.

This is a workaround for an upstream gap, not the end state — rustc_public could model these conventions instead of asserting, and its stable enum already has the X86_64SysV variant to do it. Filed upstream as rust-lang/rust#163233, and #4843 tracks what real support would take on our side (plus the separate VaList generation gap).

One thing worth a look: the test gating. The construct only exists on x86-64 — rustc rejects ... for every non-C ABI elsewhere, and sysv64 isn't even a valid ABI on aarch64 — and compiletest has no target directives, so the two x86-64-only assertions live inside check.sh while the expected file pins only what holds on every target. My first attempt was an expected-suite test, which promptly broke the aarch64 CI job. If you'd rather have target gating in compiletest, that's a better fix, but it's test infrastructure I didn't want to bundle in here.

Verified on x86-64, merged onto main 358baf8: the reported command completes, script-based-pre is 85/85 with the new test, expected 487/487, kani 617/617, cargo-kani 71/71, and C-variadics still verify.

Resolves #4817

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Non-C variadics can still trigger the compiler assertion through function pointers and reified foreign functions.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 2 High severity

Open (2)
What changed in this PR

Prevents compiler aborts for direct non-C variadic calls by reporting them as unsupported and improves autoharness diagnostics.

Changes:

  • Detects unsupported non-C variadic ABIs before stable ABI conversion.
  • Generates unsupported stubs for definitions, calls, and foreign declarations.
  • Adds target-gated regression and C-variadic compatibility coverage.
File Description
kani-compiler/​src/​kani_middle/​mod.rs Detects unsupported internal ABIs.
kani-compiler/​src/​kani_middle/​codegen_units.rs Adds the autoharness skip reason.
kani-compiler/​src/​codegen_cprover_gotoc/​context/​goto_ctx.rs Exposes detection through codegen context.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​typ.rs Avoids stable ABI conversion for declarations.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​statement.rs Reports unsupported direct calls.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​function.rs Generates unsupported function bodies.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​foreign_function.rs Adds foreign-function signature fallback.
kani-driver/​src/​autoharness/​mod.rs Displays the new skip reason.
kani_metadata/​src/​lib.rs Defines unsupported-variadic metadata.
tests/​script-based-pre/​autoharness_variadic_abi/​config.yml Configures regression test execution.
tests/​script-based-pre/​autoharness_variadic_abi/​check.sh Runs target-gated checks.
tests/​script-based-pre/​autoharness_variadic_abi/​expected Records portable expected output.
tests/​script-based-pre/​autoharness_variadic_abi/​variadic.rs Exercises autoharness diagnostics.
tests/​script-based-pre/​autoharness_variadic_abi/​calls_variadic.rs Exercises defined and foreign calls.
tests/​script-based-pre/​autoharness_variadic_abi/​c_variadic.rs Confirms C-variadic behavior remains unchanged.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

`kani autoharness` on a crate containing `unsafe extern "sysv64" fn(_: ...)`
aborted with an assertion from inside `rustc_public`:

    assertion failed: !self.c_variadic || matches!(self.conv, CanonAbi::C)

`rustc_public` only models C-variadics whose calling convention canonicalizes
to C, so asking any other variadic for its stable ABI takes the compilation
down. Rust allows those functions, and a hand-written harness that calls one
ICEd just the same -- autoharness only made it easy to hit, by making every
function reachable.

Check the *internal* ABI (exact about the calling convention, unlike the
`extern` string, which canonicalizes per target) before asking for the stable
one, and declare such a function from its signature with an
unsupported-construct body instead. Calls to it, and foreign declarations of
it, report the same unsupported construct, so a harness that cannot avoid one
fails with a message rather than crashing, and harnesses that never reach it
still verify.

C-variadics keep working (tests/kani/FunctionCall/Variadic).

Resolves model-checking#4817
Two follow-ups to the previous commit.

Autoharness already skipped these functions, but blamed the wrong thing:
"Missing Arbitrary implementation for argument(s) _: std::ffi::VaList<'_>",
which reads as though an `Arbitrary` implementation would unblock them. Kani
cannot model the call at all, so report the calling convention instead
(`AutoHarnessSkipReason::UnsupportedVariadic`). C-variadics keep the `VaList`
reason, which is accurate for them: Kani can call one, it just cannot generate
the argument.

The earlier `tests/expected/variadic-non-c-abi` test broke CI on aarch64,
where rustc rejects `...` for every non-C ABI, so the file did not compile.
Replaced by a script-based test that asserts the two x86-64-only behaviours
inside the script and leaves the `expected` file to the part that holds on
every target, since that file has to match everywhere.

The predicate moved to `kani_middle` so the backend and autoharness share one
definition.
Copilot review caught two shapes the first pass missed, and both still aborted
compilation:

- a call through `unsafe extern "sysv64" fn(u32, ...)`: the function-pointer
  branch of `codegen_funcall` converts `fn_abi_of_fn_ptr` with the same stable
  conversion that asserts. It now checks the internal ABI of the pointer's
  signature first, through a sibling of `is_unsupported_variadic` that takes a
  signature rather than an instance.
- reifying a foreign one without calling it: that builds an FFI shim, whose
  body reads the calling convention off the unavailable ABI. The shim now takes
  the convention from the declaration.

Two consequences of declaring these functions from their signature, both found
by running the new cases rather than by reading:

- CBMC rejects a function *definition* whose parameters are unnamed, and then
  whose parameter names are not symbols. Since Kani cannot model the body
  anyway, leave the symbol a declaration instead of giving it a stub body;
  calls are already replaced by unsupported-construct stubs, so nothing reaches
  it.
- the FFI shim does get a body, so its parameters are declared as named symbols
  the way the regular path declares them.

Taking the address of one without calling it now verifies, which is right: no
unsupported construct is reached. The new test asserts that too, so a future
change cannot turn address-taking into a spurious failure.
# Conflicts:
#	kani-compiler/src/kani_middle/codegen_units.rs
#	kani-compiler/src/kani_middle/mod.rs
# Conflicts:
#	kani-driver/src/autoharness/mod.rs
tautschnig and others added 2 commits September 30, 2026 10:40
`is_unsupported_variadic` runs on every direct call, every function codegen
and every function type, but almost no instance is variadic. Check the stable
signature's `c_variadic` flag first, so that only variadic instances pay for
the internal conversion and the `fn_abi_of_instance` lookup. Same for the
function-pointer variant.

Co-authored-by: Kiro <[email protected]>
`codegen_function` transformed the body and set up the current function only
to discard both for a non-C variadic. Check first, so neither the body
transformation nor `reset_current_fn` is needed on that path.

Co-authored-by: Kiro <[email protected]>
@tautschnig
tautschnig added this pull request to the merge queue Sep 30, 2026
Merged via the queue into model-checking:main with commit e8e1e1f Sep 30, 2026
32 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: autoharness: assertion failed: !self.c_variadic || matches!(self.conv, CanonAbi::C)

3 participants