Skip to content

Byte-operator lowering: Add support for byte-extracting unions #4717

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
Nov 9, 2022
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
19 changes: 19 additions & 0 deletions regression/cbmc/union17/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
int main()
{
// create a union type of non-constant, non-zero size
unsigned x;
__CPROVER_assume(x > 0);
union U
{
unsigned A[x];
};
// create an integer of arbitrary value
int i, i_before;
i_before = i;
// initialize a union of non-zero size from the integer
unsigned u = ((union U *)&i)->A[0];
// reading back an integer out of the union should yield the same value for
// the integer as it had before
i = u;
__CPROVER_assert(i == i_before, "going through union works");
}
13 changes: 13 additions & 0 deletions regression/cbmc/union17/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CORE broken-smt-backend
main.c
--no-simplify
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
--
This test passes when simplification is enabled (which gets rid of
byte-extracting a union of non-constant size), but yielded a wrong verification
outcome with both the SAT back-end before. The SMT back-end fails for it would
like to flatten an array of non-constant size.
12 changes: 12 additions & 0 deletions src/solvers/lowering/byte_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,18 @@ static exprt unpack_rec(
ns,
true);
}
else if(!union_type.components().empty())
{
member_exprt member{src, union_type.components().front()};
return unpack_rec(
member,
little_endian,
offset_bytes,
max_bytes,
bits_per_byte,
ns,
true);
}
}
else if(src.type().id() == ID_pointer)
{
Expand Down