EIPs/EIPS/eip-2315.md

205 lines
8.1 KiB
Markdown
Raw Normal View History

---
eip: 2315
title: Simple Subroutines for the EVM
status: Withdrawn
type: Standards Track
category: Core
author: Greg Colvin <greg@colvin.org>, Martin Holst Swende (@holiman)
discussions-to: https://ethereum-magicians.org/t/eip-2315-simple-subroutines-for-the-evm/3941
created: 2019-10-17
---
## Abstract
This proposal introduces three opcodes to support subroutines: `BEGINSUB`, `JUMPSUB` and `RETURNSUB`.
## Motivation
The EVM does not provide subroutines as a primitive. Instead, calls can be synthesized by fetching and pushing the current program counter on the data stack and jumping to the subroutine address; returns can be synthesized by contriving to get the return address back to the top of stack and jumping back to it. Sometimes complex calling conventions are then needed to use the same stack for computation and control flow. Memory allows for simpler conventions but still costs gas. Eschewing subroutines in user code is the least costly -- but also the most failure-prone.
Over the course of 30 years the computer industry struggled with this complexity and cost
and settled in on opcodes to directly support subroutines. These are provided in some form by most all physical and virtual machines going back at least 50 years.
Our design is modeled on the original Forth two-stack machine of 1970. The data stack is supplemented with a return stack to provide simple support for subroutines, as specified below.
## Specification
We introduce one more stack into the EVM in addition to the existing `data stack` which we call the `return stack`. The `return stack` is limited to `1023` items.
#### `BEGINSUB`
Marks the entry point to a subroutine. Attempted execution of a `BEGINSUB` causes an _`abort`_: terminate execution with an `OOG` (Out Of Gas) exception.
#### `JUMPSUB`
Transfers control to a subroutine.
2020-05-15 05:17:17 -04:00
1. Pop the `location` off the `data stack`.
2. If the opcode at `location` is not a `BEGINSUB` _`abort`_.
3. If the `return stack` already has `1023` items _`abort`_.
4. Push the current `pc + 1` to the `return stack`.
5. Set `pc` to `location + 1`.
* _pops one item off the `data stack`_
* _pushes one item on the `return stack`_
#### `RETURNSUB`
Returns control to the caller of a subroutine.
1. If the `return stack` is empty _`abort`_.
2. Pop `pc` off the `return stack`.
* _pops one item off the `return stack`_
_Note 1: If a resulting `pc` to be executed is beyond the last instruction then the opcode is implicitly a `STOP`, which is not an error._
_Note 2: Values popped off the `return stack` do not need to be validated, since they are alterable only by `JUMPSUB` and `RETURNSUB`._
_Note 3: The description above lays out the semantics of this feature in terms of a `return stack`. But the actual state of the `return stack` is not observable by EVM code or consensus-critical to the protocol. (For example, a node implementor may code `JUMPSUB` to unobservably push `pc` on the `return stack` rather than `pc + 1`, which is allowed so long as `RETURNSUB` observably returns control to the `pc + 1` location.)_
2020-05-15 05:17:17 -04:00
## Rationale
This is almost the smallest change that provides native subroutines without breaking backwards compatibility.
## Backwards Compatibility
These changes do not affect the semantics of existing EVM code.
# Test Cases
### Simple routine
This should jump into a subroutine, back out and stop.
2020-05-29 23:06:28 +02:00
Bytecode: `0x60045e005c5d` (`PUSH1 0x04, JUMPSUB, STOP, BEGINSUB, RETURNSUB`)
| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | PUSH1 | 3 | [] | [] |
2020-05-29 17:00:56 +02:00
| 2 | JUMPSUB | 10 | [4] | [] |
| 5 | RETURNSUB | 5 | [] | [ 2] |
| 3 | STOP | 0 | [] | [] |
2020-05-29 17:00:56 +02:00
Output: 0x
Consumed gas: `18`
### Two levels of subroutines
This should execute fine, going into one two depths of subroutines
2020-05-29 17:00:56 +02:00
2020-05-29 23:06:28 +02:00
Bytecode: `0x6800000000000000000c5e005c60115e5d5c5d` (`PUSH9 0x00000000000000000c, JUMPSUB, STOP, BEGINSUB, PUSH1 0x11, JUMPSUB, RETURNSUB, BEGINSUB, RETURNSUB`)
| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | PUSH9 | 3 | [] | [] |
2020-05-29 17:00:56 +02:00
| 10 | JUMPSUB | 10 | [12] | [] |
| 13 | PUSH1 | 3 | [] | [10] |
2020-05-29 17:00:56 +02:00
| 15 | JUMPSUB | 10 | [17] | [10] |
| 18 | RETURNSUB | 5 | [] | [10,15] |
| 16 | RETURNSUB | 5 | [] | [10] |
| 11 | STOP | 0 | [] | [] |
2020-05-29 17:00:56 +02:00
Consumed gas: `36`
### Failure 1: invalid jump
2020-05-29 17:00:56 +02:00
This should fail, since the given location is outside of the code-range. The code is the same as previous example,
except that the pushed location is `0x01000000000000000c` instead of `0x0c`.
2020-05-29 23:06:28 +02:00
Bytecode: `0x6801000000000000000c5e005c60115e5d5c5d` (`PUSH9 0x01000000000000000c, JUMPSUB, STOP, BEGINSUB, PUSH1 0x11, JUMPSUB, RETURNSUB, BEGINSUB, RETURNSUB`)
| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | PUSH9 | 3 | [] | [] |
2020-05-29 17:00:56 +02:00
| 10 | JUMPSUB | 10 |[18446744073709551628] | [] |
```
2020-05-29 17:00:56 +02:00
Error: at pc=10, op=JUMPSUB: invalid jump destination
```
### Failure 2: shallow `return stack`
2020-05-29 17:00:56 +02:00
This should fail at first opcode, due to shallow `return_stack`
2020-05-29 23:06:28 +02:00
Bytecode: `0x5d5858` (`RETURNSUB, PC, PC`)
| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
2020-05-29 17:00:56 +02:00
| 0 | RETURNSUB | 5 | [] | [] |
```
2020-05-29 17:00:56 +02:00
Error: at pc=0, op=RETURNSUB: invalid retsub
```
2020-04-16 13:30:18 +02:00
### Subroutine at end of code
In this example. the JUMPSUB is on the last byte of code. When the subroutine returns, it should hit the 'virtual stop' _after_ the bytecode, and not exit with error
2020-05-29 23:06:28 +02:00
Bytecode: `0x6005565c5d5b60035e` (`PUSH1 0x05, JUMP, BEGINSUB, RETURNSUB, JUMPDEST, PUSH1 0x03, JUMPSUB`)
2020-04-16 13:30:18 +02:00
| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
| 0 | PUSH1 | 3 | [] | [] |
| 2 | JUMP | 8 | [5] | [] |
| 5 | JUMPDEST | 1 | [] | [] |
| 6 | PUSH1 | 3 | [] | [] |
2020-05-29 17:00:56 +02:00
| 8 | JUMPSUB | 10 | [3] | [] |
| 4 | RETURNSUB | 5 | [] | [ 8] |
2020-04-16 13:30:18 +02:00
| 9 | STOP | 0 | [] | [] |
2020-05-29 17:00:56 +02:00
Consumed gas: `30`
### Error on "walk-into-subroutine"
In this example, the code 'walks' into a subroutine, which is not allowed, and causes an error
2020-05-29 23:06:28 +02:00
Bytecode: `0x5c5d00` (`BEGINSUB, RETURNSUB, STOP`)
2020-05-29 17:00:56 +02:00
| Pc | Op | Cost | Stack | RStack |
|-------|-------------|------|-----------|-----------|
2020-05-29 17:00:56 +02:00
| 0 | BEGINSUB | 2 | [] | [] |
```
Error: at pc=0, op=BEGINSUB: invalid subroutine entry
```
2020-05-29 17:00:56 +02:00
**Note 5**: The content of the error message, (`invalid subroutine entry`) is implementation-specific.
2020-04-16 13:30:18 +02:00
## Implementations
2020-05-29 17:00:56 +02:00
Three clients have implemented this (or an earlier version of) this proposal:
- [geth](https://github.com/ethereum/go-ethereum/pull/20619) .
- [besu](https://github.com/hyperledger/besu/pull/717), and
- [openethereum](https://github.com/openethereum/openethereum/pull/11629).
### Costs and Codes
2020-05-29 17:00:56 +02:00
We suggest that the cost of
- `BEGINSUB` be _base_ (`2`)
- Although formally specified, the cost of `BEGINSUB` does not matter in practice, since `BEGINSUB` never executes without error.
- `JUMPSUB` be _high_ (`10`)
- This is the same as `JUMPI`, and `2` more than `JUMP`.
- `RETURNSUB` be _low_ (`5`).
Benchmarking might be needed to tell if the costs are well-balanced.
We suggest the following opcodes:
```
2020-05-29 23:06:28 +02:00
0x5c BEGINSUB
0x5d RETURNSUB
0x5e JUMPSUB
```
## Security Considerations
These changes do introduce new flow control instructions, so any software which does static/dynamic analysis of evm-code
needs to be modified accordingly. The `JUMPSUB` semantics are similar to `JUMP` (but jumping to a `BEGINSUB`), whereas the `RETURNSUB` instruction is different, since it can 'land' on any opcode (but the possible destinations can be statically inferred).
**Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).**