EIP-3 · Withdrawn · Core · 2015-11-19
EIP-3: Addition of CALLDEPTH opcode
A withdrawn 2015 proposal for a CALLDEPTH opcode that would tell a contract how much call-stack depth was left, as a cheap defence against call-stack attacks.
What problem does it solve?
In early Ethereum, contracts could only call each other 1024 levels deep (the spec text cites 256, but the limit was 1024). Once that limit was reached, any further CALL simply failed.
This enabled a call-stack attack. An attacker first builds up a deep chain of calls, for example with recursion, and then calls the victim contract. Every call the victim makes from there fails. If the victim does not check whether its calls succeeded, it may carry on as if they had worked. The spec gives an example: a contract that calls B and C and then pays a reward to its caller. The attacker makes both inner calls fail and still collects the reward.
Developers had two defences: always check return values, or probe the depth on purpose by making test calls. The probing approach existed as a library, but it cost a lot of gas. EIP-3 proposed a direct, cheap answer.
How it works
The proposal is a single new opcode:
CALLDEPTHpushes the remaining call-stack depth onto the stack.- A value of
0means the stack is exhausted and no further calls can succeed.
The EVM already tracks the current depth and the limit internally. The opcode would only expose that number to contract code. The spec does not assign an opcode byte or a gas cost.
Sample code
Illustrative pseudocode only. CALLDEPTH was never added to the EVM, so this does not compile.
// Hypothetical: refuse to run if too little call depth remains
function payout() external {
// calldepth() would wrap the proposed CALLDEPTH opcode
require(calldepth() >= 2, "not enough call depth for B and C");
B.doWork();
C.doWork();
payable(msg.sender).transfer(reward);
}
The defence that actually works today is to check every call's result:
(bool ok, ) = address(b).call(data);
require(ok, "call to B failed");
Things to know
- This proposal is Withdrawn and was never implemented. The EVM has no
CALLDEPTHopcode. - The attack was later made impractical in another way. EIP-150 (Tangerine Whistle) changed calls so a caller can forward at most 63/64 of its remaining gas. Reaching a depth of 1024 now takes far more gas than any block contains.
- Checking the success flag of low-level calls is still essential. Solidity's high-level calls revert automatically on failure, and EIP-140
REVERTmakes failures easier to report. - Do not confuse the call stack (nested message calls) with the EVM's data stack of 1024 words. They are separate limits.