Repository navigation
Fix ICE caused by missing validation of types nested in mappings, dynamic arrays and arrays of structs - #17290
Conversation
| @@ -0,0 +1,16 @@ | |||
| // The storage size upper bound of S is 1 + 2**255 + (2**255 - 2) == 2**256 - 1. | |||
There was a problem hiding this comment.
This contract hits that bound exactly and so it's not the type error yet, correct?
Can you add that explanation here?
There was a problem hiding this comment.
Also, should storage be part of the test name?
| @@ -0,0 +1,12 @@ | |||
| // The memory size of M is (2**27 - 1) * 32 == 2**32 - 32. | |||
There was a problem hiding this comment.
Is 2**32 - 32 the bound for memory?
There was a problem hiding this comment.
It is actually set to the max value of c++ unsigned type...that's usually 2**32 - 1, although we know that could be different depending on the plataform 😅
I remember seeing some old discussions about that, but I guess at this point it's all over the place.
| } | ||
| contract C { | ||
| R[] d; | ||
| mapping(uint => R) m; |
There was a problem hiding this comment.
The test name says dynamic_array. Maybe the mapping shouldn't be here?
| } | ||
| contract C { | ||
| mapping(uint => S) m; | ||
| S[] d; |
There was a problem hiding this comment.
The test name says mapping. Maybe the array doesn't have to be here?
Also, what is the bound for storage? 2**256 - 1?
There was a problem hiding this comment.
Do we actually need this test?
It doesn't really fit largeTypes category, no?
Can you check if this is covered by the tests in structs/recursion? If not, maybe it should go there?
There was a problem hiding this comment.
Moved to structs/recursion.
| } | ||
|
|
||
| if (auto const* referenceType = dynamic_cast<ReferenceType const*>(_type)) | ||
| return referenceType->validForLocation(_loc); |
There was a problem hiding this comment.
Is this actually reachable?
There was a problem hiding this comment.
Yes, there is still ArraySliceType that needs to be caught by this.
There was a problem hiding this comment.
Although, I think you are right. Array slices cannot be named as a type, so this would be only a safeguard in case they ever appear...
There was a problem hiding this comment.
Yeah, it's only ArraySliceType.
But that just delegates to the underlying array, which is now non-recursive, so the answer could be wrong again.
It seems to me we could/should do slightly bigger refactoring.
How about we assert here that we have covered all reference types (so in a future, if something changes, the assert would alert people to implement this properly)?
Actually, maybe we could remove validForLocation from ReferenceType altogether? Keep it only in ArrayType and StructType, non-virtual?
There was a problem hiding this comment.
Refactored.
I changed the methods name to sizeValidForLocation.
| { | ||
| solAssert(!result.message().empty(), "Expected detailed error message"); | ||
| m_errorReporter.typeError(1534_error, _variable.location(), result.message()); | ||
| m_errorReporter.typeError(ErrorId{1534}, _variable.location(), result.message()); |
There was a problem hiding this comment.
Why are you changing this?
There was a problem hiding this comment.
Forgot to leave a comment about this.
scripts/error_codes.py checks that each error ID appears only once.
This is a workaround to avoid it complaining.
I thought about merging the mapping check with the reference one, but it seemed to be a bit convoluted and I found this to be clearer, albeit the repeated error message...
There was a problem hiding this comment.
I understand, but I don't think working around the error code check is the right approach.
I think it should be doable to have the error handling only once at the end, no?
You don't really have to merge the two cases.
How about something along the lines
BoolResult result = true;
if (dynamic_cast<MappingType const*>(varType))
result = validForLocation(varType, DataLocation::Storage);
else if (auto referenceType = dynamic_cast<ReferenceType const*>(varType))
{
result = validForLocation(referenceType, referenceType->location());
/*
reference type specific logic
*/
}
// common error handling
if (!result)
{
solAssert(!result.message().empty(), "Expected detailed error message");
m_errorReporter.typeError(1534_error, _variable.location(), result.message());
return false;
}
There was a problem hiding this comment.
Yeah, that was silly of me...don't know why I thought it would disrupt the check for the other types.
Changed.
b62a61b to
f67f521
Compare
| { | ||
|
|
||
| /// @returns the total amount of bytes in memory taken by type @a _type | ||
| bigint memorySizeInBytes(Type const* _type) |
There was a problem hiding this comment.
Structs were not being validated in StructType::sizeValidForLocation (StructType::validForLocation before the refactor) when the location was memory or calldata.
They were silently skipped and thus oversized structs were accepted in memory/calldata.
I think technically this a separate issue from the one that prompted this PR, but I am leaving it as a commit on top of it for now...maybe we can merge them together?
There was a problem hiding this comment.
Yes, sounds like a separate issue. Maybe you can stack a separate PR fixing that onto this one?
There was a problem hiding this comment.
Yeah, I will create an issue and stack the fix on top of this PR.
|
I think the fix is correct now. I would split the last commit into a separate PR. If I understood you correctly, that is another validation that was previously missing. |
f67f521 to
e7c9f9c
Compare
blishko
left a comment
There was a problem hiding this comment.
Just small suggestions for clarity.
Otherwise, this just needs a rebase and can be merged.
| BoolResult validForLocationRecursive(Type const* _type, DataLocation _loc, VisitedStructs& _visitedStructs) | ||
| { | ||
| if (auto const* mappingType = dynamic_cast<MappingType const*>(_type)) | ||
| return validForLocationRecursive(mappingType->valueType(), DataLocation::Storage, _visitedStructs); |
There was a problem hiding this comment.
I guess you hardcode DataLocation::Storage here because mappings can only be stored in storage?
Should we assert it?
For example
| return validForLocationRecursive(mappingType->valueType(), DataLocation::Storage, _visitedStructs); | |
| { | |
| solAssert(_loc == DataLocation::Storage); | |
| return validForLocationRecursive(mappingType->valueType(), _loc, _visitedStructs); | |
| } |
There was a problem hiding this comment.
Yes. I will add the assert.
There was a problem hiding this comment.
Adding the assert here makes cases like test/libsolidity/syntaxTests/immutable/non-value_type_struct_with_mapping.sol where there is a nested mapping in a variable declared outside of storage.
Currently, I think that all paths already emit an error for that, so we could skip if the location is not storage.
I implemented the check before calling validForLocation to be sure that nothing is missed now or in the future.
| @@ -0,0 +1,17 @@ | |||
| // The storage size upper bound of S is 1 + 2**255 + (2**255 - 2) == 2**256 - 1 | |||
| // It sits on the boundary of storage | |||
There was a problem hiding this comment.
I would rephrase this:
| // It sits on the boundary of storage | |
| // That is exactly the storage limit we impose |
| function f(M[] calldata) external pure {} | ||
| function g() public pure { | ||
| M[] memory m; | ||
| M[2] memory n; |
There was a problem hiding this comment.
Wait, two M structs should be over the limit, no? Is this the missing validation enforced in the follow-up?
#17312
There was a problem hiding this comment.
Yeah, I noticed the problem because of this test.
In #17312, the expectation changes and error 1534: Type too large for memory is generated.
e7c9f9c to
604e9fc
Compare
| { | ||
| auto result = validForLocation(referenceType, referenceType->location()); | ||
| if (!result) | ||
| if (referenceType->containsNestedMapping()) |
There was a problem hiding this comment.
Because of the refactor, a nested mapping reached through the validation of an array type would either fail an assert (suggested in review) or without the assert, continue assuming storage as data location.
The compiler already generate errors for all the cases, but they are not fatal.
Alternatively, the validation for mappings not in storage could just be ignored and rely on the problem being flagged previously.
There was a problem hiding this comment.
I don't particularly like that we are introducing the same error in two different places under two different error codes.
Or do you see a difference there?
However, it seems to me it would be even larger refactor, so maybe we can do that later.
I would like to get this one and the follow-up into the 0.9
There was a problem hiding this comment.
I don't particularly like that we are introducing the same error in two different places under two different error codes.
Or do you see a difference there?
It's the same situation/error being flagged in two different paths.
We could remove them, ignoring the validation of variables containing mappings outside storage and rely on the assumption that the problem was already flagged.
I just wanted to be sure that no corner case is going to be silently ignored, but we can also leave that for a follow-up.
However, it seems to me it would be even larger refactor, so maybe we can do that later.
That's what I'm afraid of 😆
2a92f42 to
e9885f1
Compare
blishko
left a comment
There was a problem hiding this comment.
I believe this can be merged, though I think we need a follow-up to deduplicate the type errors.
e9885f1 to
efa3bed
Compare
Fix #17250.
The type checker rejects variables whose type is too large for its data location.
However, some nested types are not checked and oversized types are accepted if they are only reachable through: mapping value, dynamic array element or a Struct (static and dynamic) array element.
This PR adds a helper function
validForLocation(Type const*, DataLocation)that descends into array base types, struct members and mapping value types.