Skip to content

Commit

Permalink
Merge pull request #2778 from OffchainLabs/stylusprogram-infiniteloop…
Browse files Browse the repository at this point in the history
…-outofgaserror

stylus test: infinite loop should create out-of-gas error
  • Loading branch information
joshuacolvin0 authored Nov 14, 2024
2 parents 1de23ba + 888dbad commit 57ce64f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion arbitrator/prover/test-cases/dynamic.wat
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

;; WAVM Module hash
(data (i32.const 0x000)
"\a1\49\cf\81\13\ff\9c\95\f2\c8\c2\a1\42\35\75\36\7d\e8\6d\d4\22\d8\71\14\bb\9e\a4\7b\af\53\5d\d7") ;; user
"\ae\87\91\cf\6a\c4\55\ff\28\06\b9\55\d5\a7\36\e8\1b\c7\91\f7\93\8a\22\a4\08\23\25\16\37\01\48\25") ;; user
(func $start (local $user i32) (local $internals i32)
;; link in user.wat
i32.const 0
Expand Down
2 changes: 1 addition & 1 deletion arbitrator/prover/test-cases/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const BYTES_PER_FIELD_ELEMENT = 32

var BLS_MODULUS, _ = new(big.Int).SetString("52435875175126190479447740508185965837690552500527637822603658699938581184513", 10)

var stylusModuleHash = common.HexToHash("a149cf8113ff9c95f2c8c2a1423575367de86dd422d87114bb9ea47baf535dd7") // user.wat
var stylusModuleHash = common.HexToHash("ae8791cf6ac455ff2806b955d5a736e81bc791f7938a22a40823251637014825") // user.wat

func callStylusProgram(recurse int) {
evmData := programs.EvmData{}
Expand Down
2 changes: 1 addition & 1 deletion arbitrator/prover/test-cases/link.wat
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
(data (i32.const 0x140)
"\47\f7\4f\9c\21\51\4f\52\24\ea\d3\37\5c\bf\a9\1b\1a\5f\ef\22\a5\2a\60\30\c5\52\18\90\6b\b1\51\e5") ;; iops
(data (i32.const 0x160)
"\a1\49\cf\81\13\ff\9c\95\f2\c8\c2\a1\42\35\75\36\7d\e8\6d\d4\22\d8\71\14\bb\9e\a4\7b\af\53\5d\d7") ;; user
"\ae\87\91\cf\6a\c4\55\ff\28\06\b9\55\d5\a7\36\e8\1b\c7\91\f7\93\8a\22\a4\08\23\25\16\37\01\48\25") ;; user
(data (i32.const 0x180)
"\ee\47\08\f6\47\b2\10\88\1f\89\86\e7\e3\79\6b\b2\77\43\f1\4e\ee\cf\45\4a\9b\7c\d7\c4\5b\63\b6\d7") ;; return

Expand Down
12 changes: 12 additions & 0 deletions arbitrator/prover/test-cases/user.wat
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
i32.const 0xFFFFFF
i32.load
)
(func $infinite_loop (result i32)
(loop $loop
br $loop
)
unreachable
)
(func (export "user_entrypoint") (param $args_len i32) (result i32)
;; this func uses $args_len to select which func to call

Expand All @@ -43,6 +49,12 @@
(then (call $out_of_bounds) (return))
)

;; reverts due to an out-of-gas error
(i32.eq (local.get $args_len) (i32.const 4))
(if
(then (call $infinite_loop) (return))
)

(i32.eq (local.get $args_len) (i32.const 32))
(if
(then (call $storage_load) (return))
Expand Down
25 changes: 25 additions & 0 deletions system_tests/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,31 @@ func testCreate(t *testing.T, jit bool) {
validateBlockRange(t, blocks, jit, builder)
}

func TestProgramInfiniteLoopShouldCauseErrOutOfGas(t *testing.T) {
t.Parallel()
testInfiniteLoopCausesErrOutOfGas(t, true)
testInfiniteLoopCausesErrOutOfGas(t, false)
}

func testInfiniteLoopCausesErrOutOfGas(t *testing.T, jit bool) {
builder, auth, cleanup := setupProgramTest(t, jit)
ctx := builder.ctx
l2info := builder.L2Info
l2client := builder.L2.Client
defer cleanup()

userWasm := deployWasm(t, ctx, auth, l2client, "../arbitrator/prover/test-cases/user.wat")
// Passing input of size 4 invokes $infinite_loop function that calls the infinite loop
tx := l2info.PrepareTxTo("Owner", &userWasm, 1000000, nil, make([]byte, 4))
Require(t, l2client.SendTransaction(ctx, tx))
receipt, err := EnsureTxSucceeded(ctx, l2client, tx)
if !strings.Contains(err.Error(), vm.ErrOutOfGas.Error()) {
t.Fatalf("transaction should have failed with out of gas error but instead failed with: %v", err)
}

validateBlocks(t, receipt.BlockNumber.Uint64(), jit, builder)
}

func TestProgramMemory(t *testing.T) {
t.Parallel()
testMemory(t, true)
Expand Down

0 comments on commit 57ce64f

Please sign in to comment.