Skip to content

Commit b7158df

Browse files
committed
Abort after failing C assert
The C standard specifies behaviour of the "assert" macro as resulting in an abort when the condition does not evaluate to true. Implement this behaviour by inserting assume(0) after assert(0).
1 parent 60c388e commit b7158df

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

regression/cbmc/Bool5/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CORE
22
main.c
33

4-
Generated 4 VCC\(s\), 0 remaining after simplification
4+
Generated [34] VCC\(s\), 0 remaining after simplification
55
^VERIFICATION SUCCESSFUL$
66
^EXIT=0$
77
^SIGNAL=0$

regression/cbmc/r_w_ok1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CORE
22
main.c
33

44
__CPROVER_[rw]_ok\(arbitrary_size, n \+ 1\): FAILURE$
5-
^\*\* 2 of 12 failed
5+
^\*\* [12] of 12 failed
66
^VERIFICATION FAILED$
77
^EXIT=10$
88
^SIGNAL=0$

regression/goto-instrument/assert1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ main.c
55
^SIGNAL=0$
66
--
77
^warning: ignoring
8-
^[[:space:]]*IF
8+
^[[:space:]]*if

src/goto-instrument/goto_program2code.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,23 @@ goto_programt::const_targett goto_program2codet::convert_instruction(
188188
return convert_decl(target, upper_bound, dest);
189189

190190
case ASSERT:
191+
{
191192
system_headers.insert("assert.h");
192193
dest.add(code_assertt(target->get_condition()));
193194
dest.statements().back().add_source_location().set_comment(
194195
target->source_location.get_comment());
196+
197+
goto_programt::const_targett next = target;
198+
++next;
199+
CHECK_RETURN(next != goto_program.instructions.end());
200+
if(
201+
next != upper_bound && next->is_assume() &&
202+
target->get_condition() == next->get_condition())
203+
{
204+
++target;
205+
}
195206
return target;
207+
}
196208

197209
case ASSUME:
198210
dest.add(code_assumet(target->guard));

src/goto-programs/builtin_functions.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,12 @@ void goto_convertt::do_function_call_symbol(
760760
error() << identifier << " expected not to have LHS" << eom;
761761
throw 0;
762762
}
763+
764+
// The C standard mandates that a failing assertion causes execution to
765+
// abort:
766+
dest.add(goto_programt::make_assumption(
767+
typecast_exprt::conditional_cast(arguments.front(), bool_typet()),
768+
function.source_location()));
763769
}
764770
else if(identifier == CPROVER_PREFIX "enum_is_in_range")
765771
{
@@ -1004,6 +1010,11 @@ void goto_convertt::do_function_call_symbol(
10041010
t->source_location.set_property_class(ID_assertion);
10051011
t->source_location.set_comment(description);
10061012
// we ignore any LHS
1013+
1014+
// The C standard mandates that a failing assertion causes execution to
1015+
// abort:
1016+
dest.add(goto_programt::make_assumption(
1017+
false_exprt(), function.source_location()));
10071018
}
10081019
else if(identifier=="__assert_rtn" ||
10091020
identifier=="__assert")
@@ -1042,6 +1053,11 @@ void goto_convertt::do_function_call_symbol(
10421053
t->source_location.set_property_class(ID_assertion);
10431054
t->source_location.set_comment(description);
10441055
// we ignore any LHS
1056+
1057+
// The C standard mandates that a failing assertion causes execution to
1058+
// abort:
1059+
dest.add(goto_programt::make_assumption(
1060+
false_exprt(), function.source_location()));
10451061
}
10461062
else if(identifier=="__assert_func")
10471063
{
@@ -1076,6 +1092,11 @@ void goto_convertt::do_function_call_symbol(
10761092
t->source_location.set_property_class(ID_assertion);
10771093
t->source_location.set_comment(description);
10781094
// we ignore any LHS
1095+
1096+
// The C standard mandates that a failing assertion causes execution to
1097+
// abort:
1098+
dest.add(goto_programt::make_assumption(
1099+
false_exprt(), function.source_location()));
10791100
}
10801101
else if(identifier==CPROVER_PREFIX "fence")
10811102
{

0 commit comments

Comments
 (0)