Add Kani proof harnesses and contracts for unchecked_div_exact - #672
Draft
CYJ904 wants to merge 1 commit into
Draft
Add Kani proof harnesses and contracts for unchecked_div_exact#672CYJ904 wants to merge 1 commit into
unchecked_div_exact#672CYJ904 wants to merge 1 commit into
Conversation
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.
Summary
This PR adds Kani function contracts and proof harnesses for
unchecked_div_exacton both signed and unsigned integer types (
i8–i128,isize,u8–u128,usize).unchecked_div_exactis an unstable intrinsic wrapper (tracking issue: rust-lang#139911)that computes
self / rhswithout checking for division-by-zero, non-exact division,or overflow. It currently has no verification coverage in this repository.
Changes
#[requires(...)]contracts tounchecked_div_exactin:library/core/src/num/int_macros.rs(signed integers)library/core/src/num/uint_macros.rs(unsigned integers)library/core/src/num/mod.rs:i8,i16,u8,u16)generate_unchecked_mul_intervalspattern) for larger bit-widths (
i32,i64,i128,isize,u32,u64,u128,usize),each covering three representative divisor ranges: small values, values near
MAX,and values near
MAX / 2Precondition rationale
rhs > 0 && self % rhs == 0 && (self != Self::MIN || rhs != -1)rhs > 0 && self % rhs == 0These match the runtime
assert_unsafe_precondition!checks already present in thefunction body. The extra
MIN / -1exclusion for signed types prevents the oneoverflow case possible in exact division.
Verification strategy note
For
i32and larger types, full unconstrained verification (kani::any()on bothoperands) was found to take upwards of 19 minutes per harness due to state-space
explosion in the bit-precise model checker — well above a reasonable per-harness
budget. Following the precedent set by
generate_unchecked_mul_intervalsforunchecked_mul, both operands are constrained to representative sub-ranges ratherthan the full domain. This is a deliberate coverage trade-off (representative
sampling rather than exhaustive proof for large bit-widths), consistent with the
existing pattern in this file. Values in the unconstrained middle range are not
covered by these harnesses.
Verification results
All 28 harnesses pass locally with 0 failures:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.