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=
113  to_code_decl(instruction.code).get_identifier();
114 
115  if(tracking.find(identifier)!=tracking.end())
116  {
117  goto_programt::targett i1=goto_program.insert_after(i_it);
118  goto_programt::targett i2=goto_program.insert_after(i1);
119  i_it++, i_it++;
120 
121  const irep_idt new_identifier=
122  id2string(identifier)+"#initialized";
123 
124  symbol_exprt symbol_expr(new_identifier, bool_typet());
125  i1->type=DECL;
126  i1->source_location=instruction.source_location;
127  i1->code=code_declt(symbol_expr);
128 
129  i2->type=ASSIGN;
130  i2->source_location=instruction.source_location;
131  i2->code=code_assignt(symbol_expr, false_exprt());
132  }
133  }
134  else
135  {
136  std::list<exprt> read=objects_read(instruction);
137  std::list<exprt> written=objects_written(instruction);
138 
139  // if(instruction.is_function_call())
140  // const code_function_callt &code_function_call=
141  // to_code_function_call(instruction.code);
142 
143  const std::set<irep_idt> &uninitialized=
144  uninitialized_analysis[i_it].uninitialized;
145 
146  // check tracking variables
147  for(const auto &object : read)
148  {
149  if(object.id() == ID_symbol)
150  {
151  const irep_idt &identifier = to_symbol_expr(object).get_identifier();
152 
153  if(uninitialized.find(identifier)!=uninitialized.end())
154  {
155  assert(tracking.find(identifier)!=tracking.end());
156  const irep_idt new_identifier=id2string(identifier)+"#initialized";
157 
158  // insert assertion
159  goto_programt::instructiont assertion =
161  symbol_exprt(new_identifier, bool_typet()),
162  instruction.source_location);
163  assertion.source_location.set_comment(
164  "use of uninitialized local variable");
165  assertion.source_location.set_property_class("uninitialized local");
166 
167  goto_program.insert_before_swap(i_it, assertion);
168  i_it++;
169  }
170  }
171  }
172 
173  // set tracking variables
174  for(const auto &object : written)
175  {
176  if(object.id() == ID_symbol)
177  {
178  const irep_idt &identifier = to_symbol_expr(object).get_identifier();
179 
180  if(tracking.find(identifier)!=tracking.end())
181  {
182  const irep_idt new_identifier=id2string(identifier)+"#initialized";
183 
184  goto_programt::instructiont assignment;
185  assignment.type=ASSIGN;
186  assignment.code=code_assignt(
187  symbol_exprt(new_identifier, bool_typet()), true_exprt());
188  assignment.source_location=instruction.source_location;
189 
190  goto_program.insert_before_swap(i_it, assignment);
191  i_it++;
192  }
193  }
194  }
195  }
196  }
197 }
198 
200 {
201  Forall_goto_functions(f_it, goto_model.goto_functions)
202  {
203  uninitializedt uninitialized(goto_model.symbol_table);
204 
205  uninitialized.add_assertions(f_it->first, f_it->second.body);
206  }
207 }
208 
210  const goto_modelt &goto_model,
211  std::ostream &out)
212 {
213  const namespacet ns(goto_model.symbol_table);
214 
215  forall_goto_functions(f_it, goto_model.goto_functions)
216  {
217  if(f_it->second.body_available())
218  {
219  out << "////\n";
220  out << "//// Function: " << f_it->first << '\n';
221  out << "////\n\n";
222  uninitialized_analysist uninitialized_analysis;
223  uninitialized_analysis(f_it->first, f_it->second.body, ns);
224  uninitialized_analysis.output(ns, f_it->first, f_it->second.body, out);
225  }
226  }
227 }
Forall_goto_program_instructions
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:1201
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:269
source_locationt::set_comment
void set_comment(const irep_idt &comment)
Definition: source_location.h:141
to_code_decl
const code_declt & to_code_decl(const codet &code)
Definition: std_code.h:450
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:272
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:209
add_uninitialized_locals_assertions
void add_uninitialized_locals_assertions(goto_modelt &goto_model)
Definition: uninitialized.cpp:199
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
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:82
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
code_declt::get_identifier
const irep_idt & get_identifier() const
Definition: std_code.h:418
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:893
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
goto_programt::instructiont::code
codet code
Do not read or modify directly – use get_X() instead.
Definition: goto_program.h:182
goto_programt::instructiont::is_decl
bool is_decl() const
Definition: goto_program.h:467
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:111
objects_read
void objects_read(const exprt &src, std::list< exprt > &dest)
Definition: goto_program.cpp:373
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:177
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:410
false_exprt
The Boolean constant false.
Definition: std_expr.h:3964
std_code.h
Forall_goto_functions
#define Forall_goto_functions(it, functions)
Definition: goto_functions.h:117
uninitializedt::ns
namespacet ns
Definition: uninitialized.cpp:37
ASSIGN
@ ASSIGN
Definition: goto_program.h:46
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:47
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:42
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:73
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:655
forall_goto_functions
#define forall_goto_functions(it, functions)
Definition: goto_functions.h:122
uninitializedt::uninitialized_analysis
uninitialized_analysist uninitialized_analysis
Definition: uninitialized.cpp:38
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:580
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:606
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:3955
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:179
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:579
forall_goto_program_instructions
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:1196
source_locationt::set_property_class
void set_property_class(const irep_idt &property_class)
Definition: source_location.h:136