Skip to content

Commit a599602

Browse files
committed
Add building fresh symbols for snapshot temporaries
This is needed for example when referring to malloc(ed) variables. These are build as pointing to `tmp` variables.
1 parent 7391fa7 commit a599602

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/goto-harness/memory_snapshot_harness_generator.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ void memory_snapshot_harness_generatort::add_init_section(
131131
goto_program.const_cast_target(entry_location.start_instruction)));
132132
}
133133

134+
const symbolt &memory_snapshot_harness_generatort::fresh_symbol_copy(
135+
const symbolt &snapshot_symbol,
136+
symbol_tablet &symbol_table) const
137+
{
138+
symbolt &tmp_symbol = get_fresh_aux_symbol(
139+
snapshot_symbol.type,
140+
"", // no prefix name
141+
id2string(snapshot_symbol.base_name),
142+
snapshot_symbol.location,
143+
snapshot_symbol.mode,
144+
symbol_table);
145+
tmp_symbol.is_static_lifetime = true;
146+
tmp_symbol.value = snapshot_symbol.value;
147+
148+
return tmp_symbol;
149+
}
150+
134151
code_blockt memory_snapshot_harness_generatort::add_assignments_to_globals(
135152
const symbol_tablet &snapshot,
136153
goto_modelt &goto_model) const
@@ -141,17 +158,30 @@ code_blockt memory_snapshot_harness_generatort::add_assignments_to_globals(
141158
code_blockt code;
142159
for(const auto &pair : snapshot)
143160
{
144-
const symbolt &symbol = pair.second;
145-
if(!symbol.is_static_lifetime)
161+
const symbolt &snapshot_symbol = pair.second;
162+
symbol_tablet &symbol_table = goto_model.symbol_table;
163+
164+
auto should_get_fresh = [&symbol_table](const symbolt &symbol) {
165+
return symbol_table.lookup(symbol.base_name) == nullptr &&
166+
!symbol.is_type;
167+
};
168+
const symbolt &fresh_or_snapshot_symbol =
169+
should_get_fresh(snapshot_symbol)
170+
? fresh_symbol_copy(snapshot_symbol, symbol_table)
171+
: snapshot_symbol;
172+
173+
if(!fresh_or_snapshot_symbol.is_static_lifetime)
146174
continue;
147175

148-
if(variables_to_havoc.count(symbol.base_name) == 0)
176+
if(variables_to_havoc.count(fresh_or_snapshot_symbol.base_name) == 0)
149177
{
150-
code.add(code_assignt{symbol.symbol_expr(), symbol.value});
178+
code.add(code_assignt{fresh_or_snapshot_symbol.symbol_expr(),
179+
fresh_or_snapshot_symbol.value});
151180
}
152181
else
153182
{
154-
recursive_initialization.initialize(symbol.symbol_expr(), 0, {}, code);
183+
recursive_initialization.initialize(
184+
fresh_or_snapshot_symbol.symbol_expr(), 0, {}, code);
155185
}
156186
}
157187
return code;

src/goto-harness/memory_snapshot_harness_generator.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,23 @@ class memory_snapshot_harness_generatort : public goto_harness_generatort
221221
const symbol_exprt &func_init_done_var,
222222
goto_modelt &goto_model) const;
223223

224+
/// Introduce a new symbol into \p symbol_table with the same name and type as
225+
/// \p snapshot_symbol
226+
/// \param snapshot_symbol: the unknown symbol to be introduced
227+
/// \param symbol_table: the symbol table to be updated
228+
/// \return the new symbol
229+
const symbolt &fresh_symbol_copy(
230+
const symbolt &snapshot_symbol,
231+
symbol_tablet &symbol_table) const;
232+
224233
/// For each global symbol in the \p snapshot symbol table either:
225234
/// 1) add \ref code_assignt assigning a value from the \p snapshot to the
226235
/// symbol
227236
/// or
228237
/// 2) recursively initialise the symbol to a non-deterministic value of the
229-
/// right type
238+
/// right type.
239+
/// Malloc(ed) pointers point to temporaries which do not exists in the symbol
240+
/// table: for these we introduce fresh symbols.
230241
/// \param snapshot: snapshot to load the symbols and their values from
231242
/// \param goto_model: model to initialise the havoc-ing
232243
/// \return the block of code where the assignments are added

0 commit comments

Comments
 (0)