Skip to content

Check that fast math intrinsic results are finite - #4730

Open
feliperodri wants to merge 3 commits into
model-checking:mainfrom
feliperodri:fix/fadd-fast-result-finite-check
Open

Check that fast math intrinsic results are finite#4730
feliperodri wants to merge 3 commits into
model-checking:mainfrom
feliperodri:fix/fadd-fast-result-finite-check

Conversation

@feliperodri

Copy link
Copy Markdown
Member

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.

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

@feliperodri
feliperodri requested a review from a team as a code owner August 12, 2026 20:52
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Aug 12, 2026
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
feliperodri force-pushed the fix/fadd-fast-result-finite-check branch from 49d7f96 to 141382c Compare August 12, 2026 21:08
@feliperodri feliperodri added the [F] Soundness Kani failed to detect an issue label Aug 12, 2026
@feliperodri
feliperodri requested a balanced review from Copilot August 12, 2026 21:10

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

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-fail only 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 own kani-verify-fail test (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_checks no longer describes its behavior and suggests it only enforces argument preconditions. Rename it and its call sites to something that covers both checks, such as add_fast_math_finiteness_checks.
        place: &Place,

@feliperodri feliperodri self-assigned this Aug 13, 2026
- 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>
@feliperodri feliperodri removed their assignment Aug 13, 2026
@feliperodri
feliperodri requested a review from tautschnig August 13, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[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