Skip to content

Commit

Permalink
Don't use the broken overflow builtins for pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsn committed Feb 1, 2022
1 parent bf11df0 commit af8d832
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/kani-compiler/cbmc/src/goto_program/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::InternedString;
use num::bigint::BigInt;
use std::collections::BTreeMap;
use std::fmt::Debug;
use tracing::warn;

///////////////////////////////////////////////////////////////////////////////////////////////
/// Datatypes
Expand Down Expand Up @@ -813,8 +814,8 @@ impl Expr {
match op {
// Arithmetic which can include pointers
Minus => {
(lhs.typ == rhs.typ)
&& (lhs.typ.is_pointer() || lhs.typ.is_numeric() || lhs.typ.is_vector())
((lhs.typ == rhs.typ)
&& (lhs.typ.is_pointer() || lhs.typ.is_numeric() || lhs.typ.is_vector()))
|| (lhs.typ.is_pointer() && rhs.typ.is_integer())
}
Plus => {
Expand Down Expand Up @@ -851,13 +852,10 @@ impl Expr {
// Floating Point Equalities
IeeeFloatEqual | IeeeFloatNotequal => lhs.typ == rhs.typ && lhs.typ.is_floating_point(),
// Overflow flags
OverflowMinus => {
(lhs.typ == rhs.typ && (lhs.typ.is_pointer() || lhs.typ.is_numeric()))
|| (lhs.typ.is_pointer() && rhs.typ.is_integer())
}
OverflowMult | OverflowPlus => {
(lhs.typ == rhs.typ && lhs.typ.is_integer())
|| (lhs.typ.is_pointer() && rhs.typ.is_integer())
// While one can technically use these on pointers, the result is treated as an integer.
// This is almost never what you actually want.
OverflowMinus | OverflowMult | OverflowPlus => {
lhs.typ.is_integer() && rhs.typ.is_integer()
}
}
}
Expand Down Expand Up @@ -1205,7 +1203,18 @@ impl Expr {
/// `ArithmeticOverflowResult r; >>>r.overflowed = builtin_add_overflow(self, e, &r.result)<<<`
pub fn add_overflow(self, e: Expr) -> ArithmeticOverflowResult {
let result = self.clone().plus(e.clone());
let overflowed = self.add_overflow_p(e);
// FIXME: https://github.com/model-checking/kani/issues/786
// Overflow checking on pointers is hard to do at the IREP level.
// Instead, we rely on `--pointer-overflow-check` in CBMC.
let overflowed = if self.typ.is_pointer() {
warn!(
"Overflow operations are not properly supported on pointer types {:?} {:?}",
self, e
);
Expr::bool_false()
} else {
self.add_overflow_p(e)
};
ArithmeticOverflowResult { result, overflowed }
}

Expand Down

0 comments on commit af8d832

Please sign in to comment.