Skip to content
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

goto-instrument --remove-function-body #730

Merged
merged 1 commit into from
May 25, 2017
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* GOTO-INSTRUMENT: New option --print-path-lenghts
* GOTO-ANALYZER: New option --unreachable-functions, --reachable-functions
* GOTO-INSTRUMENT: New option --undefined-function-is-assume-false
* GOTO-INSTRUMENT: New option --remove-function-body


5.7
Expand Down
21 changes: 21 additions & 0 deletions regression/goto-instrument/remove-function-body1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <assert.h>

int foo()
{
int a;
assert(a>0);
return a;
}

int bar()
{
int b;
assert(b>0);
return b;
}

int main()
{
foo();
bar();
}
10 changes: 10 additions & 0 deletions regression/goto-instrument/remove-function-body1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
main.c
--remove-function-body foo --remove-function-body bar
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
Copy link
Contributor

Choose a reason for hiding this comment

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

I appreciate that it is hard to actually test anything from the output, but it might be good if we could run --show-goto-functions on the output and check that bar is really removed?

Alternatively, if you grab #874, it might be relatively easy to add some unit tests for this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have added patterns that must not be found in test.desc, so I think this is addressed.

--
^warning: ignoring
^bar
^foo
1 change: 1 addition & 0 deletions src/goto-instrument/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SRC = accelerate/accelerate.cpp \
points_to.cpp \
race_check.cpp \
reachability_slicer.cpp \
remove_function.cpp \
rw_set.cpp \
show_locations.cpp \
skip_loops.cpp \
Expand Down
12 changes: 12 additions & 0 deletions src/goto-instrument/goto_instrument_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Author: Daniel Kroening, [email protected]
#include "unwind.h"
#include "model_argc_argv.h"
#include "undefined_functions.h"
#include "remove_function.h"

/*******************************************************************\

Expand Down Expand Up @@ -1034,6 +1035,15 @@ void goto_instrument_parse_optionst::instrument_goto_program()
throw 0;
}

if(cmdline.isset("remove-function-body"))
{
remove_functions(
symbol_table,
goto_functions,
cmdline.get_values("remove-function-body"),
get_message_handler());
}

// we add the library in some cases, as some analyses benefit

if(cmdline.isset("add-library") ||
Expand Down Expand Up @@ -1634,6 +1644,8 @@ void goto_instrument_parse_optionst::help()
HELP_REMOVE_CONST_FUNCTION_POINTERS
" --add-library add models of C library functions\n"
" --model-argc-argv <n> model up to <n> command line arguments\n"
// NOLINTNEXTLINE(whitespace/line_length)
" --remove-function-body <f> remove the implementation of function <f> (may be repeated)\n"
"\n"
"Other options:\n"
" --use-system-headers with --dump-c/--dump-cpp: generate C source with includes\n" // NOLINT(*)
Expand Down
3 changes: 2 additions & 1 deletion src/goto-instrument/goto_instrument_parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Author: Daniel Kroening, [email protected]
"(z3)(add-library)(show-dependence-graph)" \
"(horn)(skip-loops):(apply-code-contracts)(model-argc-argv):" \
"(show-threaded)(list-calls-args)(print-path-lengths)" \
"(undefined-function-is-assume-false)"
"(undefined-function-is-assume-false)" \
"(remove-function-body):"

class goto_instrument_parse_optionst:
public parse_options_baset,
Expand Down
94 changes: 94 additions & 0 deletions src/goto-instrument/remove_function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************\

Module: Remove function definition

Author: Michael Tautschnig

Date: April 2017
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment block doesn't normally have the date, how come we've added it here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

$ git grep ^Date: | wc -l
     152

Seems it's all very much inconsistent.


\*******************************************************************/

#include "remove_function.h"

#include <util/message.h>

#include <goto-programs/goto_functions.h>

/*******************************************************************\

Function: remove_function

Inputs:
symbol_table Input symbol table to be modified
goto_functions Input functions to be modified
identifier Function to be removed
message_handler Error/status output

Outputs:

Purpose: Remove the body of function "identifier" such that an
analysis will treat it as a side-effect free function with
non-deterministic return value.

\*******************************************************************/

void remove_function(
symbol_tablet &symbol_table,
goto_functionst &goto_functions,
const irep_idt &identifier,
message_handlert &message_handler)
{
messaget message(message_handler);

goto_functionst::function_mapt::iterator entry=
goto_functions.function_map.find(identifier);

if(entry==goto_functions.function_map.end())
{
message.error() << "No function " << identifier
<< " in goto program" << messaget::eom;
return;
}
else if(entry->second.is_inlined())
{
message.warning() << "Function " << identifier << " is inlined, "
<< "instantiations will not be removed"
<< messaget::eom;
}

if(entry->second.body_available())
{
message.status() << "Removing body of " << identifier
<< messaget::eom;
entry->second.clear();
symbol_table.lookup(identifier).value.make_nil();
}
}

/*******************************************************************\

Function: remove_functions

Inputs:
symbol_table Input symbol table to be modified
goto_functions Input functions to be modified
names List of functions to be removed
message_handler Error/status output

Outputs:

Purpose: Remove the body of all functions listed in "names" such that
an analysis will treat it as a side-effect free function with
non-deterministic return value.

\*******************************************************************/

void remove_functions(
symbol_tablet &symbol_table,
goto_functionst &goto_functions,
const std::list<std::string> &names,
message_handlert &message_handler)
{
for(const auto &f : names)
remove_function(symbol_table, goto_functions, f, message_handler);
}
35 changes: 35 additions & 0 deletions src/goto-instrument/remove_function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************\

Module: Remove function definition

Author: Michael Tautschnig

Date: April 2017

\*******************************************************************/

#ifndef CPROVER_GOTO_INSTRUMENT_REMOVE_FUNCTION_H
#define CPROVER_GOTO_INSTRUMENT_REMOVE_FUNCTION_H

#include <list>
#include <string>

#include <util/irep.h>

class goto_functionst;
class message_handlert;
class symbol_tablet;

void remove_function(
symbol_tablet &symbol_table,
goto_functionst &goto_functions,
const irep_idt &identifier,
message_handlert &message_handler);

void remove_functions(
symbol_tablet &symbol_table,
goto_functionst &goto_functions,
const std::list<std::string> &names,
message_handlert &message_handler);

#endif // CPROVER_GOTO_INSTRUMENT_REMOVE_FUNCTION_H