Skip to content

Fix ICE caused by missing validation of types nested in mappings, dynamic arrays and arrays of structs - #17290

Merged
blishko merged 4 commits into
developfrom
fix-oversized-types-skipped-validation
Nov 30, 2026
Merged

blishko merged 4 commits into
developfrom
fix-oversized-types-skipped-validation

Conversation

@matheusaaguiar

Copy link
Copy Markdown
Contributor

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.

@matheusaaguiar matheusaaguiar self-assigned this Nov 25, 2026
@@ -0,0 +1,16 @@
// The storage size upper bound of S is 1 + 2**255 + (2**255 - 2) == 2**256 - 1.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This contract hits that bound exactly and so it's not the type error yet, correct?
Can you add that explanation here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 2**32 - 32 the bound for memory?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name says dynamic_array. Maybe the mapping shouldn't be here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
contract C {
mapping(uint => S) m;
S[] d;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name says mapping. Maybe the array doesn't have to be here?

Also, what is the bound for storage? 2**256 - 1?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2**256 - 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to structs/recursion.

Comment thread libsolidity/ast/Types.cpp Outdated
}

if (auto const* referenceType = dynamic_cast<ReferenceType const*>(_type))
return referenceType->validForLocation(_loc);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually reachable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is still ArraySliceType that needs to be caught by this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored.
I changed the methods name to sizeValidForLocation.

Comment thread libsolidity/analysis/TypeChecker.cpp Outdated
{
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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was silly of me...don't know why I thought it would disrupt the check for the other types.
Changed.

@matheusaaguiar
matheusaaguiar force-pushed the fix-oversized-types-skipped-validation branch from b62a61b to f67f521 Compare November 25, 2026 20:33
Comment thread libsolidity/ast/Types.cpp Outdated
{

/// @returns the total amount of bytes in memory taken by type @a _type
bigint memorySizeInBytes(Type const* _type)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sounds like a separate issue. Maybe you can stack a separate PR fixing that onto this one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will create an issue and stack the fix on top of this PR.

@blishko

blishko commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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.
Also, I would wait until we merge the breaking branch into develop, so we have one version of the changelog again.

@matheusaaguiar
matheusaaguiar force-pushed the fix-oversized-types-skipped-validation branch 2 times, most recently from f67f521 to e7c9f9c Compare November 26, 2026 19:30

@blishko blishko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just small suggestions for clarity.
Otherwise, this just needs a rebase and can be merged.

Comment thread libsolidity/ast/Types.cpp Outdated
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you hardcode DataLocation::Storage here because mappings can only be stored in storage?
Should we assert it?
For example

Suggested change
return validForLocationRecursive(mappingType->valueType(), DataLocation::Storage, _visitedStructs);
{
solAssert(_loc == DataLocation::Storage);
return validForLocationRecursive(mappingType->valueType(), _loc, _visitedStructs);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I will add the assert.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rephrase this:

Suggested change
// 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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, two M structs should be over the limit, no? Is this the missing validation enforced in the follow-up?
#17312

@matheusaaguiar matheusaaguiar Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I noticed the problem because of this test.
In #17312, the expectation changes and error 1534: Type too large for memory is generated.

@matheusaaguiar
matheusaaguiar force-pushed the fix-oversized-types-skipped-validation branch from e7c9f9c to 604e9fc Compare November 27, 2026 23:32
{
auto result = validForLocation(referenceType, referenceType->location());
if (!result)
if (referenceType->containsNestedMapping())

@matheusaaguiar matheusaaguiar Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@matheusaaguiar matheusaaguiar Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😆

@matheusaaguiar
matheusaaguiar force-pushed the fix-oversized-types-skipped-validation branch 2 times, most recently from 2a92f42 to e9885f1 Compare November 28, 2026 03:17
blishko
blishko previously approved these changes Nov 30, 2026

@blishko blishko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be merged, though I think we need a follow-up to deduplicate the type errors.

@blishko
blishko merged commit 6aaca58 into develop Nov 30, 2026
82 checks passed
@blishko
blishko deleted the fix-oversized-types-skipped-validation branch November 30, 2026 19:17
@matheusaaguiar

Copy link
Copy Markdown
Contributor Author

I believe this can be merged, though I think we need a follow-up to deduplicate the type errors.

@blishko created #17342

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE: Oversized types nested in mappings, dynamic arrays or arrays of structs bypass size validation

2 participants