Skip to content

Commit c08a090

Browse files
committed
User-provided function definitions take precedence over built-ins
We should not replace calls to functions that have a body provided by the user by our built-in definitions. While function names starting with "_" are reserved in C and thus users shouldn't be using them, it is certainly not true that anything named "nondet_*" is exclusively to be captured by our built-ins.
1 parent 8febc81 commit c08a090

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

regression/cbmc/Function14/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int nondet_foo()
2+
{
3+
return 42;
4+
}
5+
6+
int main()
7+
{
8+
int x = nondet_foo();
9+
__CPROVER_assert(x == 42, "nondet_foo returns a constant");
10+
}

regression/cbmc/Function14/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/goto-programs/builtin_functions.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,22 @@ void goto_convertt::do_function_call_symbol(
647647
throw 0;
648648
}
649649

650+
// User-provided function definitions always take precedence over built-ins.
651+
// Front-ends do not (yet) consistently set ID_C_incomplete, thus also test
652+
// whether the symbol actually has some non-nil value (which might be
653+
// "compiled").
654+
if(!symbol->type.get_bool(ID_C_incomplete) && symbol->value.is_not_nil())
655+
{
656+
do_function_call_symbol(*symbol);
657+
658+
code_function_callt function_call(lhs, function, arguments);
659+
function_call.add_source_location() = function.source_location();
660+
661+
copy(function_call, FUNCTION_CALL, dest);
662+
663+
return;
664+
}
665+
650666
if(identifier==CPROVER_PREFIX "assume" ||
651667
identifier=="__VERIFIER_assume")
652668
{
@@ -702,7 +718,7 @@ void goto_convertt::do_function_call_symbol(
702718
a->source_location.set("user-provided", true);
703719
}
704720
else if(
705-
identifier == "assert" && symbol->type.get_bool(ID_C_incomplete) &&
721+
identifier == "assert" &&
706722
to_code_type(symbol->type).return_type() == signed_int_type())
707723
{
708724
if(arguments.size()!=1)

0 commit comments

Comments
 (0)