Skip to content

Use extractbits to extract non-byte-fields from unions #7861

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

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions regression/cbmc/Bitfields5/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
union
{
unsigned five : 5;
char *a;
unsigned one : 1;
} b = {8};
int main()
{
__CPROVER_assert(0, "");
}
8 changes: 8 additions & 0 deletions regression/cbmc/Bitfields5/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c

^EXIT=10$
^SIGNAL=0$
^VERIFICATION FAILED$
--
^warning: ignoring
18 changes: 16 additions & 2 deletions src/goto-programs/rewrite_union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ Author: Daniel Kroening, [email protected]
#include "rewrite_union.h"

#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/byte_operators.h>
#include <util/c_types.h>
#include <util/config.h>
#include <util/pointer_expr.h>
#include <util/pointer_offset_size.h>
#include <util/std_code.h>
Expand Down Expand Up @@ -85,8 +87,20 @@ void rewrite_union(exprt &expr)

if(op.type().id() == ID_union_tag || op.type().id() == ID_union)
{
exprt offset = from_integer(0, c_index_type());
expr = make_byte_extract(op, offset, expr.type());
if(
expr.type().id() != ID_c_bit_field ||
to_c_bit_field_type(expr.type()).get_width() %
config.ansi_c.char_width ==
0)
{
exprt offset = from_integer(0, c_index_type());
expr = make_byte_extract(op, offset, expr.type());
}
else
{
const auto upper = to_c_bit_field_type(expr.type()).get_width() - 1;
expr = extractbits_exprt(op, upper, 0, expr.type());
}
}
}
else if(expr.id()==ID_union)
Expand Down
28 changes: 20 additions & 8 deletions src/goto-symex/field_sensitivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ Author: Michael Tautschnig
#include "field_sensitivity.h"

#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/byte_operators.h>
#include <util/c_types.h>
#include <util/config.h>
#include <util/pointer_offset_size.h>
#include <util/simplify_expr.h>

Expand Down Expand Up @@ -419,14 +421,24 @@ void field_sensitivityt::field_assignments_rec(
exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
for(const auto &comp : components)
{
const exprt member_rhs = apply(
ns,
state,
simplify_opt(
make_byte_extract(
ssa_rhs, from_integer(0, c_index_type()), comp.type()),
ns),
false);
exprt extract;
if(
comp.type().id() != ID_c_bit_field ||
to_c_bit_field_type(comp.type()).get_width() %
config.ansi_c.char_width ==
0)
{
extract = make_byte_extract(
ssa_rhs, from_integer(0, c_index_type()), comp.type());
}
else
{
const auto upper = to_c_bit_field_type(comp.type()).get_width() - 1;
extract = extractbits_exprt(ssa_rhs, upper, 0, comp.type());
}

const exprt member_rhs =
apply(ns, state, simplify_opt(extract, ns), false);

const exprt &member_lhs = *fs_it;
if(
Expand Down
2 changes: 2 additions & 0 deletions src/util/format_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ std::ostream &format_rec(std::ostream &os, const typet &type)
os << u8" \u2192 "; // → -- we don't use ⟶ since that doesn't render well
return os << format(mathematical_function.codomain());
}
else if(id == ID_c_bit_field)
return os << "c_bit_field[" << to_c_bit_field_type(type).get_width() << ']';
else
return os << id;
}