Skip to content

TypeChecker: Disallow internal function types in calldata - #17355

Merged
nikola-matic merged 7 commits into
developfrom
disallow-internal-function-types-in-calldata
Dec 3, 2026
Merged

nikola-matic merged 7 commits into
developfrom
disallow-internal-function-types-in-calldata

Conversation

@blishko

@blishko blishko commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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.

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.

Comment thread Changelog.md
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.

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

@cameel cameel Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

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.

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.

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.

If former, then get rid of it, and generate a new error code for internal function pointers anyway :)

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.

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

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 first three are fine and belong together - the one you introduced doesn't quite fit as it has nothing to do with type size.

@blishko blishko Dec 2, 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.

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.

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.

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.

@cameel cameel Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@blishko blishko Dec 2, 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.

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?

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.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

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.

@msooseth

msooseth commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

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:

libsolidity/ast/Types.cpp(2311): Throw in function virtual bool solidity::frontend::StructType::isDynamicallyEncoded() const

The parameter does get the new Internal function types cannot be stored in calldata. error. But that error doesn't stop the TypeChecker, so it still visits the function body :S

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

@msooseth

msooseth commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Added a PR to this PR: #17357 to add a few more tests to cover more of #17315

@blishko

blishko commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Added a PR to this PR: #17357 to add a few more tests to cover more of #17315

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?
Do you have a fix for that ICEing case? Or should I work on that?

@matheusaaguiar

Copy link
Copy Markdown
Contributor

I checked the array slice case and I think two more patches will be needed.
I tested it and it pass all the cases produced by @msooseth.
Types.cpp

 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;

TypeChecker.cpp

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

@msooseth

msooseth commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Added a PR to this PR: #17357 to add a few more tests to cover more of #17315

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? Do you have a fix for that ICEing case? Or should I work on that?

Yes, you understand correctly. I didn't add that test, I just tried to add test that don't ICE.

blishko and others added 4 commits December 3, 2026 15:14
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.
@nikola-matic
nikola-matic force-pushed the disallow-internal-function-types-in-calldata branch from abe355e to 8274b16 Compare December 3, 2026 14:14
@nikola-matic

Copy link
Copy Markdown
Contributor

Added a PR to this PR: #17357 to add a few more tests to cover more of #17315

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? Do you have a fix for that ICEing case? Or should I work on that?

Yes, you understand correctly. I didn't add that test, I just tried to add test that don't ICE.

ICE test cases have been added in a separate commit.

@blishko blishko left a comment

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.

Not sure we need the 4 tests in largeTypes subdirectory that now report 4145.
Aren't they covered by the rest?

@nikola-matic

Copy link
Copy Markdown
Contributor

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.

@nikola-matic
nikola-matic force-pushed the disallow-internal-function-types-in-calldata branch from 8274b16 to 276fff2 Compare December 3, 2026 14:47
@nikola-matic
nikola-matic merged commit 6a4d5ea into develop Dec 3, 2026
82 checks passed
@nikola-matic
nikola-matic deleted the disallow-internal-function-types-in-calldata branch December 3, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants