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 }
Forall_goto_program_instructions
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:1172
uninitializedt::add_assertions
void add_assertions(const irep_idt &function_identifer, goto_programt &goto_program)
Definition: uninitialized.cpp:68
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
goto_programt::instructiont::source_location
source_locationt source_location
The location of the instruction in the source file.
Definition: goto_program.h:334
source_locationt::set_comment
void set_comment(const irep_idt &comment)
Definition: source_location.h:141
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
goto_programt::instructiont::type
goto_program_instruction_typet type
What kind of instruction?
Definition: goto_program.h:337
code_declt
A codet representing the declaration of a local variable.
Definition: std_code.h:402
show_uninitialized
void show_uninitialized(const goto_modelt &goto_model, std::ostream &out)
Definition: uninitialized.cpp:208
add_uninitialized_locals_assertions
void add_uninitialized_locals_assertions(goto_modelt &goto_model)
Definition: uninitialized.cpp:198
ait< uninitialized_domaint >
goto_modelt
Definition: goto_model.h:26
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool_typet
The Boolean type.
Definition: std_types.h:37
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:81
goto_programt::instructiont::decl_symbol
const symbol_exprt & decl_symbol() const
Get the declared symbol for DECL.
Definition: goto_program.h:212
uninitializedt::get_tracking
void get_tracking(goto_programt::const_targett i_it)
which variables need tracking, i.e., are uninitialized and may be read?
Definition: uninitialized.cpp:48
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
symbolt::is_thread_local
bool is_thread_local
Definition: symbol.h:65
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
goto_programt::make_assertion
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:864
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
goto_programt::instructiont::code
codet code
Do not read or modify directly – use get_X() instead.
Definition: goto_program.h:183
goto_programt::instructiont::is_decl
bool is_decl() const
Definition: goto_program.h:438
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:110
objects_read
void objects_read(const exprt &src, std::list< exprt > &dest)
Definition: goto_program.cpp:372
uninitializedt::symbol_table
symbol_tablet & symbol_table
Definition: uninitialized.cpp:36
uninitializedt::tracking
std::set< irep_idt > tracking
Definition: uninitialized.cpp:42
symbol_tablet::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Definition: symbol_table.cpp:19
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:190
uninitializedt::uninitializedt
uninitializedt(symbol_tablet &_symbol_table)
Definition: uninitialized.cpp:25
objects_written
void objects_written(const exprt &src, std::list< exprt > &dest)
Definition: goto_program.cpp:409
false_exprt
The Boolean constant false.
Definition: std_expr.h:2726
std_code.h
uninitializedt::ns
namespacet ns
Definition: uninitialized.cpp:37
ASSIGN
@ ASSIGN
Definition: goto_program.h:47
uninitialized.h
Detection for Uninitialized Local Variables.
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
DECL
@ DECL
Definition: goto_program.h:48
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
ai_baset::output
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
symbolt
Symbol table entry.
Definition: symbol.h:28
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:74
goto_programt::insert_after
targett insert_after(const_targett target)
Insertion after the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:626
uninitializedt::uninitialized_analysis
uninitialized_analysist uninitialized_analysis
Definition: uninitialized.cpp:38
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:551
symbolt::is_file_local
bool is_file_local
Definition: symbol.h:66
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
goto_programt::insert_before_swap
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:577
symbol_table.h
Author: Diffblue Ltd.
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
true_exprt
The Boolean constant true.
Definition: std_expr.h:2717
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
uninitialized_domain.h
Detection for Uninitialized Local Variables.
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
uninitializedt
Definition: uninitialized.cpp:23
std_expr.h
API to expression classes.
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:550
forall_goto_program_instructions
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:1167
source_locationt::set_property_class
void set_property_class(const irep_idt &property_class)
Definition: source_location.h:136