Skip to content

Commit 7a4328a

Browse files
authored
Merge pull request #7337 from diffblue/smt2-pointer-plus
SMT2: fix pointer arithmetic
2 parents ef4e270 + ad1812a commit 7a4328a

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

regression/cbmc/Pointer24/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE broken-smt-backend
1+
CORE
22
main.c
33
--pointer-check
44
^EXIT=0$

regression/cbmc/pointer-overflow3/no-simplify.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE broken-smt-backend
1+
CORE
22
main.c
33
--pointer-overflow-check --no-simplify
44
^\[main.pointer_arithmetic.\d+\] line 6 pointer arithmetic: pointer outside object bounds in p \+ \(signed (long (long )?)?int\)10: FAILURE

src/solvers/smt2/smt2_conv.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,21 +3644,35 @@ void smt2_convt::convert_plus(const plus_exprt &expr)
36443644
element_size = *element_size_opt;
36453645
}
36463646

3647-
out << "(bvadd ";
3647+
// First convert the pointer operand
3648+
out << "(let ((?pointerop ";
36483649
convert_expr(p);
3649-
out << " ";
3650+
out << ")) ";
3651+
3652+
// The addition is done on the offset part only.
3653+
const std::size_t pointer_width = boolbv_width(p.type());
3654+
const std::size_t offset_bits =
3655+
pointer_width - config.bv_encoding.object_bits;
3656+
3657+
out << "(concat ";
3658+
out << "((_ extract " << pointer_width - 1 << ' ' << offset_bits
3659+
<< ") ?pointerop) ";
3660+
out << "(bvadd ((_ extract " << offset_bits - 1 << " 0) ?pointerop) ";
36503661

36513662
if(element_size >= 2)
36523663
{
3653-
out << "(bvmul ";
3664+
out << "(bvmul ((_ extract " << offset_bits - 1 << " 0) ";
36543665
convert_expr(i);
3655-
out << " (_ bv" << element_size << " " << boolbv_width(expr.type())
3656-
<< "))";
3666+
out << ") (_ bv" << element_size << " " << offset_bits << "))";
36573667
}
36583668
else
3669+
{
3670+
out << "((_ extract " << offset_bits - 1 << " 0) ";
36593671
convert_expr(i);
3672+
out << ')'; // extract
3673+
}
36603674

3661-
out << ')';
3675+
out << ")))"; // bvadd, concat, let
36623676
}
36633677
else
36643678
{

0 commit comments

Comments
 (0)