Skip to content

Commit 5b95d2b

Browse files
Merge pull request #7285 from esteffin/esteffin/byte-extraction-update-2
Support byte extract and update for new SMT backend
2 parents b4a4122 + 424bed5 commit 5b95d2b

16 files changed

+442
-58
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
int main()
5+
{
6+
uint16_t x[3];
7+
x[0] = 0;
8+
x[1] = 0;
9+
x[2] = 0;
10+
uint8_t *c = x;
11+
c[1] = 1;
12+
assert(x[0] == 256);
13+
assert(x[0] == 0);
14+
assert(x[1] == 0);
15+
assert(x[2] == 0);
16+
uint16_t between = c[1];
17+
assert(between == 1);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
array-misalign-between.c
3+
--slice-formula
4+
\[main\.assertion\.1\] line \d+ assertion x\[0\] == 256: SUCCESS
5+
\[main\.assertion\.2\] line \d+ assertion x\[0\] == 0: FAILURE
6+
\[main\.assertion\.3\] line \d+ assertion x\[1\] == 0: SUCCESS
7+
\[main\.assertion\.4\] line \d+ assertion x\[2\] == 0: SUCCESS
8+
\[main\.assertion\.5\] line \d+ assertion between == 1: SUCCESS
9+
^EXIT=10$
10+
^SIGNAL=0$
11+
--
12+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
int main()
5+
{
6+
uint16_t x[3];
7+
x[0] = 0;
8+
x[1] = 0;
9+
x[2] = 0;
10+
uint8_t *c = x;
11+
c[1] = 1;
12+
assert(x[0] == 256ul);
13+
assert(x[0] == 0ul);
14+
assert(x[1] == 0ul);
15+
assert(x[2] == 0ul);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
array-misalign.c
3+
--slice-formula
4+
\[main.assertion\.1\] line \d+ assertion x\[0\] == 256ul: SUCCESS
5+
\[main.assertion\.2\] line \d+ assertion x\[0\] == 0ul: FAILURE
6+
\[main.assertion\.3\] line \d+ assertion x\[1\] == 0ul: SUCCESS
7+
\[main.assertion\.4\] line \d+ assertion x\[2\] == 0ul: SUCCESS
8+
^EXIT=10$
9+
^SIGNAL=0$
10+
--
11+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
int main()
5+
{
6+
uint8_t x[2];
7+
x[0] = 1u;
8+
x[1] = 1u;
9+
uint16_t *y = x;
10+
uint16_t z = *y;
11+
assert(z == 257);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
byte-extract-small.c
3+
--slice-formula
4+
\[main.assertion\.1\] line \d+ assertion z == 257: SUCCESS
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
int main()
5+
{
6+
uint16_t x;
7+
uint8_t *y = &x;
8+
assert(y[0] == 0);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
byte-extract.c
3+
--slice-formula
4+
\[main.assertion\.1\] line \d+ assertion y\[0\] == 0: FAILURE
5+
^EXIT=10$
6+
^SIGNAL=0$
7+
--
8+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
int main()
5+
{
6+
uint8_t x[2];
7+
x[0] = 0;
8+
x[1] = 0;
9+
uint16_t *y = x;
10+
*y = 258;
11+
assert(x[0] == 2);
12+
assert(x[1] == 1);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
byte-update-small.c
3+
--slice-formula
4+
\[main\.assertion\.1\] line \d+ assertion x\[0\] == 2: SUCCESS
5+
\[main\.assertion\.2\] line \d+ assertion x\[1\] == 1: SUCCESS
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
int main()
5+
{
6+
uint16_t x = 0;
7+
uint8_t *y = &x;
8+
y[1] = 1;
9+
assert(x == 256);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
byte-update.c
3+
--slice-formula
4+
\[main\.assertion\.1\] line \d+ assertion x == 256: SUCCESS
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
--

src/solvers/smt2_incremental/convert_expr_to_smt.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ static smt_termt convert_expr_to_smt(
367367
const bitand_exprt &bitwise_and_expr,
368368
const sub_expression_mapt &converted)
369369
{
370-
if(operands_are_of_type<integer_bitvector_typet>(bitwise_and_expr))
370+
if(operands_are_of_type<bitvector_typet>(bitwise_and_expr))
371371
{
372372
return convert_multiary_operator_to_terms(
373373
bitwise_and_expr, converted, smt_bit_vector_theoryt::make_and);
@@ -384,7 +384,7 @@ static smt_termt convert_expr_to_smt(
384384
const bitor_exprt &bitwise_or_expr,
385385
const sub_expression_mapt &converted)
386386
{
387-
if(operands_are_of_type<integer_bitvector_typet>(bitwise_or_expr))
387+
if(operands_are_of_type<bitvector_typet>(bitwise_or_expr))
388388
{
389389
return convert_multiary_operator_to_terms(
390390
bitwise_or_expr, converted, smt_bit_vector_theoryt::make_or);
@@ -401,7 +401,7 @@ static smt_termt convert_expr_to_smt(
401401
const bitxor_exprt &bitwise_xor,
402402
const sub_expression_mapt &converted)
403403
{
404-
if(operands_are_of_type<integer_bitvector_typet>(bitwise_xor))
404+
if(operands_are_of_type<bitvector_typet>(bitwise_xor))
405405
{
406406
return convert_multiary_operator_to_terms(
407407
bitwise_xor, converted, smt_bit_vector_theoryt::make_xor);
@@ -418,10 +418,7 @@ static smt_termt convert_expr_to_smt(
418418
const bitnot_exprt &bitwise_not,
419419
const sub_expression_mapt &converted)
420420
{
421-
const bool operand_is_bitvector =
422-
can_cast_type<integer_bitvector_typet>(bitwise_not.op().type());
423-
424-
if(operand_is_bitvector)
421+
if(can_cast_type<bitvector_typet>(bitwise_not.op().type()))
425422
{
426423
return smt_bit_vector_theoryt::make_not(converted.at(bitwise_not.op()));
427424
}
@@ -436,9 +433,7 @@ static smt_termt convert_expr_to_smt(
436433
const unary_minus_exprt &unary_minus,
437434
const sub_expression_mapt &converted)
438435
{
439-
const bool operand_is_bitvector =
440-
can_cast_type<integer_bitvector_typet>(unary_minus.op().type());
441-
if(operand_is_bitvector)
436+
if(can_cast_type<integer_bitvector_typet>(unary_minus.op().type()))
442437
{
443438
return smt_bit_vector_theoryt::negate(converted.at(unary_minus.op()));
444439
}

src/solvers/smt2_incremental/smt2_incremental_decision_procedure.cpp

+36-20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <util/string_utils.h>
1212
#include <util/symbol.h>
1313

14+
#include <solvers/lowering/expr_lowering.h>
1415
#include <solvers/smt2_incremental/ast/smt_commands.h>
1516
#include <solvers/smt2_incremental/ast/smt_responses.h>
1617
#include <solvers/smt2_incremental/ast/smt_terms.h>
@@ -257,19 +258,23 @@ smt2_incremental_decision_proceduret::smt2_incremental_decision_proceduret(
257258
}
258259

259260
void smt2_incremental_decision_proceduret::ensure_handle_for_expr_defined(
260-
const exprt &expr)
261+
const exprt &in_expr)
261262
{
262263
if(
263-
expression_handle_identifiers.find(expr) !=
264+
expression_handle_identifiers.find(in_expr) !=
264265
expression_handle_identifiers.cend())
265266
{
266267
return;
267268
}
268269

269-
define_dependent_functions(expr);
270+
const exprt lowered_expr = lower_byte_operators(in_expr, ns);
271+
272+
define_dependent_functions(lowered_expr);
270273
smt_define_function_commandt function{
271-
"B" + std::to_string(handle_sequence()), {}, convert_expr_to_smt(expr)};
272-
expression_handle_identifiers.emplace(expr, function.identifier());
274+
"B" + std::to_string(handle_sequence()),
275+
{},
276+
convert_expr_to_smt(lowered_expr)};
277+
expression_handle_identifiers.emplace(in_expr, function.identifier());
273278
identifier_table.emplace(
274279
function.identifier().identifier(), function.identifier());
275280
solver_process->send(function);
@@ -283,15 +288,23 @@ void smt2_incremental_decision_proceduret::define_index_identifiers(
283288
return;
284289
if(const auto with_expr = expr_try_dynamic_cast<with_exprt>(expr_node))
285290
{
286-
const auto index_expr = with_expr->where();
287-
const auto index_term = convert_expr_to_smt(index_expr);
288-
const auto index_identifier = "index_" + std::to_string(index_sequence());
289-
const auto index_definition =
290-
smt_define_function_commandt{index_identifier, {}, index_term};
291-
expression_identifiers.emplace(index_expr, index_definition.identifier());
292-
identifier_table.emplace(index_identifier, index_definition.identifier());
293-
solver_process->send(
294-
smt_define_function_commandt{index_identifier, {}, index_term});
291+
for(auto operand_ite = ++with_expr->operands().begin();
292+
operand_ite != with_expr->operands().end();
293+
operand_ite += 2)
294+
{
295+
const auto index_expr = *operand_ite;
296+
const auto index_term = convert_expr_to_smt(index_expr);
297+
const auto index_identifier =
298+
"index_" + std::to_string(index_sequence());
299+
const auto index_definition =
300+
smt_define_function_commandt{index_identifier, {}, index_term};
301+
expression_identifiers.emplace(
302+
index_expr, index_definition.identifier());
303+
identifier_table.emplace(
304+
index_identifier, index_definition.identifier());
305+
solver_process->send(
306+
smt_define_function_commandt{index_identifier, {}, index_term});
307+
}
295308
}
296309
});
297310
}
@@ -476,22 +489,25 @@ smt2_incremental_decision_proceduret::get_number_of_solver_calls() const
476489
return number_of_solver_calls;
477490
}
478491

479-
void smt2_incremental_decision_proceduret::set_to(const exprt &expr, bool value)
492+
void smt2_incremental_decision_proceduret::set_to(
493+
const exprt &in_expr,
494+
bool value)
480495
{
481-
PRECONDITION(can_cast_type<bool_typet>(expr.type()));
496+
const exprt lowered_expr = lower_byte_operators(in_expr, ns);
497+
PRECONDITION(can_cast_type<bool_typet>(lowered_expr.type()));
482498
log.conditional_output(log.debug(), [&](messaget::mstreamt &debug) {
483499
debug << "`set_to` (" << std::string{value ? "true" : "false"} << ") -\n "
484-
<< expr.pretty(2, 0) << messaget::eom;
500+
<< lowered_expr.pretty(2, 0) << messaget::eom;
485501
});
486502

487-
define_dependent_functions(expr);
503+
define_dependent_functions(lowered_expr);
488504
auto converted_term = [&]() -> smt_termt {
489505
const auto expression_handle_identifier =
490-
expression_handle_identifiers.find(expr);
506+
expression_handle_identifiers.find(lowered_expr);
491507
if(expression_handle_identifier != expression_handle_identifiers.cend())
492508
return expression_handle_identifier->second;
493509
else
494-
return convert_expr_to_smt(expr);
510+
return convert_expr_to_smt(lowered_expr);
495511
}();
496512
if(!value)
497513
converted_term = smt_core_theoryt::make_not(converted_term);

0 commit comments

Comments
 (0)