From df8bb173b9b42587debb14940f858197c6a79724 Mon Sep 17 00:00:00 2001 From: Asuka Date: Thu, 12 Nov 2026 17:19:04 +0800 Subject: [PATCH] fix: clean token IDs and decode wrapped TRON addresses --- libsolidity/codegen/ABIFunctions.cpp | 4 ++ libsolidity/codegen/ExpressionCompiler.cpp | 2 +- libsolidity/codegen/YulUtilFunctions.cpp | 4 ++ test/libsolidity/StandardCompiler.cpp | 34 +++++++++++ .../userDefinedValueType/tron_address_abi.sol | 56 +++++++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 test/libsolidity/semanticTests/userDefinedValueType/tron_address_abi.sol diff --git a/libsolidity/codegen/ABIFunctions.cpp b/libsolidity/codegen/ABIFunctions.cpp index fd18ac44b363..722a9fb6b32b 100644 --- a/libsolidity/codegen/ABIFunctions.cpp +++ b/libsolidity/codegen/ABIFunctions.cpp @@ -1105,6 +1105,10 @@ std::string ABIFunctions::abiDecodingFunction(Type const& _type, bool _fromMemor std::string ABIFunctions::abiDecodingFunctionValueType(Type const& _type, bool _fromMemory) { + if (auto const* valueType = dynamic_cast(&_type)) + if (valueType->underlyingType().category() == Type::Category::Address) + return abiDecodingFunctionValueType(valueType->underlyingType(), _fromMemory); + Type const* decodingType = _type.decodingType(); solAssert(decodingType, ""); solAssert(decodingType->sizeOnStack() == 1, ""); diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index c3dcf3647c80..ebd91d14c6b5 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -890,7 +890,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) // gas <- gas * !value m_context << Instruction::SWAP1 << Instruction::DUP2; m_context << Instruction::ISZERO << Instruction::MUL << Instruction::SWAP1; - arguments[1]->accept(*this); + acceptAndConvert(*arguments[1], *function.parameterTypes()[1], true); // will be removed in next release m_context << Instruction::DUP1 << Instruction::ISZERO; diff --git a/libsolidity/codegen/YulUtilFunctions.cpp b/libsolidity/codegen/YulUtilFunctions.cpp index 4c3cd8c0fc69..28f87bd78251 100644 --- a/libsolidity/codegen/YulUtilFunctions.cpp +++ b/libsolidity/codegen/YulUtilFunctions.cpp @@ -4041,6 +4041,10 @@ std::string YulUtilFunctions::cleanupFunction(Type const& _type) std::string YulUtilFunctions::validatorFunction(Type const& _type, bool _revertOnFailure) { + if (auto const* valueType = dynamic_cast(&_type)) + if (valueType->underlyingType().category() == Type::Category::Address) + return validatorFunction(valueType->underlyingType(), _revertOnFailure); + std::string functionName = std::string("validator_") + (_revertOnFailure ? "revert_" : "assert_") + _type.identifier(); return m_functionCollector.createFunction(functionName, [&]() { Whiskers templ(R"( diff --git a/test/libsolidity/StandardCompiler.cpp b/test/libsolidity/StandardCompiler.cpp index eced46153cad..7f48d1d6674c 100644 --- a/test/libsolidity/StandardCompiler.cpp +++ b/test/libsolidity/StandardCompiler.cpp @@ -735,6 +735,40 @@ contract C { BOOST_TEST(ir.find("if mod(returndatasize(), 0x20) { revert(0, 0) }") != std::string::npos); } +BOOST_AUTO_TEST_CASE(tron_transfer_token_narrowed_id) +{ + for (bool optimize: {false, true}) + { + auto runtimeFor = [&](std::string const& _tokenId) { + std::string sourceCode = R"( +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; +contract C { + function transfer(address payable recipient, uint256 x) external { + recipient.transferToken(100, TOKEN_ID); + } + function transferConstant(address payable recipient) external { + uint256 x = (1 << 64) + 1000001; + recipient.transferToken(100, TOKEN_ID); + } +} +)"; + boost::replace_all(sourceCode, "TOKEN_ID", _tokenId); + Json input = createLanguageAndSourcesSection("Solidity", {{"A.sol", sourceCode}}); + input["settings"]["optimizer"]["enabled"] = optimize; + input["settings"]["metadata"]["appendCBOR"] = false; + input["settings"]["outputSelection"]["*"]["*"] = Json::array({"evm.deployedBytecode.object"}); + Json const output = compile(input.dump()); + BOOST_REQUIRE(containsAtMostWarnings(output)); + return output["contracts"]["A.sol"]["C"]["evm"]["deployedBytecode"]["object"].get(); + }; + // Implicit widening to trcToken must consume the same value as explicit widening. + std::string const cleaned = runtimeFor("uint256(uint24(x))"); + BOOST_CHECK_EQUAL(runtimeFor("uint24(x)"), cleaned); + BOOST_CHECK(runtimeFor("x") != cleaned); + } +} + BOOST_AUTO_TEST_CASE(compilation_error) { char const* input = R"( diff --git a/test/libsolidity/semanticTests/userDefinedValueType/tron_address_abi.sol b/test/libsolidity/semanticTests/userDefinedValueType/tron_address_abi.sol new file mode 100644 index 000000000000..78625da7b130 --- /dev/null +++ b/test/libsolidity/semanticTests/userDefinedValueType/tron_address_abi.sol @@ -0,0 +1,56 @@ +pragma abicoder v2; +type MyAddress is address; +type MyPayableAddress is address payable; +type MyUint24 is uint24; +type MyBool is bool; +type MyBytes2 is bytes2; + +contract C { + // Payable entry points let the generic EVM test host exercise the ABI decoder + // without the TRON-specific non-payable CALLTOKENID/CALLTOKENVALUE checks. + constructor() payable {} + + function plain(address a) external payable returns (address) { return a; } + function wrapped(MyAddress a) external payable returns (MyAddress) { return a; } + function raw(MyAddress a) external payable returns (uint256 value) { + assembly { value := a } + } + function wrappedPayable(MyPayableAddress a) external payable returns (MyPayableAddress) { return a; } + function decode(bytes memory data) external payable returns (MyAddress) { + return abi.decode(data, (MyAddress)); + } + function calldataArray(MyAddress[] calldata data) external payable returns (MyAddress) { return data[0]; } + function memoryArray(MyAddress[] memory data) external payable returns (MyAddress) { return data[0]; } + + function integer(MyUint24 value) external payable returns (MyUint24) { return value; } + function boolean(MyBool value) external payable returns (MyBool) { return value; } + function fixedBytes(MyBytes2 value) external payable returns (MyBytes2) { return value; } +} +// ==== +// EVMVersion: >=byzantium +// compileViaYul: also +// ---- +// plain(address): 0x1234 -> 0x1234 +// plain(address): 0x410000000000000000000000000000000000001234 -> 0x1234 +// plain(address): 0x01410000000000000000000000000000000000001234 -> FAILURE +// wrapped(address): 0x1234 -> 0x1234 +// wrapped(address): 0x410000000000000000000000000000000000001234 -> 0x1234 +// wrapped(address): 0xff0000000000000000000000000000000000001234 -> 0x1234 +// wrapped(address): 0x01410000000000000000000000000000000000001234 -> FAILURE +// raw(address): 0x410000000000000000000000000000000000001234 -> 0x1234 +// wrappedPayable(address): 0x410000000000000000000000000000000000001234 -> 0x1234 +// decode(bytes): 0x20, 0x20, 0x1234 -> 0x1234 +// decode(bytes): 0x20, 0x20, 0x410000000000000000000000000000000000001234 -> 0x1234 +// decode(bytes): 0x20, 0x20, 0x01410000000000000000000000000000000000001234 -> FAILURE +// calldataArray(address[]): 0x20, 1, 0x1234 -> 0x1234 +// calldataArray(address[]): 0x20, 1, 0x410000000000000000000000000000000000001234 -> 0x1234 +// calldataArray(address[]): 0x20, 1, 0x01410000000000000000000000000000000000001234 -> FAILURE +// memoryArray(address[]): 0x20, 1, 0x1234 -> 0x1234 +// memoryArray(address[]): 0x20, 1, 0x410000000000000000000000000000000000001234 -> 0x1234 +// memoryArray(address[]): 0x20, 1, 0x01410000000000000000000000000000000000001234 -> FAILURE +// integer(uint24): 0xffffff -> 0xffffff +// integer(uint24): 0x1000000 -> FAILURE +// boolean(bool): 1 -> true +// boolean(bool): 2 -> FAILURE +// fixedBytes(bytes2): 0x1234000000000000000000000000000000000000000000000000000000000000 -> 0x1234000000000000000000000000000000000000000000000000000000000000 +// fixedBytes(bytes2): 0x1234000000000000000000000000000000000000000000000000000000000001 -> FAILURE