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

Add integer overflow checking for simd_div and simd_rem #2645

Merged
merged 14 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
45 changes: 39 additions & 6 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,12 @@ impl<'tcx> GotocCtx<'tcx> {
loc,
),
"simd_and" => codegen_intrinsic_binop!(bitand),
// TODO: `simd_div` and `simd_rem` don't check for overflow cases.
// TODO: `simd_rem` doesn't check for overflow cases for floating point operands.
// <https://github.com/model-checking/kani/issues/1970>
reisnera marked this conversation as resolved.
Show resolved Hide resolved
"simd_div" => codegen_intrinsic_binop!(div),
// <https://github.com/model-checking/kani/pull/2645>
"simd_div" | "simd_rem" => {
self.codegen_simd_div_with_overflow(fargs, intrinsic, p, loc)
}
"simd_eq" => self.codegen_simd_cmp(Expr::vector_eq, fargs, p, span, farg_types, ret_ty),
"simd_extract" => {
self.codegen_intrinsic_simd_extract(fargs, p, farg_types, ret_ty, span)
Expand All @@ -535,9 +538,6 @@ impl<'tcx> GotocCtx<'tcx> {
self.codegen_simd_cmp(Expr::vector_neq, fargs, p, span, farg_types, ret_ty)
}
"simd_or" => codegen_intrinsic_binop!(bitor),
// TODO: `simd_div` and `simd_rem` don't check for overflow cases.
// <https://github.com/model-checking/kani/issues/1970>
"simd_rem" => codegen_intrinsic_binop!(rem),
"simd_shl" | "simd_shr" => {
self.codegen_simd_shift_with_distance_check(fargs, intrinsic, p, loc)
}
Expand Down Expand Up @@ -1514,6 +1514,39 @@ impl<'tcx> GotocCtx<'tcx> {
self.codegen_expr_to_place(p, e)
}

/// Codegen for `simd_div` and `simd_rem` intrinsics.
/// This checks for overflow in signed integer division (i.e. when dividing the minimum integer
/// for the type by -1). Overflow checks on floating point division are handled by CBMC, as is
/// division by zero for both integers and floats.
fn codegen_simd_div_with_overflow(
&mut self,
fargs: Vec<Expr>,
intrinsic: &str,
p: &Place<'tcx>,
loc: Location,
) -> Stmt {
let op_fun = match intrinsic {
"simd_div" => Expr::div,
"simd_rem" => Expr::rem,
_ => unreachable!("expected simd_div or simd_rem"),
};
let base_type = fargs[0].typ().base_type().unwrap().clone();
if base_type.is_integer() && base_type.is_signed(self.symbol_table.machine_model()) {
let min_int_expr = base_type.min_int_expr(self.symbol_table.machine_model());
reisnera marked this conversation as resolved.
Show resolved Hide resolved
let negative_one = Expr::int_constant(-1, base_type);
self.codegen_simd_op_with_overflow(
op_fun,
|a, b| a.eq(min_int_expr.clone()).and(b.eq(negative_one.clone())),
fargs,
intrinsic,
p,
loc,
)
} else {
self.binop(p, fargs, op_fun)
}
}

/// Intrinsics which encode a SIMD arithmetic operation with overflow check.
/// We expand the overflow check because CBMC overflow operations don't accept array as
/// argument.
Expand Down Expand Up @@ -1549,7 +1582,7 @@ impl<'tcx> GotocCtx<'tcx> {
);
let res = op_fun(a, b);
let expr_place = self.codegen_expr_to_place(p, res);
Stmt::block(vec![expr_place, check_stmt], loc)
Stmt::block(vec![check_stmt, expr_place], loc)
}

/// Intrinsics which encode a SIMD bitshift.
Expand Down
4 changes: 4 additions & 0 deletions tests/expected/intrinsics/simd-div-rem-overflow/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FAILURE\
attempt to compute simd_div which would overflow
FAILURE\
attempt to compute simd_rem which would overflow
38 changes: 38 additions & 0 deletions tests/expected/intrinsics/simd-div-rem-overflow/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Checks that the `simd_div` and `simd_rem` intrinsics check for integer overflows.

#![feature(repr_simd, platform_intrinsics)]

#[repr(simd)]
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct i32x2(i32, i32);

extern "platform-intrinsic" {
fn simd_div<T>(x: T, y: T) -> T;
fn simd_rem<T>(x: T, y: T) -> T;
}

#[kani::proof]
fn test_simd_div_overflow() {
let dividend = kani::any();
kani::assume(dividend == i32::MIN);
reisnera marked this conversation as resolved.
Show resolved Hide resolved
let dividends = i32x2(dividend, dividend);
let divisor = kani::any();
kani::assume(divisor == -1);
let divisors = i32x2(divisor, divisor);
let _quotient = unsafe { simd_div(dividends, divisors) };
reisnera marked this conversation as resolved.
Show resolved Hide resolved
}

#[kani::proof]
fn test_simd_rem_overflow() {
let dividend = kani::any();
kani::assume(dividend == i32::MIN);
let dividends = i32x2(dividend, dividend);
let divisor = kani::any();
kani::assume(divisor == -1);
let divisors = i32x2(divisor, divisor);
let _remainder = unsafe { simd_rem(dividends, divisors) };
}
40 changes: 40 additions & 0 deletions tests/kani/Intrinsics/SIMD/Operators/division_float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Checks that the `simd_div` intrinsic returns the expected results for floating point numbers.
//! Checks that the `simd_rem` intrinsic exists.
#![feature(repr_simd, platform_intrinsics)]

#[repr(simd)]
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, PartialEq)]
pub struct f32x2(f32, f32);

extern "platform-intrinsic" {
fn simd_div<T>(x: T, y: T) -> T;
fn simd_rem<T>(x: T, y: T) -> T;
}

#[kani::proof]
fn test_simd_div() {
let dividend: f32 = kani::any::<i8>().into();
reisnera marked this conversation as resolved.
Show resolved Hide resolved
let divisor: f32 = kani::any::<i8>().into();
// Narrow down the divisor interval so the operation finishes in a short time
kani::assume(divisor != 0.0 && divisor.abs() < 5.0);
let normal_result = dividend / divisor;
let dividends = f32x2(dividend, dividend);
let divisors = f32x2(divisor, divisor);
let simd_result = unsafe { simd_div(dividends, divisors) };
assert_eq!(normal_result, simd_result.0);
reisnera marked this conversation as resolved.
Show resolved Hide resolved
}

#[kani::proof]
fn test_simd_rem() {
let dividend: f32 = kani::any::<i8>().into();
let divisor: f32 = kani::any::<i8>().into();
// Narrow down the divisor interval so the operation finishes in a short time
kani::assume(divisor != 0.0 && divisor.abs() < 5.0);
let dividends = f32x2(dividend, dividend);
let divisors = f32x2(divisor, divisor);
let _simd_result = unsafe { simd_rem(dividends, divisors) };
reisnera marked this conversation as resolved.
Show resolved Hide resolved
}
Loading