Skip to content

DFCC instrumentation: skip unused functions #8628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions regression/contracts-dfcc/skip_unused_instrumentation/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Using a forward declaration rather than including netdb.h to make sure we do
// not have the compiled body of functions from header files available right
// away.
struct hostent;
struct hostent *gethostbyname(const char *name);

int main()
{
(void)gethostbyname("x");
}
31 changes: 31 additions & 0 deletions regression/contracts-dfcc/skip_unused_instrumentation/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CORE
main.c

^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
--
This test checks that functions brought in from header files when loading
entries from the model library are not subject to instrumentation (when those
functions are never used). Else we end up with invariant failures like:
[...]
Instrumenting '__bswap_16'
--- begin invariant violation report ---
Invariant check failed
File: /src/goto-instrument/contracts/dynamic-frames/dfcc_instrument.cpp:329 function: instrument_function
Condition: Precondition
Reason: found != goto_model.goto_functions.function_map.end()
Backtrace:
build/bin/goto-instrument(+0xbfe182) [0x649c3d22b182]
[...]
/lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x78dc2f029d90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x78dc2f029e40]
build/bin/goto-instrument(+0x363cb5) [0x649c3c990cb5]

Diagnostics:
<< EXTRA DIAGNOSTICS >>
Function '__bswap_16' must exist in the model.
<< END EXTRA DIAGNOSTICS >>

--- end invariant violation report ---
8 changes: 7 additions & 1 deletion src/goto-instrument/contracts/dynamic-frames/dfcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@
std::set<irep_idt> &contract_symbols,
std::set<irep_idt> &other_symbols)
{
std::set<irep_idt> called_functions;
find_used_functions(
goto_functionst::entry_point(),

Check warning on line 262 in src/goto-instrument/contracts/dynamic-frames/dfcc.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-instrument/contracts/dynamic-frames/dfcc.cpp#L262

Added line #L262 was not covered by tests
goto_model.goto_functions,
called_functions);

// collect contract and other symbols
for(auto &entry : goto_model.symbol_table)
{
Expand All @@ -272,7 +278,7 @@
{
contract_symbols.insert(sym_name);
}
else
else if(called_functions.find(sym_name) != called_functions.end())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the program uses function pointers find_used_functions is not guaranteed to discover all reachable functions. Moreover users can expand function pointer calls after contract instrumentation, so we should at least inject assert(false);assume(false) sentinel instructions in the functions we skip.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then we use use remove_unused_functions afterwards, which itself uses find_used_functions` to determine which functions to remove, so I wouldn't know how users would do something after contract instrumentation?

Copy link
Collaborator

@remi-delmas-3000 remi-delmas-3000 Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not convinced we are totally sound. CBMC removes function pointers very late, right before the analysis, so if we remove functions that we think are unused, but could have been used as a target due to their signature, wouldn't we create a change in semantics for function pointers between contracts and basic symex ?

What about a case like this one ? Would intfun_contract still be considered used ?
Does find_used_functions consider a function used if its address is taken and assigned to a function pointer variable (e.g. intfun bar = baz;)?

typedef int (*intfun)(int);

intfun __VERIFIER_nondet_intfun();

int intfun_contract(int x)
__CPROVER_requires(-12 <= x && x <= 12)
__CPROVER_ensures(__CPROVER_return_value == 2*x)
;

int foo(intfun bar)
__CPROVER_requires(__CPROVER_obeys_contract(bar, intfun_contract))
__CPROVER_ensures(__CPROVER_return_value == 24)
{
  return bar(12);
}

{
// it is not a contract
other_symbols.insert(sym_name);
Expand Down
Loading