From d69e08b2b478f36a121d9ceca55edd11e5483541 Mon Sep 17 00:00:00 2001 From: zapashcanon Date: Mon, 29 Jan 2024 00:50:09 +0100 Subject: [PATCH] error properly on unknown label instead of crash --- src/typecheck.ml | 13 ++++++++----- test/script/gc.t | 32 ++++++++++++++++---------------- test/validate/dune | 2 +- test/validate/unknown_label.t | 3 +++ test/validate/unknown_label.wat | 15 +++++++++++++++ 5 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 test/validate/unknown_label.t create mode 100644 test/validate/unknown_label.wat diff --git a/src/typecheck.ml b/src/typecheck.ml index 4c3ecc4c0..08f2fec4d 100644 --- a/src/typecheck.ml +++ b/src/typecheck.ml @@ -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 @@ -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 diff --git a/test/script/gc.t b/test/script/gc.t index 2f1a77543..493fee740 100644 --- a/test/script/gc.t +++ b/test/script/gc.t @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/validate/dune b/test/validate/dune index c094dd852..b356a90fb 100644 --- a/test/validate/dune +++ b/test/validate/dune @@ -1,2 +1,2 @@ (cram - (deps %{bin:owi})) + (deps %{bin:owi} unknown_label.wat)) diff --git a/test/validate/unknown_label.t b/test/validate/unknown_label.t new file mode 100644 index 000000000..f065afeee --- /dev/null +++ b/test/validate/unknown_label.t @@ -0,0 +1,3 @@ + $ dune exec owi -- validate ./unknown_label.wat + unknown label 2 + [1] diff --git a/test/validate/unknown_label.wat b/test/validate/unknown_label.wat new file mode 100644 index 000000000..9fea1e4ec --- /dev/null +++ b/test/validate/unknown_label.wat @@ -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 + ) + ) + ))