Skip to content

Commit 93b7d71

Browse files
peterschrammelEnrico Steffinlongo
authored and
Enrico Steffinlongo
committed
Functionality for initializing shadow memory
Allocates shadows for a given original memory for each declared field. The shadows are registered in the shadow_memory_state (part of goto_symex_statet to consider scoping of local shadows). In the next step this functionality is used to allocate shadow memory for the original memory allocations encountered when symexing a goto model.
1 parent eedd91b commit 93b7d71

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/goto-symex/shadow_memory.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Author: Peter Schrammel
1212
#include "shadow_memory.h"
1313

1414
#include <util/bitvector_types.h>
15+
#include <util/expr_initializer.h>
16+
#include <util/format_expr.h>
1517
#include <util/format_type.h>
1618
#include <util/fresh_symbol.h>
1719

@@ -22,34 +24,64 @@ Author: Peter Schrammel
2224

2325
void shadow_memoryt::initialize_shadow_memory(
2426
goto_symex_statet &state,
25-
const exprt &expr,
27+
exprt expr,
2628
const shadow_memory_field_definitionst::field_definitiont &fields)
2729
{
28-
// To be implemented
30+
typet type = ns.follow(expr.type());
31+
clean_pointer_expr(expr, type);
32+
for(const auto &field_pair : fields)
33+
{
34+
const symbol_exprt &shadow = add_field(state, expr, field_pair.first, type);
35+
36+
if(
37+
field_pair.second.id() == ID_typecast &&
38+
to_typecast_expr(field_pair.second).op().is_zero())
39+
{
40+
const auto zero_value =
41+
zero_initializer(type, expr.source_location(), ns);
42+
CHECK_RETURN(zero_value.has_value());
43+
44+
symex_assign(state, shadow, *zero_value);
45+
}
46+
else
47+
{
48+
exprt init_expr = field_pair.second;
49+
if(init_expr.id() == ID_typecast)
50+
init_expr = to_typecast_expr(field_pair.second).op();
51+
const auto init_value =
52+
expr_initializer(type, expr.source_location(), ns, init_expr);
53+
CHECK_RETURN(init_value.has_value());
54+
55+
symex_assign(state, shadow, *init_value);
56+
}
57+
58+
log.debug() << "Shadow memory: initialize field "
59+
<< id2string(shadow.get_identifier()) << " for " << format(expr)
60+
<< " with initial value " << format(field_pair.second)
61+
<< messaget::eom;
62+
}
2963
}
3064

31-
symbol_exprt shadow_memoryt::add_field(
65+
const symbol_exprt &shadow_memoryt::add_field(
3266
goto_symex_statet &state,
3367
const exprt &expr,
3468
const irep_idt &field_name,
3569
const typet &field_type)
3670
{
37-
// To be completed
38-
3971
const auto &function_symbol = ns.lookup(state.source.function_id);
40-
41-
symbolt &new_symbol = get_fresh_aux_symbol(
72+
const symbolt &new_symbol = get_fresh_aux_symbol(
4273
field_type,
4374
id2string(state.source.function_id),
4475
SHADOW_MEMORY_PREFIX + from_expr(expr) + "__" + id2string(field_name),
4576
state.source.pc->source_location(),
4677
function_symbol.mode,
4778
state.symbol_table);
4879

49-
// Call some function on ns to silence the compiler in the meanwhile.
50-
ns.get_symbol_table();
80+
auto &addresses = state.shadow_memory.address_fields[field_name];
81+
addresses.push_back(
82+
shadow_memory_statet::shadowed_addresst{expr, new_symbol.symbol_expr()});
5183

52-
return new_symbol.symbol_expr();
84+
return addresses.back().shadow;
5385
}
5486

5587
void shadow_memoryt::symex_set_field(

src/goto-symex/shadow_memory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class shadow_memoryt
115115
/// \param fields The field definition to be used
116116
void initialize_shadow_memory(
117117
goto_symex_statet &state,
118-
const exprt &expr,
118+
exprt expr,
119119
const shadow_memory_field_definitionst::field_definitiont &fields);
120120

121121
/// Registers a shadow memory field for the given original memory
@@ -124,7 +124,7 @@ class shadow_memoryt
124124
/// \param field_name The field name
125125
/// \param field_type The field type
126126
/// \return The resulting shadow memory symbol expression
127-
symbol_exprt add_field(
127+
const symbol_exprt &add_field(
128128
goto_symex_statet &state,
129129
const exprt &expr,
130130
const irep_idt &field_name,

0 commit comments

Comments
 (0)