cprover
uninitialized.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Detection for Uninitialized Local Variables
4 
5 Author: Daniel Kroening
6 
7 Date: January 2010
8 
9 \*******************************************************************/
10 
13 
14 #include "uninitialized.h"
15 
16 #include <util/std_code.h>
17 #include <util/std_expr.h>
18 #include <util/symbol_table.h>
19 
21 
23 {
24 public:
25  explicit uninitializedt(symbol_tablet &_symbol_table):
26  symbol_table(_symbol_table),
27  ns(_symbol_table)
28  {
29  }
30 
31  void add_assertions(
32  const irep_idt &function_identifer,
33  goto_programt &goto_program);
34 
35 protected:
39 
40  // The variables that need tracking,
41  // i.e., are uninitialized and may be read?
42  std::set<irep_idt> tracking;
43 
45 };
46 
49 {
50  std::list<exprt> objects=objects_read(*i_it);
51 
52  for(const auto &object : objects)
53  {
54  if(object.id() == ID_symbol)
55  {
56  const irep_idt &identifier = to_symbol_expr(object).get_identifier();
57  const std::set<irep_idt> &uninitialized=
58  uninitialized_analysis[i_it].uninitialized;
59  if(uninitialized.find(identifier)!=uninitialized.end())
60  tracking.insert(identifier);
61  }
62  else if(object.id() == ID_dereference)
63  {
64  }
65  }
66 }
67 
69  const irep_idt &function_identifier,
70  goto_programt &goto_program)
71 {
72  uninitialized_analysis(function_identifier, goto_program, ns);
73 
74  // find out which variables need tracking
75 
76  tracking.clear();
77  forall_goto_program_instructions(i_it, goto_program)
78  get_tracking(i_it);
79 
80  // add tracking symbols to symbol table
81  for(std::set<irep_idt>::const_iterator
82  it=tracking.begin();
83  it!=tracking.end();
84  it++)
85  {
86  const symbolt &symbol=ns.lookup(*it);
87 
88  symbolt new_symbol;
89  new_symbol.name=id2string(symbol.name)+"#initialized";
90  new_symbol.type=bool_typet();
91  new_symbol.base_name=id2string(symbol.base_name)+"#initialized";
92  new_symbol.location=symbol.location;
93  new_symbol.mode=symbol.mode;
94  new_symbol.module=symbol.module;
95  new_symbol.is_thread_local=true;
96  new_symbol.is_static_lifetime=false;
97  new_symbol.is_file_local=true;
98  new_symbol.is_lvalue=true;
99 
100  symbol_table.insert(std::move(new_symbol));
101  }
102 
103  Forall_goto_program_instructions(i_it, goto_program)
104  {
105  goto_programt::instructiont &instruction=*i_it;
106 
107  if(instruction.is_decl())
108  {
109  // if we track it, add declaration and assignment
110  // for tracking variable!
111 
112  const irep_idt &identifier = instruction.decl_symbol().get_identifier();
113 
114  if(tracking.find(identifier)!=tracking.end())
115  {
116  goto_programt::targett i1=goto_program.insert_after(i_it);
117  goto_programt::targett i2=goto_program.insert_after(i1);
118  i_it++, i_it++;
119 
120  const irep_idt new_identifier=
121  id2string(identifier)+"#initialized";
122 
123  symbol_exprt symbol_expr(new_identifier, bool_typet());
124  i1->type=DECL;
125  i1->source_location=instruction.source_location;
126  i1->code=code_declt(symbol_expr);
127 
128  i2->type=ASSIGN;
129  i2->source_location=instruction.source_location;
130  i2->code=code_assignt(symbol_expr, false_exprt());
131  }
132  }
133  else
134  {
135  std::list<exprt> read=objects_read(instruction);
136  std::list<exprt> written=objects_written(instruction);
137 
138  // if(instruction.is_function_call())
139  // const code_function_callt &code_function_call=
140  // to_code_function_call(instruction.code);
141 
142  const std::set<irep_idt> &uninitialized=
143  uninitialized_analysis[i_it].uninitialized;
144 
145  // check tracking variables
146  for(const auto &object : read)
147  {
148  if(object.id() == ID_symbol)
149  {
150  const irep_idt &identifier = to_symbol_expr(object).get_identifier();
151 
152  if(uninitialized.find(identifier)!=uninitialized.end())
153  {
154  assert(tracking.find(identifier)!=tracking.end());
155  const irep_idt new_identifier=id2string(identifier)+"#initialized";
156 
157  // insert assertion
158  goto_programt::instructiont assertion =
160  symbol_exprt(new_identifier, bool_typet()),
161  instruction.source_location);
162  assertion.source_location.set_comment(
163  "use of uninitialized local variable");
164  assertion.source_location.set_property_class("uninitialized local");
165 
166  goto_program.insert_before_swap(i_it, assertion);
167  i_it++;
168  }
169  }
170  }
171 
172  // set tracking variables
173  for(const auto &object : written)
174  {
175  if(object.id() == ID_symbol)
176  {
177  const irep_idt &identifier = to_symbol_expr(object).get_identifier();
178 
179  if(tracking.find(identifier)!=tracking.end())
180  {
181  const irep_idt new_identifier=id2string(identifier)+"#initialized";
182 
183  goto_programt::instructiont assignment;
184  assignment.type=ASSIGN;
185  assignment.code=code_assignt(
186  symbol_exprt(new_identifier, bool_typet()), true_exprt());
187  assignment.source_location=instruction.source_location;
188 
189  goto_program.insert_before_swap(i_it, assignment);
190  i_it++;
191  }
192  }
193  }
194  }
195  }
196 }
197 
199 {
200  for(auto &gf_entry : goto_model.goto_functions.function_map)
201  {
202  uninitializedt uninitialized(goto_model.symbol_table);
203 
204  uninitialized.add_assertions(gf_entry.first, gf_entry.second.body);
205  }
206 }
207 
209  const goto_modelt &goto_model,
210  std::ostream &out)
211 {
212  const namespacet ns(goto_model.symbol_table);
213 
214  for(const auto &gf_entry : goto_model.goto_functions.function_map)
215  {
216  if(gf_entry.second.body_available())
217  {
218  out << "////\n";
219  out << "//// Function: " << gf_entry.first << '\n';
220  out << "////\n\n";
221  uninitialized_analysist uninitialized_analysis;
222  uninitialized_analysis(gf_entry.first, gf_entry.second.body, ns);
223  uninitialized_analysis.output(
224  ns, gf_entry.first, gf_entry.second.body, out);
225  }
226  }
227 }
virtual void output(const namespacet &ns, const irep_idt &function_id, const goto_programt &goto_program, std::ostream &out) const
Output the abstract states for a single function.
Definition: ai.cpp:41
The Boolean type.
Definition: std_types.h:37
A codet representing an assignment in the program.
Definition: std_code.h:295
A codet representing the declaration of a local variable.
Definition: std_code.h:402
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
The Boolean constant false.
Definition: std_expr.h:2726
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
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
codet code
Do not read or modify directly – use get_X() instead.
Definition: goto_program.h:183
source_locationt source_location
The location of the instruction in the source file.
Definition: goto_program.h:334
goto_program_instruction_typet type
What kind of instruction?
Definition: goto_program.h:337
const symbol_exprt & decl_symbol() const
Get the declared symbol for DECL.
Definition: goto_program.h:212
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:74
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:577
instructionst::iterator targett
Definition: goto_program.h:550
instructionst::const_iterator const_targett
Definition: goto_program.h:551
targett insert_after(const_targett target)
Insertion after the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:626
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:864
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
void set_comment(const irep_idt &comment)
void set_property_class(const irep_idt &property_class)
Expression to hold a symbol (variable)
Definition: std_expr.h:81
const irep_idt & get_identifier() const
Definition: std_expr.h:110
The symbol table.
Definition: symbol_table.h:20
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Symbol table entry.
Definition: symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool is_file_local
Definition: symbol.h:66
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
bool is_static_lifetime
Definition: symbol.h:65
bool is_thread_local
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
irep_idt name
The unique identifier.
Definition: symbol.h:40
bool is_lvalue
Definition: symbol.h:66
irep_idt mode
Language mode.
Definition: symbol.h:49
The Boolean constant true.
Definition: std_expr.h:2717
void add_assertions(const irep_idt &function_identifer, goto_programt &goto_program)
std::set< irep_idt > tracking
void get_tracking(goto_programt::const_targett i_it)
which variables need tracking, i.e., are uninitialized and may be read?
uninitialized_analysist uninitialized_analysis
uninitializedt(symbol_tablet &_symbol_table)
symbol_tablet & symbol_table
void objects_read(const exprt &src, std::list< exprt > &dest)
void objects_written(const exprt &src, std::list< exprt > &dest)
#define forall_goto_program_instructions(it, program)
#define Forall_goto_program_instructions(it, program)
@ ASSIGN
Definition: goto_program.h:47
@ DECL
Definition: goto_program.h:48
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
API to expression classes.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:190
Author: Diffblue Ltd.
void add_uninitialized_locals_assertions(goto_modelt &goto_model)
void show_uninitialized(const goto_modelt &goto_model, std::ostream &out)
Detection for Uninitialized Local Variables.
Detection for Uninitialized Local Variables.