-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Make ConstantEvaluator truncate the result of shift operations
#16597
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
Open
matheusaaguiar
wants to merge
5
commits into
develop
Choose a base branch
from
ConstantEvaluator-truncate-shiftL-value
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd96b75
Add tests
matheusaaguiar 4dc1eba
Fix ConstantEvaluator not truncating the result of shifts
matheusaaguiar f50d5a8
Changelog
matheusaaguiar 36f5fa3
Truncate bit not operation result
matheusaaguiar d62fe3f
WIP: smt test
matheusaaguiar 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
36 changes: 36 additions & 0 deletions
36
test/libsolidity/semanticTests/constantEvaluator/bit_not_operation_runtime_equivalence.sol
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,36 @@ | ||
| uint8 constant U253 = 253; // 1111 1101 | ||
| int8 constant I128 = -128; // 1000 0000 | ||
| int8 constant IONE = 1; // 0000 0001 | ||
| contract C { | ||
| uint8 constant UNSIGNED = ~U253; // = 2 (0000 0010) | ||
| uint[UNSIGNED] a; | ||
| int8 constant NEGATIVE_SIGNED = ~I128; // = 127 (0111 1111) | ||
| uint[NEGATIVE_SIGNED] b; | ||
| int8 constant POSITIVE_SIGNED = ~IONE; // = -2 (1111 1110) | ||
| uint[POSITIVE_SIGNED * -1] c; | ||
| function testUnsignedEquivalence() public view returns (bool) { | ||
| uint8 runTimeResult = ~U253; | ||
|
|
||
| return | ||
| UNSIGNED == runTimeResult && | ||
| a.length == runTimeResult; | ||
| } | ||
| function testNegativeSignedEquivalence() public view returns (bool) { | ||
| int8 runTimeResult = ~I128; | ||
|
|
||
| return | ||
| NEGATIVE_SIGNED == runTimeResult && | ||
| b.length == 127; | ||
| } | ||
| function testPositiveSignedEquivalence() public view returns (bool) { | ||
| int8 runTimeResult = ~IONE; | ||
|
|
||
| return | ||
| POSITIVE_SIGNED == runTimeResult && | ||
| c.length == 2; | ||
| } | ||
| } | ||
| // ---- | ||
| // testUnsignedEquivalence() -> true | ||
| // testNegativeSignedEquivalence() -> true | ||
| // testPositiveSignedEquivalence() -> true |
53 changes: 53 additions & 0 deletions
53
...olidity/semanticTests/constantEvaluator/shift_signed_left_operand_runtime_equivalence.sol
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,53 @@ | ||
| uint256 constant ONE = 1; | ||
| int8 constant I8_NEGATIVE_63 = -63; | ||
| int8 constant I8_POSITIVE_127 = 127; | ||
| int16 constant I16_POSITIVE_127 = 127; | ||
|
|
||
| contract C { | ||
| // right side cannot be signed | ||
| int256 constant LITERAL_WRAP = -2**255 << ONE; // = 0 | ||
| uint[LITERAL_WRAP + 1] a; | ||
| int8 constant CONST_NO_WRAP = I8_NEGATIVE_63 << 1; | ||
| uint[CONST_NO_WRAP * -1] b; | ||
| int8 constant CONST_WRAP = I8_POSITIVE_127 << 1; // = -2 (1111 1110) | ||
| uint[CONST_WRAP * -1] c; | ||
| int16 constant CONST_SIGN_CHANGED = I16_POSITIVE_127 << 9; // = -512 (1111 1110 0000 0000) | ||
| uint[CONST_SIGN_CHANGED * -1] d; | ||
|
|
||
| function testLiteralWrapEquivalence() public view returns (bool) { | ||
| int256 runTimeResult = -2**255 << ONE; | ||
|
|
||
| return | ||
| LITERAL_WRAP == runTimeResult && | ||
| a.length == 1; | ||
| } | ||
|
|
||
| function testConstNoWrapEquivalence() public view returns (bool) { | ||
| int8 runTimeResult = I8_NEGATIVE_63 << 1; | ||
|
|
||
| return | ||
| CONST_NO_WRAP == runTimeResult && | ||
| b.length == 126; | ||
| } | ||
|
|
||
| function testConstWrapEquivalence() public view returns (bool) { | ||
| int8 runTimeResult = I8_POSITIVE_127 << 1; | ||
|
|
||
| return | ||
| CONST_WRAP == runTimeResult && | ||
| c.length == 2; | ||
| } | ||
|
|
||
| function testConstSignChanged() public view returns (bool) { | ||
| int16 runTimeResult = I16_POSITIVE_127 << 9; | ||
|
|
||
| return | ||
| CONST_SIGN_CHANGED == runTimeResult && | ||
| d.length == 512; | ||
| } | ||
| } | ||
| // ---- | ||
| // testLiteralWrapEquivalence() -> true | ||
| // testConstNoWrapEquivalence() -> true | ||
| // testConstWrapEquivalence() -> true | ||
| // testConstSignChanged() -> true |
43 changes: 43 additions & 0 deletions
43
...idity/semanticTests/constantEvaluator/shift_unsigned_left_operand_runtime_equivalence.sol
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,43 @@ | ||
| uint256 constant ONE = 1; | ||
| uint8 constant U8_64 = 64; | ||
| uint8 constant U8_255 = 255; | ||
|
|
||
| contract C { | ||
| // Expression with only literals have rational type with unlimited precision, | ||
| // so we use a integer constant to force the literal have uint256 (mobileType). | ||
| // The whole expression then has type uint256. | ||
| uint256 constant LITERAL_WRAP = 2**255 << ONE; // = 0 | ||
| uint[LITERAL_WRAP + 1] a; | ||
| uint8 constant CONST_NO_WRAP = U8_64 << 1; | ||
| uint[CONST_NO_WRAP] b; | ||
| uint8 constant CONST_WRAP = U8_255 << 4; // = 240 (1111 0000) | ||
| uint[CONST_WRAP] c; | ||
|
|
||
| function testLiteralWrapEquivalence() public view returns (bool) { | ||
| uint256 runTimeResult = 2**255 << ONE; | ||
|
|
||
| return | ||
| LITERAL_WRAP == runTimeResult && | ||
| a.length == runTimeResult + 1; | ||
| } | ||
|
|
||
| function testConstNoWrapEquivalence() public view returns (bool) { | ||
| uint8 runTimeResult = U8_64 << 1; | ||
|
|
||
| return | ||
| CONST_NO_WRAP == runTimeResult && | ||
| b.length == runTimeResult; | ||
| } | ||
|
|
||
| function testConstWrapEquivalence() public view returns (bool) { | ||
| uint8 runTimeResult = U8_255 << 4; | ||
|
|
||
| return | ||
| CONST_WRAP == runTimeResult && | ||
| c.length == runTimeResult; | ||
| } | ||
| } | ||
| // ---- | ||
| // testLiteralWrapEquivalence() -> true | ||
| // testConstNoWrapEquivalence() -> true | ||
| // testConstWrapEquivalence() -> true |
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
1 change: 0 additions & 1 deletion
1
test/libsolidity/syntaxTests/storageLayoutSpecifier/constant_divided_by_its_negation.sol
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,3 @@ | ||
| uint constant N = 100; | ||
| contract C layout at N / ~N {} | ||
| // ---- | ||
| // TypeError 3667: (48-50): Arithmetic error when computing constant value. |
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.
why can we assume that, because it's an
IntegerType? Isrationalalways in reduced/normalized form?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.
Because it's
IntegerType. IIRC, in comptime, it is always normalized. I will confirm this.