EIP-7705 · Stagnant · Core · 2024-05-09
EIP-7705: NONREENTRANT and REENTRANT opcodes
Proposes two opcodes, NONREENTRANT and REENTRANT, that let a contract cheaply lock itself against incoming calls for the rest of a transaction.
What problem does it solve?
Reentrancy, where a contract calls out and the callee calls back in before the first call finishes, is behind a large share of stolen funds on EVM chains, including the 2016 DAO hack. The usual defence is a reentrancy guard: write a "locked" flag to storage on entry and clear it on exit.
That costs gas on every protected function. Transient storage (EIP-1153) made guards cheaper, but the author argues it is still not cheap enough for developers to use it everywhere by default. EIP-7705 proposes to make the guard a native EVM feature.
How it works
Two opcodes are added:
| Opcode | Byte | Gas | Effect |
|---|---|---|---|
NONREENTRANT |
0xF6 |
5 | Set the current contract's nonreentrancy flag |
REENTRANT |
0xF7 |
5 | Clear the flag |
While the flag is set, any CALL, STATICCALL or DELEGATECALL into that contract behaves as if the callee executed a single REVERT.
Rules:
- Both opcodes are idempotent (setting an already-set flag is a no-op).
- The flag lives only for the current transaction and is cleared at the end of it.
- If a call frame reverts or halts exceptionally, the flag rolls back to its value before that call, just like other state.
The spec mentions an alternative single-opcode design, a NONREENTRANT that takes a stack value to set or clear the flag, open for feedback.
Sample code
Today's guard with transient storage, which the opcodes would replace:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Vault {
modifier nonReentrant() {
assembly {
if tload(0) { revert(0, 0) }
tstore(0, 1)
}
_;
assembly { tstore(0, 0) }
}
function withdraw(uint256 amount) external nonReentrant {
// ... update balances, then send ETH
}
}
What it would look like with EIP-7705, in pseudo-assembly (no compiler supports these opcodes):
NONREENTRANT ; 0xF6 - any call back into this contract now reverts
... ; checks, effects, external call
REENTRANT ; 0xF7 - allow calls again
Things to know
- Status: Stagnant. It has not been scheduled for any network upgrade, and its test cases, reference implementation and security considerations sections are empty.
- Note the semantics: the lock applies to the whole contract, not to a single function, and blocks all incoming calls, including view-style
STATICCALLs, while set. - Until something like this ships, use a well-reviewed guard (for example OpenZeppelin's
ReentrancyGuardor its transient variant) and follow checks-effects-interactions.