-
Notifications
You must be signed in to change notification settings - Fork 274
[RFC] Rewrite unions such that all members are of the same size #5738
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
Closed
+343
−68
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CORE broken-smt-backend | ||
main.c | ||
--big-endian --no-simplify | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
^warning: ignoring | ||
-- | ||
Although this test can now be fully solved via constant propagation and | ||
simplification, it should also succeed without simplification. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
CORE broken-smt-backend | ||
CORE | ||
main.c | ||
--big-endian | ||
Generated 3 VCC\(s\), 0 remaining after simplification | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
^warning: ignoring | ||
-- | ||
This test reports a VERIFICATION ERROR when running with the SMT2 solver on | ||
Windows only. | ||
This test can now be fully solved via constant propagation and simplification. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected] | |
#include <util/c_types.h> | ||
#include <util/config.h> | ||
#include <util/cprover_prefix.h> | ||
#include <util/expr_initializer.h> | ||
#include <util/expr_util.h> | ||
#include <util/floatbv_expr.h> | ||
#include <util/ieee_float.h> | ||
|
@@ -1105,6 +1106,36 @@ void c_typecheck_baset::typecheck_expr_typecast(exprt &expr) | |
expr.set(ID_C_lvalue, true); | ||
return; | ||
} | ||
else if(c.get_anonymous() && c.type().id() == ID_struct_tag) | ||
{ | ||
// we also look into anonymous members as we might have wrapped a | ||
// non-maximal member in such a struct | ||
const struct_typet &struct_type = | ||
follow_tag(to_struct_tag_type(c.type())); | ||
if( | ||
!struct_type.components().empty() && | ||
struct_type.components().front().type() == op.type()) | ||
{ | ||
auto nondet = | ||
nondet_initializer(c.type(), expr.source_location(), *this); | ||
if(!nondet.has_value()) | ||
{ | ||
error().source_location = expr.source_location(); | ||
error() << "cannot nondet-initialize union component of type '" | ||
<< to_string(c.type()) << "'" << eom; | ||
throw 0; | ||
} | ||
CHECK_RETURN(nondet->id() == ID_struct); | ||
CHECK_RETURN(!nondet->operands().empty()); | ||
|
||
nondet->operands().front() = op; | ||
union_exprt union_expr(c.get_name(), *nondet, expr.type()); | ||
union_expr.add_source_location() = expr.source_location(); | ||
expr = union_expr; | ||
expr.set(ID_C_lvalue, true); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// not found, complain | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,9 @@ Author: Daniel Kroening, [email protected] | |
#include <util/mathematical_types.h> | ||
#include <util/pointer_expr.h> | ||
#include <util/pointer_offset_size.h> | ||
#include <util/prefix.h> | ||
#include <util/simplify_expr.h> | ||
#include <util/string2int.h> | ||
|
||
#include "ansi_c_convert_type.h" | ||
#include "c_qualifiers.h" | ||
|
@@ -1073,6 +1075,148 @@ void c_typecheck_baset::typecheck_compound_body( | |
else | ||
it++; | ||
} | ||
|
||
if(type.id() == ID_union && !components.empty()) | ||
{ | ||
const auto widest_member = | ||
to_union_type(type).find_widest_union_component(*this); | ||
optionalt<exprt> size_expr_opt; | ||
|
||
if(!widest_member.has_value()) | ||
{ | ||
// GCC extension: arrays of non-constant size | ||
size_expr_opt = size_of_expr(type, *this); | ||
|
||
if(!size_expr_opt.has_value()) | ||
{ | ||
error().source_location = type.source_location(); | ||
error() << "cannot determine size of union" << eom; | ||
throw 0; | ||
} | ||
} | ||
|
||
// ensure non-conflicting member names | ||
for(auto &component : components) | ||
{ | ||
if( | ||
id2string(component.get_name()).size() > 5 && | ||
has_prefix(id2string(component.get_name()), "$anon")) | ||
{ | ||
const std::string suffix{id2string(component.get_name()), 5}; | ||
auto int_suffix = string2optional_unsigned(suffix); | ||
if(int_suffix.has_value()) | ||
anon_member_counter = std::max(anon_member_counter, *int_suffix + 1); | ||
} | ||
} | ||
|
||
for(auto &component : components) | ||
{ | ||
std::vector<typet> padding_types; | ||
|
||
const auto &component_bits = pointer_offset_bits(component.type(), *this); | ||
if( | ||
component_bits.has_value() && | ||
(!widest_member.has_value() || | ||
*component_bits < widest_member->second) && | ||
*component_bits % 8 != 0) | ||
{ | ||
std::size_t bit_field_padding_width = | ||
8 - numeric_cast_v<std::size_t>(*component_bits % 8); | ||
padding_types.push_back(c_bit_field_typet{ | ||
unsignedbv_typet{bit_field_padding_width}, bit_field_padding_width}); | ||
} | ||
|
||
if(size_expr_opt.has_value()) | ||
{ | ||
const auto component_size_expr_opt = | ||
size_of_expr(component.type(), *this); | ||
CHECK_RETURN(component_size_expr_opt.has_value()); | ||
// size_of_expr will round up to full bytes, and also works for bit | ||
// fields (which C sizeof does not support). Thus, we don't need to | ||
// special-case bit_field_padding_opt being set. | ||
if_exprt padding_bytes{ | ||
binary_relation_exprt{ | ||
*component_size_expr_opt, ID_lt, *size_expr_opt}, | ||
minus_exprt{*size_expr_opt, *component_size_expr_opt}, | ||
from_integer(0, size_expr_opt->type())}; | ||
simplify(padding_bytes, *this); | ||
|
||
if(!padding_bytes.is_zero()) | ||
{ | ||
make_index_type(padding_bytes); | ||
|
||
padding_types.push_back( | ||
array_typet{unsigned_char_type(), padding_bytes}); | ||
} | ||
} | ||
else | ||
{ | ||
CHECK_RETURN(component_bits.has_value()); | ||
// Truncating division ensures that we don't need to special-case | ||
// bit_field_padding_opt being set. | ||
std::size_t padding_bytes = | ||
numeric_cast_v<std::size_t>(widest_member->second - *component_bits) / | ||
8; | ||
|
||
if(padding_bytes != 0) | ||
{ | ||
padding_types.push_back(array_typet{ | ||
unsigned_char_type(), from_integer(padding_bytes, index_type())}); | ||
} | ||
} | ||
|
||
if(padding_types.empty()) | ||
continue; | ||
|
||
struct_typet::componentst component_with_padding_type; | ||
component_with_padding_type.push_back(component); | ||
for(const auto &t : padding_types) | ||
{ | ||
// TODO: perhaps use the same naming as padding.cpp uses | ||
component_with_padding_type.push_back(struct_union_typet::componentt{ | ||
"$anon" + std::to_string(anon_member_counter++), t}); | ||
// TODO: not sure whether this should be anonymous | ||
component_with_padding_type.back().set_anonymous(true); | ||
component_with_padding_type.back().set_is_padding(true); | ||
component_with_padding_type.back().add_source_location() = | ||
component.source_location(); | ||
} | ||
|
||
struct_union_typet::componentt component_with_padding{ | ||
"$anon" + std::to_string(anon_member_counter++), | ||
struct_typet{component_with_padding_type}}; | ||
component_with_padding.set_anonymous(true); | ||
component_with_padding.add_source_location() = | ||
component.source_location(); | ||
|
||
// based on typecheck_compound_type, branch | ||
// "if(type.find(ID_tag).is_nil())" | ||
// produce type symbol | ||
type_symbolt compound_symbol{component_with_padding.type()}; | ||
compound_symbol.location = component_with_padding.source_location(); | ||
|
||
std::string typestr = type2name(compound_symbol.type, *this); | ||
compound_symbol.base_name = "#anon#" + typestr; | ||
compound_symbol.name = "tag-#anon#" + typestr; | ||
|
||
// We might already have the same anonymous struct, and this is simply | ||
// ok as those are exactly the same types, just introduced at a | ||
// different source location. | ||
symbolt *new_symbol = &compound_symbol; | ||
if( | ||
symbol_table.symbols.find(compound_symbol.name) == | ||
symbol_table.symbols.end()) | ||
{ | ||
move_symbol(compound_symbol, new_symbol); | ||
} | ||
|
||
struct_tag_typet tag_type{new_symbol->name}; | ||
tag_type.add_source_location() = component_with_padding.source_location(); | ||
component_with_padding.type().swap(tag_type); | ||
|
||
component.swap(component_with_padding); | ||
} | ||
} | ||
} | ||
|
||
typet c_typecheck_baset::enum_constant_type( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,6 @@ Author: Daniel Kroening, [email protected] | |
#include <goto-programs/remove_skip.h> | ||
#include <goto-programs/remove_unused_functions.h> | ||
#include <goto-programs/remove_vector.h> | ||
#include <goto-programs/rewrite_union.h> | ||
#include <goto-programs/set_properties.h> | ||
#include <goto-programs/show_goto_functions.h> | ||
#include <goto-programs/show_properties.h> | ||
|
@@ -946,7 +945,6 @@ bool cbmc_parse_optionst::process_goto_program( | |
remove_returns(goto_model); | ||
remove_vector(goto_model); | ||
remove_complex(goto_model); | ||
rewrite_union(goto_model); | ||
|
||
// add generic checks | ||
log.status() << "Generic Property Instrumentation" << messaget::eom; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is part one we could make configurable to cover all behaviours permitted by the C standard.