evmasm codegen rotates the stack slots of a growing nested-tuple component
Originally reported by Gzeon.
Description
Solidity's default code generator can deliver the values of a tuple assignment to the wrong variables, and the compiler says nothing about it.
A tuple assignment must bind each right-hand component to the left-hand component at the same index. When a component occupies at least one stack slot and grows during type conversion, legacy codegen writes its converted slots back rotated, so the pair arrives exchanged. The trigger is an inner tuple that pairs an inline string, hex or unicode literal with a sibling occupying one or more stack slots: ((z, a), b) = (("x", 111), 222) with bytes32 z; uint256 a assigns 111 to z and bytes32("x") to a. When that sibling is a function parameter, the caller's uint256 becomes the runtime memory pointer of a string memory local the source fixes to a constant. Everything the contract then does with that string — ABI-encoding it, hashing it, copying it to storage — dereferences an offset the caller chose.
The affected pipeline is the default one: solc with no flags emits this bytecode, and --via-ir is correct.
Environment
- Compiler version: 0.8.36
- Compilation pipeline (legacy, IR, SSA CFG): legacy (evmasm)
- Target EVM version (as per compiler settings): default
- Framework/IDE (e.g. Foundry, Hardhat, Remix):
- EVM execution environment / backend / blockchain client:
- Operating system: GNU/Linux
Steps to Reproduce
// SPDX-License-Identifier: GPL-3.0
// Same construct as C.silent(), but 0.6-compatible, to locate the regression
// boundary. Correct output has `7800...` as its first 32-byte word.
pragma solidity >=0.6.0;
contract B {
function silent() external pure returns (bytes32 z, uint256 a, uint256 b) {
((z, a), b) = (("x", 111), 222);
}
}((z, a), b) = (("x", 111), 222) with bytes32 z; uint256 a returns z = 111 and a = bytes32("x") — the two components arrive exchanged, and the compiler emits no error or warning.
Root cause
CompilerUtils::convertType, tuple case — CompilerUtils.cpp#L1276-L1292 at the tested release 8a079791d9cca7a6c03fd6a8429b93aa3bddefed. Each component is copied to the stack top, converted, then written back over the S = sourceSize slots it came from, where T = targetSize:
if (sourceSize > 0 || targetSize > 0)
{
// Move it back into its place.
for (unsigned j = 0; j < std::min(sourceSize, targetSize); ++j)
m_context <<
swapInstruction(depth + targetSize - sourceSize) << // <-- writes back from the TOP slot downward
Instruction::POP;
// Value shrank
for (unsigned j = targetSize; j < sourceSize; ++j)
{
moveToStackTop(depth + targetSize - sourceSize, 1);
m_context << Instruction::POP;
}
// Value grew
if (targetSize > sourceSize)
moveIntoStack(depth - sourceSize, targetSize - sourceSize); // <-- relocates the FIRST T-S slots, not the last
}The write-back loop runs min(S, T) times and walks the converted value from its top slot downward. For T > S it therefore deposits the last S converted slots into the source region and leaves the first T - S on the stack top. The growth step then moves that leftover block down past the components not yet processed, using CompilerUtils::moveIntoStack. The component ends up as c[T-S] … c[T-1], c[0] … c[T-S-1] — its intended slots rotated left by T - S.
Source: argotorg/solidity