Skip to content

Commit ddbfd00

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 8b02ecd commit ddbfd00

File tree

24 files changed

+326
-67
lines changed

24 files changed

+326
-67
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
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE broken-smt-backend
2+
main.c
3+
--big-endian --no-simplify
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Although this test can now be fully solved via constant propagation and
11+
simplification, it should also succeed without simplification.

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.

regression/cbmc/xml-trace/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ VERIFICATION FAILED
77
<assignment assignment_type="state" base_name="u" display_name="test::u" hidden="false" identifier="test::u" mode="C" step_nr="\d+" thread="0">
88
<location file=".*" function="test" line="\d+" working-directory=".*"/>
99
<type>union myunion</type>
10-
<full_lhs>byte_extract_little_endian\(u, 0ll?, .*int.*\)</full_lhs>
10+
<full_lhs>u</full_lhs>
1111
<full_lhs_value binary="[01]+">\d+ll?</full_lhs_value>
1212
<value>\{ \.i=\d+ll? \}</value>
1313
<value_expression>

scripts/expected_doxygen_warnings.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@
7979
/home/runner/work/cbmc/cbmc/src/analyses/variable-sensitivity/variable_sensitivity_object_factory.cpp:188: warning: The following parameter of initialize_abstract_object(const typet type, bool top, bool bottom, const exprt &e, const abstract_environmentt &environment, const namespacet &ns, const vsd_configt &configuration) is not documented:
8080
parameter 'configuration'
8181

82-
warning: Include graph for 'cbmc_parse_options.cpp' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
82+
warning: Include graph for 'cbmc_parse_options.cpp' not generated, too many nodes (60), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8383
warning: Include graph for 'goto_instrument_parse_options.cpp' not generated, too many nodes (97), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8484
warning: Included by graph for 'goto_functions.h' not generated, too many nodes (66), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8585
warning: Included by graph for 'goto_model.h' not generated, too many nodes (109), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8686
warning: Included by graph for 'arith_tools.h' not generated, too many nodes (181), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
87-
warning: Included by graph for 'c_types.h' not generated, too many nodes (110), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
88-
warning: Included by graph for 'config.h' not generated, too many nodes (85), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
87+
warning: Included by graph for 'c_types.h' not generated, too many nodes (111), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
88+
warning: Included by graph for 'config.h' not generated, too many nodes (86), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8989
warning: Included by graph for 'exception_utils.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9090
warning: Included by graph for 'expr.h' not generated, too many nodes (88), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9191
warning: Included by graph for 'expr_util.h' not generated, too many nodes (60), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.

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>
@@ -1099,6 +1100,36 @@ void c_typecheck_baset::typecheck_expr_typecast(exprt &expr)
10991100
expr.set(ID_C_lvalue, true);
11001101
return;
11011102
}
1103+
else if(c.get_anonymous() && c.type().id() == ID_struct_tag)
1104+
{
1105+
// we also look into anonymous members as we might have wrapped a
1106+
// non-maximal member in such a struct
1107+
const struct_typet &struct_type =
1108+
follow_tag(to_struct_tag_type(c.type()));
1109+
if(
1110+
!struct_type.components().empty() &&
1111+
struct_type.components().front().type() == op.type())
1112+
{
1113+
auto nondet =
1114+
nondet_initializer(c.type(), expr.source_location(), *this);
1115+
if(!nondet.has_value())
1116+
{
1117+
error().source_location = expr.source_location();
1118+
error() << "cannot nondet-initialize union component of type '"
1119+
<< to_string(c.type()) << "'" << eom;
1120+
throw 0;
1121+
}
1122+
CHECK_RETURN(nondet->id() == ID_struct);
1123+
CHECK_RETURN(!nondet->operands().empty());
1124+
1125+
nondet->operands().front() = op;
1126+
union_exprt union_expr(c.get_name(), *nondet, expr.type());
1127+
union_expr.add_source_location() = expr.source_location();
1128+
expr = union_expr;
1129+
expr.set(ID_C_lvalue, true);
1130+
return;
1131+
}
1132+
}
11021133
}
11031134

11041135
// not found, complain

src/ansi-c/c_typecheck_initializer.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -530,27 +530,36 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
530530
// initializer; other bytes have an unspecified value (C Standard
531531
// 6.2.6.1(7)). In practice, objects of static lifetime are fully zero
532532
// initialized.
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-
543533
if(current_symbol.is_static_lifetime)
544534
{
545-
byte_update_exprt byte_update{
546-
byte_update_id(), *dest, from_integer(0, index_type()), *zero};
547-
byte_update.add_source_location() = value.source_location();
548-
*dest = std::move(byte_update);
549-
dest = &(to_byte_update_expr(*dest).op2());
535+
const auto zero =
536+
zero_initializer(component.type(), value.source_location(), *this);
537+
if(!zero.has_value())
538+
{
539+
error().source_location = value.source_location();
540+
error() << "cannot zero-initialize union component of type '"
541+
<< to_string(component.type()) << "'" << eom;
542+
throw 0;
543+
}
544+
545+
union_exprt union_expr(component.get_name(), *zero, type);
546+
union_expr.add_source_location() = value.source_location();
547+
*dest = std::move(union_expr);
548+
dest = &(to_union_expr(*dest).op());
550549
}
551550
else
552551
{
553-
union_exprt union_expr(component.get_name(), *zero, type);
552+
const auto nondet = nondet_initializer(
553+
component.type(), value.source_location(), *this);
554+
if(!nondet.has_value())
555+
{
556+
error().source_location = value.source_location();
557+
error() << "cannot nondet-initialize union component of type '"
558+
<< to_string(component.type()) << "'" << eom;
559+
throw 0;
560+
}
561+
562+
union_exprt union_expr(component.get_name(), *nondet, type);
554563
union_expr.add_source_location() = value.source_location();
555564
*dest = std::move(union_expr);
556565
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>

0 commit comments

Comments
 (0)