Repository navigation
TypeChecker: Disallow internal function types in calldata - #17355
Conversation
| Bugfixes: | ||
| * AST Import: Fix source location fields that are not decimal integers being silently truncated or causing an uncaught exception instead of being rejected. | ||
| * Disassembler: Fix opcodes not available in the selected EVM version being printed as instructions in the `opcodes` output instead of as raw bytes. | ||
| * Type Checker: Disallow internal function types in calldata, including nested in structs and arrays, which previously led to a segmentation fault or an internal compiler error. |
There was a problem hiding this comment.
I'm leaning toward this being a breaking change because we're now disallowing something that was previously allowed, even though our test coverage and docs said nothing about it. In any case, given that this will be going out with 0.9.0, I don't see the harm in making it breaking.
There was a problem hiding this comment.
I think non-breaking makes more sense as it was never an intentional feature. It's a bugfix (i.e. the current changelog is fine).
If it was a breaking change, we would have to provide some guidance on what is the replacement and how to migrate.
| function g(P[2] calldata) internal {} | ||
| } | ||
| // ---- | ||
| // TypeError 1534: (109-119): Type too large for calldata. |
There was a problem hiding this comment.
From what I can tell, 1534 can now emit two different errors? Either the original 'Type too large for calldata.' is now dead (which I assume it isn't), or we're reusing a single error code for two distinct errors. If it's the latter, please create a new error code.
There was a problem hiding this comment.
If former, then get rid of it, and generate a new error code for internal function pointers anyway :)
There was a problem hiding this comment.
Well, that depends. I would argue it's one error kind: "Type is not valid for its given data location".
Already today there are three different instances:
- "Type too large for calldata"
- "Type too large for memory"
- "Type too large for storage"
This just adds forth instance of this error: "Internal function types cannot be stored in calldata."
There was a problem hiding this comment.
The first three are fine and belong together - the one you introduced doesn't quite fit as it has nothing to do with type size.
There was a problem hiding this comment.
Yes, the question is what does this type error represents.
In your view it represents "size of type not valid for location".
In my view it represents "type not valid for location".
The function that we call to check for this error is validForLocation, not sizeValidForLocation.
That's where my position is coming from.
Introducing a new error will require a bit more involved change, I think.
I can do that, if others agree with you.
There was a problem hiding this comment.
Actually, you can just delete this test since it doesn't really do what it says anymore, and it's just a copy of the new one anyway.
There was a problem hiding this comment.
I'd err on the side of distinguishing sub-cases with separate errors where possible. The practical aspect is that we have CI checks that enforce coverage for each error code. If we make errors too broad, the check gets less effective at detecting missing coverage. Here "too big for location" seems pretty distinct from "not an ABI type" to me.
In the end though, we don't really have a hard rule and it's all over the place right now. So one "invalid for location" error covering all of these wouldn't be strictly wrong.
My general feeling is that the error code system we have is half-baked and we could do better. There should be a bit more hierarchy and more features to unify same error thrown in different places. So far we did not even enable these codes by default on the CLI, I think because there was not that much confidence in it.
There was a problem hiding this comment.
Here "too big for location" seems pretty distinct from "not an ABI type" to me.
When you phrase it like this then I agree that they are actually two different kinds of error. I'll try to implement it like that.
My general feeling is that the error code system we have is half-baked and we could do better
I agree with that.
One thing that I am confused about is when is a type error fatal and when it is non-fatal. Is there any rule for that?
There should be a bit more hierarchy and more features to unify same error thrown in different places
We actually have a check in error_codes.py that the same error id (literal <id>_error) cannot appear more than once. So literally the same error cannot be thrown in multiple places. Or did you mean something else?
So far we did not even enable these codes by default on the CLI, I think because there was not that much confidence in it.
What do you mean by enabling these codes on the CLI?
There was a problem hiding this comment.
What do you mean by enabling these codes on the CLI?
I suppose he meant making --error-codes option the default behavior, i.e., always print the error code.
There was a problem hiding this comment.
One thing that I am confused about is when is a type error fatal and when it is non-fatal. Is there any rule for that?
The soft rule is that we try to make errors non-fatal as much as possible. This makes the output more useful to the user (can see multiple errors) and tools (especially editors that highlight lines that contain errors). It's not a hard rule, because this means that after that point in analysis you still have to account for the possibility of such an error. Sometimes you even need to repeat checks against it. That makes analysis more complex so it's a trade-off, not a hard rule.
We actually have a check in
error_codes.pythat the same error id (literal<id>_error) cannot appear more than once. So literally the same error cannot be thrown in multiple places. Or did you mean something else?
By "unify" I meant being able to throw it from multiple places and still be recognized as the same logical error (i.e. same code). There are workarounds like putting the error into a helper invoked from multiple places, but these bypass the mechanism - if you have two different paths throwing the error that way, error_codes.py will enforce coverage only for one of them.
What do you mean by enabling these codes on the CLI?
Like @matheusaaguiar said, --error-codes should be the default.
|
This still ICEs on the PR branch, on both pipelines: struct S { function() internal f; }
contract C { function g(S[] calldata s) internal pure { s[1:]; } }Error: The parameter does get the new These crash the same way: function g(S[] calldata s) internal pure { s[1:][0]; }
function g(S[] calldata s) internal pure { (s[1:]); }
function g(S[] calldata s, bool b) internal pure { b ? s[1:] : s[2:]; }
function g(S[] calldata s) internal pure { S[] calldata t = s[1:]; t; }
function g(S[] calldata s) internal pure { h(s[1:]); } // with: function h(S[] calldata) internal pure {} |
Do I understand it correctly that your PR adds more test cases that are currently covered by this fix, but not the case you found still ICEs even with current fix? |
|
I checked the array slice case and I think two more patches will be needed. Type const* ArraySliceType::mobileType() const
{
if (
m_arrayType.dataStoredIn(DataLocation::CallData) &&
m_arrayType.isDynamicallySized() &&
+ m_arrayType.baseType()->interfaceType(false) &&
!m_arrayType.baseType()->isDynamicallyEncoded()
)
return &m_arrayType;
if (arrayType->location() != DataLocation::CallData || !arrayType->isDynamicallySized())
m_errorReporter.typeError(1227_error, _access.location(), "Index range access is only supported for dynamic calldata arrays.");
- else if (arrayType->baseType()->isDynamicallyEncoded()
+ else if (
+ validForLocation(arrayType, DataLocation::CallData) &&
+ arrayType->baseType()->isDynamicallyEncoded()
+ )
m_errorReporter.typeError(2148_error, _access.location(), "Index range access is not supported for arrays with dynamically encoded base types.");
_access.annotation().type = TypeProvider::arraySlice(*arrayType); |
Yes, you understand correctly. I didn't add that test, I just tried to add test that don't ICE. |
The type checker currently accepts calldata variables whose types contain internal function types, directly or nested in structs and arrays, even though calldata only holds ABI-encoded data and internal function types have no ABI encoding. Such declarations compile, but generating code that reads such a value or copies it to memory crashes the compiler.
Calldata only holds ABI-encoded data and internal function types have no ABI encoding, so a calldata value containing one can never be produced without inline assembly. Code generation also cannot handle such values: reading an internal function pointer from calldata segfaults the legacy code generator, and copying such values to memory hits internal compiler errors in both pipelines. Structs containing internal function types also trip an assertion in StructType::isDynamicallyEncoded() during the calldata size check when they are struct members or elements of static arrays. validForLocation() already descends into all nested types, so reject internal function types at its leaves when validating for calldata. Since member errors are returned before a struct's own size check, this also prevents the assertion in the size check.
abe355e to
8274b16
Compare
ICE test cases have been added in a separate commit. |
blishko
left a comment
There was a problem hiding this comment.
Not sure we need the 4 tests in largeTypes subdirectory that now report 4145.
Aren't they covered by the rest?
Yeah, you're right, I'll get rid of them. |
8274b16 to
276fff2
Compare
Calldata only holds ABI-encoded data and internal function types have no ABI encoding, so a calldata value containing one can never be produced without inline assembly.
Code generation also cannot handle such values: reading an internal function pointer from calldata segfaults the legacy
code generator, and copying such values to memory hits internal compiler errors in both pipelines.
Structs containing internal function types also trip an assertion in
StructType::isDynamicallyEncoded()during the calldatasize check when they are struct members or elements of static arrays.
validForLocation()already descends into all nested types, so reject internal function types at its leaves when validating for calldata. Since member errors are returned before a struct's own size check, this also prevents the assertion in the size check.We also update documentation to explicitly mention that internal function types are forbidden in calldata.
The language of the docs was not explicit in this regard, but the intent was there already.
Fixes #17354.
Fixes #17315.
Fixes #16622.
Fixes #16223.
AI Disclosure
FIx proposed by Opus 5.5 under my guidance.