Skip to content

Add --nondet-static-matching regex option #7389

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

Merged
merged 3 commits into from
Feb 10, 2023
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
4 changes: 4 additions & 0 deletions doc/man/goto-instrument.1
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ add nondeterministic initialization of variables with static lifetime
same as nondet\-static except for the variable \fIe\fR
(use multiple times if required)
.TP
\fB\-\-nondet\-static\-matching\fR \fIr\fR
add nondeterministic initialization of variables
with static lifetime matching regex \fIr\fR
.TP
\fB\-\-function\-enter\fR \fIf\fR
.TQ
\fB\-\-function\-exit\fR \fIf\fR
Expand Down
17 changes: 17 additions & 0 deletions regression/goto-instrument/nondet_static_matching/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>

int do_nondet_init1;
int init1;

int main(int argc, char **argv)
{
static int do_nondet_init2;
static int init2;

assert(do_nondet_init1 == 0);
assert(init1 == 0);
assert(do_nondet_init2 == 0);
assert(init2 == 0);

return 0;
}
12 changes: 12 additions & 0 deletions regression/goto-instrument/nondet_static_matching/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE
main.c
--nondet-static-matching '.*\.c:.*nondet.*'
^VERIFICATION FAILED$
^EXIT=10$
^SIGNAL=0$
assertion do_nondet_init1 == 0: FAILURE
assertion init1 == 0: SUCCESS
assertion do_nondet_init2 == 0: FAILURE
assertion init2 == 0: SUCCESS
--
--
13 changes: 13 additions & 0 deletions src/goto-instrument/goto_instrument_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ int goto_instrument_parse_optionst::doit()
}
}

// ignore default/user-specified initialization
// of matching variables with static lifetime
if(cmdline.isset("nondet-static-matching"))
{
log.status() << "Adding nondeterministic initialization "
"of matching static/global variables"
<< messaget::eom;
nondet_static_matching(
goto_model, cmdline.get_value("nondet-static-matching"));
}

instrument_goto_program();

if(cmdline.isset("validate-goto-model"))
Expand Down Expand Up @@ -1898,6 +1909,8 @@ void goto_instrument_parse_optionst::help()
" --nondet-static add nondeterministic initialization of variables with static lifetime\n" // NOLINT(*)
" --nondet-static-exclude e same as nondet-static except for the variable e\n" //NOLINT(*)
" (use multiple times if required)\n"
" --nondet-static-matching r add nondeterministic initialization of variables\n" // NOLINT(*)
" with static lifetime matching regex r\n"
" --function-enter <f>, --function-exit <f>, --branch <f>\n"
" instruments a call to <f> at the beginning,\n" // NOLINT(*)
" the exit, or a branch point, respectively\n"
Expand Down
1 change: 1 addition & 0 deletions src/goto-instrument/goto_instrument_parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Author: Daniel Kroening, [email protected]
"(isr):" \
"(stack-depth):(nondet-static)" \
"(nondet-static-exclude):" \
"(nondet-static-matching):" \
"(function-enter):(function-exit):(branch):" \
OPT_SHOW_GOTO_FUNCTIONS \
OPT_SHOW_PROPERTIES \
Expand Down
30 changes: 30 additions & 0 deletions src/goto-instrument/nondet_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Date: November 2011

#include <linking/static_lifetime_init.h>

#include <regex>

/// See the return.
/// \param symbol_expr: The symbol expression to analyze.
/// \param ns: Namespace for resolving type information
Expand Down Expand Up @@ -200,3 +202,31 @@ void nondet_static(

nondet_static(ns, goto_model.goto_functions, INITIALIZE_FUNCTION);
}

/// Nondeterministically initializes global scope variables that
/// match the given regex.
/// \param [out] goto_model: Existing goto-model to be updated.
/// \param regex: regex for matching variables in the format
/// "filename:variable" (same format as those of except_values in
/// nondet_static(goto_model, except_values) variant above).
void nondet_static_matching(goto_modelt &goto_model, const std::string &regex)
{
const auto regex_matcher = std::regex(regex);
symbol_tablet &symbol_table = goto_model.symbol_table;

for(symbol_tablet::iteratort symbol_it = symbol_table.begin();
symbol_it != symbol_table.end();
symbol_it++)
{
symbolt &symbol = symbol_it.get_writeable_symbol();
std::string qualified_name = id2string(symbol.location.get_file()) + ":" +
id2string(symbol.display_name());
if(!std::regex_match(qualified_name, regex_matcher))
{
symbol.value.set(ID_C_no_nondet_initialization, 1);
}
}

const namespacet ns(goto_model.symbol_table);
nondet_static(ns, goto_model.goto_functions, INITIALIZE_FUNCTION);
}
2 changes: 2 additions & 0 deletions src/goto-instrument/nondet_static.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ void nondet_static(goto_modelt &);

void nondet_static(goto_modelt &, const std::set<std::string> &);

void nondet_static_matching(goto_modelt &, const std::string &);

#endif // CPROVER_GOTO_INSTRUMENT_NONDET_STATIC_H