Skip to content

Commit

Permalink
Merge pull request #4717 from tautschnig/byte-operator-union
Browse files Browse the repository at this point in the history
Byte-operator lowering: Add support for byte-extracting unions
  • Loading branch information
tautschnig authored Nov 9, 2022
2 parents 5b95d2b + 7032a1f commit 70b7cf7
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 70b7cf7

Please sign in to comment.