-
Notifications
You must be signed in to change notification settings - Fork 275
Check for overflow on left shift of signed ints #1528
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
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
int main() | ||
{ | ||
unsigned char x; | ||
unsigned r=x << ((sizeof(unsigned)-1)*8); | ||
r=x << ((sizeof(unsigned)-1)*8-1); | ||
r=(unsigned)x << ((sizeof(unsigned)-1)*8); | ||
|
||
int s=-1 << ((sizeof(int)-1)*8); | ||
s=-256 << ((sizeof(int)-1)*8); | ||
return 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,9 @@ | ||
CORE | ||
main.c | ||
--signed-overflow-check | ||
^SIGNAL=0$ | ||
^\[.*\] arithmetic overflow on signed shl in .*: FAILURE$ | ||
^\*\* 2 of 4 failed | ||
^VERIFICATION FAILED$ | ||
-- | ||
^warning: ignoring |
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 @@ | ||
int main() | ||
{ | ||
unsigned char x; | ||
unsigned r=x << ((sizeof(unsigned)-1)*8); | ||
r=x << ((sizeof(unsigned)-1)*8-1); | ||
r=(unsigned)x << ((sizeof(unsigned)-1)*8); | ||
|
||
int s=-1 << ((sizeof(int)-1)*8); | ||
s=-256 << ((sizeof(int)-1)*8); | ||
return 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,9 @@ | ||
CORE | ||
main.c | ||
--undefined-shift-check | ||
^SIGNAL=0$ | ||
^\[.*\] shift operand is negative in .*: FAILURE$ | ||
^\*\* 1 of 2 failed | ||
^VERIFICATION FAILED$ | ||
-- | ||
^warning: ignoring |
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 |
---|---|---|
|
@@ -8,8 +8,7 @@ Author: Daniel Kroening, [email protected] | |
|
||
#include "boolbv.h" | ||
|
||
#include <cassert> | ||
|
||
#include <util/invariant.h> | ||
#include <util/prefix.h> | ||
#include <util/string2int.h> | ||
|
||
|
@@ -59,7 +58,7 @@ literalt boolbvt::convert_overflow(const exprt &expr) | |
if(operands[0].type()!=operands[1].type()) | ||
throw "operand type mismatch on overflow-*"; | ||
|
||
assert(bv0.size()==bv1.size()); | ||
DATA_INVARIANT(bv0.size()==bv1.size(), "operands size mismatch"); | ||
std::size_t old_size=bv0.size(); | ||
std::size_t new_size=old_size*2; | ||
|
||
|
@@ -86,7 +85,7 @@ literalt boolbvt::convert_overflow(const exprt &expr) | |
bv_overflow.reserve(old_size); | ||
|
||
// get top result bits, plus one more | ||
assert(old_size!=0); | ||
DATA_INVARIANT(old_size!=0, "zero-size operand"); | ||
for(std::size_t i=old_size-1; i<result.size(); i++) | ||
bv_overflow.push_back(result[i]); | ||
|
||
|
@@ -96,6 +95,72 @@ literalt boolbvt::convert_overflow(const exprt &expr) | |
return !prop.lor(all_one, all_zero); | ||
} | ||
} | ||
else if(expr.id() == ID_overflow_shl) | ||
{ | ||
if(operands.size() != 2) | ||
throw "operator " + expr.id_string() + " takes two operands"; | ||
|
||
const bvt &bv0=convert_bv(operands[0]); | ||
const bvt &bv1=convert_bv(operands[1]); | ||
|
||
std::size_t old_size = bv0.size(); | ||
std::size_t new_size = old_size * 2; | ||
|
||
bv_utilst::representationt rep= | ||
operands[0].type().id()==ID_signedbv?bv_utilst::representationt::SIGNED: | ||
bv_utilst::representationt::UNSIGNED; | ||
|
||
bvt bv_ext=bv_utils.extension(bv0, new_size, rep); | ||
|
||
bvt result=bv_utils.shift(bv_ext, bv_utilst::shiftt::LEFT, bv1); | ||
|
||
// a negative shift is undefined; yet this isn't an overflow | ||
literalt neg_shift = | ||
operands[1].type().id() == ID_unsignedbv ? | ||
const_literal(false) : | ||
bv1.back(); // sign bit | ||
|
||
// an undefined shift of a non-zero value always results in overflow; the | ||
// use of unsigned comparision is safe here as we cover the signed negative | ||
// case via neg_shift | ||
literalt undef = | ||
bv_utils.rel( | ||
bv1, | ||
ID_gt, | ||
bv_utils.build_constant(old_size, bv1.size()), | ||
bv_utilst::representationt::UNSIGNED); | ||
|
||
literalt overflow; | ||
|
||
if(rep == bv_utilst::representationt::UNSIGNED) | ||
{ | ||
// get top result bits | ||
result.erase(result.begin(), result.begin() + old_size); | ||
|
||
// one of the top bits is non-zero | ||
overflow=prop.lor(result); | ||
} | ||
else | ||
{ | ||
// get top result bits plus sign bit | ||
DATA_INVARIANT(old_size != 0, "zero-size operand"); | ||
result.erase(result.begin(), result.begin() + old_size - 1); | ||
|
||
// the sign bit has changed | ||
literalt sign_change=!prop.lequal(bv0.back(), result.front()); | ||
|
||
// these need to be either all 1's or all 0's | ||
literalt all_one=prop.land(result); | ||
literalt all_zero=!prop.lor(result); | ||
|
||
overflow=prop.lor(sign_change, !prop.lor(all_one, all_zero)); | ||
} | ||
|
||
// a negative shift isn't an overflow; else check the conditions built | ||
// above | ||
return | ||
prop.land(!neg_shift, prop.lselect(undef, prop.lor(bv0), overflow)); | ||
} | ||
else if(expr.id()==ID_overflow_unary_minus) | ||
{ | ||
if(operands.size()!=1) | ||
|
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.
I believe that the right-hand-side operand may be either signed or unsigned. I think the above will only work if the right-hand side has enough bits (i.e., sign extension may be needed).
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.
I was thinking that negative shifts should not be caught here, but indeed I need to make sure that this case is truly ignored.
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.
I've added this check in the updated revision.