Skip to content

Commit 96a15e3

Browse files
committed
Adds procedure to inject containment checks
Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent f7ff4bf commit 96a15e3

File tree

2 files changed

+30
-35
lines changed

2 files changed

+30
-35
lines changed

src/goto-instrument/contracts/contracts.cpp

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -715,14 +715,7 @@ void code_contractst::instrument_assign_statement(
715715
assigns_clause.add_to_local_write_set(lhs);
716716
}
717717

718-
goto_programt alias_assertion;
719-
alias_assertion.add(goto_programt::make_assertion(
720-
assigns_clause.generate_containment_check(lhs),
721-
instruction_iterator->source_location));
722-
alias_assertion.instructions.back().source_location.set_comment(
723-
"Check that " + from_expr(ns, lhs.id(), lhs) + " is assignable");
724-
insert_before_swap_and_advance(
725-
program, instruction_iterator, alias_assertion);
718+
add_containment_check(program, assigns_clause, instruction_iterator, lhs);
726719
}
727720

728721
void code_contractst::instrument_call_statement(
@@ -767,37 +760,24 @@ void code_contractst::instrument_call_statement(
767760
}
768761
else if(called_name == "free")
769762
{
770-
goto_programt alias_assertion;
771763
const exprt &lhs_dereference = dereference_exprt(
772764
to_typecast_expr(instruction_iterator->call_arguments().front()).op());
773-
alias_assertion.add(goto_programt::make_assertion(
774-
assigns_clause.generate_containment_check(lhs_dereference),
775-
instruction_iterator->source_location));
776-
alias_assertion.instructions.back().source_location.set_comment(
777-
"Check that " + from_expr(ns, lhs_dereference.id(), lhs_dereference) +
778-
" is assignable");
765+
add_containment_check(
766+
program, assigns_clause, instruction_iterator, lhs_dereference);
779767
assigns_clause.remove_from_local_write_set(lhs_dereference);
780768
assigns_clause.remove_from_global_write_set(lhs_dereference);
781-
insert_before_swap_and_advance(
782-
program, instruction_iterator, alias_assertion);
783769
return;
784770
}
785771

786772
if(
787773
instruction_iterator->call_lhs().is_not_nil() &&
788774
instruction_iterator->call_lhs().id() == ID_symbol)
789775
{
790-
const auto alias_expr = assigns_clause.generate_containment_check(
776+
add_containment_check(
777+
program,
778+
assigns_clause,
779+
instruction_iterator,
791780
instruction_iterator->call_lhs());
792-
793-
goto_programt alias_assertion;
794-
alias_assertion.add(goto_programt::make_assertion(
795-
alias_expr, instruction_iterator->source_location));
796-
alias_assertion.instructions.back().source_location.set_comment(
797-
"Check that " + from_expr(ns, alias_expr.id(), alias_expr) +
798-
" is assignable");
799-
insert_before_swap_and_advance(
800-
program, instruction_iterator, alias_assertion);
801781
}
802782

803783
const symbolt &called_symbol = ns.lookup(called_name);
@@ -977,22 +957,29 @@ void code_contractst::check_frame_conditions(
977957
instruction_it->is_other() &&
978958
instruction_it->get_other().get_statement() == ID_havoc_object)
979959
{
980-
goto_programt alias_assertion;
981960
const exprt &havoc_argument = dereference_exprt(
982961
to_typecast_expr(instruction_it->get_other().operands().front()).op());
983-
alias_assertion.add(goto_programt::make_assertion(
984-
assigns.generate_containment_check(havoc_argument),
985-
instruction_it->source_location));
986-
alias_assertion.instructions.back().source_location.set_comment(
987-
"Check that " + from_expr(ns, havoc_argument.id(), havoc_argument) +
988-
" is assignable");
962+
add_containment_check(program, assigns, instruction_it, havoc_argument);
989963
assigns.remove_from_local_write_set(havoc_argument);
990964
assigns.remove_from_global_write_set(havoc_argument);
991-
insert_before_swap_and_advance(program, instruction_it, alias_assertion);
992965
}
993966
}
994967
}
995968

969+
void code_contractst::add_containment_check(
970+
goto_programt &program,
971+
assigns_clauset &assigns,
972+
goto_programt::instructionst::iterator &instruction_it,
973+
const exprt &expr)
974+
{
975+
goto_programt assertion;
976+
assertion.add(goto_programt::make_assertion(
977+
assigns.generate_containment_check(expr), instruction_it->source_location));
978+
assertion.instructions.back().source_location.set_comment(
979+
"Check that " + from_expr(ns, expr.id(), expr) + " is assignable");
980+
insert_before_swap_and_advance(program, instruction_it, assertion);
981+
}
982+
996983
bool code_contractst::enforce_contract(const irep_idt &function)
997984
{
998985
// Add statements to the source function

src/goto-instrument/contracts/contracts.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ class code_contractst
152152
/// assigned memory is within the assignable memory frame.
153153
void check_frame_conditions(goto_programt &, assigns_clauset &);
154154

155+
/// Inserts an assertion into the goto program to ensure that
156+
/// an expression is within the assignable memory frame.
157+
void add_containment_check(
158+
goto_programt &,
159+
assigns_clauset &,
160+
goto_programt::instructionst::iterator &,
161+
const exprt &);
162+
155163
/// Check if there are any malloc statements which may be repeated because of
156164
/// a goto statement that jumps back.
157165
bool check_for_looped_mallocs(const goto_programt &program);

0 commit comments

Comments
 (0)