fix(sema): reject internal function references in abi.encode* arguments#1891
Open
aryanbaranwal001 wants to merge 1 commit intohyperledger-solang:mainfrom
Open
Conversation
Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a compiler panic when an internal function reference is passed as an argument to
abi.encode*.unreachable!("This type cannot be encoded")when abi-encoding an internal function reference #1862The root cause is that sema's
abi.encode*argument resolution loop atsema/builtin.rs:1403only guards against mapping and recursive types. It has no guard forType::InternalFunction. So when a bare internal function reference likefis passed as an argument, sema accepts it without error and stores it in the AST withType::InternalFunctionand put into the args ofast::Expression::Builtin.codegen/expression.rs:1048matchesast::Builtin::AbiEncodePackedand callsabi_encode_packed()atcodegen/expression.rs:1853.abi_encode_packedmapsexpression()over each arg and passes the result toabi_encode()atcodegen/expression.rs:1858.abi_encodecallscalculate_size_args()atcodegen/encoding/mod.rs:48, which callsget_expr_size()atcodegen/encoding/mod.rs:176.get_expr_sizematches on the expression's type.Type::InternalFunctionhits the unreachable arm atcodegen/encoding/mod.rs:1448–1453:Fix
Add a
Type::InternalFunctionguard in the arg resolution loop atsema/builtin.rs:1407, that is, immediately after the existing mapping/recursive check:This makes sema reject internal function references before they reach codegen. The fix covers
abi.encode*variants such asabi.encode,abi.encodePacked,abi.encodeWithSelector, andabi.encodeWithSignature, since they all share the same loop atsema/builtin.rs:1403.Previously panicking inputs:
abi.encodePacked(f);abi.encode(f);where
fis a function in the same contractReproduces on
polkadot,solana, andevmtargets.