Skip to content

Preserve memory initialization checks at unresolved calls - #5172

Merged
feliperodri merged 9 commits into
model-checking:mainfrom
Tiramisu-Cake:fix/kani-uninit-unresolved-call-v3
Nov 21, 2026
Merged

feliperodri merged 9 commits into
model-checking:mainfrom
Tiramisu-Cake:fix/kani-uninit-unresolved-call-v3

Conversation

@Tiramisu-Cake

@Tiramisu-Cake Tiramisu-Cake commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Refs #3300

With -Z uninit-checks, when the uninitialized-memory visitor cannot resolve a call (a function pointer, a trait object, ...), visit_terminator returns early. Everything it had already collected for that terminator is dropped: the checks on the call's operands and, in a statement-free entry block, the argument loads. Kani still reports that it does not support checks across such calls, but the actual uninitialized read in the operand is no longer reported.

Example (pending-ops-plain.rs, custom MIR so that the read is an operand of the call):

#[custom_mir(dialect = "runtime", phase = "optimized")]
unsafe fn indirect(f: fn(u8) -> u8, pointer: *const u8) -> u8 {
    mir! {
        { Call(RET = f(*pointer), ReturnTo(done), UnwindUnreachable()) }
        done = { Return() }
    }
}

The harness passes a pointer to an uninitialized heap byte. On main the only failed check is the "calls through function pointers" diagnostic, and the indirect.safety_check for *pointer is missing. With this change both are reported.

Change

  • Visit the operands once, add Unsupported after the operations already collected, and push the target through the common path. The resulting order is entry LoadArgument, operand checks, Unsupported, then the call.
  • The local visitor owns the diagnostic, so it no longer depends on delayed-UB analysis having seeds.
  • The visitor and the points-to analysis share one fallible call resolution. Unresolved items, virtual calls, LLVM intrinsics and non-foreign callees without a body are rejected. Bodies are looked up per instance with checked_body instead of the definition-level has_body(), so generated shims are covered. Seed collection skips failed resolutions instead of unwrapping, and points-to keeps its existing graph when it cannot follow a callee.
  • MIR-backed shims keep the normal path for ordinary by-value storage. A shim call whose argument or return storage contains a union, or whose storage cannot be classified, is rejected before StoreArgument/LoadArgument. No new shadow transfer is added.

Testing

tests/expected/uninit/unresolved-calls adds 22 harnesses: direct calls, function pointers, trait objects, erased, wrapped and global function pointers, Fn-trait pointers, generated shims, pending operand reads and statement-free entry blocks with initialized companions. Each comes in a plain variant and a "seeded" variant whose ordinary byte copy gives delayed-UB analysis targets.

On main six of them fail: in the entry-block and pending-read cases the indirect.safety_check properties are missing from the results. With this change all uninit-checks suites pass.

Kani CI for 88fec3b397 (this branch merged with current main) in my fork passed the regression jobs on all five platforms, plus documentation, perf, LLBC and benchcomp. Format and Clippy and Cargo Deny also passed. On macOS 15 Intel, script-based-pre/verify_std_cmd hit its timeout on the first attempt and passed on a rerun.

Not in this PR

  • Reachability still unwraps the resolution of FnDef calls before this pass runs.
  • The early flush in the UnionAsField statement path is a separate issue.
  • General ABI support, union return values and memory effects of indirect calls are not added.

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

Disclosure: this change and description were prepared with the help of AI assistants.

@Tiramisu-Cake

Copy link
Copy Markdown
Contributor Author

Pushed a small follow-up: resolve_call now checks for a body with checked_body, as the note in kani_middle/mod.rs asks, and a few comments are reworded. No behaviour change for the tests in this PR.

Disclosure: this comment was prepared with the help of AI assistants.

@feliperodri feliperodri self-assigned this Nov 18, 2026

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.

🟡 Changes recommended

Repeated traversal of shared field types can make shim classification exponential and stall compilation, while uncached callee-body availability checks add redundant work during points-to analysis.

2 open issues
What changed in this PR

This PR preserves Kani’s memory-initialization checks when a call cannot be resolved, advancing #3300 without adding support for indirect-call memory effects.

Changes:

  • Preserve pending argument loads and operand checks before unsupported-call diagnostics.
  • Share fallible call resolution and guard unsupported memory-state transfers through generated shims.
  • Add paired plain and seeded regressions for direct calls, indirect calls, generated shims, and entry-block checks.
File Description
tests/​expected/​uninit/​unresolved-calls/​wrapped-pointer-seeded.rs Covers wrapped-pointer calls with analysis seeds.
tests/​expected/​uninit/​unresolved-calls/​wrapped-pointer-seeded.expected Requires the function-pointer diagnostic.
tests/​expected/​uninit/​unresolved-calls/​wrapped-pointer-plain.rs Covers wrapped-pointer calls without seeds.
tests/​expected/​uninit/​unresolved-calls/​wrapped-pointer-plain.expected Requires the function-pointer diagnostic.
tests/​expected/​uninit/​unresolved-calls/​virtual-seeded.rs Covers trait-object calls with analysis seeds.
tests/​expected/​uninit/​unresolved-calls/​virtual-seeded.expected Requires the trait-object diagnostic.
tests/​expected/​uninit/​unresolved-calls/​virtual-plain.rs Covers trait-object calls without seeds.
tests/​expected/​uninit/​unresolved-calls/​virtual-plain.expected Requires the trait-object diagnostic.
tests/​expected/​uninit/​unresolved-calls/​pending-ops-seeded.rs Exercises uninitialized call operands with seeds.
tests/​expected/​uninit/​unresolved-calls/​pending-ops-seeded.expected Requires both operand and unsupported-call failures.
tests/​expected/​uninit/​unresolved-calls/​pending-ops-plain.rs Exercises uninitialized call operands without seeds.
tests/​expected/​uninit/​unresolved-calls/​pending-ops-plain.expected Requires both operand and unsupported-call failures.
tests/​expected/​uninit/​unresolved-calls/​global-escaped-seeded.rs Covers indirect global-pointer access with seeds.
tests/​expected/​uninit/​unresolved-calls/​global-escaped-seeded.expected Requires the function-pointer diagnostic.
tests/​expected/​uninit/​unresolved-calls/​global-escaped-plain.rs Covers indirect global-pointer access without seeds.
tests/​expected/​uninit/​unresolved-calls/​global-escaped-plain.expected Requires the function-pointer diagnostic.
tests/​expected/​uninit/​unresolved-calls/​generated-positive-seeded.rs Covers supported generated-shim paths with seeds.
tests/​expected/​uninit/​unresolved-calls/​generated-positive-seeded.expected Requires successful generated-shim verification.
tests/​expected/​uninit/​unresolved-calls/​generated-positive-plain.rs Covers supported generated-shim paths without seeds.
tests/​expected/​uninit/​unresolved-calls/​generated-positive-plain.expected Requires successful generated-shim verification.
tests/​expected/​uninit/​unresolved-calls/​fn-trait-pointer-seeded.rs Covers Fn-trait function-pointer calls with seeds.
tests/​expected/​uninit/​unresolved-calls/​fn-trait-pointer-seeded.expected Requires rejection of the underlying indirect call.
tests/​expected/​uninit/​unresolved-calls/​fn-trait-pointer-plain.rs Covers Fn-trait function-pointer calls without seeds.
tests/​expected/​uninit/​unresolved-calls/​fn-trait-pointer-plain.expected Requires rejection of the underlying indirect call.
tests/​expected/​uninit/​unresolved-calls/​fn-pointer-seeded.rs Covers basic function-pointer calls with seeds.
tests/​expected/​uninit/​unresolved-calls/​fn-pointer-seeded.expected Requires the function-pointer diagnostic.
tests/​expected/​uninit/​unresolved-calls/​fn-pointer-plain.rs Covers basic function-pointer calls without seeds.
tests/​expected/​uninit/​unresolved-calls/​fn-pointer-plain.expected Requires the function-pointer diagnostic.
tests/​expected/​uninit/​unresolved-calls/​erased-pointer-seeded.rs Covers type-erased pointer arguments with seeds.
tests/​expected/​uninit/​unresolved-calls/​erased-pointer-seeded.expected Requires the function-pointer diagnostic.
tests/​expected/​uninit/​unresolved-calls/​erased-pointer-plain.rs Covers type-erased pointer arguments without seeds.
tests/​expected/​uninit/​unresolved-calls/​erased-pointer-plain.expected Requires the function-pointer diagnostic.
tests/​expected/​uninit/​unresolved-calls/​entry-partial-seeded.rs Exercises partially initialized entry arguments with seeds.
tests/​expected/​uninit/​unresolved-calls/​entry-partial-seeded.expected Requires union-read and unsupported-call failures.
tests/​expected/​uninit/​unresolved-calls/​entry-partial-plain.rs Exercises partially initialized entry arguments without seeds.
tests/​expected/​uninit/​unresolved-calls/​entry-partial-plain.expected Requires union-read and unsupported-call failures.
tests/​expected/​uninit/​unresolved-calls/​entry-initialized-seeded.rs Exercises initialized entry arguments with seeds.
tests/​expected/​uninit/​unresolved-calls/​entry-initialized-seeded.expected Requires a successful read check before call rejection.
tests/​expected/​uninit/​unresolved-calls/​entry-initialized-plain.rs Exercises initialized entry arguments without seeds.
tests/​expected/​uninit/​unresolved-calls/​entry-initialized-plain.expected Requires a successful read check before call rejection.
tests/​expected/​uninit/​unresolved-calls/​direct-seeded.rs Provides a supported direct-call control with seeds.
tests/​expected/​uninit/​unresolved-calls/​direct-seeded.expected Requires successful direct-call verification.
tests/​expected/​uninit/​unresolved-calls/​direct-plain.rs Provides a supported direct-call control without seeds.
tests/​expected/​uninit/​unresolved-calls/​direct-plain.expected Requires successful direct-call verification.
kani-compiler/​src/​kani_middle/​transform/​check_uninit/​ptr_uninit/​uninit_visitor.rs Preserves pending checks and guards shim storage transfers.
kani-compiler/​src/​kani_middle/​transform/​check_uninit/​ptr_uninit/​mod.rs Supplies compiler and instance context to the visitor.
kani-compiler/​src/​kani_middle/​transform/​check_uninit/​delayed_ub/​instrumentation_visitor.rs Delegates unsupported-call diagnostics to the local visitor.
kani-compiler/​src/​kani_middle/​transform/​check_uninit/​delayed_ub/​initial_target_visitor.rs Skips failed resolutions during seed collection.
kani-compiler/​src/​kani_middle/​points_to/​points_to_analysis.rs Shares call resolution and preserves graph state on failure.
kani-compiler/​src/​kani_middle/​points_to/​mod.rs Exports the shared call resolver.

Review effort: Balanced

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, the change is right. Dropping the operations already collected for an unresolved call lost real checks (the *pointer read in your custom-MIR example), and keeping them while still adding the unsupported check fixes it without hiding the limitation. Sharing one fallible resolver between the visitor and the points-to analysis, and looking bodies up per instance with checked_body, also removes the unwraps on unresolvable calls. Treating shims whose by-value storage may carry union state as unsupported is the conservative choice until state can flow through them.

Blocking: the merge of main brought in a toolchain where FieldDef::ty takes GenericArgs by value, so kani-compiler doesn't build (error[E0308] at uninit_visitor.rs:787, which is every red job). With the one-line fix below it builds, and locally all 22 unresolved-calls harnesses pass, as do the expected uninit (53) and kani Uninit (10) suites. Clippy and fmt are clean. I'll approve once CI is green.

A heads-up on #5196 (nested unions), which touches the same visit_terminator call handling: whichever lands second will need a rebase. After #5196, [MaybeUninit<T>; N] and tuples containing MaybeUninit are supported, so their clone shims (a.clone()) will hit your shim guard and be reported as unsupported. That's sound, but it's worth a follow-up issue to carry the state through those shims the way #5196 does for ordinary calls.

@feliperodri feliperodri added [C] Bug This is a bug. Something isn't working. [F] Soundness Kani failed to detect an issue labels Nov 20, 2026
@Tiramisu-Cake

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I merged current main and pushed 88fec3b397:

  • FieldDef::ty now gets *args, as you suggested, so kani-compiler builds on the new toolchain.
  • From the Copilot review: the shim type walk keeps a visited set, so each distinct type is expanded once per classification, and the resolver now returns the checked body, which the points-to analysis reuses instead of requesting it again.
  • The merge had one conflict in points_to_analysis.rs with the recursion check from Report recursive calls in delayed UB analysis instead of overflowing the stack #5184; both changes are kept.

On #5196: agreed, I'll open a follow-up issue about carrying initialization state through clone shims once it lands.

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the update. The *args fix builds on the current toolchain, and both Copilot points are addressed: the visited set keeps the shim walk linear and is sound (a repeated type has either already made the result true or contributes false again), and points-to reuses the resolver's body. The merge with #5184 keeps both checks. Locally, the uninit expected (56) and kani Uninit (10) suites pass, and clippy and fmt are clean. The red CI jobs are the codegen_cache flake (fixed by #5207, the rest were cancelled by fail-fast) and a runner shutdown in perf-benchcomp. Approving.

A few non-blocking follow-ups:

  • try_resolve_instance in the visitor now calls checked_body for every resolved call, which builds the callee's stable MIR and discards it. Availability doesn't need the body (Item with has_body() or any Shim), so the visitor could skip building it and leave fetching the body to points-to, where caching it per instance would also help the dataflow iterations.
  • unsupported_call_reason is defined twice with the same message; one shared copy would avoid drift.
  • let Some(body) = body else { return }; in points-to cannot fail, since the resolver always returns a body for non-foreign items. An expect with that invariant (or a comment) would keep a future change from silently dropping the call effect.

@feliperodri feliperodri removed their assignment Nov 21, 2026
@feliperodri
feliperodri added this pull request to the merge queue Nov 21, 2026
Merged via the queue into model-checking:main with commit 6154b31 Nov 21, 2026
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Bug This is a bug. Something isn't working. [F] Soundness Kani failed to detect an issue 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.

3 participants