Skip to content

Commit eedd91b

Browse files
peterschrammelEnrico Steffinlongo
authored and
Enrico Steffinlongo
committed
Add util for cleaning pointer argument expr
Clean the given pointer expression so that it has the right shape for being used for identifying shadow memory. This handles some quirks regarding array sizes containing L2 symbols and string constants not having char-pointer type.
1 parent 1ecce42 commit eedd91b

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/goto-symex/shadow_memory_util.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ Author: Peter Schrammel
1111

1212
#include "shadow_memory_util.h"
1313

14+
#include <util/c_types.h>
1415
#include <util/invariant.h>
1516
#include <util/pointer_expr.h>
17+
#include <util/ssa_expr.h>
1618
#include <util/std_expr.h>
1719

1820
irep_idt extract_field_name(const exprt &string_expr)
@@ -30,3 +32,42 @@ irep_idt extract_field_name(const exprt &string_expr)
3032
else
3133
UNREACHABLE;
3234
}
35+
36+
/// If the given type is an array type, then remove the L2 level
37+
/// renaming from its size.
38+
/// \param type Type to be modified
39+
static void remove_array_type_l2(typet &type)
40+
{
41+
if(to_array_type(type).size().id() != ID_symbol)
42+
return;
43+
44+
ssa_exprt &size = to_ssa_expr(to_array_type(type).size());
45+
size.remove_level_2();
46+
}
47+
48+
void clean_pointer_expr(exprt &expr, const typet &type)
49+
{
50+
if(
51+
type.id() == ID_array && expr.id() == ID_symbol &&
52+
to_array_type(type).size().get_bool(ID_C_SSA_symbol))
53+
{
54+
remove_array_type_l2(expr.type());
55+
exprt original_expr = to_ssa_expr(expr).get_original_expr();
56+
remove_array_type_l2(original_expr.type());
57+
to_ssa_expr(expr).set_expression(original_expr);
58+
}
59+
if(expr.id() == ID_string_constant)
60+
{
61+
expr = address_of_exprt(expr);
62+
expr.type() = pointer_type(char_type());
63+
}
64+
else if(expr.id() == ID_dereference)
65+
{
66+
expr = to_dereference_expr(expr).pointer();
67+
}
68+
else
69+
{
70+
expr = address_of_exprt(expr);
71+
}
72+
POSTCONDITION(expr.type().id() == ID_pointer);
73+
}

src/goto-symex/shadow_memory_util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ Author: Peter Schrammel
1515
#include <util/irep.h>
1616

1717
class exprt;
18+
class typet;
1819

1920
/// Extracts the field name identifier from a string expression,
2021
/// e.g. as passed as argument to a __CPROVER_field_decl_local call.
2122
/// \param string_expr The string argument expression
2223
/// \return The identifier denoted by the string argument expression
2324
irep_idt extract_field_name(const exprt &string_expr);
2425

26+
/// Clean the given pointer expression so that it has the right
27+
/// shape for being used for identifying shadow memory.
28+
/// This handles some quirks regarding array sizes containing
29+
/// L2 symbols and string constants not having char-pointer type.
30+
/// \param expr The pointer to the original memory, e.g. as passed to
31+
/// __CPROVER_field_get.
32+
/// \param type The followed type of expr.
33+
void clean_pointer_expr(exprt &expr, const typet &type);
34+
2535
#endif // CPROVER_GOTO_SYMEX_SHADOW_MEMORY_STATE_H

0 commit comments

Comments
 (0)