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

Paged memory model #489

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/vm/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ A complete instruction set of the Fuel VM is documented in [the following page](

## VM Initialization

Every time the VM runs, a single monolithic memory of size `VM_MAX_RAM` bytes is allocated, indexed by individual byte. A stack and heap memory model is used, allowing for dynamic memory allocation in higher-level languages. The stack begins at `0` and grows upward. The heap begins at `VM_MAX_RAM - 1` and grows downward.
The VM has `VM_MAX_RAM` bytes of ram, indexed by individual byte. A stack and heap memory model is used, allowing for dynamic memory allocation in higher-level languages. The stack begins at `0` and grows upward. The heap begins at `VM_MAX_RAM - 1` and grows downward. Unused memory is initialized to zero.

Memory is gas-metered by usage. It's allocated in 16 KiB pages, which are transparently creted when either stack or heap grows beyond the current page. Both stack and heap are contiguous, and pages for those are allocated separately. The stack and heap are not allowed to overlap, but it's possible for them to share the last available page of memory.
Copy link
Member

Choose a reason for hiding this comment

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

@Dentosal Fix "creted" typo.


To initialize the VM, the following is pushed on the stack sequentially:

Expand Down Expand Up @@ -192,3 +194,5 @@ If the context is internal, the owned memory range for a call frame is:

1. `[$ssp, $sp)`: the writable stack area of the call frame.
1. `[$hp, $fp->$hp)`: the heap area allocated by this call frame.

A single write must fall within a single owned memory range. Even if `$hp == $sp`, a write instruction like `SW` cannot cross the boundary between the stack and the heap.
22 changes: 11 additions & 11 deletions src/vm/instruction_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -1275,13 +1275,13 @@ All these instructions advance the program counter `$pc` by `4` after performing

### ALOC: Allocate memory

| | |
|-------------|-------------------------------------------|
| Description | Allocate a number of bytes from the heap. |
| Operation | ```$hp = $hp - $rA;``` |
| Syntax | `aloc $rA` |
| Encoding | `0x00 rA - - -` |
| Notes | Does not initialize memory. |
| | |
|-------------|--------------------------------------------------------------------------------------|
| Description | Allocate a number of bytes from the heap. |
| Operation | ```$hp = $hp - $rA;``` |
| Syntax | `aloc $rA` |
| Encoding | `0x00 rA - - -` |
| Notes | Does not clear memory. After this, `$hp` points to the first byte of the allocation. |

Panic if:

Expand All @@ -1296,7 +1296,7 @@ Panic if:
| Operation | ```$sp = $sp + $rA``` |
| Syntax | `cfei $rA` |
| Encoding | `0x00 rA - - -` |
| Notes | Does not initialize memory. |
| Notes | Does not clear memory. |

Panic if:

Expand All @@ -1311,7 +1311,7 @@ Panic if:
| Operation | ```$sp = $sp + imm``` |
| Syntax | `cfei imm` |
| Encoding | `0x00 i i i i` |
| Notes | Does not initialize memory. |
| Notes | Does not clear memory. |

Panic if:

Expand Down Expand Up @@ -1487,7 +1487,7 @@ Panic if:

- `$rA + imm + 1` overflows
- `$rA + imm + 1 > VM_MAX_RAM`
- The memory range `MEM[$rA + imm, 1]` does not pass [ownership check](./index.md#ownership)
- The memory range `MEM[$rA + imm, 1]` does not pass [ownership check](./index.md#ownership)

### SW: Store word

Expand All @@ -1503,7 +1503,7 @@ Panic if:

- `$rA + (imm * 8) + 8` overflows
- `$rA + (imm * 8) + 8 > VM_MAX_RAM`
- The memory range `MEM[$rA + (imm * 8), 8]` does not pass [ownership check](./index.md#ownership)
- The memory range `MEM[$rA + (imm * 8), 8]` does not pass [ownership check](./index.md#ownership)

## Contract Instructions

Expand Down