-
Notifications
You must be signed in to change notification settings - Fork 274
Initialise union of static lifetime with zeros #5705
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
42b4740
dump-c regression test of union initializers
tautschnig 7e02b53
Bitvector decoding of pointer-typed constants must use bvrep
tautschnig a835405
C front-end: clean up use of ID_designated_initializer
tautschnig db4f25c
dump-c: rewrite byte_updates of unions to union expressions
tautschnig a8592a7
Initialise union of static lifetime with zeros
tautschnig 9a51a92
Simplify byte updates of constants
tautschnig e8ac4d6
bits2expr: create union expressions when components aren't full width
tautschnig ed75598
expr2bits: bit strings for null pointers must have pointer width
tautschnig ac31213
Simplify initializer to remove byte_update
tautschnig 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
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,4 +1,4 @@ | ||
KNOWNBUG | ||
CORE | ||
main.c | ||
|
||
^EXIT=0$ | ||
|
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 @@ | ||
#include <assert.h> | ||
|
||
union U { | ||
int *p; | ||
unsigned long long p_int; | ||
} u = {.p_int = 42}; | ||
|
||
int main() | ||
{ | ||
assert(u.p_int == 42); | ||
} |
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,13 @@ | ||
CORE | ||
main.c | ||
--dump-c | ||
=(\(signed int \*\))?42 | ||
VERIFICATION SUCCESSFUL | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
^warning: ignoring | ||
irep | ||
-- | ||
This test must pass compiling the output generated using dump-c, which implies | ||
that no irep strings can occur. |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ Author: Daniel Kroening, [email protected] | |
#include "c_typecheck_base.h" | ||
|
||
#include <util/arith_tools.h> | ||
#include <util/byte_operators.h> | ||
#include <util/c_types.h> | ||
#include <util/config.h> | ||
#include <util/cprover_prefix.h> | ||
|
@@ -71,7 +72,10 @@ exprt c_typecheck_baset::do_initializer_rec( | |
} | ||
|
||
if(value.id()==ID_initializer_list) | ||
return do_initializer_list(value, type, force_constant); | ||
{ | ||
return simplify_expr( | ||
do_initializer_list(value, type, force_constant), *this); | ||
} | ||
|
||
if( | ||
value.id() == ID_array && value.get_bool(ID_C_string_constant) && | ||
|
@@ -520,13 +524,15 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer( | |
{ | ||
// Already right union component. We can initialize multiple submembers, | ||
// so do not overwrite this. | ||
dest = &(to_union_expr(*dest).op()); | ||
} | ||
else | ||
{ | ||
// The first component is not the maximum member, which the (default) | ||
// zero initializer prepared. Replace this by a component-specific | ||
// initializer; other bytes have an unspecified value (C Standard | ||
// 6.2.6.1(7)). | ||
// 6.2.6.1(7)). In practice, objects of static lifetime are fully zero | ||
// initialized. | ||
const auto zero = | ||
zero_initializer(component.type(), value.source_location(), *this); | ||
if(!zero.has_value()) | ||
|
@@ -536,12 +542,23 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer( | |
<< to_string(component.type()) << "'" << eom; | ||
throw 0; | ||
} | ||
union_exprt union_expr(component.get_name(), *zero, type); | ||
union_expr.add_source_location()=value.source_location(); | ||
*dest=union_expr; | ||
} | ||
|
||
dest = &(to_union_expr(*dest).op()); | ||
if(current_symbol.is_static_lifetime) | ||
{ | ||
byte_update_exprt byte_update{ | ||
byte_update_id(), *dest, from_integer(0, index_type()), *zero}; | ||
byte_update.add_source_location() = value.source_location(); | ||
*dest = std::move(byte_update); | ||
dest = &(to_byte_update_expr(*dest).op2()); | ||
} | ||
else | ||
{ | ||
union_exprt union_expr(component.get_name(), *zero, type); | ||
union_expr.add_source_location() = value.source_location(); | ||
*dest = std::move(union_expr); | ||
dest = &(to_union_expr(*dest).op()); | ||
} | ||
} | ||
} | ||
else | ||
UNREACHABLE; | ||
|
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected] | |
|
||
#include "dump_c.h" | ||
|
||
#include <util/byte_operators.h> | ||
#include <util/config.h> | ||
#include <util/find_symbols.h> | ||
#include <util/get_base_name.h> | ||
|
@@ -1413,6 +1414,60 @@ void dump_ct::cleanup_expr(exprt &expr) | |
} | ||
#endif | ||
} | ||
else if( | ||
expr.id() == ID_byte_update_little_endian || | ||
expr.id() == ID_byte_update_big_endian) | ||
{ | ||
const byte_update_exprt &bu = to_byte_update_expr(expr); | ||
|
||
if(bu.op().id() == ID_union && bu.offset().is_zero()) | ||
{ | ||
const union_exprt &union_expr = to_union_expr(bu.op()); | ||
const union_typet &union_type = | ||
to_union_type(ns.follow(union_expr.type())); | ||
|
||
for(const auto &comp : union_type.components()) | ||
{ | ||
if(bu.value().type() == comp.type()) | ||
{ | ||
exprt member1{ID_member}; | ||
member1.set(ID_component_name, union_expr.get_component_name()); | ||
exprt designated_initializer1{ID_designated_initializer}; | ||
designated_initializer1.add_to_operands(union_expr.op()); | ||
designated_initializer1.add(ID_designator).move_to_sub(member1); | ||
|
||
exprt member2{ID_member}; | ||
member2.set(ID_component_name, comp.get_name()); | ||
exprt designated_initializer2{ID_designated_initializer}; | ||
designated_initializer2.add_to_operands(bu.value()); | ||
designated_initializer2.add(ID_designator).move_to_sub(member2); | ||
|
||
binary_exprt initializer_list{std::move(designated_initializer1), | ||
ID_initializer_list, | ||
std::move(designated_initializer2)}; | ||
expr.swap(initializer_list); | ||
break; | ||
} | ||
} | ||
} | ||
else if( | ||
bu.op().id() == ID_side_effect && | ||
to_side_effect_expr(bu.op()).get_statement() == ID_nondet && | ||
ns.follow(bu.op().type()).id() == ID_union && bu.offset().is_zero()) | ||
{ | ||
const union_typet &union_type = to_union_type(ns.follow(bu.op().type())); | ||
|
||
for(const auto &comp : union_type.components()) | ||
{ | ||
if(bu.value().type() == comp.type()) | ||
{ | ||
union_exprt union_expr{comp.get_name(), bu.value(), bu.op().type()}; | ||
expr.swap(union_expr); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
void dump_ct::cleanup_type(typet &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
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
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.
Good catch!