Skip to content

Fix simplification of pointer-object comparison [blocks: #4628] #4627

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/util/simplify_expr_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,25 @@ bool simplify_exprt::simplify_inequality_address_of(exprt &expr)

return false;
}
else if(
tmp0.op0().id() == ID_dynamic_object &&
tmp1.op0().id() == ID_dynamic_object)
{
bool equal = to_dynamic_object_expr(tmp0.op0()).get_instance() ==
to_dynamic_object_expr(tmp1.op0()).get_instance();

expr.make_bool(expr.id() == ID_equal ? equal : !equal);

return false;
}
else if(
(tmp0.op0().id() == ID_symbol && tmp1.op0().id() == ID_dynamic_object) ||
(tmp0.op0().id() == ID_dynamic_object && tmp1.op0().id() == ID_symbol))
{
expr.make_bool(expr.id() != ID_equal);

return false;
}

return true;
}
Expand All @@ -458,6 +477,7 @@ bool simplify_exprt::simplify_inequality_pointer_object(exprt &expr)
DATA_INVARIANT(
expr.operands().size() == 2, "(in)equalities have two operands");

exprt::operandst new_inequality_ops;
forall_operands(it, expr)
{
PRECONDITION(it->id() == ID_pointer_object);
Expand All @@ -474,16 +494,23 @@ bool simplify_exprt::simplify_inequality_pointer_object(exprt &expr)
return true;
}
}
else if(op.id() != ID_constant || op.get(ID_value) != ID_NULL)
else if(op.id() != ID_constant || !op.is_zero())
{
return true;
}
}

bool equal=expr.op0().op0()==expr.op1().op0();

expr.make_bool(expr.id()==ID_equal?equal:!equal);
if(new_inequality_ops.empty())
new_inequality_ops.push_back(op);
else
{
new_inequality_ops.push_back(typecast_exprt::conditional_cast(
op, new_inequality_ops.front().type()));
simplify_node(new_inequality_ops.back());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought for the future: in this case I think we have a very particular simplification in mind -- presumably that nulls are typed so the cast might be folded, and that (T*)(S*)x can become (T*)x. However bluntly using the simplifier always risks that a much more expensive transformation might be attempted. This suggests factoring out particular simplifications as utility methods, used both during simplification and as the routine way of creating a pointer cast.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That certainly is something to review all the simplifier code for. I actually have some patches in a branch already, but it's not quite a systematic review yet. Will work on this in a separate PR.

}
}

expr.operands() = std::move(new_inequality_ops);
simplify_inequality(expr);
return false;
}

Expand Down
44 changes: 36 additions & 8 deletions unit/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Author: Michael Tautschnig
#include <util/std_expr.h>
#include <util/symbol_table.h>

TEST_CASE("Simplify pointer_offset(address of array index)")
TEST_CASE("Simplify pointer_offset(address of array index)", "[core][util]")
{
config.set_arch("none");

Expand All @@ -42,7 +42,7 @@ TEST_CASE("Simplify pointer_offset(address of array index)")
REQUIRE(offset_value==1);
}

TEST_CASE("Simplify const pointer offset")
TEST_CASE("Simplify const pointer offset", "[core][util]")
{
config.set_arch("none");

Expand All @@ -63,7 +63,7 @@ TEST_CASE("Simplify const pointer offset")
REQUIRE(offset_value==1234);
}

TEST_CASE("Simplify byte extract")
TEST_CASE("Simplify byte extract", "[core][util]")
{
// this test does require a proper architecture to be set so that byte extract
// uses adequate endianness
Expand All @@ -84,7 +84,7 @@ TEST_CASE("Simplify byte extract")
REQUIRE(simp == s);
}

TEST_CASE("expr2bits and bits2expr respect bit order")
TEST_CASE("expr2bits and bits2expr respect bit order", "[core][util]")
{
symbol_tablet symbol_table;
namespacet ns(symbol_table);
Expand All @@ -109,7 +109,7 @@ TEST_CASE("expr2bits and bits2expr respect bit order")
REQUIRE(deadbeef == *should_be_deadbeef2);
}

TEST_CASE("Simplify extractbit")
TEST_CASE("Simplify extractbit", "[core][util]")
{
// this test does require a proper architecture to be set so that byte extract
// uses adequate endianness
Expand Down Expand Up @@ -139,7 +139,7 @@ TEST_CASE("Simplify extractbit")
REQUIRE(eb2 == true_exprt());
}

TEST_CASE("Simplify extractbits")
TEST_CASE("Simplify extractbits", "[core][util]")
{
// this test does require a proper architecture to be set so that byte extract
// uses adequate endianness
Expand All @@ -158,7 +158,7 @@ TEST_CASE("Simplify extractbits")
REQUIRE(eb == from_integer(0xbe, unsignedbv_typet(8)));
}

TEST_CASE("Simplify shift")
TEST_CASE("Simplify shift", "[core][util]")
{
const symbol_tablet symbol_table;
const namespacet ns(symbol_table);
Expand Down Expand Up @@ -207,12 +207,40 @@ TEST_CASE("Simplify dynamic object comparison", "[core][util]")
REQUIRE(simplify_expr(compare_null_through_cast, ns) == false_exprt());

dynamic_object_exprt other_dynamic_object(signedbv_typet(8));
dynamic_object.set_instance(2);
other_dynamic_object.set_instance(2);
address_of_exprt address_of_other_dynamic_object(other_dynamic_object);

equal_exprt compare_pointer_objects(
pointer_object(address_of_dynamic_object),
pointer_object(address_of_other_dynamic_object));

REQUIRE(simplify_expr(compare_pointer_objects, ns) == false_exprt());

symbol_exprt s{"foo", signedbv_typet{8}};
address_of_exprt address_of_symbol{s};

equal_exprt compare_symbol_dynamic{address_of_dynamic_object,
address_of_symbol};

REQUIRE(simplify_expr(compare_symbol_dynamic, ns) == false_exprt{});
}

TEST_CASE("Simplify pointer_object equality", "[core][util]")
{
// this test requires a proper architecture to be set up so that pointers have
// a width
const cmdlinet cmdline;
config.set(cmdline);

symbol_tablet symbol_table;
namespacet ns(symbol_table);

exprt p_o_void =
pointer_object(null_pointer_exprt{pointer_type(empty_typet{})});
exprt p_o_int =
pointer_object(null_pointer_exprt{pointer_type(signedbv_typet(32))});

exprt simp = simplify_expr(equal_exprt{p_o_void, p_o_int}, ns);

REQUIRE(simp.is_true());
}