Skip to content

Commit 4359fc1

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 295e988 commit 4359fc1

File tree

5 files changed

+232
-3
lines changed

5 files changed

+232
-3
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-3
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,37 @@ void goto_symext::apply_goto_condition(
3030
const exprt &new_guard,
3131
const namespacet &ns)
3232
{
33-
jump_taken_state.apply_condition(new_guard, current_state, ns);
33+
bool changed_taken_state =
34+
jump_taken_state.apply_condition(new_guard, current_state, ns);
3435

36+
bool changed_not_taken_state = false;
3537
// Try to find a negative condition that implies an equality constraint on
3638
// the branch-not-taken path.
3739
// Could use not_exprt + simplify, but let's avoid paying that cost on quite
3840
// a hot path:
3941
if(new_guard.id() == ID_not)
40-
jump_not_taken_state.apply_condition(new_guard.op0(), current_state, ns);
42+
{
43+
changed_not_taken_state =
44+
jump_not_taken_state.apply_condition(new_guard.op0(), current_state, ns);
45+
}
4146
else if(new_guard.id() == ID_notequal)
4247
{
43-
jump_not_taken_state.apply_condition(
48+
changed_not_taken_state = jump_not_taken_state.apply_condition(
4449
equal_exprt(new_guard.op0(), new_guard.op1()), current_state, ns);
4550
}
51+
52+
const value_sett &original_value_set = changed_taken_state
53+
? jump_not_taken_state.value_set
54+
: jump_taken_state.value_set;
55+
// update our value-set when particular objects are only compatible with
56+
// one or other branch
57+
try_filter_value_sets(
58+
current_state,
59+
original_guard,
60+
original_value_set,
61+
changed_taken_state ? nullptr : &jump_taken_state.value_set,
62+
changed_not_taken_state ? nullptr : &jump_not_taken_state.value_set,
63+
ns);
4664
}
4765

4866
void goto_symext::symex_goto(statet &state)

src/goto-symex/symex_main.cpp

+164
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>
@@ -124,6 +125,7 @@ void goto_symext::vcc(
124125

125126
void goto_symext::symex_assume(statet &state, const exprt &cond)
126127
{
128+
127129
exprt simplified_cond=cond;
128130

129131
clean_expr(simplified_cond, state, false);
@@ -135,6 +137,13 @@ void goto_symext::symex_assume(statet &state, const exprt &cond)
135137
bool apply_condition_successful =
136138
state.apply_condition(simplified_cond, state, ns);
137139

140+
if(!apply_condition_successful)
141+
{
142+
// try to update the value sets using information contained in the guard
143+
try_filter_value_sets(
144+
state, cond, state.value_set, &state.value_set, nullptr, ns);
145+
}
146+
138147
symex_assume_l2(state, simplified_cond);
139148
}
140149

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

src/pointer-analysis/value_set.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -1615,3 +1615,25 @@ exprt value_sett::make_member(
16151615

16161616
return member_expr;
16171617
}
1618+
1619+
void value_sett::erase_value_from_entry(
1620+
entryt &entry,
1621+
const exprt &value_to_erase)
1622+
{
1623+
std::vector<object_map_dt::key_type> keys_to_erase;
1624+
1625+
for(const auto &key_value : entry.object_map.read())
1626+
{
1627+
const auto &rhs_object = to_expr(key_value);
1628+
if(rhs_object == value_to_erase)
1629+
{
1630+
keys_to_erase.emplace_back(key_value.first);
1631+
}
1632+
}
1633+
1634+
DATA_INVARIANT(
1635+
keys_to_erase.size() == 1,
1636+
"value_sett::erase_value_from_entry() should erase exactly one value");
1637+
1638+
entry.object_map.write().erase(keys_to_erase[0]);
1639+
}

src/pointer-analysis/value_set.h

+2
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ class value_sett
473473
const std::string &suffix,
474474
const namespacet &ns) const;
475475

476+
void erase_value_from_entry(entryt &entry, const exprt &value_to_erase);
477+
476478
protected:
477479
/// Reads the set of objects pointed to by `expr`, including making
478480
/// recursive lookups for dereference operations etc.

0 commit comments

Comments
 (0)