Skip to content

WIP implement guards in rem and div unsound cases #55

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

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions crates/core_simd/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,20 @@ macro_rules! impl_unsigned_int_ops {

#[inline]
fn div(self, rhs: Self) -> Self::Output {
// TODO there is probably a better way of doing this
if AsRef::<[$scalar]>::as_ref(&rhs)
if rhs.as_slice()
.iter()
.any(|x| *x == 0)
{
panic!("attempt to divide by zero");
}

// Guards for div(MIN, -1),
// this check only applies to signed ints
if <$scalar>::MIN != 0 && self.as_slice().iter()
.zip(rhs.as_slice().iter())
.any(|(x,y)| *x == <$scalar>::MIN && *y == -1 as _) {
panic!("attempt to divide with overflow");
}
unsafe { crate::intrinsics::simd_div(self, rhs) }
}
}
Expand All @@ -304,6 +311,11 @@ macro_rules! impl_unsigned_int_ops {
if rhs == 0 {
panic!("attempt to divide by zero");
}
if <$scalar>::MIN != 0 &&
self.as_slice().iter().any(|x| *x == <$scalar>::MIN) &&
rhs == -1 as _ {
panic!("attempt to divide with overflow");
}
let rhs = Self::splat(rhs);
unsafe { crate::intrinsics::simd_div(self, rhs) }
}
Expand Down Expand Up @@ -353,6 +365,14 @@ macro_rules! impl_unsigned_int_ops {
{
panic!("attempt to calculate the remainder with a divisor of zero");
}

// Guards for rem(MIN, -1)
// this branch applies the check only to signed ints
if <$scalar>::MIN != 0 && self.as_slice().iter()
.zip(rhs.as_slice().iter())
.any(|(x,y)| *x == <$scalar>::MIN && *y == -1 as _) {
panic!("attempt to calculate the remainder with overflow");
}
unsafe { crate::intrinsics::simd_rem(self, rhs) }
}
}
Expand All @@ -367,6 +387,11 @@ macro_rules! impl_unsigned_int_ops {
if rhs == 0 {
panic!("attempt to calculate the remainder with a divisor of zero");
}
if <$scalar>::MIN != 0 &&
self.as_slice().iter().any(|x| *x == <$scalar>::MIN) &&
rhs == -1 as _ {
panic!("attempt to calculate the remainder with overflow");
}
let rhs = Self::splat(rhs);
unsafe { crate::intrinsics::simd_rem(self, rhs) }
}
Expand Down
57 changes: 57 additions & 0 deletions crates/core_simd/tests/ops_impl/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,39 @@ macro_rules! int_tests {
assert_biteq!(a, expected);
}

#[test]
#[should_panic]
fn div_min_panics() {
let a = from_slice(&vec![$scalar::MIN; 64]);
let b = from_slice(&vec![-1; 64]);
let _ = a / b;
}

#[test]
#[should_panic]
fn div_by_all_zeros_panics() {
let a = from_slice(&A);
let b = from_slice(&vec![0 ; 64]);
let _ = a / b;
}

#[test]
#[should_panic]
fn div_by_one_zero_panics() {
let a = from_slice(&A);
let mut b = from_slice(&B);
b[0] = 0 as _;
let _ = a / b;
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn div_min_neg_one_no_panic() {
let a = from_slice(&A);
let b = from_slice(&vec![-1; 64]);
let _ = a / b;
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn rem() {
Expand Down Expand Up @@ -275,6 +308,30 @@ macro_rules! int_tests {
assert_biteq!(a, expected);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn rem_min_neg_one_no_panic() {
let a = from_slice(&A);
let b = from_slice(&vec![-1; 64]);
let _ = a % b;
}

#[test]
#[should_panic]
fn rem_min_panic() {
let a = from_slice(&vec![$scalar::MIN; 64]);
let b = from_slice(&vec![-1 ; 64]);
let _ = a % b;
}

#[test]
#[should_panic]
fn rem_min_zero_panic() {
let a = from_slice(&A);
let b = from_slice(&vec![0 ; 64]);
let _ = a % b;
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn bitand() {
Expand Down