Repository navigation
Conversation
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>
Member
|
@sliedes could you add regression tests? |
Contributor
There was a problem hiding this comment.
🔵 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
voidand 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>
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
-Z c-ffi --c-lib, a C functionvoid f(int)declared in Rust asfn f(x: i32)could not be linked: Kani declared it as returningstruct Unit, and goto-cc rejected the C definition as a conflicting declaration ("type classes differ"). The only workaround was to change the C to returnstruct 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 returningvoid. 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 (returningstruct 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.