cprover
nondet_static.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Nondeterministically initializes global scope variables, except for
4  constants (such as string literals, final fields) and internal variables
5  (such as CPROVER and symex variables, language specific internal
6  variables).
7 
8 Author: Daniel Kroening, Michael Tautschnig
9 
10 Date: November 2011
11 
12 \*******************************************************************/
13 
19 
20 #include "nondet_static.h"
21 
22 #include <util/prefix.h>
23 
25 
27 
34  const symbol_exprt &symbol_expr,
35  const namespacet &ns)
36 {
37  const irep_idt &id = symbol_expr.get_identifier();
38 
39  // is it a __CPROVER_* variable?
41  return false;
42 
43  // variable not in symbol table such as symex variable?
44  if(!ns.get_symbol_table().has_symbol(id))
45  return false;
46 
47  const symbolt &symbol = ns.lookup(id);
48 
49  // is the symbol explicitly marked as not to be nondet initialized?
50  if(symbol.value.get_bool(ID_C_no_nondet_initialization))
51  {
52  return false;
53  }
54 
55  // is the type explicitly marked as not to be nondet initialized?
56  if(symbol.type.get_bool(ID_C_no_nondet_initialization))
57  return false;
58 
59  // is the type explicitly marked as not to be initialized?
60  if(symbol.type.get_bool(ID_C_no_initialization_required))
61  return false;
62 
63  // static lifetime?
64  if(!symbol.is_static_lifetime)
65  return false;
66 
67  // constant?
68  return !is_constant_or_has_constant_components(symbol_expr.type(), ns) &&
70 }
71 
80  const namespacet &ns,
81  goto_functionst &goto_functions,
82  const irep_idt &fct_name)
83 {
84  goto_functionst::function_mapt::iterator fct_entry =
85  goto_functions.function_map.find(fct_name);
86  CHECK_RETURN(fct_entry != goto_functions.function_map.end());
87 
88  goto_programt &init = fct_entry->second.body;
89 
90  for(auto &instruction : init.instructions)
91  {
92  if(instruction.is_assign())
93  {
94  const symbol_exprt sym =
95  to_symbol_expr(as_const(instruction).get_assign().lhs());
96 
98  {
99  const auto source_location = instruction.source_location;
100  instruction = goto_programt::make_assignment(
101  code_assignt(
102  sym, side_effect_expr_nondett(sym.type(), source_location)),
103  source_location);
104  }
105  }
106  else if(instruction.is_function_call())
107  {
108  const symbol_exprt &fsym = to_symbol_expr(instruction.call_function());
109 
110  if(has_prefix(id2string(fsym.get_identifier()), "#ini#"))
111  nondet_static(ns, goto_functions, fsym.get_identifier());
112  }
113  }
114 
115  // update counters etc.
116  goto_functions.update();
117 }
118 
123 void nondet_static(const namespacet &ns, goto_functionst &goto_functions)
124 {
125  nondet_static(ns, goto_functions, INITIALIZE_FUNCTION);
126 }
127 
133 void nondet_static(goto_modelt &goto_model)
134 {
135  const namespacet ns(goto_model.symbol_table);
136  nondet_static(ns, goto_model.goto_functions);
137 }
138 
146  goto_modelt &goto_model,
147  const optionst::value_listt &except_values)
148 {
149  const namespacet ns(goto_model.symbol_table);
150  std::set<std::string> to_exclude;
151 
152  for(auto const &except : except_values)
153  {
154  const bool file_prefix_found = except.find(":") != std::string::npos;
155 
156  if(file_prefix_found)
157  {
158  to_exclude.insert(except);
159  if(has_prefix(except, "./"))
160  {
161  to_exclude.insert(except.substr(2, except.length() - 2));
162  }
163  else
164  {
165  to_exclude.insert("./" + except);
166  }
167  }
168  else
169  {
170  irep_idt symbol_name(except);
171  symbolt lookup_results = ns.lookup(symbol_name);
172  to_exclude.insert(
173  id2string(lookup_results.location.get_file()) + ":" + except);
174  }
175  }
176 
177  symbol_tablet &symbol_table = goto_model.symbol_table;
178 
179  for(symbol_tablet::iteratort symbol_it = symbol_table.begin();
180  symbol_it != symbol_table.end();
181  symbol_it++)
182  {
183  symbolt &symbol = symbol_it.get_writeable_symbol();
184  std::string qualified_name = id2string(symbol.location.get_file()) + ":" +
185  id2string(symbol.display_name());
186  if(to_exclude.find(qualified_name) != to_exclude.end())
187  {
188  symbol.value.set(ID_C_no_nondet_initialization, 1);
189  }
190  }
191 
193 }
const T & as_const(T &value)
Return a reference to the same object but ensures the type is const.
Definition: as_const.h:14
A codet representing an assignment in the program.
Definition: std_code.h:293
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
typet & type()
Return the type of the expression.
Definition: expr.h:82
A collection of goto functions.
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:71
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:652
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:431
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:58
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
const symbol_table_baset & get_symbol_table() const
Return first symbol table registered with the namespace.
Definition: namespace.h:123
std::list< std::string > value_listt
Definition: options.h:25
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1966
const irep_idt & get_file() const
Expression to hold a symbol (variable)
Definition: std_expr.h:80
const irep_idt & get_identifier() const
Definition: std_expr.h:109
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
The symbol table.
Definition: symbol_table.h:14
virtual iteratort begin() override
Definition: symbol_table.h:108
virtual iteratort end() override
Definition: symbol_table.h:112
Symbol table entry.
Definition: symbol.h:28
const irep_idt & display_name() const
Return language specific display name if present.
Definition: symbol.h:55
bool is_static_lifetime
Definition: symbol.h:65
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
typet type
Type of symbol.
Definition: symbol.h:31
exprt value
Initial value of symbol.
Definition: symbol.h:34
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
#define CPROVER_PREFIX
Symbol Table + CFG.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
bool is_nondet_initializable_static(const symbol_exprt &symbol_expr, const namespacet &ns)
See the return.
void nondet_static(const namespacet &ns, goto_functionst &goto_functions, const irep_idt &fct_name)
Nondeterministically initializes global scope variables in a goto-function.
Nondeterministically initializes global scope variables, except for constants (such as string literal...
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define INITIALIZE_FUNCTION
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:189
bool is_constant_or_has_constant_components(const typet &type, const namespacet &ns)
Identify whether a given type is constant itself or contains constant components.
Definition: std_types.cpp:171