Skip to content

Commit 7075866

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

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

src/goto-instrument/contracts/contracts.cpp

Lines changed: 23 additions & 37 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(
@@ -748,8 +741,7 @@ void code_contractst::instrument_call_statement(
748741
called_name =
749742
to_symbol_expr(instruction_iterator->call_function()).get_identifier();
750743
}
751-
log.warning() << "called function: " << id2string(called_name)
752-
<< messaget::eom;
744+
753745
if(called_name == "malloc")
754746
{
755747
// malloc statments return a void pointer, which is then cast and assigned
@@ -767,37 +759,24 @@ void code_contractst::instrument_call_statement(
767759
}
768760
else if(called_name == "free")
769761
{
770-
goto_programt alias_assertion;
771762
const exprt &lhs_dereference = dereference_exprt(
772763
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");
764+
add_containment_check(
765+
program, assigns_clause, instruction_iterator, lhs_dereference);
779766
assigns_clause.remove_from_local_write_set(lhs_dereference);
780767
assigns_clause.remove_from_global_write_set(lhs_dereference);
781-
insert_before_swap_and_advance(
782-
program, instruction_iterator, alias_assertion);
783768
return;
784769
}
785770

786771
if(
787772
instruction_iterator->call_lhs().is_not_nil() &&
788773
instruction_iterator->call_lhs().id() == ID_symbol)
789774
{
790-
const auto alias_expr = assigns_clause.generate_containment_check(
775+
add_containment_check(
776+
program,
777+
assigns_clause,
778+
instruction_iterator,
791779
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);
801780
}
802781

803782
const symbolt &called_symbol = ns.lookup(called_name);
@@ -977,22 +956,29 @@ void code_contractst::check_frame_conditions(
977956
instruction_it->is_other() &&
978957
instruction_it->get_other().get_statement() == ID_havoc_object)
979958
{
980-
goto_programt alias_assertion;
981959
const exprt &havoc_argument = dereference_exprt(
982960
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");
961+
add_containment_check(program, assigns, instruction_it, havoc_argument);
989962
assigns.remove_from_local_write_set(havoc_argument);
990963
assigns.remove_from_global_write_set(havoc_argument);
991-
insert_before_swap_and_advance(program, instruction_it, alias_assertion);
992964
}
993965
}
994966
}
995967

968+
void code_contractst::add_containment_check(
969+
goto_programt &program,
970+
const assigns_clauset &assigns,
971+
goto_programt::instructionst::iterator &instruction_it,
972+
const exprt &expr)
973+
{
974+
goto_programt assertion;
975+
assertion.add(goto_programt::make_assertion(
976+
assigns.generate_containment_check(expr), instruction_it->source_location));
977+
assertion.instructions.back().source_location.set_comment(
978+
"Check that " + from_expr(ns, expr.id(), expr) + " is assignable");
979+
insert_before_swap_and_advance(program, instruction_it, assertion);
980+
}
981+
996982
bool code_contractst::enforce_contract(const irep_idt &function)
997983
{
998984
// 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+
const 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)