CBMC: /home/runner/work/cbmc/cbmc/src/linking/static_lifetime_init.cpp Source File

CBMC
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
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
19#include <goto-programs/goto_model.h>
20
21#include <ansi-c/goto-conversion/goto_convert_functions.h>
22
23#include <set>
24
25 static std::optional<codet> static_lifetime_init(
26 const irep_idt &identifier,
27 symbol_table_baset &symbol_table)
28{
29 const namespacet ns(symbol_table);
30 const symbolt &symbol = ns.lookup(identifier);
31
32 if(!symbol.is_static_lifetime)
33 return {};
34
35 if(symbol.is_type || symbol.is_macro)
36 return {};
37
38 // special values
39 if(
40 identifier == CPROVER_PREFIX "constant_infinity_uint" ||
41 identifier == CPROVER_PREFIX "memory" || identifier == "__func__" ||
42 identifier == "__FUNCTION__" || identifier == "__PRETTY_FUNCTION__" ||
43 identifier == "argc'" || identifier == "argv'" || identifier == "envp'" ||
44 identifier == "envp_size'")
45 return {};
46
47 // just for linking
48 if(identifier.starts_with(CPROVER_PREFIX "architecture_"))
49 return {};
50
51 // check type
52 if(symbol.type.id() == ID_code || symbol.type.id() == ID_empty)
53 return {};
54
55 if(symbol.type.id() == ID_array && to_array_type(symbol.type).size().is_nil())
56 {
57 if(symbol.is_extern)
58 return {};
59 // The C front-end adjusts non-extern tentative array definitions to size 1
60 // during typecheck (C standard 6.9.2, paragraph 5), keeping the symbol's
61 // type and its uses in code consistent. As a safety net for symbols
62 // produced by other means (other front-ends, instrumentation, or
63 // pre-existing goto binaries) that did not undergo that adjustment, patch
64 // the size in place here rather than aborting.
65 symbol_table.get_writeable_ref(identifier)
67 }
68
69 if(
70 (symbol.type.id() == ID_struct_tag &&
71 ns.follow_tag(to_struct_tag_type(symbol.type)).is_incomplete()) ||
72 (symbol.type.id() == ID_union_tag &&
73 ns.follow_tag(to_union_tag_type(symbol.type)).is_incomplete()))
74 {
75 return {}; // do not initialize
76 }
77
78 exprt rhs;
79
80 if((symbol.value.is_nil() && symbol.is_extern) ||
81 symbol.value.id() == ID_nondet)
82 {
84 return {};
85
86 // Nondet initialise if not linked, or if explicitly requested.
87 // Compilers would usually complain about the unlinked symbol case.
88 const auto nondet = nondet_initializer(symbol.type, symbol.location, ns);
89 CHECK_RETURN(nondet.has_value());
90 rhs = *nondet;
91 }
92 else if(symbol.value.is_nil())
93 {
94 const auto zero = zero_initializer(symbol.type, symbol.location, ns);
95 CHECK_RETURN(zero.has_value());
96 rhs = *zero;
97 }
98 else
99 rhs = symbol.value;
100
101 return code_frontend_assignt{symbol.symbol_expr(), rhs, symbol.location};
102}
103
105 symbol_table_baset &symbol_table,
106 const source_locationt &source_location)
107{
109
110 const namespacet ns(symbol_table);
111
113
114 init_symbol.value=code_blockt();
115 init_symbol.value.add_source_location()=source_location;
116
118
119 // add the magic label to hide
120 dest.add(code_labelt(CPROVER_PREFIX "HIDE", code_skipt()));
121
122 // do assignments based on "value"
123
124 // sort alphabetically for reproducible results
125 std::set<std::string> symbols;
126
127 for(const auto &symbol_pair : symbol_table.symbols)
128 {
129 symbols.insert(id2string(symbol_pair.first));
130 }
131
132 // first do framework variables
133 for(const std::string &id : symbols)
135 {
136 auto code = static_lifetime_init(id, symbol_table);
137 if(code.has_value())
138 dest.add(std::move(*code));
139 }
140
141 // now all other variables
142 for(const std::string &id : symbols)
143 if(!has_prefix(id, CPROVER_PREFIX))
144 {
145 auto code = static_lifetime_init(id, symbol_table);
146 if(code.has_value())
147 dest.add(std::move(*code));
148 }
149
150 // now call designated "initialization" functions
151
152 for(const std::string &id : symbols)
153 {
154 const symbolt &symbol=ns.lookup(id);
155
156 if(symbol.type.id() != ID_code)
157 continue;
158
159 const code_typet &code_type = to_code_type(symbol.type);
160 if(
161 code_type.return_type().id() == ID_constructor &&
162 code_type.parameters().empty())
163 {
165 symbol.symbol_expr(), {}, code_type.return_type(), source_location}});
166 }
167 }
168}
169
171 goto_modelt &goto_model,
172 message_handlert &message_handler)
173{
174 auto unloaded = goto_model.unload(INITIALIZE_FUNCTION);
176
178 goto_model.symbol_table,
179 goto_model.symbol_table.lookup_ref(INITIALIZE_FUNCTION).location);
182 goto_model.symbol_table,
183 goto_model.goto_functions,
184 message_handler);
185 goto_model.goto_functions.update();
186}
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
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
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:582
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:107
Base class for all expressions.
Definition expr.h:57
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:412
const irep_idt & id() const
Definition irep.h:388
bool is_nil() const
Definition irep.h:368
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
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:134
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
bool has_prefix(const std::string &s, const std::string &prefix)
Definition converter.cpp:13
#define CPROVER_PREFIX
std::optional< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
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...
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:44
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:787
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition std_types.h:517
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition std_types.h:887
Author: Diffblue Ltd.
#define size_type
Definition unistd.c:186

AltStyle によって変換されたページ (->オリジナル) /