Skip to content

Refactor : Amortize Opcode stack bound checks#10138

Open
Demuirgos wants to merge 29 commits intomasterfrom
feature/amortized-stack-checks
Open

Refactor : Amortize Opcode stack bound checks#10138
Demuirgos wants to merge 29 commits intomasterfrom
feature/amortized-stack-checks

Conversation

@Demuirgos
Copy link
Contributor

@Demuirgos Demuirgos commented Jan 7, 2026

Fixes Closes Resolves #

Please choose one of the keywords above to refer to the issue this PR solves followed by the issue number (e.g. Fixes #000). If no issue number, remove the line. Also, remove everything marked optional that is not applicable. Remove this note after reading.

Changes

  • Removes stack checks from EvmStack
  • Delegates the responsibility of bound checks to opcodes
  • Each opcode checks for underflows and overflows when possible :
  • most opcodes have a net negative effect on stack height, so only underflow checks are needed for them
  • only push\dup\env opcodes will require stack overflow checks

Types of changes

What types of changes does your code introduce?

  • Bugfix (a non-breaking change that fixes an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (a change that causes existing functionality not to work as expected)
  • Optimization
  • Refactoring
  • Documentation update
  • Build-related changes
  • Other: Description

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No

Notes on testing

Optional. Remove if not applicable.

Documentation

Requires documentation update

  • Yes
  • No

If yes, link the PR to the docs update or the issue with the details labeled docs. Remove if not applicable.

Requires explanation in Release Notes

  • Yes
  • No

If yes, fill in the details here. Remove if not applicable.

Remarks

Optional. Remove if not applicable.

@LukaszRozmej
Copy link
Member

Will it potentially mess with tracing and need we support 2 paths?

@Demuirgos
Copy link
Contributor Author

Demuirgos commented Jan 7, 2026

Will it potentially mess with tracing and need we support 2 paths?

I am still analyzing the effect it will have, although i doubt it will change much (since it is still an opcode level change) it doesnt amortize chunks

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the EVM stack implementation by moving stack bound checking from the EvmStack class to individual opcode implementations. The goal is to amortize stack overflow and underflow checks by performing them once per opcode rather than on each push/pop operation.

Key Changes:

  • Removed bounds checking from all EvmStack push/pop methods, changing return types from bool to void
  • Added CheckStackUnderflow and CheckStackOverflow helper methods in EvmInstructions.Stack.cs
  • Updated all opcode instructions across 14 files to check stack bounds before performing stack operations
  • Added interface methods to operation types (e.g., IOpMath2Param, IOpCall) for consistent stack checking

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
EvmStack.cs Removed all bounds checking from push/pop operations; changed methods from returning bool to void; added unused PopUInt256Unchecked method; removed inline comments
EvmInstructions.Stack.cs Added CheckStackUnderflow and CheckStackOverflow helper methods; updated POP, PUSH, DUP, SWAP, and LOG instructions with explicit stack checks
EvmInstructions.Storage.cs Added stack underflow checks to TLOAD, TSTORE, SLOAD, SSTORE, MLOAD, MSTORE, MSTORE8, MCOPY, and CALLDATALOAD
EvmInstructions.Shifts.cs Added stack underflow checks to shift operations (SAR, SHL, SHR) via interface method
EvmInstructions.Math3Param.cs Added stack underflow check via interface method for 3-parameter math operations
EvmInstructions.Math2Param.cs Added stack underflow checks via interface method for 2-parameter math operations; moved stack check before gas consumption in EXP
EvmInstructions.Math1Param.cs Added stack underflow checks for 1-parameter operations, BYTE, and SIGNEXTEND
EvmInstructions.Eof.cs Added stack checks to EOF-specific instructions including RETURNDATASIZE, RETURNDATACOPY, DATALOAD, DATALOADN, DATACOPY, DUPN, SWAPN, EOFCREATE, RETURNCODE, RETURNDATALOAD, and EOFCALL
EvmInstructions.Environment.cs Added stack overflow checks to instructions that push values and underflow checks to instructions that pop values
EvmInstructions.Crypto.cs Added stack underflow check to KECCAK256 instruction
EvmInstructions.Create.cs Added stack underflow checks via interface to CREATE and CREATE2 instructions
EvmInstructions.ControlFlow.cs Added stack checks to PC, JUMP, JUMPI, REVERT, and SELFDESTRUCT instructions
EvmInstructions.CodeCopy.cs Added stack underflow checks to CODECOPY, EXTCODECOPY, and EXTCODESIZE; introduced logic bug in peephole optimization
EvmInstructions.Call.cs Added stack underflow checks via interface to CALL, CALLCODE, DELEGATECALL, STATICCALL, and RETURN instructions
EvmInstructions.Bitwise.cs Added stack underflow checks via interface to bitwise operations
DebugTracerTests.cs Updated test to check stack Head before calling PopLimbo

@Demuirgos Demuirgos marked this pull request as ready for review January 9, 2026 10:12
@LukaszRozmej LukaszRozmej requested a review from Copilot January 9, 2026 11:02
Copy link
Member

@LukaszRozmej LukaszRozmej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea
@benaadams this needs a deep review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

@LukaszRozmej
Copy link
Member

Connected to #10120 , is it still needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants