Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EIP-4750: Add EOF Modules #4919

Closed
wants to merge 19 commits into from
Closed
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 47 additions & 55 deletions EIPS/eip-4750.md
Original file line number Diff line number Diff line change
@@ -1,100 +1,89 @@
---
eip: 4750
title: EOF - Functions
description: Individual sections for functions with `CALLF` and `RETF` instructions
title: EOF - Modules and Procedures
description: Individual sections for modules with CALLPROC and RETURNPROC instructions
author: Andrei Maiboroda (@gumb0), Alex Beregszaszi (@axic), Paweł Bylica (@chfast)
discussions-to: https://ethereum-magicians.org/t/eip-4750-eof-functions/8195
status: Draft
type: Standards Track
category: Core
created: 2022-01-10
requires: 3540, 3670
requires: 2315, 3540, 3670, 3779, 4200
---

## Abstract

Introduce the ability to have several code sections in EOF-formatted ([EIP-3540](./eip-3540.md)) bytecode, each one representing a separate subroutine/function. Two new opcodes,`CALLF` and `RETF`, are introduced to call and return from such a function.
Introduce the ability to have several code sections in EOF-formatted [EIP-3540](./eip-3540.md) bytecode, each one representing a separate module.

Each code section has a corresponding type section which specifies one or more procedural entry points to that module.

Two new opcodes,`CALLPROC` and `RETURNPROC`, are introduced to call and return from these procedures.

## Motivation

Currently in the EVM everything is a dynamic jump. Languages like Solidity generate most jumps in a static manner (i.e. the destination is pushed to the stack right before, `PUSHn .. JUMP`). Unfortunately however this cannot be used by most EVM interpreters, because of added requirement of validation/analysis. This also restricts them from making optimisations and potentially reducing the cost of jumps.

[EIP-4200](./eip-4200.md) introduces static jump instructions, which remove the need for *most* dynamic jump use cases, but not everything can be solved with them.
Dynamic jumps also impede the validation of safety properties promised by [EIP-3779](./eip-3779.md), for programs whose jumps are all static: that no valid program will encounter an exceptional halting condition except via lack of gas or recursive stack overflow.

This EIP aims to remove the need for dynamic jumps as it offers the most important feature those are used for: calling into and returning from procedures. While it removes the need, it does not disallow those instructions.

[EIP-4200: Static relative jumps](./eip-4200.md) introduces static jump instructions, which remove the need for *most* dynamic jump use cases, but not everything can be solved with them.

[EIP-2315](./eip-2315.md) provides for calling into and returning from the subroutine. But only a bare mechanism is provided – no structure is imposed on the code.

This EIP aims to remove the need for dynamic jumps as it offers the most important feature those are used for: calling into and returning from functions. While it removes the need, it does not disallow those instructions.
This proposal goes further in providing a typed, modular structure. Modules are mapped to multiple code sections, and procedures are mapped to typed entry points in each code section. It aims to improve analysis opportunities by encoding the number of inputs and outputs as the type for each given procedure, and isolating the stack of each procedure (i.e. a procedure cannot read the stack of the caller/callee).

Furthermore it aims to improve analysis opportunities by encoding the number of inputs and outputs for each given function, and isolating the stack of each function (i.e. a function cannot read the stack of the caller/callee).
We are following Intel in calling the low level concept subroutines and the higher level concept procedures. The distinction is that subroutines are little more than a jump that knows where it came from, whereas procedures have a defined interface and manage memory as a stack. So this EIP defines typed procedures in terms of the untyped subroutines of EIP-2315.
gcolvin marked this conversation as resolved.
Show resolved Hide resolved

## Specification

### EOF Code and Type Sections

We propose to allow for multiple `EOF` *code* and corresponding *type* sections. Code sections after the first can be entered only via a `CALLPROC` to one of the entry point procedures specified in its corresponding type section, and can be left only via `RETURNPROC`.

`CALLPROC` and `RETURNPROC` are in turn defined in terms of `JUMPSUB` and `RETURNSUB` from [EIP-2315](./eip-2315.md).

### EOF container changes

1. The requirement of [EIP-3540](./eip-3540.md) "Exactly one code section MUST be present." is relaxed to "At least one code section MUST be present.", i.e. multiple code sections (`kind = 1`) are allowed.
2. Total number of code sections MUST NOT exceed 1024.
3. All code sections MUST precede a data section, if data section is present.
4. New section with `kind = 3` is introduced called the *type section*.
5. Exactly one type section MAY be present.
6. The type section, if present, MUST directly precede all code sections.
7. The type section, if present, contains a sequence of pairs of bytes: first byte in a pair encodes number of inputs, and second byte encodes number of outputs of the code section with the same index. *Note:* This implies that there is a limit of 256 stack for the input and in the output.
8. Therefore type section size MUST be `n * 2` bytes, where `n` is the number of code sections.
9. First code section MUST have 0 inputs and 0 outputs.
10. Type section MAY be omitted if only a single code
section is present. In that case it implicitly defines 0 inputs and 0 outputs for this code section.
5. Exactly one type section MUST be present for each code section.
6. The type sections MUST directly precede all code sections.
7. The type section contains a sequence of triples:
* the first uint8 encodes number of inputs
* a second unint8 encodes number of outputs, and
* a final uint16 encodes the offset of the entry point, relative to the beginning of each code section.
* *Note: This implies that there is a limit of 256 stack for the input and in the output.*
9. First code section MUST have 1 entry point at offset 0 with 0 inputs and 0 outputs.

To summarize, a well-formed EOF bytecode will have the following format:
```
bytecode := magic, version, [type_section_header], (code_section_header)+, [data_section_header], 0, [type_section_contents], (code_section_contents)+, [data_section_contents]
bytecode := format, magic, version, (type_section_header)+, (code_section_header)+, [data_section_header], 0, (type_section_contents)+, (code_section_contents)+, [data_section_contents]

type_section_header := 3, number_of_code_sections * 2 # section kind and size
type_section_contents := 0, 0, code_section_1_inputs, code_section_1_outputs, code_section_2_inputs, code_section_2_outputs, ..., code_section_n_inputs, code_section_n_outputs
type_section_header := 3, number_of_code_sections * 4 # section kind and size
type_section_contents := 0, 0, code_section_1_inputs, code_section_1_outputs, code_section_2_inputs, code_section_2_outputs, ..., code_section_n_inputs, code_section_n_outputs, code_section_entry_offest
```

### New execution state in EVM

A return stack is introduced, separate from the data stack. It is a stack of items representing execution state to return to after function execution is finished. Each item is comprised of: code section index, offset in the code section (PC value), calling function stack height.

Note: Implementations are free to choose particular encoding for a stack item. In the specification below we assume that representation is three unsigned integers: `code_section_index`, `offset`, `stack_height`.

The return stack is limited to a maximum 1024 items.

Additionally, EVM keeps track of the index of currently executing section - `current_section_index`.

### New instructions

We introduce two new instructions:

1. `CALLF` (`0x5e`)
2. `RETF` (`0x5f`)
1. **`CALLPROC`** (`0x5e`)
2. **`RETURNPROC`** (`0x5f`)

If the code is legacy bytecode, both of these instructions result in an *exceptional halt*. (*Note: This means no change to behaviour.*)

First we define several helper values:
- `caller_stack_height = return_stack.top().stack_height` - stack height value saved in the top item of return stack
- `type[i].inputs = type_section_contents[i * 2]` - number of inputs of ith section
- `type[i].outputs = type_section_contents[i * 2 + 1]` - number of outputs of ith section

If the code is valid EOF1, the following execution rules apply:

#### `CALLF`
#### CALLPROC (0x5e) dest_section: uint8, dest_proc: uint8

1. Has one immediate argument,`code_section_index`, encoded as a 16-bit unsigned big-endian value.
2. If data stack has less than `caller_stack_height + type[code_section_index].inputs`, execution results in exceptional halt.
3. If return stack already has `1024` items, execution results in exceptional halt.
4. Pops nothing and pushes nothing to data stack.
5. Pushes to return stack an item:
```
(code_section_index = current_section_index,
offset = PC_post_instruction,
stack_height = data_stack.height - types[code_section_index].inputs)
```

Under `PC_post_instruction` we mean the PC position after the entire immediate argument of `CALLF`. Data stack height is saved as it was before function inputs were pushed.
> Transfer control as if via `JUMPSUB` to the offset of the Nth (N=*dest_proc*) _procedure_ in the Mth(M=*dest_section*) _section_ of the code. _Section 0_ is the current code section, any other code sections are indexed starting at _1_.

*Note:* Code validation rules of [EIP-3670](./eip-3670.md) guarantee there is always an instruction following `CALLF` (since terminating instruction is required to be final one in the section), therefore `PC_post_instruction` always points to an instruction inside section bounds.

6. Sets `current_section_index` to `code_section_index` and `PC` to `0`, and execution continues in the called section.
#### RETURNPROC (0x??)

#### `RETF`
> Return control to the calling procedure as if via `RETURNSUB`.

1. Does not have immediate arguments.
2. If data stack has less than `caller_stack_height + types[code_section_index].outputs`, execution results in exceptional halt.
Expand All @@ -117,8 +106,9 @@ In addition to container format validation rules above, we extend code section v

1. Execution starts at the first byte of the first code section, and PC is set to 0.
2. Return stack is initialized to contain one item: `(code_section_index = 0, offset = 0, stack_height = 0)`
3. Destinations of jumps are allowed only to be inside current code section. `JUMP` and `JUMPI` result in exceptional halt when destination is outside of current section bounds.
4. If any instruction would access a data stack item below `caller_stack_height`, execution results in exceptional halt. This rule replaces the old stack underflow check.
3. Destinations of jumps are allowed only to be inside the current code section. `JUMP`, `JUMPI`, `RJUMP`, and `RJUMPI` result in an invalid program when the destination is outside of current section bounds.

*Note: That jumps are valid must be shown at validation time.*

#### Implications on the JUMPDEST analysis

Expand All @@ -127,11 +117,13 @@ In addition to container format validation rules above, we extend code section v

## Rationale

Each code section is a module with defined procedural interfaces, identified with address-independent indexes. Given that all jumps within a section are relative, and cannot jump out of a section, this allows external tools to compose contracts out of pre-compiled code sections.

### `RETF` in the top frame ends execution vs exceptionally halts

Alternative logic for executing `RETF` in the top frame could be to exceptionally halt execution, because there is arguably no caller for the starting function. This would mean that return stack is initialized as empty, and `RETF` exceptionally aborts when return stack is empty.
Alternative logic for executing `RETF` in the top frame could be to exceptionally halt execution, because there is arguably no caller for the starting procedure. This would mean that return stack is initialized as empty, and `RETF` exceptionally aborts when return stack is empty.

We have decided in favor of always having at least one item in the return stack, because it allows to avoid having a special case for empty stack in the interpreter loop stack underflow check. We keep the stack underflow rule general by having `caller_stack_height = 0` in the top frame.
We have decided in favor of always having at least one item in the return stack, because it allows to avoid having a special case for empty stack in the interpreter loop stack underflow check. We keep the stack underflow.

## Backwards Compatibility

Expand Down