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

Add LDC mode 2, which allows code from memory #611

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Changes from 2 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
20 changes: 11 additions & 9 deletions src/fuel-vm/instruction-set.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
- [`CCP`: Code copy](#ccp-code-copy)
- [`CROO`: Code Merkle root](#croo-code-merkle-root)
- [`CSIZ`: Code size](#csiz-code-size)
- [`LDC`: Load code from an external contract](#ldc-load-code-from-an-external-contract-or-blob)
- [`LDC`: Load code from an external contract, blob or memory](#ldc-load-code-from-an-external-contract-blob-or-memory)
- [`LOG`: Log event](#log-log-event)
- [`LOGD`: Log data event](#logd-log-data-event)
- [`MINT`: Mint new coins](#mint-mint-new-coins)
Expand Down Expand Up @@ -1752,7 +1752,7 @@ Panic if:
| Notes | If `$rD` is greater than the code size, zero bytes are filled in. |

This is used only for reading and inspecting code of other contracts.
Use [`LDC`](#ldc-load-code-from-an-external-contract-or-blob) to load code for executing.
Use [`LDC`](#ldc-load-code-from-an-external-contract-blob-or-memory) to load code for executing.

Panic if:

Expand Down Expand Up @@ -1796,33 +1796,35 @@ Panic if:
- `$rB + 32` overflows or `> VM_MAX_RAM`
- Contract with ID `MEM[$rB, 32]` is not in `tx.inputs`

### `LDC`: Load code from an external contract or blob
### `LDC`: Load code from an external contract, blob or memory

| | |
|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
| Description | Copy `$rC` bytes of code at offset `$rB` from object with 32 byte id starting at `$rA` into memory starting at `$ssp`. Object type is in `imm`. |
| Operation | `id = mem[$rA,32]; code = match imm { 0 => contract_code($id), 1 => blob_payload($id) }; MEM[$ssp, $rC] = code[$rB, $rC];` |
| Description | Copy `$rC` bytes of code at offset `$rB` from object identified with `$rA` into memory starting at `$ssp`. Object type is in `imm`. |
Dentosal marked this conversation as resolved.
Show resolved Hide resolved
| Operation | `code = match imm { 0 => contract_code(mem[$rA,32]), 1 => blob_payload(mem[$rA,32]), 2 => mem[$ra, ..] }; MEM[$ssp, $rC] = code[$rB, $rC];` |
| Syntax | `ldc $rA, $rB, $rC, imm` |
| Encoding | `0x00 rA rB rC imm` |
| Notes | If `$rC` is greater than the code size, zero bytes are filled in. |

Object type from `imm` determined the source for loading as follows:
Object type from `imm` determines the source for loading as follows:

| `imm` | Object type |
|-------|---------------|
| `0` | Contract code |
| `1` | Blob payload |
| `2` | VM memory |
| other | _reserved_ |

Panic if:

- `$ssp + $rC` overflows or `> VM_MAX_RAM`
- `$rA + 32` overflows or `> VM_MAX_RAM`
- `imm <= 1` and `$rA + 32` overflows or `> VM_MAX_RAM`
- `$ssp + $rC >= $hp`
- `imm == 0` and `$rC > CONTRACT_MAX_SIZE`
- `imm == 0` and contract with ID `MEM[$rA, 32]` is not in `tx.inputs`
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to hi light that we return an error if it is called in the context of predicates?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's already called out in the predicate section of #612, but we could do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Dentosal marked this conversation as resolved.
Show resolved Hide resolved
- `imm == 1` and contract with ID `MEM[$rA, 32]` is not found in the chain state
- `imm >= 2` (reserved value)
- `imm == 1` and blob with ID `MEM[$rA, 32]` is not found in the chain state
- `imm == 2` and `$rA + $rB + $rC` overflows or `> VM_MAX_RAM`
- `imm >= 3` (reserved value)

Increment `$fp->codesize`, `$ssp` by `$rC` padded to word alignment. Then set `$sp` to `$ssp`.

Expand Down
Loading