Skip to content

C-FFI: declare C functions returning void as void - #5171

Open
sliedes wants to merge 2 commits into
model-checking:mainfrom
sliedes:c-ffi-void-return
Open

sliedes wants to merge 2 commits into
model-checking:mainfrom
sliedes:c-ffi-void-return

Conversation

@sliedes

@sliedes sliedes commented Nov 14, 2026

Copy link
Copy Markdown

With -Z c-ffi --c-lib, a C function void f(int) declared in Rust as fn f(x: i32) could not be linked: Kani declared it as returning struct Unit, and goto-cc rejected the C definition as a conflicting declaration ("type classes differ"). The only workaround was to change the C to return struct Unit (as tests/kani/ForeignItems/lib.c did), which is not possible for unmodified C code.

Declare user C-FFI functions whose Rust return type is () as returning void. Direct calls already discard a unit result. When such a function is coerced to a Rust function pointer, cast its address to the Rust pointer type (returning struct Unit): calls through the pointer do not use the unit result, and CBMC's function pointer removal ignores the return type in that case, so the function remains a call target. The Rust allocation shims in kani_lib.c are unchanged.

tests/kani/ForeignItems/lib.c now defines its two void functions as void, and extern_fn_ptr.rs checks a call through a pointer to one.

Resolves #2151, resolves #1817

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

With `-Z c-ffi --c-lib`, a C function `void f(int)` declared in Rust as
`fn f(x: i32)` could not be linked: Kani declared it as returning
`struct Unit`, and goto-cc rejected the C definition as a conflicting
declaration ("type classes differ"). The only workaround was to change
the C to return `struct Unit` (as tests/kani/ForeignItems/lib.c did),
which is not possible for unmodified C code.

Declare user C-FFI functions whose Rust return type is `()` as returning
`void`. Direct calls already discard a unit result. When such a function
is coerced to a Rust function pointer, cast its address to the Rust
pointer type (returning `struct Unit`): calls through the pointer do not
use the unit result, and CBMC's function pointer removal ignores the
return type in that case, so the function remains a call target.
The Rust allocation shims in kani_lib.c are unchanged.

tests/kani/ForeignItems/lib.c now defines its two void functions as
`void`, and extern_fn_ptr.rs checks a call through a pointer to one.

Resolves model-checking#2151, resolves model-checking#1817

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sliedes
sliedes requested review from a team as code owners November 14, 2026 04:04
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Nov 14, 2026
@feliperodri

Copy link
Copy Markdown
Member

@sliedes could you add regression tests?

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.

🔵 Needs a closer look

The void/Unit indirect-call type mismatch requires end-to-end validation with the pinned goto-cc/CBMC toolchain, which was unavailable during review.

No open issues 🎉

What changed in this PR

This PR lets Kani link C functions returning void without requiring C code to return struct Unit, addressing issues #2151 and #1817.

Changes:

  • Declare unit-returning user C-FFI functions as void, leaving Rust allocation shims unchanged.
  • Cast function addresses to the expected Rust function-pointer type.
  • Update C fixtures to use void and add a regression harness for indirect calls.
File Description
tests/​kani/​ForeignItems/​lib.c Replace the struct Unit workaround with ordinary void returns.
tests/​kani/​ForeignItems/​extern_fn_ptr.rs Add a harness checking a void-returning C call through a function pointer.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​rvalue.rs Cast reified function pointers to their expected Rust type.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​foreign_function.rs Map unit returns to void for user C-FFI declarations.

Review effort: Balanced

Regression tests for model-checking#2151 and model-checking#1817. On Kani 0.68.0 each of them fails
to link: goto-cc reports "conflicting function declarations" ("type
classes differ") between the C `void` definition and Kani's declaration
returning `struct Unit`.

tests/kani/ForeignItems/void_return.rs links plain C `void` functions
(void_fns.c) and checks their side effects, so the calls cannot be
dropped:
- no parameters, updating a global (the minimal case);
- a symbolic argument;
- several parameters with an out pointer, and two pointer parameters;
- a function pointer with two `void` targets.

tests/expected/c-ffi-void checks that the calls are not vacuous, both
direct and through a function pointer: a cover of the side effect is
satisfied and an assertion it contradicts fails. Without the cast of
the function's address to the Rust pointer type, the pointer harnesses
make kani-compiler panic ("assign statement with unequal types").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sliedes

sliedes commented Nov 15, 2026

Copy link
Copy Markdown
Author

Added some more regression tests on top of what the first patch had. Is this what you had in mind?

This branch has not been deployed

No deployments
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.

C-FFI: Support for void vs unit return type in CBMC linker Unit types for external void functions shouldn't be needed

3 participants