Skip to content

Commit d049450

Browse files
committed
Rewrite unions such that all members are of the same size
In the C front-end, rewrite union components with types smaller than the union's size to anonymous structs. Each such struct contains the original union component plus padding. Assignments to union members thus always assign all bytes that make up the object representation of a union. The use of an anonymous struct ensures that member accesses can still be resolved. As this is a change in the semantics of goto programs, the goto binary version is incremented. rewrite_union is no longer necessary, but bugs (hidden by rewrite_union) in handling endianness in simplify_expr_member and convert_member surfaced and had to be fixed.
1 parent 2c0d833 commit d049450

File tree

21 files changed

+320
-58
lines changed

21 files changed

+320
-58
lines changed

jbmc/src/jdiff/jdiff_parse_options.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ Author: Peter Schrammel
4040
#include <goto-programs/remove_unused_functions.h>
4141
#include <goto-programs/remove_vector.h>
4242
#include <goto-programs/remove_virtual_functions.h>
43-
#include <goto-programs/rewrite_union.h>
4443
#include <goto-programs/set_properties.h>
4544
#include <goto-programs/show_properties.h>
4645
#include <goto-programs/string_abstraction.h>
@@ -296,7 +295,6 @@ bool jdiff_parse_optionst::process_goto_program(
296295
remove_returns(goto_model);
297296
remove_vector(goto_model);
298297
remove_complex(goto_model);
299-
rewrite_union(goto_model);
300298

301299
// add generic checks
302300
log.status() << "Generic Property Instrumentation" << messaget::eom;
693 Bytes
Binary file not shown.

regression/ansi-c/arch_flags_mcpu_bad/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This tests the -mcpu=cortex=a15 flag that should activate ARM-32 mode.
1111
The object file 'object.intel' was compiled from 'source.c' with goto-cc
1212
on a 64-bit platform:
1313

14-
goto-cc -c source.c
14+
goto-cc -c source.c -o object.intel
1515

1616
preproc.i is already pre-processed so that it can be linked in without
1717
needing to invoke a pre-processor from a cross-compile toolchain on your
687 Bytes
Binary file not shown.
Binary file not shown.

regression/ansi-c/arch_flags_mthumb_bad/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This tests the -mthumb flag that should activate ARM-32 mode. The object
1111
file 'object.intel' was compiled from 'source.c' with goto-cc on a
1212
64-bit platform:
1313

14-
goto-cc -c source.c
14+
goto-cc -c source.c -o object.intel
1515

1616
preproc.i is already pre-processed so that it can be linked in without
1717
needing to invoke a pre-processor from a cross-compile toolchain on your
686 Bytes
Binary file not shown.

regression/cbmc-library/float-nan-check/test.desc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
CORE
22
main.c
33
--nan-check
4-
\[main.NaN.1\] line \d+ NaN on \+ in byte_extract_little_endian\(c, (0|0l|0ll), float\) \+ myzero: SUCCESS
4+
\[main.NaN.1\] line \d+ NaN on \+ in c\.f \+ myzero: SUCCESS
55
\[main.NaN.2\] line \d+ NaN on / in myzero / myzero: FAILURE
66
\[main.NaN.3\] line \d+ NaN on / in \(float\)n / myinf: FAILURE
77
\[main.NaN.4\] line \d+ NaN on \* in myinf \* myzero: FAILURE
88
\[main.NaN.5\] line \d+ NaN on \+ in -myinf \+ myinf: FAILURE
99
\[main.NaN.6\] line \d+ NaN on - in myinf - myinf: FAILURE
1010
\[main.NaN.7\] line \d+ NaN on - in -myinf - -myinf: FAILURE
11-
\[main.NaN.8\] line \d+ NaN on \+ in byte_extract_little_endian\(c, (0|0l|0ll), float\) \+ byte_extract_little_endian\(c, (0|0l|0ll), float\): SUCCESS
12-
\[main.NaN.9\] line \d+ NaN on / in byte_extract_little_endian\(c, (0|0l|0ll), float\) / myzero: SUCCESS
11+
\[main.NaN.8\] line \d+ NaN on \+ in c\.f \+ c\.f: SUCCESS
12+
\[main.NaN.9\] line \d+ NaN on / in c\.f / myzero: SUCCESS
1313
\[main.NaN.10\] line \d+ NaN on \* in mynan \* mynan: SUCCESS
1414
\[main.NaN.11\] line \d+ NaN on \+ in mynan \+ mynan: SUCCESS
1515
\[main.NaN.12\] line \d+ NaN on - in mynan - mynan: SUCCESS

regression/cbmc/union7/test.desc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
CORE broken-smt-backend
1+
CORE
22
main.c
33
--big-endian
4+
Generated 3 VCC\(s\), 0 remaining after simplification
45
^EXIT=0$
56
^SIGNAL=0$
67
^VERIFICATION SUCCESSFUL$
78
--
89
^warning: ignoring
910
--
10-
This test reports a VERIFICATION ERROR when running with the SMT2 solver on
11-
Windows only.
11+
This test can now be fully solved via constant propagation and simplification.

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/c_types.h>
1818
#include <util/config.h>
1919
#include <util/cprover_prefix.h>
20+
#include <util/expr_initializer.h>
2021
#include <util/expr_util.h>
2122
#include <util/ieee_float.h>
2223
#include <util/mathematical_expr.h>
@@ -1086,6 +1087,36 @@ void c_typecheck_baset::typecheck_expr_typecast(exprt &expr)
10861087
expr.set(ID_C_lvalue, true);
10871088
return;
10881089
}
1090+
else if(c.get_anonymous() && c.type().id() == ID_struct_tag)
1091+
{
1092+
// we also look into anonymous members as those we might have wrapped a
1093+
// non-maximal member in such a struct
1094+
const struct_typet &struct_type =
1095+
follow_tag(to_struct_tag_type(c.type()));
1096+
if(
1097+
!struct_type.components().empty() &&
1098+
struct_type.components().front().type() == op.type())
1099+
{
1100+
auto nondet =
1101+
nondet_initializer(c.type(), expr.source_location(), *this);
1102+
if(!nondet.has_value())
1103+
{
1104+
error().source_location = expr.source_location();
1105+
error() << "cannot nondet-initialize union component of type '"
1106+
<< to_string(c.type()) << "'" << eom;
1107+
throw 0;
1108+
}
1109+
CHECK_RETURN(nondet->id() == ID_struct);
1110+
CHECK_RETURN(!nondet->operands().empty());
1111+
1112+
nondet->operands().front() = op;
1113+
union_exprt union_expr(c.get_name(), *nondet, expr.type());
1114+
union_expr.add_source_location() = expr.source_location();
1115+
expr = union_expr;
1116+
expr.set(ID_C_lvalue, true);
1117+
return;
1118+
}
1119+
}
10891120
}
10901121

10911122
// not found, complain

src/ansi-c/c_typecheck_initializer.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -526,19 +526,42 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
526526
// The first component is not the maximum member, which the (default)
527527
// zero initializer prepared. Replace this by a component-specific
528528
// initializer; other bytes have an unspecified value (C Standard
529-
// 6.2.6.1(7)).
530-
const auto zero =
531-
zero_initializer(component.type(), value.source_location(), *this);
532-
if(!zero.has_value())
529+
// 6.2.6.1(7)). In practice, objects of static lifetime are fully zero
530+
// initialized.
531+
if(current_symbol.is_static_lifetime)
533532
{
534-
error().source_location = value.source_location();
535-
error() << "cannot zero-initialize union component of type '"
536-
<< to_string(component.type()) << "'" << eom;
537-
throw 0;
533+
const auto zero =
534+
zero_initializer(component.type(), value.source_location(), *this);
535+
if(!zero.has_value())
536+
{
537+
error().source_location = value.source_location();
538+
error() << "cannot zero-initialize union component of type '"
539+
<< to_string(component.type()) << "'" << eom;
540+
throw 0;
541+
}
542+
543+
union_exprt union_expr(component.get_name(), *zero, type);
544+
union_expr.add_source_location() = value.source_location();
545+
*dest = std::move(union_expr);
546+
dest = &(to_union_expr(*dest).op());
547+
}
548+
else
549+
{
550+
const auto nondet = nondet_initializer(
551+
component.type(), value.source_location(), *this);
552+
if(!nondet.has_value())
553+
{
554+
error().source_location = value.source_location();
555+
error() << "cannot nondet-initialize union component of type '"
556+
<< to_string(component.type()) << "'" << eom;
557+
throw 0;
558+
}
559+
560+
union_exprt union_expr(component.get_name(), *nondet, type);
561+
union_expr.add_source_location() = value.source_location();
562+
*dest = std::move(union_expr);
563+
dest = &(to_union_expr(*dest).op());
538564
}
539-
union_exprt union_expr(component.get_name(), *zero, type);
540-
union_expr.add_source_location()=value.source_location();
541-
*dest=union_expr;
542565
}
543566

544567
dest = &(to_union_expr(*dest).op());

src/ansi-c/c_typecheck_type.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,134 @@ void c_typecheck_baset::typecheck_compound_body(
10721072
else
10731073
it++;
10741074
}
1075+
1076+
if(type.id() == ID_union && !components.empty())
1077+
{
1078+
const auto widest_member =
1079+
to_union_type(type).find_widest_union_component(*this);
1080+
optionalt<exprt> size_expr_opt;
1081+
1082+
if(!widest_member.has_value())
1083+
{
1084+
// GCC extension: arrays of non-constant size
1085+
size_expr_opt = size_of_expr(type, *this);
1086+
1087+
if(!size_expr_opt.has_value())
1088+
{
1089+
error().source_location = type.source_location();
1090+
error() << "cannot determine size of union" << eom;
1091+
throw 0;
1092+
}
1093+
}
1094+
1095+
for(auto &component : components)
1096+
{
1097+
std::vector<typet> padding_types;
1098+
1099+
const auto &component_bits = pointer_offset_bits(component.type(), *this);
1100+
if(
1101+
component_bits.has_value() &&
1102+
(!widest_member.has_value() ||
1103+
*component_bits < widest_member->second) &&
1104+
*component_bits % 8 != 0)
1105+
{
1106+
std::size_t bit_field_padding_width =
1107+
8 - numeric_cast_v<std::size_t>(*component_bits % 8);
1108+
padding_types.push_back(c_bit_field_typet{
1109+
unsignedbv_typet{bit_field_padding_width}, bit_field_padding_width});
1110+
}
1111+
1112+
if(size_expr_opt.has_value())
1113+
{
1114+
const auto component_size_expr_opt =
1115+
size_of_expr(component.type(), *this);
1116+
CHECK_RETURN(component_size_expr_opt.has_value());
1117+
// size_of_expr will round up to full bytes, and also works for bit
1118+
// fields (which C sizeof does not support). Thus, we don't need to
1119+
// special-case bit_field_padding_opt being set.
1120+
if_exprt padding_bytes{
1121+
binary_relation_exprt{
1122+
*component_size_expr_opt, ID_lt, *size_expr_opt},
1123+
minus_exprt{*size_expr_opt, *component_size_expr_opt},
1124+
from_integer(0, size_expr_opt->type())};
1125+
simplify(padding_bytes, *this);
1126+
1127+
if(!padding_bytes.is_zero())
1128+
{
1129+
make_index_type(padding_bytes);
1130+
1131+
padding_types.push_back(
1132+
array_typet{unsigned_char_type(), padding_bytes});
1133+
}
1134+
}
1135+
else
1136+
{
1137+
CHECK_RETURN(component_bits.has_value());
1138+
// Truncating division ensures that we don't need to special-case
1139+
// bit_field_padding_opt being set.
1140+
std::size_t padding_bytes =
1141+
numeric_cast_v<std::size_t>(widest_member->second - *component_bits) /
1142+
8;
1143+
1144+
if(padding_bytes != 0)
1145+
{
1146+
padding_types.push_back(array_typet{
1147+
unsigned_char_type(), from_integer(padding_bytes, index_type())});
1148+
}
1149+
}
1150+
1151+
if(padding_types.empty())
1152+
continue;
1153+
1154+
struct_typet::componentst component_with_padding_type;
1155+
component_with_padding_type.push_back(component);
1156+
for(const auto &t : padding_types)
1157+
{
1158+
// TODO: perhaps use the same naming as padding.cpp uses
1159+
component_with_padding_type.push_back(struct_union_typet::componentt{
1160+
"$anon" + std::to_string(anon_member_counter++), t});
1161+
// TODO: not sure whether this should be anonymous
1162+
component_with_padding_type.back().set_anonymous(true);
1163+
component_with_padding_type.back().set_is_padding(true);
1164+
component_with_padding_type.back().add_source_location() =
1165+
component.source_location();
1166+
}
1167+
1168+
struct_union_typet::componentt component_with_padding{
1169+
"$anon" + std::to_string(anon_member_counter++),
1170+
struct_typet{component_with_padding_type}};
1171+
component_with_padding.set_anonymous(true);
1172+
component_with_padding.add_source_location() =
1173+
component.source_location();
1174+
1175+
// based on typecheck_compound_type, branch
1176+
// "if(type.find(ID_tag).is_nil())"
1177+
// produce type symbol
1178+
type_symbolt compound_symbol{component_with_padding.type()};
1179+
compound_symbol.location = component_with_padding.source_location();
1180+
1181+
std::string typestr = type2name(compound_symbol.type, *this);
1182+
compound_symbol.base_name = "#anon#" + typestr;
1183+
compound_symbol.name = "tag-#anon#" + typestr;
1184+
1185+
// We might already have the same anonymous struct, and this is simply
1186+
// ok as those are exactly the same types, just introduced at a
1187+
// different source location.
1188+
symbolt *new_symbol = &compound_symbol;
1189+
if(
1190+
symbol_table.symbols.find(compound_symbol.name) ==
1191+
symbol_table.symbols.end())
1192+
{
1193+
move_symbol(compound_symbol, new_symbol);
1194+
}
1195+
1196+
struct_tag_typet tag_type{new_symbol->name};
1197+
tag_type.add_source_location() = component_with_padding.source_location();
1198+
component_with_padding.type().swap(tag_type);
1199+
1200+
component.swap(component_with_padding);
1201+
}
1202+
}
10751203
}
10761204

10771205
typet c_typecheck_baset::enum_constant_type(

src/cbmc/cbmc_parse_options.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ Author: Daniel Kroening, [email protected]
6262
#include <goto-programs/remove_skip.h>
6363
#include <goto-programs/remove_unused_functions.h>
6464
#include <goto-programs/remove_vector.h>
65-
#include <goto-programs/rewrite_union.h>
6665
#include <goto-programs/set_properties.h>
6766
#include <goto-programs/show_goto_functions.h>
6867
#include <goto-programs/show_properties.h>
@@ -943,7 +942,6 @@ bool cbmc_parse_optionst::process_goto_program(
943942
remove_returns(goto_model);
944943
remove_vector(goto_model);
945944
remove_complex(goto_model);
946-
rewrite_union(goto_model);
947945

948946
// add generic checks
949947
log.status() << "Generic Property Instrumentation" << messaget::eom;

src/goto-diff/goto_diff_parse_options.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ Author: Peter Schrammel
4040
#include <goto-programs/remove_unused_functions.h>
4141
#include <goto-programs/remove_vector.h>
4242
#include <goto-programs/remove_virtual_functions.h>
43-
#include <goto-programs/rewrite_union.h>
4443
#include <goto-programs/set_properties.h>
4544
#include <goto-programs/show_properties.h>
4645
#include <goto-programs/string_abstraction.h>
@@ -341,7 +340,6 @@ bool goto_diff_parse_optionst::process_goto_program(
341340
remove_returns(goto_model);
342341
remove_vector(goto_model);
343342
remove_complex(goto_model);
344-
rewrite_union(goto_model);
345343

346344
// add generic checks
347345
log.status() << "Generic Property Instrumentation" << messaget::eom;

src/goto-programs/write_goto_binary.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Author: CM Wintersteiger
1212
#ifndef CPROVER_GOTO_PROGRAMS_WRITE_GOTO_BINARY_H
1313
#define CPROVER_GOTO_PROGRAMS_WRITE_GOTO_BINARY_H
1414

15-
#define GOTO_BINARY_VERSION 5
15+
#define GOTO_BINARY_VERSION 6
1616

1717
#include <iosfwd>
1818
#include <string>

src/goto-symex/symex_assign.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ void symex_assignt::assign_rec(
5454
}
5555
else if(type.id() == ID_union || type.id() == ID_union_tag)
5656
{
57-
// should have been replaced by byte_extract
58-
throw unsupported_operation_exceptiont(
59-
"assign_rec: unexpected assignment to union member");
57+
assign_union_member(to_member_expr(lhs), full_lhs, rhs, guard);
6058
}
6159
else
6260
throw unsupported_operation_exceptiont(
@@ -343,6 +341,30 @@ void symex_assignt::assign_struct_member(
343341
}
344342
}
345343

344+
void symex_assignt::assign_union_member(
345+
const member_exprt &lhs,
346+
const expr_skeletont &full_lhs,
347+
const exprt &rhs,
348+
exprt::operandst &guard)
349+
{
350+
// Symbolic execution of a union member assignment.
351+
352+
// lhs must be member operand, which takes one operand, which must be a union.
353+
exprt lhs_union = lhs.op();
354+
const irep_idt &component_name = lhs.get_component_name();
355+
356+
// turn
357+
// a.c=e
358+
// into
359+
// a'== { .c=e }
360+
// as the front-end guarantees that .c will have the full size of the union
361+
362+
union_exprt new_rhs{component_name, rhs, lhs_union.type()};
363+
const expr_skeletont new_skeleton =
364+
full_lhs.compose(expr_skeletont::remove_op0(lhs));
365+
assign_rec(lhs_union, new_skeleton, new_rhs, guard);
366+
}
367+
346368
void symex_assignt::assign_if(
347369
const if_exprt &lhs,
348370
const expr_skeletont &full_lhs,

src/goto-symex/symex_assign.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ class symex_assignt
9292
const exprt &rhs,
9393
exprt::operandst &guard);
9494

95+
void assign_union_member(
96+
const member_exprt &lhs,
97+
const expr_skeletont &full_lhs,
98+
const exprt &rhs,
99+
exprt::operandst &guard);
100+
95101
void assign_if(
96102
const if_exprt &lhs,
97103
const expr_skeletont &full_lhs,

0 commit comments

Comments
 (0)