EIP-1051 EVM overflow checking feature (Overflow checking for the EVM)
No call decision · EVM & execution · EIP status Stagnant · created 2018-05-02
A proposal to add a flag that automatically records whether an overflow occurred during the EVM's addition, subtraction, and multiplication operations, along with two new instructions (OFV, SOVF) to read and reset it.
Why it is needed
Because the EVM treats 256-bit integers as remainders modulo 2^256, if an addition or multiplication result becomes too large, or a subtraction result goes negative, it silently returns an incorrect value without an error. Many contracts today must insert explicit comparison code for each individual operation to prevent this kind of overflow, which increases gas cost, and omitting it leads to security vulnerabilities. This proposal brings the carry/overflow flag approach used by real CPUs into the EVM, recording whether an overflow occurred in state each time an operation happens, so it can be checked all at once whenever needed. Because the existing behavior of ADD/SUB/MUL itself is left unchanged, compatibility with already-deployed contracts is not broken.
What changes
Today
- Even when ADD/SUB/MUL overflows, the EVM does not report it and silently returns an incorrect value (mod 2^256)
- To prevent overflow, a contract must write explicit comparison and check code for each operation
- Such safeguard code increases gas cost and code complexity
After EIP-1051
- When ADD/SUB/MUL executes, whether an overflow occurred is automatically recorded in the ovf (unsigned) and sovf (signed) flags
- The new instructions OFV (0x0c) and SOVF (0x0d) let the flags be read and reset at once, whenever needed
- Checks can be done in bulk afterward instead of inserting check code for every single operation
Who is affected
- Smart contract developers: Overflow checks can be handled more cheaply and simply with OFV/SOVF instead of writing them for every operation.
- Language and compiler teams: This creates room for languages like Solidity to change their arithmetic safety check code to use this opcode.
- Client and EVM implementation teams: The ovf/sovf flag state and the two opcodes must be newly implemented in the EVM executor.
In depth
Conditions under which the ovf flag is set
ovf records overflow when values are treated as unsigned integers. ovf is set when the ideal result of ADD exceeds 2^256 - 1, when the ideal result of SUB is less than 0, or when the ideal result of MUL exceeds 2^256 - 1.
Conditions under which the sovf flag is set
sovf is set together with ovf in every case where ovf is set, and additionally catches overflow when values are treated as signed integers. sovf is set when the MSBs of the two inputs to ADD are the same but the result's MSB differs, when the result's MSB in SUB becomes equal to that of the second operand (the subtrahend), and in MUL when both operands are positive but the result is negative, when both are negative but the result is negative, or when a positive and a negative are multiplied but the result is positive.
The OFV (0x0c) instruction
This is an opcode that pops no value from the stack. When executed, it pushes the current ovf flag value (1 or 0) onto the stack, and immediately afterward resets the ovf flag to false. In other words, it is used to check, just once, whether an unsigned overflow has accumulated up to the point OFV is called.
The SOVF (0x0d) instruction
With the same structure as OFV, it pushes the sovf flag value onto the stack and resets sovf to false. Since ovf and sovf are separate flags, OFV and SOVF must also be called independently; calling one does not reset the other flag.
Why a flag approach instead of throw/revert
An approach of halting execution immediately with a throw or revert when an overflow occurs could also have been considered, but that would fix the way overflow is handled to a single method. Instead, this proposal brings the carry/overflow flags used by real CPUs directly into the EVM, letting a contract choose for itself how to respond when an overflow occurs (ignore it, revert, run different logic, and so on). It also leaves the behavior of the existing ADD/SUB/MUL operations unchanged, so no compatibility problem arises with existing contracts.
Why unsigned and signed overflow are kept separate
A value may be an overflow by signed-integer standards while not being an overflow by unsigned-integer standards. For example, the result of adding two negative numbers may fall outside the signed range while still fitting within the unsigned 256-bit range. Because of this difference, ovf and sovf were separated into distinct flags.
Current Status and Remaining Work
This EIP is a Standards Track (Core) proposal created on 2018-05-02, and its status is Stagnant. That is, it is no longer being actively discussed, and the Test Cases and Implementation sections are also left as TBD in the source text, so neither real-world adoption nor inclusion in a fork has been confirmed.