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

Unsound Div, Rem #54

Closed
calebzulawski opened this issue Jan 7, 2021 · 9 comments
Closed

Unsound Div, Rem #54

calebzulawski opened this issue Jan 7, 2021 · 9 comments
Labels
C-bug Category: Bug E-easy Call for participation. Experience needed: Low.

Comments

@calebzulawski
Copy link
Member

calebzulawski commented Jan 7, 2021

The following should panic for integers:

  • neg(MIN)
  • div(MIN, -1)
  • rem(MIN, -1)
  • div(x, 0)
  • rem(x, 0)
@calebzulawski calebzulawski added C-bug Category: Bug E-easy Call for participation. Experience needed: Low. labels Jan 7, 2021
@calebzulawski
Copy link
Member Author

This should probably be fixed after or with #49

@workingjubilee
Copy link
Member

workingjubilee commented Jan 26, 2021

NB: these are all excepting neg(MIN), these are considered UB by LLVM and currently these scalar Rust ops panic (or are even prevented with a "this will panic at runtime" warning).

@calebzulawski
Copy link
Member Author

calebzulawski commented Jan 27, 2021

looking again, neg in llvm is implemented with sub and appears to be allowed to wrap, so maybe that needs to be checked in the compiler only in debug mode?

@thomcc
Copy link
Member

thomcc commented Jan 27, 2021

Is there any chance of having a wrapping_simd_foo version that we could use instead? I suspect in practice the branching here will be very bad for performance.

@calebzulawski
Copy link
Member Author

For neg I think we want to extend the compiler intrinsics eventually. For div and rem the branching is unavoidable (but we can try to optimize it in the future)

@workingjubilee
Copy link
Member

workingjubilee commented Jan 27, 2021

looking again, neg in llvm is implemented with sub and appears to be allowed to wrap, so maybe that needs to be checked in the compiler only in debug mode?

Playground

fn main() {
    println!("{}", -i32::MIN);
}

Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 1.69s
Running target/debug/playground
thread 'main' panicked at 'attempt to negate with overflow', src/main.rs:2:20
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

However...

Running target/release/playground

-2147483648

So yeah! That one only panics in debug, actually.

@thomcc
Copy link
Member

thomcc commented Jan 27, 2021

For neg I think we want to extend the compiler intrinsics eventually. For div and rem the branching is unavoidable (but we can try to optimize it in the future)

div/rem are heavyweight operations that this seems fine for, so long as the branch is correctly predicted against and sufficiently outlined the way normal int divs and such are.

@workingjubilee workingjubilee changed the title Unsound Neg, Div, Rem Unsound Div, Rem Jan 27, 2021
@workingjubilee
Copy link
Member

I think we have a separate issue on our hands which is re: whether or not add/sub/neg should overflow in Debug mode, and as such have removed that from this issue and into #56.

@workingjubilee
Copy link
Member

Resolved by #55.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Bug E-easy Call for participation. Experience needed: Low.
Projects
None yet
Development

No branches or pull requests

3 participants