Skip to content

Commit a351ac8

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 2daf3dd commit a351ac8

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

src/goto-symex/symex_goto.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Author: Daniel Kroening, [email protected]
2020
#include <util/std_expr.h>
2121

2222
#include <analyses/dirty.h>
23+
#include <util/replace_symbol.h>
2324
#include <util/simplify_expr.h>
2425

2526
void goto_symext::symex_goto(statet &state)
@@ -214,6 +215,18 @@ void goto_symext::symex_goto(statet &state)
214215

215216
symex_transition(state, state_pc, backward);
216217

218+
if(!new_guard.is_true())
219+
{
220+
// update our value-set when particular objects are only compatible with
221+
// one or other branch
222+
try_filter_value_sets(
223+
state,
224+
instruction.get_condition(),
225+
backward ? &state.value_set : &goto_state_list.back().second.value_set,
226+
!backward ? &state.value_set : &goto_state_list.back().second.value_set,
227+
ns);
228+
}
229+
217230
// adjust guards
218231
if(new_guard.is_true())
219232
{

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

125126
void goto_symext::symex_assume(statet &state, const exprt &cond)
126127
{
128+
if(!cond.is_false())
129+
{
130+
// try to update the value sets using information contained in the guard
131+
try_filter_value_sets(state, cond, &state.value_set, nullptr, ns);
132+
}
133+
127134
exprt simplified_cond=cond;
128135

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

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
@@ -468,6 +468,8 @@ class value_sett
468468
const std::string &suffix,
469469
const namespacet &ns) const;
470470

471+
void erase_value_from_entry(entryt &entry, const exprt &value_to_erase);
472+
471473
protected:
472474
/// Reads the set of objects pointed to by `expr`, including making
473475
/// recursive lookups for dereference operations etc.

0 commit comments

Comments
 (0)