Skip to content

Commit 4085b97

Browse files
authored
Merge pull request #7124 from NlightNFotis/fix_minus_exprt_support_incr_smt2
Add support for subtracting a constant value from a pointer in the conversion code of new SMT backend
2 parents ef7291c + 731ff1f commit 4085b97

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

src/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -724,15 +724,17 @@ static smt_termt convert_expr_to_smt(
724724
}
725725
else if(one_operand_pointer)
726726
{
727-
UNIMPLEMENTED_FEATURE(
728-
"convert_expr_to_smt::minus_exprt doesn't handle expressions where"
729-
"only one operand is a pointer - this is because these expressions"
730-
"are normally handled by convert_expr_to_smt::plus_exprt due to"
731-
"transformations of the expressions by previous passes bringing"
732-
"them into a form more suitably handled by that version of the function."
733-
"If you are here, this is a mistake or something went wrong before."
734-
"The expression that caused the problem is: " +
735-
minus.pretty());
727+
// It's semantically void to have an expression `3 - a` where `a`
728+
// is a pointer.
729+
INVARIANT(
730+
lhs_is_pointer,
731+
"minus expressions of pointer and integer expect lhs to be the pointer");
732+
const auto lhs_base_type = to_pointer_type(minus.lhs().type()).base_type();
733+
734+
return smt_bit_vector_theoryt::subtract(
735+
converted.at(minus.lhs()),
736+
smt_bit_vector_theoryt::multiply(
737+
converted.at(minus.rhs()), pointer_sizes.at(lhs_base_type)));
736738
}
737739
else
738740
{

unit/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,20 +557,49 @@ TEST_CASE(
557557
smt_term_four_32bit));
558558
}
559559

560-
SECTION(
561-
"Ensure that conversion of a minus node with only one operand"
562-
"being a pointer fails")
560+
SECTION("Subtraction of an integer value from a pointer")
563561
{
564562
// (*int32_t)a - 2
565-
const cbmc_invariants_should_throwt invariants_throw;
566-
// We don't support that - look at the test above.
563+
564+
// NOTE: This may look similar to the above, but the above test
565+
// is a desugared version of this construct - with the difference
566+
// being that there exist cases where the construct is not desugared,
567+
// so we can still come across this expression as an input to
568+
// convert_expr_to_smt.
569+
const auto two_bvint = from_integer(2, signedbv_typet{pointer_width});
567570
const auto pointer_arith_expr = minus_exprt{pointer_a, two_bvint};
571+
572+
const symbol_tablet symbol_table;
573+
const namespacet ns{symbol_table};
574+
track_expression_objects(pointer_arith_expr, ns, test.object_map);
575+
associate_pointer_sizes(
576+
pointer_arith_expr,
577+
ns,
578+
test.pointer_sizes,
579+
test.object_map,
580+
test.object_size_function.make_application);
581+
INFO("Input expr: " + pointer_arith_expr.pretty(2, 0));
582+
const auto constructed_term = test.convert(pointer_arith_expr);
583+
const auto expected_term = smt_bit_vector_theoryt::subtract(
584+
smt_term_a,
585+
smt_bit_vector_theoryt::multiply(
586+
smt_term_two_32bit, smt_term_four_32bit));
587+
REQUIRE(constructed_term == expected_term);
588+
}
589+
590+
SECTION("Subtraction of pointer from integer")
591+
{
592+
// 2 - (*int32_t)a -- Semantically void expression, need to make sure
593+
// we throw in this case.
594+
const cbmc_invariants_should_throwt invariants_throw;
595+
596+
const auto pointer_arith_expr = minus_exprt{two_bvint, pointer_a};
597+
568598
REQUIRE_THROWS_MATCHES(
569599
test.convert(pointer_arith_expr),
570600
invariant_failedt,
571-
invariant_failure_containing(
572-
"convert_expr_to_smt::minus_exprt doesn't handle expressions where"
573-
"only one operand is a pointer - this is because these expressions"));
601+
invariant_failure_containing("minus expressions of pointer and integer "
602+
"expect lhs to be the pointer"));
574603
}
575604

576605
SECTION("Subtraction of two pointer arguments")

0 commit comments

Comments
 (0)