-
Notifications
You must be signed in to change notification settings - Fork 274
Value-set dereference: use cond_exprt to avoid quadratic guards #4555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
93e4622
1a553e0
e860099
ae870f1
f57ebf2
739e11d
3629f2a
3645437
59145f1
b56063b
2fc837c
2e48edd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,9 @@ void goto_symext::symex_assign_rec( | |
else if(lhs.id()==ID_if) | ||
symex_assign_if( | ||
state, to_if_expr(lhs), full_lhs, rhs, guard, assignment_type); | ||
else if(lhs.id() == ID_cond) | ||
symex_assign_cond( | ||
state, to_cond_expr(lhs), full_lhs, rhs, guard, assignment_type); | ||
else if(lhs.id()==ID_typecast) | ||
symex_assign_typecast( | ||
state, to_typecast_expr(lhs), full_lhs, rhs, guard, assignment_type); | ||
|
@@ -613,6 +616,44 @@ void goto_symext::symex_assign_if( | |
} | ||
} | ||
|
||
void goto_symext::symex_assign_cond( | ||
statet &state, | ||
const cond_exprt &lhs, | ||
const exprt &full_lhs, | ||
const exprt &rhs, | ||
exprt::operandst &guard, | ||
assignment_typet assignment_type) | ||
{ | ||
std::size_t old_guard_size = guard.size(); | ||
|
||
for(std::size_t i = 0; i < lhs.get_n_cases(); ++i) | ||
{ | ||
exprt renamed_guard = state.rename(lhs.condition(i), ns).get(); | ||
do_simplify(renamed_guard); | ||
if(!renamed_guard.is_false()) | ||
{ | ||
guard.push_back(renamed_guard); | ||
symex_assign_rec( | ||
state, lhs.value(i), full_lhs, rhs, guard, assignment_type); | ||
guard.pop_back(); | ||
} | ||
|
||
// If this one is a certainty the remaining cases are irrelevant: | ||
if(renamed_guard.is_true()) | ||
break; | ||
|
||
// If the conditions are non-exclusive, further cases can only happen if | ||
// this one did not. If they are exclusive then they can be tested | ||
// independently. | ||
if(!lhs.is_exclusive()) | ||
guard.push_back(not_exprt(renamed_guard)); | ||
} | ||
|
||
// Restore the guard to its state before entering this function: | ||
INVARIANT(guard.size() >= old_guard_size, "must not shrink the guard!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/shrink/grow/? |
||
guard.resize(old_guard_size); | ||
} | ||
|
||
void goto_symext::symex_assign_byte_extract( | ||
statet &state, | ||
const byte_extract_exprt &lhs, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ Author: Daniel Kroening, [email protected] | |
#include <util/arith_tools.h> | ||
#include <util/byte_operators.h> | ||
#include <util/c_types.h> | ||
#include <util/expr_iterator.h> | ||
#include <util/pointer_offset_size.h> | ||
#include <util/simplify_expr.h> | ||
|
||
|
@@ -49,6 +50,27 @@ process_array_expr(exprt &expr, bool do_simplify, const namespacet &ns) | |
|
||
if_expr.type()=if_expr.true_case().type(); | ||
} | ||
else if(expr.id() == ID_cond) | ||
{ | ||
cond_exprt &cond_expr = to_cond_expr(expr); | ||
typet result_type; | ||
for(std::size_t i = 0; i < cond_expr.get_n_cases(); ++i) | ||
{ | ||
process_array_expr(cond_expr.value(i), do_simplify, ns); | ||
if(i == 0) | ||
result_type = cond_expr.value(i).type(); | ||
else if(cond_expr.value(i).type() != result_type) | ||
{ | ||
cond_expr.value(i) = byte_extract_exprt( | ||
byte_extract_id(), | ||
cond_expr.value(i), | ||
from_integer(0, index_type()), | ||
result_type); | ||
} | ||
} | ||
|
||
cond_expr.type() = result_type; | ||
} | ||
else if(expr.id()==ID_address_of) | ||
{ | ||
// strip | ||
|
@@ -169,6 +191,33 @@ replace_nondet(exprt &expr, symex_nondet_generatort &build_symex_nondet) | |
} | ||
} | ||
|
||
static void lower_cond_expr(exprt &expr) | ||
{ | ||
for(auto it = expr.depth_begin(), itend = expr.depth_end(); it != itend; ++it) | ||
{ | ||
if(it->id() == ID_cond) | ||
{ | ||
exprt new_expr; | ||
const auto &cond_expr = to_cond_expr(*it); | ||
INVARIANT(cond_expr.get_n_cases() >= 1, "cond_expr should not be empty"); | ||
for(std::size_t i = cond_expr.get_n_cases() - 1;; --i) | ||
{ | ||
if(i == cond_expr.get_n_cases() - 1) | ||
new_expr = cond_expr.value(i); | ||
else | ||
{ | ||
new_expr = | ||
if_exprt(cond_expr.condition(i), cond_expr.value(i), new_expr); | ||
} | ||
if(i == 0) | ||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use |
||
} | ||
|
||
it.mutate() = std::move(new_expr); | ||
} | ||
} | ||
} | ||
|
||
void goto_symext::clean_expr( | ||
exprt &expr, | ||
statet &state, | ||
|
@@ -177,6 +226,12 @@ void goto_symext::clean_expr( | |
replace_nondet(expr, path_storage.build_symex_nondet); | ||
dereference(expr, state, write); | ||
|
||
// We know how to handle cond_exprt on the LHS (symex_assign_rec does this), | ||
// but cond_exprt on the RHS is currently patchily handled, especially by the | ||
// Java string solver. For now, lower such expressions to nested if_exprts. | ||
if(!write) | ||
lower_cond_expr(expr); | ||
|
||
// make sure all remaining byte extract operations use the root | ||
// object to avoid nesting of with/update and byte_update when on | ||
// lhs | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,14 +36,21 @@ exprt value_set_dereferencet::dereference(const exprt &pointer) | |
throw "dereference expected pointer type, but got "+ | ||
pointer.type().pretty(); | ||
|
||
// we may get ifs due to recursive calls | ||
// we may get ifs or conds due to recursive calls | ||
if(pointer.id()==ID_if) | ||
{ | ||
const if_exprt &if_expr=to_if_expr(pointer); | ||
exprt true_case = dereference(if_expr.true_case()); | ||
exprt false_case = dereference(if_expr.false_case()); | ||
return if_exprt(if_expr.cond(), true_case, false_case); | ||
} | ||
else if(pointer.id() == ID_cond) | ||
{ | ||
cond_exprt result = to_cond_expr(pointer); | ||
for(std::size_t i = 0; i < result.get_n_cases(); ++i) | ||
result.value(i) = dereference(result.value(i)); | ||
return std::move(result); | ||
} | ||
|
||
// type of the object | ||
const typet &type=pointer.type().subtype(); | ||
|
@@ -107,12 +114,12 @@ exprt value_set_dereferencet::dereference(const exprt &pointer) | |
may_fail=true; | ||
} | ||
|
||
exprt failure_value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find it too non-trivial to check that |
||
|
||
if(may_fail) | ||
{ | ||
// first see if we have a "failed object" for this pointer | ||
|
||
exprt failure_value; | ||
|
||
if( | ||
const symbolt *failed_symbol = | ||
dereference_callback.get_or_create_failed_symbol(pointer)) | ||
|
@@ -138,36 +145,61 @@ exprt value_set_dereferencet::dereference(const exprt &pointer) | |
failure_value=symbol.symbol_expr(); | ||
failure_value.set(ID_C_invalid_object, true); | ||
} | ||
|
||
valuet value; | ||
value.value=failure_value; | ||
value.pointer_guard=true_exprt(); | ||
values.push_front(value); | ||
} | ||
|
||
// now build big case split, but we only do "good" objects | ||
|
||
exprt value=nil_exprt(); | ||
|
||
for(std::list<valuet>::const_iterator | ||
it=values.begin(); | ||
it!=values.end(); | ||
it++) | ||
optionalt<exprt> value_without_condition; | ||
cond_exprt cond({}, type, true); | ||
for(const auto &alias_value : values) | ||
{ | ||
if(it->value.is_not_nil()) | ||
if(alias_value.value.is_not_nil()) | ||
{ | ||
if(value.is_nil()) // first? | ||
value=it->value; | ||
if(alias_value.pointer_guard.is_false()) | ||
{ | ||
INVARIANT( | ||
!value_without_condition.has_value(), | ||
"can't discriminate between two different catch-all aliases"); | ||
value_without_condition = alias_value.value; | ||
} | ||
else | ||
value=if_exprt(it->pointer_guard, it->value, value); | ||
{ | ||
cond.add_case(alias_value.pointer_guard, alias_value.value); | ||
INVARIANT( | ||
alias_value.value.type() == type, | ||
"deref value types should match the pointer being derefd"); | ||
} | ||
} | ||
} | ||
|
||
#if 0 | ||
std::cout << "R: " << format(value) << "\n\n"; | ||
#endif | ||
// I'd like to put an invariant here that values without a pointer guard, such | ||
// as integer_address, cannot co-occur with failed objects, but this isn't the | ||
// case. There's no way to write a GOTO condition to discriminate between the | ||
// two however, so purely by historical accident, the failed object takes | ||
// precedence: | ||
|
||
if(may_fail || value_without_condition.has_value()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just combine |
||
{ | ||
// The cases must be disjoint, so add | ||
// "!(p == &o1 || p == &o2 || p == &o3 || ...) => failure-value" | ||
exprt::operandst other_case_conditions; | ||
for(std::size_t i = 0; i < cond.get_n_cases(); ++i) | ||
other_case_conditions.push_back(cond.condition(i)); | ||
cond.add_case( | ||
not_exprt(disjunction(other_case_conditions)), | ||
may_fail ? failure_value : *value_without_condition); | ||
} | ||
|
||
return value; | ||
#if 0 | ||
std::cout << "R: " << format(cond) << "\n\n"; | ||
#endif | ||
|
||
if(cond.get_n_cases() == 0) | ||
return nil_exprt(); | ||
else if(cond.get_n_cases() == 1) | ||
return cond.value(0); | ||
else | ||
return std::move(cond); | ||
} | ||
|
||
/// Check if the two types have matching number of ID_pointer levels, with | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 It's confusing that the variable names don't distinguish between the incoming guard and the extra bit coming from the current case we're considering. Maybe
renamed_case_guard
?