Skip to content

Fix for getting values of pointers in traces in new SMT backend #6831

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 4 commits into from
Apr 27, 2022
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
12 changes: 12 additions & 0 deletions regression/cbmc-incr-smt2/pointers/pointer_values.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
int main()
{
int *a;
int *b = 0;
int *c;
__CPROVER_assume(c != 0);

__CPROVER_assert(
a != b, "should fail because a can be any pointer val, including NULL");
__CPROVER_assert(
a != c, "should fail because a and c can point to same object");
}
12 changes: 12 additions & 0 deletions regression/cbmc-incr-smt2/pointers/pointer_values.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE
pointer_values.c
--trace
\[main\.assertion\.1\] line \d+ should fail because a can be any pointer val, including NULL: FAILURE
\[main\.assertion\.2\] line \d+ should fail because a and c can point to same object: FAILURE
a=\(\(signed int \*\)NULL\)
c=\(signed int \*\)1
^EXIT=10$
^SIGNAL=0$
--
--
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ Missing test description - "Test printing of NULL pointer values in trace."

Test printing of NULL pointer values in trace.
8 changes: 8 additions & 0 deletions regression/cbmc-incr-smt2/pointers/pointer_values_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
int main()
{
int *a;
int *b;
__CPROVER_assume(a == 0xDEADBEEF);

__CPROVER_assert(a != b, "should fail as b can also be assigned 0xDEADBEEF");
}
12 changes: 12 additions & 0 deletions regression/cbmc-incr-smt2/pointers/pointer_values_2.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE
pointer_values_2.c
--trace --slice-formula
\[main\.assertion\.1\] line \d should fail as b can also be assigned 0xDEADBEEF: FAILURE
a=\(signed int \*\)3735928559
b=\(signed int \*\)3735928559
^EXIT=10$
^SIGNAL=0$
--
--
3735928559 is the decimal value of the hex constant 0xDEADBEEF that
the pointer is assumed to point to in this example.
34 changes: 28 additions & 6 deletions src/solvers/smt2_incremental/construct_value_expr_from_smt.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Author: Diffblue Ltd.

#include <solvers/smt2_incremental/construct_value_expr_from_smt.h>

#include <solvers/smt2_incremental/smt_terms.h>

#include <util/arith_tools.h>
#include <util/bitvector_types.h>
#include <util/pointer_expr.h>
#include <util/std_expr.h>
#include <util/std_types.h>
#include <util/type.h>

#include <solvers/smt2_incremental/construct_value_expr_from_smt.h>
#include <solvers/smt2_incremental/smt_terms.h>

class value_expr_from_smt_factoryt : public smt_term_const_downcast_visitort
{
private:
Expand Down Expand Up @@ -37,13 +37,35 @@ class value_expr_from_smt_factoryt : public smt_term_const_downcast_visitort

void visit(const smt_bit_vector_constant_termt &bit_vector_constant) override
{
const auto sort_width = bit_vector_constant.get_sort().bit_width();
if(
const auto pointer_type =
type_try_dynamic_cast<pointer_typet>(type_to_construct))
{
INVARIANT(
pointer_type->get_width() == sort_width,
"Width of smt bit vector term must match the width of pointer type.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ This change appears to have been squashed into the wrong commit.

if(bit_vector_constant.value() == 0)
{
result = null_pointer_exprt{*pointer_type};
}
else
{
// The reason we are manually constructing a constant_exprt here is a
// limitation in the design of `from_integer`, which only allows it to
// be used with pointer values of 0 (null pointers).
result = constant_exprt{
integer2bvrep(bit_vector_constant.value(), sort_width),
*pointer_type};
}
return;
}
if(
const auto bitvector_type =
type_try_dynamic_cast<bitvector_typet>(type_to_construct))
{
INVARIANT(
bitvector_type->get_width() ==
bit_vector_constant.get_sort().bit_width(),
bitvector_type->get_width() == sort_width,
"Width of smt bit vector term must match the width of bit vector "
"type.");
result = from_integer(bit_vector_constant.value(), type_to_construct);
Expand Down
44 changes: 31 additions & 13 deletions unit/solvers/smt2_incremental/construct_value_expr_from_smt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ TEST_CASE("Value expr construction from smt.", "[core][smt2_incremental]")
rowt{smt_bool_literal_termt{false}, false_exprt{}},
rowt{smt_bit_vector_constant_termt{0, 8}, from_integer(0, c_bool_typet(8))},
rowt{smt_bit_vector_constant_termt{1, 8}, from_integer(1, c_bool_typet(8))},
rowt{
smt_bit_vector_constant_termt{0, 64},
from_integer(0, pointer_typet(void_type(), 64 /* bits */))},
// The reason for the more intricate elaboration of a pointer with a value
// of 12 is a limitation in the design of from_integer, which only handles
// pointers with value 0 (null pointers).
rowt{
smt_bit_vector_constant_termt{12, 64},
constant_exprt(
integer2bvrep(12, 64), pointer_typet(void_type(), 64 /* bits */))},
UNSIGNED_BIT_VECTOR_TESTS(8),
SIGNED_BIT_VECTOR_TESTS(8),
UNSIGNED_BIT_VECTOR_TESTS(16),
Expand Down Expand Up @@ -96,23 +106,31 @@ TEST_CASE(

using rowt = std::tuple<smt_termt, typet, std::string>;
std::tie(input_term, input_type, invariant_reason) = GENERATE(
rowt{smt_bool_literal_termt{true},
unsignedbv_typet{16},
"Bool terms may only be used to construct bool typed expressions."},
rowt{smt_identifier_termt{"foo", smt_bit_vector_sortt{16}},
unsignedbv_typet{16},
"Unexpected conversion of identifier to value expression."},
rowt{
smt_bool_literal_termt{true},
unsignedbv_typet{16},
"Bool terms may only be used to construct bool typed expressions."},
rowt{
smt_identifier_termt{"foo", smt_bit_vector_sortt{16}},
unsignedbv_typet{16},
"Unexpected conversion of identifier to value expression."},
rowt{
smt_bit_vector_constant_termt{0, 8},
unsignedbv_typet{16},
"Width of smt bit vector term must match the width of bit vector type."},
rowt{smt_bit_vector_constant_termt{0, 8},
empty_typet{},
"construct_value_expr_from_smt for bit vector should not be applied "
"to unsupported type empty"},
rowt{smt_core_theoryt::make_not(smt_bool_literal_termt{true}),
unsignedbv_typet{16},
"Unexpected conversion of function application to value expression."});
rowt{
smt_bit_vector_constant_termt{0, 8},
empty_typet{},
"construct_value_expr_from_smt for bit vector should not be applied "
"to unsupported type empty"},
rowt{
smt_core_theoryt::make_not(smt_bool_literal_termt{true}),
unsignedbv_typet{16},
"Unexpected conversion of function application to value expression."},
rowt{
smt_bit_vector_constant_termt{0, 16},
pointer_typet{unsigned_int_type(), 0},
"Width of smt bit vector term must match the width of pointer type"});
SECTION(invariant_reason)
{
const cbmc_invariants_should_throwt invariants_throw;
Expand Down