From 7df45173bc3472a78dc207585c0c88a5e790db83 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Sun, 6 Oct 2024 10:57:29 +0300 Subject: [PATCH 1/5] Add LDC mode 2, which allows code from memory --- src/fuel-vm/instruction-set.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index ec416ac2..53d73163 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -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) @@ -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`. | +| 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` -- `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`. From 9c122c84b070d17c6eb99cf458da38180a79cec8 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Sun, 6 Oct 2024 11:01:18 +0300 Subject: [PATCH 2/5] Fix link to LDC --- src/fuel-vm/instruction-set.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index 53d73163..480fd40b 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -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: From 6aa02acc3d7ed24c4036e5623f64deda900623a9 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 7 Oct 2024 20:49:26 +0300 Subject: [PATCH 3/5] Clarify padding behavior --- src/fuel-vm/instruction-set.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index 480fd40b..a0120951 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -1804,7 +1804,7 @@ Panic if: | 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. | +| Notes | If `$rC` is greater than the code size, zero bytes are filled in. Length final is always padded to word boundary. | Object type from `imm` determines the source for loading as follows: From 697d0eb07d42763e3e80bf65d6c0c280f13d32f5 Mon Sep 17 00:00:00 2001 From: Hannes Karppila <2204863+Dentosal@users.noreply.github.com> Date: Tue, 8 Oct 2024 13:46:53 +0300 Subject: [PATCH 4/5] Update src/fuel-vm/instruction-set.md Co-authored-by: Brandon Kite --- src/fuel-vm/instruction-set.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index a0120951..bddf7520 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -1804,7 +1804,7 @@ Panic if: | 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. Length final is always padded to word boundary. | +| Notes | If `$rC` is greater than the code size, zero bytes are filled in. Final length is always padded to word boundary. | Object type from `imm` determines the source for loading as follows: From e4ee95b01c286d58230c0c89e621ffe685dc1165 Mon Sep 17 00:00:00 2001 From: Hannes Karppila <2204863+Dentosal@users.noreply.github.com> Date: Tue, 8 Oct 2024 13:47:05 +0300 Subject: [PATCH 5/5] Update src/fuel-vm/instruction-set.md --- src/fuel-vm/instruction-set.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index bddf7520..47bb3d8c 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -1822,6 +1822,7 @@ Panic if: - `$ssp + $rC >= $hp` - `imm == 0` and `$rC > CONTRACT_MAX_SIZE` - `imm == 0` and contract with ID `MEM[$rA, 32]` is not in `tx.inputs` +- `imm == 0` and context is a predicate - `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)