Skip to content

Incorrect floating point result with remainder (%) operator #2669

Open
@reisnera

Description

@reisnera

I tried this code:

#[kani::proof]
fn test_rem() {
    let dividend: f32 = kani::any::<i8>().into();
    let divisor: f32 = kani::any::<i8>().into();
    kani::assume(divisor != 0.0);
    let result = dividend % divisor;
    assert!(result == 0.0 || result.is_normal());
}

using the following command line invocation:

kani <file>

with Kani version: 0.33

I expected to see this happen: proof should succeed

Instead, this happened: proof failed with the following output:

click to expand
Kani Rust Verifier 0.33.0 (standalone)
Checking harness test_rem...
CBMC 5.88.0 (cbmc-5.88.0)
CBMC version 5.88.0 (cbmc-5.88.0) 64-bit x86_64 linux
Reading GOTO program from file /home/alex/rust-projects/tmp/test__RNvCsd4v5NBjnlwJ_4test8test_rem.out
Generating GOTO Program
Adding CPROVER library (x86_64)
Removal of function pointers and virtual functions
Generic Property Instrumentation
Running with 16 object bits, 48 offset bits (user-specified)
Starting Bounded Model Checking
Runtime Symex: 0.0122388s
size of program expression: 599 steps
slicing removed 328 assignments
Generated 3 VCC(s), 3 remaining after simplification
Runtime Postprocess Equation: 0.0002305s
Passing problem to propositional reduction
converting SSA
warning: ignoring mod
  * type: floatbv
      * width: 32
      * f: 23
      * #c_type: float
  0: symbol
      * type: floatbv
          * width: 32
          * f: 23
          * #c_type: float
      * identifier: _RNvCsd4v5NBjnlwJ_4test8test_rem::1::var_9!0@1#2
      * expression: symbol
          * type: floatbv
              * width: 32
              * f: 23
              * #c_type: float
          * identifier: _RNvCsd4v5NBjnlwJ_4test8test_rem::1::var_9
      * #SSA_symbol: 1
      * L0: 0
      * L1: 1
      * L2: 2
      * L1_object_identifier: _RNvCsd4v5NBjnlwJ_4test8test_rem::1::var_9!0@1
  1: symbol
      * type: floatbv
          * width: 32
          * f: 23
          * #c_type: float
      * identifier: _RNvCsd4v5NBjnlwJ_4test8test_rem::1::var_10!0@1#2
      * expression: symbol
          * type: floatbv
              * width: 32
              * f: 23
              * #c_type: float
          * identifier: _RNvCsd4v5NBjnlwJ_4test8test_rem::1::var_10
      * #SSA_symbol: 1
      * L0: 0
      * L1: 1
      * L2: 2
      * L1_object_identifier: _RNvCsd4v5NBjnlwJ_4test8test_rem::1::var_10!0@1
Runtime Convert SSA: 0.0018622s
Running propositional reduction
Post-processing
Runtime Post-process: 6.3e-06s
Solving with MiniSAT 2.2.1 with simplifier
881 variables, 2964 clauses
SAT checker: instance is SATISFIABLE
Runtime Solver: 0.0038988s
Runtime decision procedure: 0.0058917s
Running propositional reduction
Solving with MiniSAT 2.2.1 with simplifier
881 variables, 376 clauses
SAT checker inconsistent: instance is UNSATISFIABLE
Runtime Solver: 1.41e-05s
Runtime decision procedure: 4.9e-05s

RESULTS:
Check 1: test_rem.assertion.1
         - Status: FAILURE
         - Description: "assertion failed: result == 0.0 || result.is_normal()"
         - Location: ../tmp/test.rs:40:5 in function test_rem

Check 2: test_rem.division-by-zero.1
         - Status: SUCCESS
         - Description: "division by zero"
         - Location: ../tmp/test.rs:39:18 in function test_rem


SUMMARY:
 ** 1 of 2 failed
Failed Checks: assertion failed: result == 0.0 || result.is_normal()
 File: "/home/alex/rust-projects/tmp/test.rs", line 40, in test_rem

VERIFICATION:- FAILED
Verification Time: 0.049368992s

Summary:
Verification failed for - test_rem
Complete - 0 successfully verified harnesses, 1 failures, 1 total.

Also as a sanity check, I brute forced a quick double check of this on the Rust Playground here to prove to myself that this Kani result was unexpected.

Metadata

Metadata

Assignees

Labels

[C] BugThis is a bug. Something isn't working.[F] SoundnessKani failed to detect an issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions