Check that fast math intrinsic results are finite - #4730
Open
feliperodri wants to merge 3 commits into
Open
Conversation
Previously, Kani only verified that the inputs to fast math intrinsics (fadd_fast, fsub_fast, fmul_fast, fdiv_fast) were finite. However, per the Rust documentation, producing a non-finite result (infinity or NaN) from these intrinsics is also undefined behavior, even when both inputs are finite (e.g., f32::MAX + f32::MAX overflows to infinity). This commit adds a result finiteness check to all four fast math intrinsics. After the operation is performed and assigned to the destination, the result is asserted (and assumed) to be finite. Regression tests are added covering: - All four fast math intrinsics with finite inputs that overflow to infinity (kani-verify-fail). - Pointer wrapping_add with extreme offsets confirming these are correctly NOT flagged as UB under default settings. Signed-off-by: Felipe Monteiro <felisous@amazon.com>
feliperodri
force-pushed
the
fix/fadd-fast-result-finite-check
branch
from
August 12, 2026 21:08
49d7f96 to
141382c
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (3)
tests/kani/Intrinsics/FastMath/fast_math_result_overflow.rs:32
- This is overflow, not underflow: the magnitude grows beyond
f32::MAX. Underflow refers to results whose magnitude is too small to represent normally.
/// Check that fsub_fast detects underflow to negative infinity as UB.
/// (-f32::MAX) - f32::MAX underflows to negative infinity.
tests/kani/Intrinsics/FastMath/fast_math_result_overflow.rs:10
- This file-level
kani-verify-failonly checks that the overall Kani process exits unsuccessfully. Since one failing harness makes that true, the test still passes if result-finiteness checking regresses for any three of these four intrinsics. Please give each intrinsic its ownkani-verify-failtest (as the existing FastMath overflow tests do), so every implementation path is independently required to fail.
This issue also appears on line 31 of the same file.
// kani-verify-fail
kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs:704
- Now that this helper also validates the destination result,
add_finite_args_checksno longer describes its behavior and suggests it only enforces argument preconditions. Rename it and its call sites to something that covers both checks, such asadd_fast_math_finiteness_checks.
place: &Place,
- Split the combined fast_math_result_overflow.rs into one file per intrinsic (add/mul/sub/div), so each intrinsic's result-finiteness check is independently required to fail. A single kani-verify-fail over four harnesses passed as long as any one harness failed, masking a regression in the other three. - Fix the fsub_fast test wording: the result grows in magnitude beyond f32::MAX, so it overflows to negative infinity (not underflow). - Rename add_finite_args_checks to add_fast_math_finiteness_checks to reflect that it now validates the result as well as the arguments. Signed-off-by: Felipe Monteiro <felisous@amazon.com>
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.
Previously, Kani only verified that the inputs to fast math intrinsics (
fadd_fast,fsub_fast,fmul_fast,fdiv_fast) were finite. However, per the Rust documentation, producing a non-finite result (infinity or NaN) from these intrinsics is also undefined behavior, even when both inputs are finite (e.g.,f32::MAX+f32::MAXoverflows to infinity).This commit adds a result finiteness check to all four fast math intrinsics. After the operation is performed and assigned to the destination, the result is asserted (and assumed) to be finite.
Regression tests are added covering:
kani-verify-fail).wrapping_addwith extreme offsets confirming these are correctly NOT flagged as UB under default settings.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.