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