Skip to content

Commit c78b4c8

Browse files
committed
DFCC instrumentation: skip unused functions
Do not attempt to instrument functions that will never be used anyway. As we eventually use `remove_unused_functions` there is no point in trying to instrument them, and there is a scenario where the function may not have been compiled (see included test).
1 parent 9db5b1a commit c78b4c8

File tree

3 files changed

+48
-1
lines changed
  • regression/contracts-dfcc/skip_unused_instrumentation
  • src/goto-instrument/contracts/dynamic-frames

3 files changed

+48
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Using a forward declaration rather than including netdb.h to make sure we do
2+
// not have the compiled body of functions from header files available right
3+
// away.
4+
struct hostent;
5+
struct hostent *gethostbyname(const char *name);
6+
7+
int main()
8+
{
9+
(void)gethostbyname("x");
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
This test checks that functions brought in from header files when loading
10+
entries from the model library are not subject to instrumentation (when those
11+
functions are never used). Else we end up with invariant failures like:
12+
[...]
13+
Instrumenting '__bswap_16'
14+
--- begin invariant violation report ---
15+
Invariant check failed
16+
File: /src/goto-instrument/contracts/dynamic-frames/dfcc_instrument.cpp:329 function: instrument_function
17+
Condition: Precondition
18+
Reason: found != goto_model.goto_functions.function_map.end()
19+
Backtrace:
20+
build/bin/goto-instrument(+0xbfe182) [0x649c3d22b182]
21+
[...]
22+
/lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x78dc2f029d90]
23+
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x78dc2f029e40]
24+
build/bin/goto-instrument(+0x363cb5) [0x649c3c990cb5]
25+
26+
Diagnostics:
27+
<< EXTRA DIAGNOSTICS >>
28+
Function '__bswap_16' must exist in the model.
29+
<< END EXTRA DIAGNOSTICS >>
30+
31+
--- end invariant violation report ---

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ void dfcct::partition_function_symbols(
257257
std::set<irep_idt> &contract_symbols,
258258
std::set<irep_idt> &other_symbols)
259259
{
260+
std::set<irep_idt> called_functions;
261+
find_used_functions(
262+
goto_functionst::entry_point(),
263+
goto_model.goto_functions,
264+
called_functions);
265+
260266
// collect contract and other symbols
261267
for(auto &entry : goto_model.symbol_table)
262268
{
@@ -272,7 +278,7 @@ void dfcct::partition_function_symbols(
272278
{
273279
contract_symbols.insert(sym_name);
274280
}
275-
else
281+
else if(called_functions.find(sym_name) != called_functions.end())
276282
{
277283
// it is not a contract
278284
other_symbols.insert(sym_name);

0 commit comments

Comments
 (0)