Skip to content

Commit ef52a57

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 e9a67bd commit ef52a57

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-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().value_set,
226+
!backward ? &state.value_set : &goto_state_list.back().value_set,
227+
ns);
228+
}
229+
217230
// adjust guards
218231
if(new_guard.is_true())
219232
{

src/goto-symex/symex_main.cpp

+159
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>
@@ -125,6 +126,12 @@ void goto_symext::vcc(
125126

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

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

src/pointer-analysis/value_set.cpp

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

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

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)