EIPs Explained

EIP-214 · Final · Core · 2017-02-13

EIP-214: New opcode STATICCALL

Adds the STATICCALL opcode (0xfa), which calls another contract while forbidding any state change in that call and its subcalls, giving safe read-only calls; Solidity uses it for view functions.

What problem does it solve?

Before 2017, every call to another contract was a full CALL. The callee could write storage, emit logs, create contracts, send ether or call back into you. Even when you only wanted to read a price or a balance, you had to trust the target not to do anything else, or audit it yourself. This uncertainty is how re-entrancy bugs happen.

EIP-214 gives the EVM a call that guarantees nothing changes. All account state is the same after a static call as it was before.

How it works

  • A new opcode, STATICCALL (0xfa), takes the same arguments as CALL minus value: gas, addr, argsOffset, argsSize, retOffset, retSize. The value is always zero.
  • The EVM gains a STATIC flag. STATICCALL sets it to true for the child call, and every nested call inherits it. When the static call returns, the flag goes back to what it was.
  • While STATIC is set, these operations throw an exception instead of running:
    • SSTORE
    • LOG0 to LOG4
    • CREATE, CREATE2
    • SELFDESTRUCT
    • CALL with non-zero value
  • CALLCODE is not counted as state-changing, even with value. Ordinary CALL with zero value, DELEGATECALL and all reads are allowed.

Existing opcodes behave exactly as before unless they run inside a static context, so old contracts are unaffected. STATICCALL shipped in the Byzantium hard fork (EIP-609).

Sample code

Solidity emits STATICCALL automatically when you call an external view or pure function:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IOracle {
    function price() external view returns (uint256);
}

contract Reader {
    function read(IOracle o) external view returns (uint256) {
        return o.price(); // compiled as STATICCALL
    }
}

The same call in assembly, with the success flag checked:

function rawRead(address target, bytes memory data) internal view returns (bytes memory out) {
    bool ok;
    assembly {
        ok := staticcall(gas(), target, add(data, 0x20), mload(data), 0, 0)
    }
    require(ok, "static call failed (or tried to modify state)");
    out = new bytes(returndatasize());
    assembly { returndatacopy(add(out, 0x20), 0, returndatasize()) }
}

Off-chain, eth_call runs code without saving any changes, but it does not set the STATIC flag. A view function is enforced on-chain only when it is called through STATICCALL.

Things to know

  • A state-changing operation inside a static context is an exceptional halt. The static call returns failure and consumes the gas given to it.
  • Because the flag is inherited, a target cannot escape the restriction by calling a third contract.
  • STATICCALL prevents state changes. It does not make results trustworthy: a read-only oracle can still return manipulated values.
  • Later opcodes respect the flag too. For example, TSTORE from EIP-1153 is forbidden in a static context.
  • Reading the full return data is done with RETURNDATASIZE and RETURNDATACOPY from EIP-211, which also shipped in Byzantium.

#evm #opcode #security #core #byzantium

Details

Authors
Vitalik Buterin <[email protected]>, Christian Reitwiessner <[email protected]>
Status
Final
Type
Standards Track / Core
Created
2017-02-13
Required by

Machine-readable: Markdown · JSON