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

After EIP-1051

Who is affected

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.

Original EIP · ← EIPs