Skip to content

Replace byte-inspection NonZero contracts with niche test and raw_eq - #624

Open
tautschnig wants to merge 1 commit into
model-checking:mainfrom
tautschnig:fix-nonzero-contract-cost
Open

Replace byte-inspection NonZero contracts with niche test and raw_eq#624
tautschnig wants to merge 1 commit into
model-checking:mainfrom
tautschnig:fix-nonzero-contract-cost

Conversation

@tautschnig

Copy link
Copy Markdown
Member

The requires/ensures clauses of NonZero::new_unchecked (and the requires of from_mut_unchecked) expressed "n is not zero" and "result equals n" by building a raw byte slice over the value with slice::from_raw_parts and iterating it. Under symbolic execution, every asserted occurrence of these clauses pays for pointer indirection, slice-iterator reasoning, and allocation tracking — and since new_unchecked sits beneath most NonZero operations, harnesses whose call graph contains NonZero constructions were dominated by this: with dependency contracts asserted (the Kani default since model-checking/kani#3802), num::nonzero::verify::check_mul_u32_small takes 59.6s of CBMC solve time, against 0.3s with --no-assert-contracts.

This PR expresses the same properties through operations the verifier resolves directly: NonZero::new(n).is_some() performs the canonical zero test via the niche layout (a transmute plus discriminant test), and intrinsics::raw_eq compares object representations without constructing slices. Neither requires additional trait bounds on T.

Measured with Kani 152c6a8c + CBMC 6.10.0:

  • check_mul_u32_small with contracts asserted: 59.6s → 0.6s solve time (103x).
  • All 56 harnesses matching nonzero_check_new_unchecked_for* / nonzero_check_from_mut_unchecked* / check_mul* pass both with and without --no-assert-contracts.

Part of the effort to make dropping --no-assert-contracts from run-kani.sh feasible (see also #622, #623): the byte-inspection clauses were the single largest per-call-site cost multiplier identified when asserting dependency contracts across a 125-harness sample.

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

The requires/ensures clauses of NonZero::new_unchecked (and the
requires of from_mut_unchecked) expressed "n is not zero" and "result
equals n" by building a raw byte slice over the value with
slice::from_raw_parts and iterating it. Under symbolic execution every
asserted occurrence of these clauses then pays for pointer indirection,
slice-iterator reasoning, and allocation tracking - and since
new_unchecked sits beneath most NonZero operations, harnesses whose
call graph contains NonZero constructions were dominated by this: with
dependency contracts asserted (the Kani default since
model-checking/kani#3802), num::nonzero::verify::check_mul_u32_small
takes 59.6s of CBMC solve time, against 0.3s with
--no-assert-contracts.

Express the same properties through operations the verifier resolves
directly: `NonZero::new(n).is_some()` performs the canonical zero test
via the niche layout (a transmute plus discriminant test), and
`intrinsics::raw_eq` compares object representations without
constructing slices. Neither requires additional trait bounds on T.

With contracts asserted, check_mul_u32_small drops from 59.6s to 0.6s
solve time (103x). All 56 harnesses matching
nonzero_check_new_unchecked_for / nonzero_check_from_mut_unchecked /
check_mul pass both with and without --no-assert-contracts
(Kani 152c6a8c + CBMC 6.10.0).

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig
tautschnig requested a review from a team as a code owner August 3, 2026 16:54
Copilot AI review requested due to automatic review settings August 3, 2026 16:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Optimizes the Kani contract clauses for core::num::NonZero constructors to reduce symbolic-execution overhead when dependency contracts are asserted, without changing runtime semantics (the runtime contract macros are currently no-ops).

Changes:

  • Replaced byte-slice “non-zero” preconditions for NonZero::new_unchecked / from_mut_unchecked with a niche-layout check via NonZero::new(..).is_some().
  • Replaced byte-slice equality postcondition for new_unchecked with intrinsics::raw_eq to compare object representations directly.
  • Added explanatory comments documenting why these contract forms are cheaper for the verifier.

// indirection and iterator reasoning, which dominated verification time
// of harnesses whose call graph contains NonZero constructions.
#[requires(NonZero::new(n).is_some())]
#[ensures(|result: &Self| unsafe { core::intrinsics::raw_eq(&result.get(), &n) })]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants