cprover
Loading...
Searching...
No Matches
static_lifetime_init.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
10
11#include <util/arith_tools.h>
12#include <util/c_types.h>
14#include <util/namespace.h>
15#include <util/prefix.h>
16#include <util/std_code.h>
18
21
22#include <set>
23
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(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 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)
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,
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)
unsignedbv_typet size_type()
Definition c_types.cpp:55
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:539
const parameterst & parameters() const
Definition std_types.h:655
const typet & return_type() const
Definition std_types.h:645
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:39
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:420
const irep_idt & id() const
Definition irep.h:396
bool is_nil() const
Definition irep.h:376
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().
A side_effect_exprt representation of a function call side effect.
Definition std_code.h:1692
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.
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
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
The type of an expression, extends irept.
Definition type.h:29
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.
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:47
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)
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_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:744
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition std_types.h:844
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.