Skip to content

Commit

Permalink
Support byte-extract lowering over union of non-constant size
Browse files Browse the repository at this point in the history
The test included made apparent that we weren't yet handling unbounded
byte extracts (out of a bounded object) for unions, which just fell back
to unpacking an empty array.
  • Loading branch information
tautschnig committed Nov 8, 2022
1 parent b4a4122 commit 7032a1f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
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

0 comments on commit 7032a1f

Please sign in to comment.