-
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
3
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.
+249
−40
Open
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
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 |
|---|---|---|
|
|
@@ -38,19 +38,32 @@ namespace solidity::frontend | |
| class TypeChecker; | ||
|
|
||
| /** | ||
| * Small drop-in replacement for TypeChecker to evaluate simple expressions of integer constants. | ||
| * Small drop-in replacement for TypeChecker to evaluate simple expressions of integer and string constants. | ||
| * | ||
| * Note: This always use "checked arithmetic" in the sense that any over- or underflow | ||
| * results in "unknown" value. | ||
| */ | ||
| class ConstantEvaluator: private ASTConstVisitor | ||
| { | ||
| public: | ||
| struct TypedValue | ||
| class TypedValue | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This refactor was suggested here and it is simple enough that I think it can be slipped in this PR... |
||
| { | ||
| public: | ||
| using Value = std::variant<std::monostate, rational, std::string>; | ||
|
|
||
| TypedValue(): m_type(nullptr), m_value(std::monostate()) {} | ||
| TypedValue(Type const* _type, Value const _value); | ||
| bool empty() const { return std::holds_alternative<std::monostate>(m_value); } | ||
| bool isString() const { return std::holds_alternative<std::string>(m_value); } | ||
| bool isRational() const { return std::holds_alternative<rational>(m_value); } | ||
| Type const* type() const { return m_type; } | ||
| Value const& value() const { return m_value; } | ||
| std::string const& asString() const; | ||
| rational const& asRational() const; | ||
| private: | ||
| // Type may be RationalType or IntegerType for value rational | ||
| Type const* type; | ||
| std::variant<std::monostate, rational, std::string> value; | ||
| Type const* m_type; | ||
| Value m_value; | ||
| }; | ||
|
|
||
| static TypedValue evaluate( | ||
|
|
||
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
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 |
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.
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.