CBMC
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_base.h>
18 
21 
22 #include <set>
23 
24 static std::optional<codet> static_lifetime_init(
25  const irep_idt &identifier,
26  symbol_table_baset &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(identifier.starts_with(CPROVER_PREFIX "architecture_"))
48  return {};
49 
50  // check type
51  if(symbol.type.id() == ID_code || symbol.type.id() == ID_empty)
52  return {};
53 
54  if(symbol.type.id() == ID_array && to_array_type(symbol.type).size().is_nil())
55  {
56  // C standard 6.9.2, paragraph 5
57  // adjust the type to an array of size 1
58  symbolt &writable_symbol = symbol_table.get_writeable_ref(identifier);
59  writable_symbol.type = symbol.type;
60  writable_symbol.type.set(ID_size, from_integer(1, size_type()));
61  }
62 
63  if(
64  (symbol.type.id() == ID_struct_tag &&
66  (symbol.type.id() == ID_union_tag &&
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  if(symbol.value.get_bool(ID_C_no_nondet_initialization))
78  return {};
79 
80  // Nondet initialise if not linked, or if explicitly requested.
81  // Compilers would usually complain about the unlinked symbol case.
82  const auto nondet = nondet_initializer(symbol.type, symbol.location, ns);
83  CHECK_RETURN(nondet.has_value());
84  rhs = *nondet;
85  }
86  else if(symbol.value.is_nil())
87  {
88  const auto zero = zero_initializer(symbol.type, symbol.location, ns);
89  CHECK_RETURN(zero.has_value());
90  rhs = *zero;
91  }
92  else
93  rhs = symbol.value;
94 
95  return code_frontend_assignt{symbol.symbol_expr(), rhs, symbol.location};
96 }
97 
99  symbol_table_baset &symbol_table,
100  const source_locationt &source_location)
101 {
103 
104  const namespacet ns(symbol_table);
105 
107 
108  init_symbol.value=code_blockt();
109  init_symbol.value.add_source_location()=source_location;
110 
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  {
159  symbol.symbol_expr(), {}, code_type.return_type(), source_location}});
160  }
161  }
162 }
163 
165  goto_modelt &goto_model,
166  message_handlert &message_handler)
167 {
168  auto unloaded = goto_model.unload(INITIALIZE_FUNCTION);
169  PRECONDITION(unloaded == 1);
170 
172  goto_model.symbol_table,
174  goto_convert(
176  goto_model.symbol_table,
177  goto_model.goto_functions,
178  message_handler);
179  goto_model.goto_functions.update();
180 }
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition: c_types.h:224
const exprt & size() const
Definition: std_types.h:840
A codet representing sequential composition of program statements.
Definition: std_code.h:130
void add(const codet &code)
Definition: std_code.h:168
codet representation of an expression statement.
Definition: std_code.h:1394
A codet representing an assignment in the program.
Definition: std_code.h:24
codet representation of a label for branch targets.
Definition: std_code.h:959
A codet representing a skip statement.
Definition: std_code.h:322
Base type of functions.
Definition: std_types.h:583
const typet & return_type() const
Definition: std_types.h:689
const parameterst & parameters() const
Definition: std_types.h:699
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool starts_with(const char *s) const
equivalent of as_string().starts_with(s)
Definition: dstring.h:95
Base class for all expressions.
Definition: expr.h:56
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:31
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:34
std::size_t unload(const irep_idt &name)
Remove the function named name from the function map, if it exists.
Definition: goto_model.h:72
bool get_bool(const irep_idt &name) const
Definition: irep.cpp:57
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:408
const irep_idt & id() const
Definition: irep.h:384
bool is_nil() const
Definition: irep.h:364
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition: namespace.cpp:63
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:148
A side_effect_exprt representation of a function call side effect.
Definition: std_code.h:1692
bool is_incomplete() const
A struct/union may be incomplete.
Definition: std_types.h:185
The symbol table base class interface.
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.
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
Symbol table entry.
Definition: symbol.h:28
bool is_extern
Definition: symbol.h:74
bool is_macro
Definition: symbol.h:62
bool is_static_lifetime
Definition: symbol.h:70
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
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
#define CPROVER_PREFIX
std::optional< 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...
std::optional< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
Expression Initialization.
void goto_convert(const codet &code, symbol_table_baset &symbol_table, goto_programt &dest, message_handlert &message_handler, const irep_idt &mode)
Goto Programs with Functions.
Symbol Table + CFG.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:40
static std::unordered_set< irep_idt > init_symbol(const symbolt &sym, code_blockt &code_block, symbol_table_baset &symbol_table, const source_locationt &source_location, bool assume_init_pointers_not_null, const java_object_factory_parameterst &object_factory_parameters, const select_pointer_typet &pointer_type_selector, bool string_refinement_enabled, message_handlert &message_handler)
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
static std::optional< codet > static_lifetime_init(const irep_idt &identifier, symbol_table_baset &symbol_table)
void recreate_initialize_function(goto_modelt &goto_model, message_handlert &message_handler)
Regenerates the CPROVER_INITIALIZE function, which initializes all non-function symbols of the goto m...
#define INITIALIZE_FUNCTION
const code_blockt & to_code_block(const codet &code)
Definition: std_code.h:203
const codet & to_code(const exprt &expr)
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:788
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:888
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:518
Author: Diffblue Ltd.
#define size_type
Definition: unistd.c:347