Skip to content

Commit 9940fd9

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 f437084 commit 9940fd9

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
@@ -187,11 +187,23 @@ goto_programt::const_targett goto_program2codet::convert_instruction(
187187
return convert_decl(target, upper_bound, dest);
188188

189189
case ASSERT:
190+
{
190191
system_headers.insert("assert.h");
191192
dest.add(code_assertt(target->get_condition()));
192193
dest.statements().back().add_source_location().set_comment(
193194
target->source_location.get_comment());
195+
196+
goto_programt::const_targett next = target;
197+
++next;
198+
CHECK_RETURN(next != goto_program.instructions.end());
199+
if(
200+
next != upper_bound && next->is_assume() &&
201+
target->get_condition() == next->get_condition())
202+
{
203+
++target;
204+
}
194205
return target;
206+
}
195207

196208
case ASSUME:
197209
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
@@ -758,6 +758,12 @@ void goto_convertt::do_function_call_symbol(
758758
error() << identifier << " expected not to have LHS" << eom;
759759
throw 0;
760760
}
761+
762+
// The C standard mandates that a failing assertion causes execution to
763+
// abort:
764+
dest.add(goto_programt::make_assumption(
765+
typecast_exprt::conditional_cast(arguments.front(), bool_typet()),
766+
function.source_location()));
761767
}
762768
else if(identifier == CPROVER_PREFIX "enum_is_in_range")
763769
{
@@ -1002,6 +1008,11 @@ void goto_convertt::do_function_call_symbol(
10021008
t->source_location.set_property_class(ID_assertion);
10031009
t->source_location.set_comment(description);
10041010
// we ignore any LHS
1011+
1012+
// The C standard mandates that a failing assertion causes execution to
1013+
// abort:
1014+
dest.add(goto_programt::make_assumption(
1015+
false_exprt(), function.source_location()));
10051016
}
10061017
else if(identifier=="__assert_rtn" ||
10071018
identifier=="__assert")
@@ -1040,6 +1051,11 @@ void goto_convertt::do_function_call_symbol(
10401051
t->source_location.set_property_class(ID_assertion);
10411052
t->source_location.set_comment(description);
10421053
// we ignore any LHS
1054+
1055+
// The C standard mandates that a failing assertion causes execution to
1056+
// abort:
1057+
dest.add(goto_programt::make_assumption(
1058+
false_exprt(), function.source_location()));
10431059
}
10441060
else if(identifier=="__assert_func")
10451061
{
@@ -1074,6 +1090,11 @@ void goto_convertt::do_function_call_symbol(
10741090
t->source_location.set_property_class(ID_assertion);
10751091
t->source_location.set_comment(description);
10761092
// we ignore any LHS
1093+
1094+
// The C standard mandates that a failing assertion causes execution to
1095+
// abort:
1096+
dest.add(goto_programt::make_assumption(
1097+
false_exprt(), function.source_location()));
10771098
}
10781099
else if(identifier==CPROVER_PREFIX "fence")
10791100
{

0 commit comments

Comments
 (0)