Skip to content

Commit

Permalink
error properly on unknown label instead of crash
Browse files Browse the repository at this point in the history
  • Loading branch information
zapashcanon committed Jan 29, 2024
1 parent 6b06f38 commit d69e08b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
13 changes: 8 additions & 5 deletions src/typecheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ module Env = struct
let (Bt_raw ((None | Some _), t)) = t.desc in
t

let block_type_get i env = List.nth env.blocks i
let block_type_get i env =
match List.nth_opt env.blocks i with
| None -> Format.ksprintf Result.error "unknown label %d" i
| Some bt -> Ok bt

let table_type_get_from_module i (modul : Simplified.modul) =
let value = Indexed.get_at_exn i modul.table.values in
Expand Down Expand Up @@ -442,22 +445,22 @@ let rec typecheck_instr (env : env) (stack : stack) (instr : simplified instr) :
if not @@ Hashtbl.mem env.refs i then Error "undeclared function reference"
else Stack.push [ Ref_type Func_ht ] stack
| Br (Raw i) ->
let jt = Env.block_type_get i env in
let* jt = Env.block_type_get i env in
let* _stack = Stack.pop jt stack in
Ok [ any ]
| Br_if (Raw i) ->
let* stack = Stack.pop [ i32 ] stack in
let jt = Env.block_type_get i env in
let* jt = Env.block_type_get i env in
let* stack = Stack.pop jt stack in
Stack.push jt stack
| Br_table (branches, Raw i) ->
let* stack = Stack.pop [ i32 ] stack in
let default_jt = Env.block_type_get i env in
let* default_jt = Env.block_type_get i env in
let* _stack = Stack.pop default_jt stack in
let* () =
array_iter
(fun (Raw i : simplified indice) ->
let jt = Env.block_type_get i env in
let* jt = Env.block_type_get i env in
if not (List.length jt = List.length default_jt) then
Error "type mismatch (br table)"
else
Expand Down
32 changes: 16 additions & 16 deletions test/script/gc.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
[1]
$ dune exec owi -- script --no-exhaustion gc/extern.wast
owi: internal error, uncaught exception:
File "src/typecheck.ml", line 493, characters 4-10: Assertion failed
Raised at Owi__Typecheck.typecheck_instr in file "src/typecheck.ml", line 493, characters 4-16
File "src/typecheck.ml", line 496, characters 4-10: Assertion failed
Raised at Owi__Typecheck.typecheck_instr in file "src/typecheck.ml", line 496, characters 4-16
Called from Stdlib__List.fold_left in file "list.ml", line 123, characters 24-34
Called from Owi__Typecheck.typecheck_expr in file "src/typecheck.ml", line 505, characters 15-59
Called from Owi__Typecheck.typecheck_function in file "src/typecheck.ml", line 527, characters 6-112
Called from Owi__Typecheck.typecheck_expr in file "src/typecheck.ml", line 508, characters 15-59
Called from Owi__Typecheck.typecheck_function in file "src/typecheck.ml", line 530, characters 6-112
Called from Stdlib__List.fold_left in file "list.ml", line 123, characters 24-34
Called from Owi__Compile.until_typecheck in file "src/compile.ml", line 28, characters 14-31
Called from Owi__Compile.until_optimize in file "src/compile.ml", line 32, characters 11-36
Expand Down Expand Up @@ -49,11 +49,11 @@
[125]
$ dune exec owi -- script --no-exhaustion gc/ref_cast.wast
owi: internal error, uncaught exception:
File "src/typecheck.ml", line 493, characters 4-10: Assertion failed
Raised at Owi__Typecheck.typecheck_instr in file "src/typecheck.ml", line 493, characters 4-16
File "src/typecheck.ml", line 496, characters 4-10: Assertion failed
Raised at Owi__Typecheck.typecheck_instr in file "src/typecheck.ml", line 496, characters 4-16
Called from Stdlib__List.fold_left in file "list.ml", line 123, characters 24-34
Called from Owi__Typecheck.typecheck_expr in file "src/typecheck.ml", line 505, characters 15-59
Called from Owi__Typecheck.typecheck_function in file "src/typecheck.ml", line 527, characters 6-112
Called from Owi__Typecheck.typecheck_expr in file "src/typecheck.ml", line 508, characters 15-59
Called from Owi__Typecheck.typecheck_function in file "src/typecheck.ml", line 530, characters 6-112
Called from Stdlib__List.fold_left in file "list.ml", line 123, characters 24-34
Called from Owi__Compile.until_typecheck in file "src/compile.ml", line 28, characters 14-31
Called from Owi__Compile.until_optimize in file "src/compile.ml", line 32, characters 11-36
Expand All @@ -71,11 +71,11 @@
[125]
$ dune exec owi -- script --no-exhaustion gc/ref_eq.wast
owi: internal error, uncaught exception:
File "src/typecheck.ml", line 493, characters 4-10: Assertion failed
Raised at Owi__Typecheck.typecheck_instr in file "src/typecheck.ml", line 493, characters 4-16
File "src/typecheck.ml", line 496, characters 4-10: Assertion failed
Raised at Owi__Typecheck.typecheck_instr in file "src/typecheck.ml", line 496, characters 4-16
Called from Stdlib__List.fold_left in file "list.ml", line 123, characters 24-34
Called from Owi__Typecheck.typecheck_expr in file "src/typecheck.ml", line 505, characters 15-59
Called from Owi__Typecheck.typecheck_function in file "src/typecheck.ml", line 527, characters 6-112
Called from Owi__Typecheck.typecheck_expr in file "src/typecheck.ml", line 508, characters 15-59
Called from Owi__Typecheck.typecheck_function in file "src/typecheck.ml", line 530, characters 6-112
Called from Stdlib__List.fold_left in file "list.ml", line 123, characters 24-34
Called from Owi__Compile.until_typecheck in file "src/compile.ml", line 28, characters 14-31
Called from Owi__Compile.until_optimize in file "src/compile.ml", line 32, characters 11-36
Expand All @@ -93,11 +93,11 @@
[125]
$ dune exec owi -- script --no-exhaustion gc/ref_test.wast
owi: internal error, uncaught exception:
File "src/typecheck.ml", line 493, characters 4-10: Assertion failed
Raised at Owi__Typecheck.typecheck_instr in file "src/typecheck.ml", line 493, characters 4-16
File "src/typecheck.ml", line 496, characters 4-10: Assertion failed
Raised at Owi__Typecheck.typecheck_instr in file "src/typecheck.ml", line 496, characters 4-16
Called from Stdlib__List.fold_left in file "list.ml", line 123, characters 24-34
Called from Owi__Typecheck.typecheck_expr in file "src/typecheck.ml", line 505, characters 15-59
Called from Owi__Typecheck.typecheck_function in file "src/typecheck.ml", line 527, characters 6-112
Called from Owi__Typecheck.typecheck_expr in file "src/typecheck.ml", line 508, characters 15-59
Called from Owi__Typecheck.typecheck_function in file "src/typecheck.ml", line 530, characters 6-112
Called from Stdlib__List.fold_left in file "list.ml", line 123, characters 24-34
Called from Owi__Compile.until_typecheck in file "src/compile.ml", line 28, characters 14-31
Called from Owi__Compile.until_optimize in file "src/compile.ml", line 32, characters 11-36
Expand Down
2 changes: 1 addition & 1 deletion test/validate/dune
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(cram
(deps %{bin:owi}))
(deps %{bin:owi} unknown_label.wat))
3 changes: 3 additions & 0 deletions test/validate/unknown_label.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$ dune exec owi -- validate ./unknown_label.wat
unknown label 2
[1]
15 changes: 15 additions & 0 deletions test/validate/unknown_label.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(module
(func $f
(loop
br 0
br 1
br 2
(loop
br 0
br 1
br 2
br 3
br 4
)
)
))

0 comments on commit d69e08b

Please sign in to comment.