Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong encoding of signed divisions #1505

Closed
fpoli opened this issue Mar 5, 2024 · 2 comments · Fixed by #1511
Closed

Wrong encoding of signed divisions #1505

fpoli opened this issue Mar 5, 2024 · 2 comments · Fixed by #1511
Assignees
Labels
bug Something isn't working good first issue Good for newcomers unsoundness Unsoudness in Prusti

Comments

@fpoli
Copy link
Member

fpoli commented Mar 5, 2024

Given a / b, for a < 0 in Rust the result is still rounded toward 0, while in Viper it is rounded away from 0. Prusti currently encodes signed integer divisions using Viper's semantics, which is wrong.

use prusti_contracts::*;

// To inhibit constant-propagation optimizations:
#[pure]
fn id<T>(x: T) -> T { x}

pub fn main(){
    assert!(id(3i32) / 2 == 1);
    assert!(id(-3i32) / 2 == -1);
    assert!(id(3i32) / -2 == -1);
    assert!(id(-3i32) / -2 == 1);
    prusti_assert!(id(3i32) / 2 == 1);
    prusti_assert!(id(-3i32) / 2 == -1);
    prusti_assert!(id(3i32) / -2 == -1);
    prusti_assert!(id(-3i32) / -2 == 1);
    assert!(false); // Smoke test, the only one that should fail
}

If someone wants to work on this, the fix can be done similarly to how fn rem is used to encode the correct semantics of signed modulo operations.

@fpoli fpoli added bug Something isn't working good first issue Good for newcomers unsoundness Unsoudness in Prusti labels Mar 5, 2024
@fpoli
Copy link
Member Author

fpoli commented Mar 5, 2024

It might be better to add something like -Z mir-opt-level=0 to all our test cases. I'm afraid that many useful tests might have been invalidated by the experimental MIR-level optimizations that are enabled by default in nighly compiler versions.

@fpoli
Copy link
Member Author

fpoli commented Mar 25, 2024

Silver has the same issue: viperproject/silver#782

fpoli added a commit that referenced this issue Mar 25, 2024
@fpoli fpoli mentioned this issue Mar 25, 2024
@fpoli fpoli self-assigned this Mar 25, 2024
fpoli added a commit that referenced this issue Mar 26, 2024
* Add regression test for issue #1505
* Fix encoding of signed integer divisions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers unsoundness Unsoudness in Prusti
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant