cprover
static_lifetime_init.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "static_lifetime_init.h"
10 
11 #include <util/arith_tools.h>
12 #include <util/c_types.h>
13 #include <util/expr_initializer.h>
14 #include <util/namespace.h>
15 #include <util/prefix.h>
16 #include <util/std_code.h>
17 #include <util/symbol_table.h>
18 
19 #include <set>
20 
21 static optionalt<codet>
22 static_lifetime_init(const irep_idt &identifier, symbol_tablet &symbol_table)
23 {
24  const namespacet ns(symbol_table);
25  const symbolt &symbol = ns.lookup(identifier);
26 
27  if(!symbol.is_static_lifetime)
28  return {};
29 
30  if(symbol.is_type || symbol.is_macro)
31  return {};
32 
33  // special values
34  if(
35  identifier == CPROVER_PREFIX "constant_infinity_uint" ||
36  identifier == CPROVER_PREFIX "memory" || identifier == "__func__" ||
37  identifier == "__FUNCTION__" || identifier == "__PRETTY_FUNCTION__" ||
38  identifier == "argc'" || identifier == "argv'" || identifier == "envp'" ||
39  identifier == "envp_size'")
40  return {};
41 
42  // just for linking
43  if(has_prefix(id2string(identifier), CPROVER_PREFIX "architecture_"))
44  return {};
45 
46  const typet &type = ns.follow(symbol.type);
47 
48  // check type
49  if(type.id() == ID_code || type.id() == ID_empty)
50  return {};
51 
52  if(type.id() == ID_array && to_array_type(type).size().is_nil())
53  {
54  // C standard 6.9.2, paragraph 5
55  // adjust the type to an array of size 1
56  symbolt &writable_symbol = symbol_table.get_writeable_ref(identifier);
57  writable_symbol.type = type;
58  writable_symbol.type.set(ID_size, from_integer(1, size_type()));
59  }
60 
61  if(
62  (type.id() == ID_struct || type.id() == ID_union) &&
63  to_struct_union_type(type).is_incomplete())
64  {
65  return {}; // do not initialize
66  }
67 
68  exprt rhs;
69 
70  if((symbol.value.is_nil() && symbol.is_extern) ||
71  symbol.value.id() == ID_nondet)
72  {
73  // Nondet initialise if not linked, or if explicitly requested.
74  // Compilers would usually complain about the unlinked symbol case.
75  const auto nondet = nondet_initializer(symbol.type, symbol.location, ns);
76  CHECK_RETURN(nondet.has_value());
77  rhs = *nondet;
78  }
79  else if(symbol.value.is_nil())
80  {
81  const auto zero = zero_initializer(symbol.type, symbol.location, ns);
82  CHECK_RETURN(zero.has_value());
83  rhs = *zero;
84  }
85  else
86  rhs = symbol.value;
87 
88  code_assignt code(symbol.symbol_expr(), rhs);
89  code.add_source_location() = symbol.location;
90 
91  return std::move(code);
92 }
93 
95  symbol_tablet &symbol_table,
96  const source_locationt &source_location)
97 {
99 
100  const namespacet ns(symbol_table);
101 
102  symbolt &init_symbol = symbol_table.get_writeable_ref(INITIALIZE_FUNCTION);
103 
104  init_symbol.value=code_blockt();
105  init_symbol.value.add_source_location()=source_location;
106 
107  code_blockt &dest=to_code_block(to_code(init_symbol.value));
108 
109  // add the magic label to hide
110  dest.add(code_labelt(CPROVER_PREFIX "HIDE", code_skipt()));
111 
112  // do assignments based on "value"
113 
114  // sort alphabetically for reproducible results
115  std::set<std::string> symbols;
116 
117  for(const auto &symbol_pair : symbol_table.symbols)
118  {
119  symbols.insert(id2string(symbol_pair.first));
120  }
121 
122  // first do framework variables
123  for(const std::string &id : symbols)
124  if(has_prefix(id, CPROVER_PREFIX))
125  {
126  auto code = static_lifetime_init(id, symbol_table);
127  if(code.has_value())
128  dest.add(std::move(*code));
129  }
130 
131  // now all other variables
132  for(const std::string &id : symbols)
133  if(!has_prefix(id, CPROVER_PREFIX))
134  {
135  auto code = static_lifetime_init(id, symbol_table);
136  if(code.has_value())
137  dest.add(std::move(*code));
138  }
139 
140  // now call designated "initialization" functions
141 
142  for(const std::string &id : symbols)
143  {
144  const symbolt &symbol=ns.lookup(id);
145 
146  if(symbol.type.id() != ID_code)
147  continue;
148 
149  const code_typet &code_type = to_code_type(symbol.type);
150  if(
151  code_type.return_type().id() == ID_constructor &&
152  code_type.parameters().empty())
153  {
154  code_function_callt function_call(symbol.symbol_expr());
155  function_call.add_source_location()=source_location;
156  dest.add(function_call);
157  }
158  }
159 }
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
unsignedbv_typet size_type()
Definition: c_types.cpp:58
A codet representing an assignment in the program.
Definition: std_code.h:293
A codet representing sequential composition of program statements.
Definition: std_code.h:168
void add(const codet &code)
Definition: std_code.h:206
codet representation of a function call statement.
Definition: std_code.h:1213
codet representation of a label for branch targets.
Definition: std_code.h:1405
A codet representing a skip statement.
Definition: std_code.h:268
Base type of functions.
Definition: std_types.h:539
const typet & return_type() const
Definition: std_types.h:645
const parameterst & parameters() const
Definition: std_types.h:655
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
source_locationt & add_source_location()
Definition: expr.h:235
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:431
const irep_idt & id() const
Definition: irep.h:407
bool is_nil() const
Definition: irep.h:387
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
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
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
const symbolst & symbols
Read-only field, used to look up symbols given their names.
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
Symbol table entry.
Definition: symbol.h:28
bool is_extern
Definition: symbol.h:66
bool is_macro
Definition: symbol.h:61
bool is_static_lifetime
Definition: symbol.h:65
bool is_type
Definition: symbol.h:61
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
typet type
Type of symbol.
Definition: symbol.h:31
exprt value
Initial value of symbol.
Definition: symbol.h:34
The type of an expression, extends irept.
Definition: type.h:28
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
#define CPROVER_PREFIX
optionalt< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
optionalt< exprt > nondet_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create a non-deterministic value for type type, with all subtypes independently expanded as non-deter...
Expression Initialization.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
nonstd::optional< T > optionalt
Definition: optional.h:35
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
static optionalt< codet > static_lifetime_init(const irep_idt &identifier, symbol_tablet &symbol_table)
#define INITIALIZE_FUNCTION
const codet & to_code(const exprt &expr)
Definition: std_code.h:153
const code_blockt & to_code_block(const codet &code)
Definition: std_code.h:254
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:813
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:214
Author: Diffblue Ltd.