Skip to content

better precision of value_set on unions #2851

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 1 commit into from
Sep 23, 2018
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
35 changes: 35 additions & 0 deletions regression/cbmc/union11/union_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <assert.h>

struct list_item
{
// there could be more members here, this single one suffices to demonstrate
// the problem/fix
struct list_item *previous;
};

struct List
{
// an element such that the offset of "index" is non-zero
unsigned something;
struct list_item *index;
struct list_item end;
};

union U
{
// there could be more members here, this single one suffices to demonstrate
// the problem/fix
struct List my_list;
};

int main()
{
union U u;

u.my_list.index = &(u.my_list.end);
u.my_list.end.previous = u.my_list.index;
struct list_item *p1 = u.my_list.index;
struct list_item *p2 = p1->previous;
struct list_item *p3 = p2->previous;
assert(p3 != 0); // should pass
}
8 changes: 8 additions & 0 deletions regression/cbmc/union11/union_list.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
union_list.c

^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
2 changes: 1 addition & 1 deletion src/pointer-analysis/value_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ void value_sett::assign(

rhs_member=make_member(rhs, name, ns);

assign(lhs_member, rhs_member, ns, is_simplified, add_to_sets);
assign(lhs_member, rhs_member, ns, false, add_to_sets);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/util/simplify_expr_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ bool simplify_exprt::simplify_member(exprt &expr)

return false;
}
else if(op_type.id() == ID_union)
{
// rewrite byte_extract(X, 0).member to X
// if the type of X is that of the member
const auto &byte_extract_expr = to_byte_extract_expr(op);
if(byte_extract_expr.offset().is_zero())
{
const union_typet &union_type = to_union_type(op_type);
const typet &subtype = union_type.component_type(component_name);

if(subtype == byte_extract_expr.op().type())
{
exprt tmp = byte_extract_expr.op();
expr.swap(tmp);

return false;
}
}
}
}
else if(op.id()==ID_union && op_type.id()==ID_union)
{
Expand Down