Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

EIP-211 introduces a way for the Ethereum Virtual Machine (EVM) to handle return data whose length is unknown before a call. This article explains the return data buffer and the two opcodes used to access it.

This article translates, summarizes, and expands on the proposal below.

For more articles on Ethereum proposals, see:

The following guide covers the EVM opcodes mentioned in this article.

Overview

Before EIP-211, a contract calling another contract had to specify how much memory to reserve for the result. That made it difficult to retrieve a large result when its size could not be predicted. EIP-211 allows a contract to return variable-length data and lets the caller copy all or part of it into memory as needed.

Think of the buffer as a browser temporarily retaining a server response after a form submission. The next call replaces the previous result. This gives contracts more flexibility and can reduce gas use by avoiding unnecessary calls or allocations. Apart from introducing the new opcodes, the proposal is fully backward compatible with existing contracts.

Motivation

When a contract needs data of an unpredictable length, it can use multiple calls to determine the length and retrieve the data, but this costs gas. A generic forwarding contract is especially exposed to this problem because it can call contracts whose behavior and output size it does not know in advance.

If the size is unknown, a compiler can set the call's output area to zero length. After the call, the caller can inspect the size with RETURNDATASIZE and retrieve the required bytes with RETURNDATACOPY.

This mechanism also makes EIP-140 more useful. EIP-140 lets a contract return data when it intentionally reverts. Failure data may be larger than ordinary return data. The caller can retrieve it even after CALL indicates failure, which makes detailed error handling and debugging possible.

For more on EIP-140, see the following article.

[EIP-140] How the REVERT opcode works

Generic forwarding contracts

A generic forwarding contract passes the calldata it receives, such as a function selector and arguments, to another contract. It is generic because forwarding does not require interpreting that calldata or relying on a particular target contract's interface. It can relay arbitrary calls to another address.

Common applications include the following.

  • Meta-transactions
    • Another account pays gas on the user's behalf. Applications can use this to let new users interact with Ethereum contracts without paying gas directly.
  • Upgradeable contracts
    • A forwarding contract can direct execution to a new implementation, allowing an application to upgrade its logic despite the immutability of deployed code.
  • Delegation
    • Executing selected functions through a forwarding contract can allow another account to perform specific operations on the user's behalf.

A typical forwarding flow works as follows.

  1. The forwarding contract receives an external call.
  2. It forwards the calldata as received, without inspecting it, to a configured contract address.
  3. The target processes the calldata and returns the result to the forwarding contract.
  4. The forwarding contract returns the result to the original caller.

This pattern supports different contract interactions without requiring the forwarder to understand each application's data.

Zero length

A zero-length region has a size of 0 bytes and contains no data. When the length of a result is unknown, the caller can request a zero-length output area instead of allocating an output buffer before calling the target.

The EVM separately maintains a return data buffer for each call frame. That buffer starts empty. After a call returns, the EVM stores the result in the caller's buffer, and the buffer length reflects the length of the result.

Using a zero-length output area and copying only the bytes required after the call lets contracts handle dynamic results while avoiding unnecessary memory allocation and its gas cost.

Specification

The EVM changes took effect at the Byzantium fork (BYZANTIUM_FORK_BLKNUM). The specification adds RETURNDATASIZE and RETURNDATACOPY and changes the behavior of call-like instructions such as CALL, CREATE, and DELEGATECALL.

Call frames

A call frame is a data structure that holds the execution context for a call. In a stack-based execution environment such as the EVM, a call to another contract creates a new execution frame.

A conventional function call frame typically provides the following.

  • Function arguments
    • The values passed to the function.
  • Local variables
    • The values defined within the function.
  • Return address
    • Where execution resumes after the function returns.
  • A link to the preceding frame
    • A reference to the previous frame that helps represent the execution stack.

In the EVM, an inter-contract call such as CALL or DELEGATECALL creates a frame for the callee. Execution takes place in that frame, which holds the necessary execution data, including its own return data buffer for results from calls it makes.

Call frames structure calls and returns and provide the execution context for interactions and state changes across contracts.

Each EVM call frame starts with a new, empty return data buffer. Executing a call-like opcode clears that buffer and sets its size to 0. When the call finishes, its complete return data, or failure data as described in EIP-140, is written into the caller's buffer and the size is updated. CREATE and CREATE2 are exceptions: they leave the buffer empty on success and make failure data available on failure.

An implementation can reuse the buffer across call frames because at most one logical return data buffer needs to contain data at any time.

A call frame holds the state and data associated with a contract's execution. It includes a logical return data buffer for results from calls that the frame makes.

Sharing the buffer across frames means reusing the same underlying storage, instead of allocating new backing storage for each frame. This can reduce memory consumption.

Suppose a caller has just received return data. If it makes another call, that instruction clears its existing buffer before the callee starts executing. The new frame begins with an empty buffer. When the call finishes, the caller receives the new result.

As a result, even when several call frames are active, at most one return data buffer needs to retain data. Sharing the backing storage uses memory more efficiently while preserving each frame's logical behavior.

RETURNDATASIZE (0x3d) pushes the length of the return data buffer onto the stack. It costs 2 gas, the same as CALLDATASIZE.

RETURNDATACOPY (0x3e) works like CALLDATACOPY, using the return data buffer as the source. If the source offset plus the requested length exceeds RETURNDATASIZE, or the addition overflows, the current call halts with an out-of-gas exception. Copying 0 bytes at the end of the buffer succeeds. Copying zero bytes starting one byte beyond the end raises an exception.

The buffer temporarily holds data returned by calls to other contracts. The boundary cases are as follows.

  • Copying 0 bytes at the end of the buffer
    • The operation copies no bytes and completes successfully.
  • Attempting to copy 0 bytes one byte beyond the end
    • The requested offset is out of bounds even though the length is zero, so the operation raises an exception.

The EVM checks the requested range even when no bytes will be read. This prevents out-of-bounds accesses.

The copy charge for RETURNDATACOPY is 3 plus 3 times the number of 32-byte words copied, rounded up. This is the same formula used for CALLDATACOPY.

bash
3 + 3 * ceil(amount / 32)

These changes allow the EVM to handle the result of a call efficiently even when its length is unknown beforehand.

Additional notes

Alternative ways to return dynamic data were considered, but they all required deducting gas from the call opcode, complicating the implementation and specification. EIP-211 follows the existing model for handling calldata. The eWASM architecture already handles return data in the same way.

The EVM needs to retain the return data until the current frame returns or makes another call. The memory holding the result has already been paid for as part of the callee's execution. A client can copy the result into a dedicated buffer or retain the callee's memory until the next call.

Retaining the callee's memory until the next call-like opcode does not increase the peak compared with allocating the caller's memory before the call, provided the change does not affect gas costs.

RETURNDATASIZE and RETURNDATACOPY also share an opcode nibble block with CALLDATASIZE and CALLDATACOPY. This groups related operations and lets the EVM handle variable-length results without complicating the call opcodes.

Nibble blocks

A nibble is 4 bits, or half of an 8-bit byte. It holds four binary digits, equivalent to one hexadecimal digit from 0 to F, and represents values from 0 through 15.

Nibbles are useful in low-level programming for representing and manipulating data, including memory addresses. Instruction encodings can use a nibble to identify an operation or register.

An opcode is a machine instruction that a CPU or virtual machine, such as the EVM, recognizes and executes. An instruction set can group opcodes by a shared nibble to organize related operations and make the instruction set easier to design and analyze.

For example, CALLDATASIZE and CALLDATACOPY appear in the same EVM nibble block. Both handle calldata. This grouping makes related instructions easier to identify.

Compatibility

The proposal adds two opcodes. It is fully backward compatible in all other respects.

Citation

Christian Reitwiessner chris@ethereum.org, "EIP-211: New opcodes: RETURNDATASIZE and RETURNDATACOPY," Ethereum Improvement Proposals, no. 211, February 2017. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-211.

Conclusion

EIP-211 gives EVM callers a way to handle variable-length results. RETURNDATASIZE reports how many bytes are available, and RETURNDATACOPY copies the bytes the caller needs.

Questions are welcome by direct message.

Twitter @cardene777

You can also find my work here.

https://chaldene.net/