Skip to content

Commit

Permalink
Merge pull request #2704 from OffchainLabs/gligneul/fix-stylus-captur…
Browse files Browse the repository at this point in the history
…e-hostio

[NIT-2814] Fix CaptureHostIO when slices are bigger than 2^16
  • Loading branch information
PlasmaPower authored Sep 25, 2024
2 parents 84eb59b + 12253bd commit 24fa893
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
7 changes: 4 additions & 3 deletions arbitrator/arbutil/src/evm/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,10 @@ impl<D: DataReader, H: RequestHandler<D>> EvmApi<D> for EvmApiRequestor<D, H> {
let mut request = Vec::with_capacity(2 * 8 + 3 * 2 + name.len() + args.len() + outs.len());
request.extend(start_ink.to_be_bytes());
request.extend(end_ink.to_be_bytes());
request.extend((name.len() as u16).to_be_bytes());
request.extend((args.len() as u16).to_be_bytes());
request.extend((outs.len() as u16).to_be_bytes());
// u32 is enough to represent the slices lengths because the WASM environment runs in 32 bits.
request.extend((name.len() as u32).to_be_bytes());
request.extend((args.len() as u32).to_be_bytes());
request.extend((outs.len() as u32).to_be_bytes());
request.extend(name.as_bytes());
request.extend(args);
request.extend(outs);
Expand Down
24 changes: 24 additions & 0 deletions arbitrator/stylus/tests/write-result-len.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
;; Copyright 2024, Offchain Labs, Inc.
;; For license information, see https://github.com/nitro/blob/master/LICENSE

(module
(import "vm_hooks" "read_args" (func $read_args (param i32)))
(import "vm_hooks" "write_result" (func $write_result (param i32 i32)))
(memory (export "memory") 2 2)
(func $main (export "user_entrypoint") (param $args_len i32) (result i32)
(local $len i32)

;; write args to 0x0
(call $read_args (i32.const 0))

;; treat first 4 bytes as size to write
(i32.load (i32.const 0))
local.set $len

;; call write
(call $write_result (i32.const 0) (local.get $len))

;; return success
i32.const 0
)
)
6 changes: 3 additions & 3 deletions arbos/programs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,9 @@ func newApiClosures(
}
startInk := takeU64()
endInk := takeU64()
nameLen := takeU16()
argsLen := takeU16()
outsLen := takeU16()
nameLen := takeU32()
argsLen := takeU32()
outsLen := takeU32()
name := string(takeFixed(int(nameLen)))
args := takeFixed(int(argsLen))
outs := takeFixed(int(outsLen))
Expand Down
15 changes: 15 additions & 0 deletions system_tests/stylus_trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package arbtest
import (
"bytes"
"encoding/binary"
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -478,3 +479,17 @@ func TestStylusOpcodeTraceEquivalence(t *testing.T) {
checkOpcode(t, wasmResult, 12, vm.RETURN, offset, returnLen)
checkOpcode(t, evmResult, 5078, vm.RETURN, offset, returnLen)
}

func TestStylusHugeWriteResultTrace(t *testing.T) {
const jit = false
builder, auth, cleanup := setupProgramTest(t, jit)
ctx := builder.ctx
l2client := builder.L2.Client
defer cleanup()

program := deployWasm(t, ctx, auth, l2client, watFile("write-result-len"))
const returnLen = math.MaxUint16 + 1
args := binary.LittleEndian.AppendUint32(nil, returnLen)
result := sendAndTraceTransaction(t, builder, program, nil, args)
checkOpcode(t, result, 3, vm.RETURN, nil, intToBe32(returnLen))
}

0 comments on commit 24fa893

Please sign in to comment.