Skip to content

Commit 821d2db

Browse files
author
Owen
committed
filter value sets at gotos and assumes
When a conditional change of control flow in goto-symex (i.e. gotos and assumes), if there is a pointer-typed symbol in the condition then try to filter its value set separately for each branch based on which values are actually possible on that branch. We only do this when there is only one pointer-typed symbol in the condition. The intention is to make the value-sets more accurate. In particular, this lays the groundwork for dealing with virtual method dispatch much more efficiently. See diffblue#2122 for an approach that was rejected. For example in java, code like `x.equals(y)`, where `x` could point to an Object or a String, gets turned into code like ``` if (x.@class_identifier == 'java.lang.Object') x . java.lang.Object.equals(y) else if (x.@class_identifier == 'java.lang.String') x . java.lang.String.equals(y) ``` When symex goes into java.lang.String.equals it doesn't filter the value-set so that `this` (which is `x`) only points to the String, not the Object. This causes symex to add complicated expressions to the formula for field accesses to fields that x won't have if it points to an Object.
1 parent 3a2f2bb commit 821d2db

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

src/goto-symex/goto_symex.h

+23
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,29 @@ class goto_symext
333333
const exprt &new_guard,
334334
const namespacet &ns);
335335

336+
/// Try to filter value sets based on whether possible values of a
337+
/// pointer-typed symbol make the condition true or false. We only do this
338+
/// when there is only one pointer-typed symbol in \p condition.
339+
/// \param state: The current state
340+
/// \param condition: The condition which is being evaluated, which it expects
341+
/// will not have been cleaned or renamed. In practice, it's fine if it has
342+
/// been cleaned and renamed up to level L1.
343+
/// \param original_value_set: The value set we will read from
344+
/// \param jump_taken_value_set: The value set that will be used when the
345+
/// condition is true, so we remove any elements which we can tell will
346+
/// make the condition false, or nullptr if this shouldn't be done
347+
/// \param jump_not_taken_value_set: The value set that will be used when the
348+
/// condition is false, so we remove any elements which we can tell will
349+
/// make the condition true, or nullptr if this shouldn't be done
350+
/// \param ns: A namespace
351+
void try_filter_value_sets(
352+
goto_symex_statet &state,
353+
exprt condition,
354+
const value_sett &original_value_set,
355+
value_sett *jump_taken_value_set,
356+
value_sett *jump_not_taken_value_set,
357+
const namespacet &ns);
358+
336359
virtual void vcc(
337360
const exprt &,
338361
const std::string &msg,

src/goto-symex/symex_goto.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ void goto_symext::apply_goto_condition(
2929
const exprt &new_guard,
3030
const namespacet &ns)
3131
{
32+
// It would be better to call try_filter_value_sets after apply_condition,
33+
// and pass nullptr for value sets which apply_condition successfully updated
34+
// already. However, try_filter_value_sets calls rename to do constant
35+
// propagation, and apply_condition can update the constant propagator. Since
36+
// apply condition will never succeed on both jump_taken_state and
37+
// jump_not_taken_state, it should be possible to pass a reference to an
38+
// unmodified goto_statet to use for renaming. But renaming needs a
39+
// goto_symex_statet, not just a goto_statet, and we only have access to one
40+
// of those. A future improvement would be to split rename into two parts:
41+
// one part on goto_symex_statet which is non-const and deals with
42+
// l2 thread reads, and one part on goto_statet which is const and could be
43+
// used in try_filter_value_sets.
44+
45+
try_filter_value_sets(
46+
current_state,
47+
original_guard,
48+
jump_taken_state.value_set,
49+
&jump_taken_state.value_set,
50+
&jump_not_taken_state.value_set,
51+
ns);
52+
3253
jump_taken_state.apply_condition(new_guard, current_state, ns);
3354

3455
// Try to find a negative condition that implies an equality constraint on

src/goto-symex/symex_main.cpp

+161
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <memory>
1616

1717
#include <util/exception_utils.h>
18+
#include <util/expr_iterator.h>
1819
#include <util/expr_util.h>
1920
#include <util/make_unique.h>
2021
#include <util/mathematical_expr.h>
@@ -128,6 +129,13 @@ void goto_symext::symex_assume(statet &state, const exprt &cond)
128129
simplified_cond = state.rename(std::move(simplified_cond), ns).get();
129130
do_simplify(simplified_cond);
130131

132+
// It would be better to call try_filter_value_sets after apply_condition,
133+
// but it is not currently possible. See the comment at the beginning of
134+
// \ref apply_goto_condition for more information.
135+
136+
try_filter_value_sets(
137+
state, cond, state.value_set, &state.value_set, nullptr, ns);
138+
131139
// apply_condition must come after rename because it might change the
132140
// constant propagator and the value-set and we read from those in rename
133141
state.apply_condition(simplified_cond, state, ns);
@@ -543,3 +551,156 @@ void goto_symext::symex_step(
543551
UNREACHABLE;
544552
}
545553
}
554+
555+
/// Check if an expression only contains one unique symbol (possibly repeated
556+
/// multiple times)
557+
/// \param expr: The expression to examine
558+
/// \return If only one unique symbol occurs in \p expr then return it;
559+
/// otherwise return an empty optionalt
560+
static optionalt<symbol_exprt>
561+
find_unique_pointer_typed_symbol(const exprt &expr)
562+
{
563+
optionalt<symbol_exprt> return_value;
564+
for(auto it = expr.depth_cbegin(); it != expr.depth_cend(); ++it)
565+
{
566+
const symbol_exprt *symbol_expr = expr_try_dynamic_cast<symbol_exprt>(*it);
567+
if(symbol_expr && can_cast_type<pointer_typet>(symbol_expr->type()))
568+
{
569+
// If we already have a potential return value, check if it is the same
570+
// symbol, and return an empty optionalt if not
571+
if(return_value && *symbol_expr != *return_value)
572+
{
573+
return {};
574+
}
575+
return_value = *symbol_expr;
576+
}
577+
}
578+
579+
// Either expr contains no pointer-typed symbols or it contains one unique
580+
// pointer-typed symbol, possibly repeated multiple times
581+
return return_value;
582+
}
583+
584+
/// This is a simplified version of value_set_dereferencet::build_reference_to.
585+
/// It ignores the ID_dynamic_object case (which doesn't occur in goto-symex)
586+
/// and gives up for integer addresses and non-trivial symbols
587+
/// \param value_set_element: An element of a value-set
588+
/// \param type: the type of the expression that might point to
589+
/// \p value_set_element
590+
/// \return An expression for the value of the pointer indicated by \p
591+
/// value_set_element if it is easy to determine, or an empty optionalt
592+
/// otherwise
593+
static optionalt<exprt>
594+
value_set_element_to_expr(exprt value_set_element, pointer_typet type)
595+
{
596+
const object_descriptor_exprt *object_descriptor =
597+
expr_try_dynamic_cast<object_descriptor_exprt>(value_set_element);
598+
if(!object_descriptor)
599+
{
600+
return {};
601+
}
602+
603+
const exprt &root_object = object_descriptor->root_object();
604+
const exprt &object = object_descriptor->object();
605+
606+
if(root_object.id() == ID_null_object)
607+
{
608+
return null_pointer_exprt{type};
609+
}
610+
else if(root_object.id() == ID_integer_address)
611+
{
612+
return {};
613+
}
614+
else
615+
{
616+
// We should do something like
617+
// value_set_dereference::dereference_type_compare, which deals with
618+
// arrays having types containing void
619+
if(object_descriptor->offset().is_zero() && object.type() == type.subtype())
620+
{
621+
return address_of_exprt(object);
622+
}
623+
else
624+
{
625+
return {};
626+
}
627+
}
628+
}
629+
630+
void goto_symext::try_filter_value_sets(
631+
goto_symex_statet &state,
632+
exprt condition,
633+
const value_sett &original_value_set,
634+
value_sett *jump_taken_value_set,
635+
value_sett *jump_not_taken_value_set,
636+
const namespacet &ns)
637+
{
638+
condition = state.rename<L1>(std::move(condition), ns).get();
639+
640+
optionalt<symbol_exprt> symbol_expr =
641+
find_unique_pointer_typed_symbol(condition);
642+
643+
if(!symbol_expr)
644+
{
645+
return;
646+
}
647+
648+
const pointer_typet &symbol_type = to_pointer_type(symbol_expr->type());
649+
650+
value_setst::valuest value_set_elements;
651+
original_value_set.get_value_set(*symbol_expr, value_set_elements, ns);
652+
653+
// Try evaluating the condition with the symbol replaced by a pointer to each
654+
// one of its possible values in turn. If that leads to a true for some
655+
// value_set_element then we can delete it from the value set that will be
656+
// used if the condition is false, and vice versa.
657+
for(const auto &value_set_element : value_set_elements)
658+
{
659+
optionalt<exprt> possible_value =
660+
value_set_element_to_expr(value_set_element, symbol_type);
661+
662+
if(!possible_value)
663+
{
664+
continue;
665+
}
666+
667+
exprt modified_condition(condition);
668+
669+
address_of_aware_replace_symbolt replace_symbol{};
670+
replace_symbol.insert(*symbol_expr, *possible_value);
671+
replace_symbol(modified_condition);
672+
673+
// This do_simplify() is needed for the following reason: if `condition` is
674+
// `*p == a` and we replace `p` with `&a` then we get `*&a == a`. Suppose
675+
// our constant propagation knows that `a` is `1`. Without this call to
676+
// do_simplify(), state.rename() turns this into `*&a == 1` (because
677+
// rename() doesn't do constant propagation inside addresses), which
678+
// do_simplify() turns into `a == 1`, which cannot be evaluated as true
679+
// without another round of constant propagation.
680+
// It would be sufficient to replace this call to do_simplify() with
681+
// something that just replaces `*&x` with `x` whenever it finds it.
682+
do_simplify(modified_condition);
683+
684+
const bool record_events = state.record_events;
685+
state.record_events = false;
686+
modified_condition = state.rename(std::move(modified_condition), ns).get();
687+
state.record_events = record_events;
688+
689+
do_simplify(modified_condition);
690+
691+
if(jump_taken_value_set && modified_condition.is_false())
692+
{
693+
value_sett::entryt *entry = jump_taken_value_set->get_entry_for_symbol(
694+
symbol_expr->get_identifier(), symbol_type, "", ns);
695+
jump_taken_value_set->erase_value_from_entry(*entry, value_set_element);
696+
}
697+
else if(jump_not_taken_value_set && modified_condition.is_true())
698+
{
699+
value_sett::entryt *entry =
700+
jump_not_taken_value_set->get_entry_for_symbol(
701+
symbol_expr->get_identifier(), symbol_type, "", ns);
702+
jump_not_taken_value_set->erase_value_from_entry(
703+
*entry, value_set_element);
704+
}
705+
}
706+
}

src/pointer-analysis/value_set.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -1623,3 +1623,25 @@ void value_sett::guard(
16231623
assign(expr.op0(), address_of, ns, false, false);
16241624
}
16251625
}
1626+
1627+
void value_sett::erase_value_from_entry(
1628+
entryt &entry,
1629+
const exprt &value_to_erase)
1630+
{
1631+
std::vector<object_map_dt::key_type> keys_to_erase;
1632+
1633+
for(const auto &key_value : entry.object_map.read())
1634+
{
1635+
const auto &rhs_object = to_expr(key_value);
1636+
if(rhs_object == value_to_erase)
1637+
{
1638+
keys_to_erase.emplace_back(key_value.first);
1639+
}
1640+
}
1641+
1642+
DATA_INVARIANT(
1643+
keys_to_erase.size() == 1,
1644+
"value_sett::erase_value_from_entry() should erase exactly one value");
1645+
1646+
entry.object_map.write().erase(keys_to_erase[0]);
1647+
}

src/pointer-analysis/value_set.h

+2
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ class value_sett
483483
const std::string &suffix,
484484
const namespacet &ns) const;
485485

486+
void erase_value_from_entry(entryt &entry, const exprt &value_to_erase);
487+
486488
protected:
487489
/// Reads the set of objects pointed to by `expr`, including making
488490
/// recursive lookups for dereference operations etc.

0 commit comments

Comments
 (0)