Skip to content

Commit 882bd0d

Browse files
authored
Merge pull request #7389 from peterschrammel/nondet-static-matching
Add --nondet-static-matching regex option
2 parents fbd2b88 + 22152fc commit 882bd0d

File tree

7 files changed

+79
-0
lines changed

7 files changed

+79
-0
lines changed

doc/man/goto-instrument.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ add nondeterministic initialization of variables with static lifetime
392392
same as nondet\-static except for the variable \fIe\fR
393393
(use multiple times if required)
394394
.TP
395+
\fB\-\-nondet\-static\-matching\fR \fIr\fR
396+
add nondeterministic initialization of variables
397+
with static lifetime matching regex \fIr\fR
398+
.TP
395399
\fB\-\-function\-enter\fR \fIf\fR
396400
.TQ
397401
\fB\-\-function\-exit\fR \fIf\fR
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
3+
int do_nondet_init1;
4+
int init1;
5+
6+
int main(int argc, char **argv)
7+
{
8+
static int do_nondet_init2;
9+
static int init2;
10+
11+
assert(do_nondet_init1 == 0);
12+
assert(init1 == 0);
13+
assert(do_nondet_init2 == 0);
14+
assert(init2 == 0);
15+
16+
return 0;
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--nondet-static-matching '.*\.c:.*nondet.*'
4+
^VERIFICATION FAILED$
5+
^EXIT=10$
6+
^SIGNAL=0$
7+
assertion do_nondet_init1 == 0: FAILURE
8+
assertion init1 == 0: SUCCESS
9+
assertion do_nondet_init2 == 0: FAILURE
10+
assertion init2 == 0: SUCCESS
11+
--
12+
--

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ int goto_instrument_parse_optionst::doit()
154154
}
155155
}
156156

157+
// ignore default/user-specified initialization
158+
// of matching variables with static lifetime
159+
if(cmdline.isset("nondet-static-matching"))
160+
{
161+
log.status() << "Adding nondeterministic initialization "
162+
"of matching static/global variables"
163+
<< messaget::eom;
164+
nondet_static_matching(
165+
goto_model, cmdline.get_value("nondet-static-matching"));
166+
}
167+
157168
instrument_goto_program();
158169

159170
if(cmdline.isset("validate-goto-model"))
@@ -1898,6 +1909,8 @@ void goto_instrument_parse_optionst::help()
18981909
" --nondet-static add nondeterministic initialization of variables with static lifetime\n" // NOLINT(*)
18991910
" --nondet-static-exclude e same as nondet-static except for the variable e\n" //NOLINT(*)
19001911
" (use multiple times if required)\n"
1912+
" --nondet-static-matching r add nondeterministic initialization of variables\n" // NOLINT(*)
1913+
" with static lifetime matching regex r\n"
19011914
" --function-enter <f>, --function-exit <f>, --branch <f>\n"
19021915
" instruments a call to <f> at the beginning,\n" // NOLINT(*)
19031916
" the exit, or a branch point, respectively\n"

src/goto-instrument/goto_instrument_parse_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Author: Daniel Kroening, [email protected]
6565
"(isr):" \
6666
"(stack-depth):(nondet-static)" \
6767
"(nondet-static-exclude):" \
68+
"(nondet-static-matching):" \
6869
"(function-enter):(function-exit):(branch):" \
6970
OPT_SHOW_GOTO_FUNCTIONS \
7071
OPT_SHOW_PROPERTIES \

src/goto-instrument/nondet_static.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Date: November 2011
2626

2727
#include <linking/static_lifetime_init.h>
2828

29+
#include <regex>
30+
2931
/// See the return.
3032
/// \param symbol_expr: The symbol expression to analyze.
3133
/// \param ns: Namespace for resolving type information
@@ -200,3 +202,31 @@ void nondet_static(
200202

201203
nondet_static(ns, goto_model.goto_functions, INITIALIZE_FUNCTION);
202204
}
205+
206+
/// Nondeterministically initializes global scope variables that
207+
/// match the given regex.
208+
/// \param [out] goto_model: Existing goto-model to be updated.
209+
/// \param regex: regex for matching variables in the format
210+
/// "filename:variable" (same format as those of except_values in
211+
/// nondet_static(goto_model, except_values) variant above).
212+
void nondet_static_matching(goto_modelt &goto_model, const std::string &regex)
213+
{
214+
const auto regex_matcher = std::regex(regex);
215+
symbol_tablet &symbol_table = goto_model.symbol_table;
216+
217+
for(symbol_tablet::iteratort symbol_it = symbol_table.begin();
218+
symbol_it != symbol_table.end();
219+
symbol_it++)
220+
{
221+
symbolt &symbol = symbol_it.get_writeable_symbol();
222+
std::string qualified_name = id2string(symbol.location.get_file()) + ":" +
223+
id2string(symbol.display_name());
224+
if(!std::regex_match(qualified_name, regex_matcher))
225+
{
226+
symbol.value.set(ID_C_no_nondet_initialization, 1);
227+
}
228+
}
229+
230+
const namespacet ns(goto_model.symbol_table);
231+
nondet_static(ns, goto_model.goto_functions, INITIALIZE_FUNCTION);
232+
}

src/goto-instrument/nondet_static.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ void nondet_static(goto_modelt &);
4040

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

43+
void nondet_static_matching(goto_modelt &, const std::string &);
44+
4345
#endif // CPROVER_GOTO_INSTRUMENT_NONDET_STATIC_H

0 commit comments

Comments
 (0)