Skip to content

Commit 14875e9

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 064f648 commit 14875e9

File tree

8 files changed

+159
-1
lines changed

8 files changed

+159
-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,8 @@
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

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

+11
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
@@ -1037,6 +1038,15 @@ void goto_instrument_parse_optionst::instrument_goto_program()
10371038
throw 0;
10381039
}
10391040

1041+
if(cmdline.isset("remove-function-body"))
1042+
{
1043+
remove_functions(
1044+
symbol_table,
1045+
goto_functions,
1046+
cmdline.get_values("remove-function-body"),
1047+
get_message_handler());
1048+
}
1049+
10401050
// we add the library in some cases, as some analyses benefit
10411051

10421052
if(cmdline.isset("add-library") ||
@@ -1637,6 +1647,7 @@ void goto_instrument_parse_optionst::help()
16371647
HELP_REMOVE_CONST_FUNCTION_POINTERS
16381648
" --add-library add models of C library functions\n"
16391649
" --model-argc-argv <n> model up to <n> command line arguments\n"
1650+
" --remove-function-body <f> remove the implementation of function <f>\n"
16401651
"\n"
16411652
"Other options:\n"
16421653
" --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,
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*******************************************************************\
2+
3+
Module: Remove function definition
4+
5+
Author: Michael Tautschnig
6+
7+
Date: April 2017
8+
9+
\*******************************************************************/
10+
11+
#include <util/message.h>
12+
13+
#include <goto-programs/goto_functions.h>
14+
15+
#include "remove_function.h"
16+
17+
/*******************************************************************\
18+
19+
Function: remove_function
20+
21+
Inputs:
22+
23+
Outputs:
24+
25+
Purpose:
26+
27+
\*******************************************************************/
28+
29+
void remove_function(
30+
symbol_tablet &symbol_table,
31+
goto_functionst &goto_functions,
32+
const irep_idt &identifier,
33+
message_handlert &message_handler)
34+
{
35+
messaget message(message_handler);
36+
37+
goto_functionst::function_mapt::iterator entry=
38+
goto_functions.function_map.find(identifier);
39+
40+
if(entry==goto_functions.function_map.end())
41+
{
42+
message.error() << "No function " << identifier
43+
<< " in goto program" << messaget::eom;
44+
return;
45+
}
46+
else if(entry->second.is_inlined())
47+
{
48+
message.warning() << "Function " << identifier << " is inlined, "
49+
<< "instantiations will not be removed"
50+
<< messaget::eom;
51+
}
52+
53+
if(entry->second.body_available())
54+
{
55+
message.status() << "Removing body of " << identifier
56+
<< messaget::eom;
57+
entry->second.clear();
58+
symbol_table.lookup(identifier).value.make_nil();
59+
}
60+
}
61+
62+
/*******************************************************************\
63+
64+
Function: remove_functions
65+
66+
Inputs:
67+
68+
Outputs:
69+
70+
Purpose:
71+
72+
\*******************************************************************/
73+
74+
void remove_functions(
75+
symbol_tablet &symbol_table,
76+
goto_functionst &goto_functions,
77+
const std::list<std::string> &names,
78+
message_handlert &message_handler)
79+
{
80+
for(const auto &f : names)
81+
remove_function(symbol_table, goto_functions, f, message_handler);
82+
}

src/goto-instrument/remove_function.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
class goto_functionst;
18+
class message_handlert;
19+
class symbol_tablet;
20+
21+
void remove_function(
22+
symbol_tablet &symbol_table,
23+
goto_functionst &goto_functions,
24+
const irep_idt &identifier,
25+
message_handlert &message_handler);
26+
27+
void remove_functions(
28+
symbol_tablet &symbol_table,
29+
goto_functionst &goto_functions,
30+
const std::list<std::string> &names,
31+
message_handlert &message_handler);
32+
33+
#endif // CPROVER_GOTO_INSTRUMENT_REMOVE_FUNCTION_H

0 commit comments

Comments
 (0)