-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Fix ICE when assigning calldata Struct with external function type member to storage variable. #16764
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
base: develop
Are you sure you want to change the base?
Fix ICE when assigning calldata Struct with external function type member to storage variable. #16764
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3792,23 +3792,27 @@ std::string YulUtilFunctions::copyStructToStorageFunction(StructType const& _fro | |
| { | ||
| Type const& memberType = *structMembers[i].type; | ||
| solAssert(memberType.memoryHeadSize() == 32, ""); | ||
|
|
||
| // Value types cannot be dynamically encoded | ||
| if (_from.location() == DataLocation::CallData) | ||
| solAssert(!memberType.isDynamicallyEncoded() || !memberType.isValueType()); | ||
|
|
||
| auto const&[slotDiff, offset] = _to.storageOffsetsOfMember(structMembers[i].name); | ||
|
|
||
| Whiskers t(R"( | ||
| let memberSlot := add(slot, <memberStorageSlotDiff>) | ||
| let memberSrcPtr := add(value, <memberOffset>) | ||
|
|
||
| <?fromCalldata> | ||
| let <memberValues> := | ||
| <?dynamicallyEncodedMember> | ||
| <accessCalldataTail>(value, memberSrcPtr) | ||
| <!dynamicallyEncodedMember> | ||
| memberSrcPtr | ||
| </dynamicallyEncodedMember> | ||
|
|
||
| <?isValueType> | ||
| <memberValues> := <read>(<memberValues>) | ||
| </isValueType> | ||
| <?dynamicallyEncodedMember> | ||
| let <memberValues> := <accessCalldataTail>(value, memberSrcPtr) | ||
| <!dynamicallyEncodedMember> | ||
| <?isValueType> | ||
| let <memberValues> := <read>(memberSrcPtr) | ||
| <!isValueType> | ||
| let <memberValues> := memberSrcPtr | ||
| </isValueType> | ||
| </dynamicallyEncodedMember> | ||
|
Comment on lines
+3807
to
+3815
Collaborator
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 introduces a silent assumption that value types are never dynamically encoded. I think that's true now, but such types are technically possible in the encoding - it would be a case where you have an offset in the head and the value is fixed-size, but still stored in the tail. No need to handle that, but please at least explicitly assert that
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. Right. |
||
| </fromCalldata> | ||
|
|
||
| <?fromMemory> | ||
|
|
||
|
Contributor
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. I would add a few more tests:
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| pragma abicoder v2; | ||
| struct S { | ||
| function(uint) external fn_uint; | ||
| } | ||
|
|
||
| contract C { | ||
| S storageStruct; | ||
|
|
||
| function test(S calldata calldataStruct) public returns (bool) { | ||
| storageStruct = calldataStruct; | ||
|
|
||
| assert(storageStruct.fn_uint.address == address(bytes20("01234567890123456789"))); | ||
| assert(storageStruct.fn_uint.selector == bytes4("abcd")); | ||
| return true; | ||
| } | ||
| } | ||
| // ---- | ||
| // test((function)): "01234567890123456789abcd" -> true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| pragma abicoder v2; | ||
| struct S { | ||
| function(uint) external fn_uint; | ||
| } | ||
|
|
||
| contract C { | ||
| S[] storageStruct; | ||
|
|
||
| function test(S[] calldata calldataStruct) public returns (bool) { | ||
| storageStruct = calldataStruct; | ||
|
|
||
| assert(storageStruct.length == 2); | ||
| assert(storageStruct[0].fn_uint.address == address(bytes20("01234567890123456789"))); | ||
| assert(storageStruct[0].fn_uint.selector == bytes4("abcd")); | ||
| assert(storageStruct[1].fn_uint.address == address(bytes20("98765432109876543210"))); | ||
| assert(storageStruct[1].fn_uint.selector == bytes4("efgh")); | ||
| return true; | ||
| } | ||
| } | ||
| // ==== | ||
| // compileViaYul: true | ||
| // ---- | ||
| // test((function)[]): 0x20, 2, "01234567890123456789abcd", "98765432109876543210efgh" -> true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| pragma abicoder v2; | ||
| struct S { | ||
| function(uint) external fn_uint; | ||
| uint256[] arr; | ||
| } | ||
|
|
||
| contract C { | ||
| S storageStruct; | ||
|
|
||
| function test(S calldata calldataStruct) public returns (bool) { | ||
| storageStruct = calldataStruct; | ||
|
|
||
| assert(storageStruct.fn_uint.address == address(bytes20("01234567890123456789"))); | ||
| assert(storageStruct.fn_uint.selector == bytes4("abcd")); | ||
| assert(storageStruct.arr.length == 3); | ||
| assert(storageStruct.arr[0] == 11); | ||
| assert(storageStruct.arr[1] == 22); | ||
| assert(storageStruct.arr[2] == 33); | ||
| return true; | ||
| } | ||
| } | ||
| // ---- | ||
| // test((function,uint256[])): 0x20, "01234567890123456789abcd", 0x40, 3, 11, 22, 33 -> true | ||
| // gas irOptimized: 134866 | ||
| // gas legacy: 137443 | ||
| // gas legacyOptimized: 135246 | ||
| // gas ssaCFGOptimized: 134897 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| pragma abicoder v2; | ||
| struct S { | ||
| function(uint) external fn_uint; | ||
| } | ||
|
|
||
| contract C { | ||
| S storageStruct; | ||
|
|
||
| function test(S calldata calldataStruct) public returns (bool) { | ||
| storageStruct = calldataStruct; | ||
| return true; | ||
| } | ||
| } | ||
| // ---- | ||
| // test((function)): "01234567890123456789abcd" -> true | ||
| // test((function)): 0x3031323334353637383930313233343536373839616263640000000000000001 -> FAILURE | ||
| // test((function)): 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff -> FAILURE |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| pragma abicoder v2; | ||
| struct S { | ||
| function(uint) external fn_uint; | ||
| uint64 packed; | ||
| } | ||
|
|
||
| contract C { | ||
| S storageStruct; | ||
|
|
||
| function test(S calldata calldataStruct) public returns (bool) { | ||
| storageStruct = calldataStruct; | ||
|
|
||
| assert(storageStruct.fn_uint.address == address(bytes20("01234567890123456789"))); | ||
| assert(storageStruct.fn_uint.selector == bytes4("abcd")); | ||
| assert(storageStruct.packed == 42); | ||
| return true; | ||
| } | ||
| } | ||
| // ---- | ||
| // test((function,uint64)): "01234567890123456789abcd", 42 -> true |
Uh oh!
There was an error while loading. Please reload this page.