Skip to content

Commit accc412

Browse files
committed
goto-instrument --remove-function-body
Removes the implementation of a function (but not its declaration or its call sites) from a goto program. This enables stubbing of possibly costly functions, such as custom memset implementations.
1 parent d999e56 commit accc412

File tree

8 files changed

+176
-1
lines changed

8 files changed

+176
-1
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* GOTO-INSTRUMENT: New option --print-path-lenghts
88
* GOTO-ANALYZER: New option --unreachable-functions, --reachable-functions
99
* GOTO-INSTRUMENT: New option --undefined-function-is-assume-false
10+
* GOTO-INSTRUMENT: New option --remove-function-body
1011

1112

1213
5.7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <assert.h>
2+
3+
int foo()
4+
{
5+
int a;
6+
assert(a>0);
7+
return a;
8+
}
9+
10+
int bar()
11+
{
12+
int b;
13+
assert(b>0);
14+
return b;
15+
}
16+
17+
int main()
18+
{
19+
foo();
20+
bar();
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--remove-function-body foo --remove-function-body bar
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
^bar
10+
^foo

src/goto-instrument/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ SRC = accelerate/accelerate.cpp \
4242
points_to.cpp \
4343
race_check.cpp \
4444
reachability_slicer.cpp \
45+
remove_function.cpp \
4546
rw_set.cpp \
4647
show_locations.cpp \
4748
skip_loops.cpp \

src/goto-instrument/goto_instrument_parse_options.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Author: Daniel Kroening, [email protected]
9090
#include "unwind.h"
9191
#include "model_argc_argv.h"
9292
#include "undefined_functions.h"
93+
#include "remove_function.h"
9394

9495
/*******************************************************************\
9596
@@ -1034,6 +1035,15 @@ void goto_instrument_parse_optionst::instrument_goto_program()
10341035
throw 0;
10351036
}
10361037

1038+
if(cmdline.isset("remove-function-body"))
1039+
{
1040+
remove_functions(
1041+
symbol_table,
1042+
goto_functions,
1043+
cmdline.get_values("remove-function-body"),
1044+
get_message_handler());
1045+
}
1046+
10371047
// we add the library in some cases, as some analyses benefit
10381048

10391049
if(cmdline.isset("add-library") ||
@@ -1634,6 +1644,8 @@ void goto_instrument_parse_optionst::help()
16341644
HELP_REMOVE_CONST_FUNCTION_POINTERS
16351645
" --add-library add models of C library functions\n"
16361646
" --model-argc-argv <n> model up to <n> command line arguments\n"
1647+
// NOLINTNEXTLINE(whitespace/line_length)
1648+
" --remove-function-body <f> remove the implementation of function <f> (may be repeated)\n"
16371649
"\n"
16381650
"Other options:\n"
16391651
" --use-system-headers with --dump-c/--dump-cpp: generate C source with includes\n" // NOLINT(*)

src/goto-instrument/goto_instrument_parse_options.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ Author: Daniel Kroening, [email protected]
7272
"(z3)(add-library)(show-dependence-graph)" \
7373
"(horn)(skip-loops):(apply-code-contracts)(model-argc-argv):" \
7474
"(show-threaded)(list-calls-args)(print-path-lengths)" \
75-
"(undefined-function-is-assume-false)"
75+
"(undefined-function-is-assume-false)" \
76+
"(remove-function-body):"
7677

7778
class goto_instrument_parse_optionst:
7879
public parse_options_baset,
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*******************************************************************\
2+
3+
Module: Remove function definition
4+
5+
Author: Michael Tautschnig
6+
7+
Date: April 2017
8+
9+
\*******************************************************************/
10+
11+
#include "remove_function.h"
12+
13+
#include <util/message.h>
14+
15+
#include <goto-programs/goto_functions.h>
16+
17+
/*******************************************************************\
18+
19+
Function: remove_function
20+
21+
Inputs:
22+
symbol_table Input symbol table to be modified
23+
goto_functions Input functions to be modified
24+
identifier Function to be removed
25+
message_handler Error/status output
26+
27+
Outputs:
28+
29+
Purpose: Remove the body of function "identifier" such that an
30+
analysis will treat it as a side-effect free function with
31+
non-deterministic return value.
32+
33+
\*******************************************************************/
34+
35+
void remove_function(
36+
symbol_tablet &symbol_table,
37+
goto_functionst &goto_functions,
38+
const irep_idt &identifier,
39+
message_handlert &message_handler)
40+
{
41+
messaget message(message_handler);
42+
43+
goto_functionst::function_mapt::iterator entry=
44+
goto_functions.function_map.find(identifier);
45+
46+
if(entry==goto_functions.function_map.end())
47+
{
48+
message.error() << "No function " << identifier
49+
<< " in goto program" << messaget::eom;
50+
return;
51+
}
52+
else if(entry->second.is_inlined())
53+
{
54+
message.warning() << "Function " << identifier << " is inlined, "
55+
<< "instantiations will not be removed"
56+
<< messaget::eom;
57+
}
58+
59+
if(entry->second.body_available())
60+
{
61+
message.status() << "Removing body of " << identifier
62+
<< messaget::eom;
63+
entry->second.clear();
64+
symbol_table.lookup(identifier).value.make_nil();
65+
}
66+
}
67+
68+
/*******************************************************************\
69+
70+
Function: remove_functions
71+
72+
Inputs:
73+
symbol_table Input symbol table to be modified
74+
goto_functions Input functions to be modified
75+
names List of functions to be removed
76+
message_handler Error/status output
77+
78+
Outputs:
79+
80+
Purpose: Remove the body of all functions listed in "names" such that
81+
an analysis will treat it as a side-effect free function with
82+
non-deterministic return value.
83+
84+
\*******************************************************************/
85+
86+
void remove_functions(
87+
symbol_tablet &symbol_table,
88+
goto_functionst &goto_functions,
89+
const std::list<std::string> &names,
90+
message_handlert &message_handler)
91+
{
92+
for(const auto &f : names)
93+
remove_function(symbol_table, goto_functions, f, message_handler);
94+
}

src/goto-instrument/remove_function.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************\
2+
3+
Module: Remove function definition
4+
5+
Author: Michael Tautschnig
6+
7+
Date: April 2017
8+
9+
\*******************************************************************/
10+
11+
#ifndef CPROVER_GOTO_INSTRUMENT_REMOVE_FUNCTION_H
12+
#define CPROVER_GOTO_INSTRUMENT_REMOVE_FUNCTION_H
13+
14+
#include <list>
15+
#include <string>
16+
17+
#include <util/irep.h>
18+
19+
class goto_functionst;
20+
class message_handlert;
21+
class symbol_tablet;
22+
23+
void remove_function(
24+
symbol_tablet &symbol_table,
25+
goto_functionst &goto_functions,
26+
const irep_idt &identifier,
27+
message_handlert &message_handler);
28+
29+
void remove_functions(
30+
symbol_tablet &symbol_table,
31+
goto_functionst &goto_functions,
32+
const std::list<std::string> &names,
33+
message_handlert &message_handler);
34+
35+
#endif // CPROVER_GOTO_INSTRUMENT_REMOVE_FUNCTION_H

0 commit comments

Comments
 (0)