kani icon indicating copy to clipboard operation
kani copied to clipboard

Disable Divide by 0 Checks for Floats

Open cvick32 opened this issue 1 year ago • 4 comments

Proposed change: Remove divide by 0 checks for floating point numbers, as they are well-defined in Rust? A similar change for +/- Inf was made in this PR.

Motivation: This change would eliminate false positives in verification failures.

cvick32 avatar Jul 05 '24 16:07 cvick32

Would you have a concrete example to use as test? With CBMC v6 (merged in #2995) we do not enable division-by-zero checks for floating point numbers by default anymore, so this one might as well quietly have been fixed by #2995.

tautschnig avatar Aug 01 '24 11:08 tautschnig

Kani previously found a counterexample on this function using this harness:

fn div_euclid_f64(lhs: f64, rhs: f64) -> f64 {
    let q = (lhs / rhs).trunc();
    if lhs % rhs < 0.0 {
        if rhs > 0.0 {
            q - 1.0
        } else {
            q + 1.0
        }
    } else {
        q
    }
}

#[kani::proof]
fn kani_harness_div_euclid_f64() {
  let lhs: f64 = kani::any();
  let rhs: f64 = kani::any();
  div_euclid_f64(lhs, rhs);
}

Kani's output summary from running cargo kani --harness kani_harness_div_euclid_f64:

SUMMARY:
 ** 4 of 62 failed
Failed Checks: division by zero
 File: "src/epoch/mod.rs", line 1186, in epoch::div_euclid_f64
Failed Checks: NaN on division
 File: "src/epoch/mod.rs", line 1186, in epoch::div_euclid_f64
Failed Checks: arithmetic overflow on floating-point division
 File: "src/epoch/mod.rs", line 1186, in epoch::div_euclid_f64
Failed Checks: division by zero
 File: "src/epoch/mod.rs", line 1187, in epoch::div_euclid_f64

cvick32 avatar Aug 01 '24 17:08 cvick32

Hmm, looks like we have a bug on the CBMC side in that floating-point division does not have checks enabled by default anymore, but floating-point remainder still has those.

tautschnig avatar Aug 01 '24 19:08 tautschnig

I intend to fold the fix for this on the CBMC side into https://github.com/diffblue/cbmc/pull/7885 as, until that work is completed, any floating-point remainder operations are very questionable.

tautschnig avatar Aug 02 '24 20:08 tautschnig