From a199be5c18f8ce53e64acfe8fba89f1fc303b6f1 Mon Sep 17 00:00:00 2001 From: Pierrick Couderc Date: Wed, 30 Nov 2022 14:43:09 +0100 Subject: [PATCH] WASM/PVM: improve error reporting when `kernel_run` is not defined --- src/lib_scoru_wasm/test/test_init.ml | 61 ++++++++++++++++++++++++++++ src/lib_scoru_wasm/wasm_vm.ml | 20 ++++++--- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/lib_scoru_wasm/test/test_init.ml b/src/lib_scoru_wasm/test/test_init.ml index d8510512ac74..ea383cb00d64 100644 --- a/src/lib_scoru_wasm/test/test_init.ml +++ b/src/lib_scoru_wasm/test/test_init.ml @@ -197,6 +197,62 @@ let test_host_func_start_restriction () = "host functions must not access memory during initialisation" stuck) +let test_bad_entrypoint_name () = + let open Lwt_result_syntax in + let module_ = + {| + (module + (memory 1) + (export "mem"(memory 0)) + (func (export "bad_entrypoint") + (unreachable) + ) + )|} + in + + let*! bad_module_tree = initial_tree module_ in + let*! bad_module_tree = eval_until_input_requested bad_module_tree in + let*! bad_module_tree = set_empty_inbox_step 0l bad_module_tree in + let* stuck, _ = eval_until_stuck bad_module_tree in + assert ( + check_error + ~expected_kind:`Invalid_state + ~expected_reason: + (Format.sprintf + "Invalid_module: no `%s` function exported" + Constants.wasm_entrypoint) + stuck) ; + return_unit + +(* `kernel_run` will be found, but this is not a function. *) +let test_bad_export () = + let open Lwt_result_syntax in + let module_ = + {| + (module + (memory 1) + (export "kernel_run" (memory 0)) + (func (export "bad_entrypoint") + (unreachable) + ) + )|} + in + + let*! bad_module_tree = initial_tree module_ in + let*! bad_module_tree = eval_until_input_requested bad_module_tree in + let*! bad_module_tree = set_empty_inbox_step 0l bad_module_tree in + let* stuck, _ = eval_until_stuck bad_module_tree in + Format.printf "%a\n%!" pp_state (Stuck stuck) ; + assert ( + check_error + ~expected_kind:`Invalid_state + ~expected_reason: + (Format.sprintf + "Invalid_module: no `%s` function exported" + Constants.wasm_entrypoint) + stuck) ; + return_unit + let tests = [ tztest "init requires memory 0 export" `Quick test_memory0_export; @@ -206,4 +262,9 @@ let tests = "host functions are restricted in start" `Quick test_host_func_start_restriction; + tztest "Check not found `kernel_run` error" `Quick test_bad_entrypoint_name; + tztest + "Check `kernel_run` not being a function error" + `Quick + test_bad_export; ] diff --git a/src/lib_scoru_wasm/wasm_vm.ml b/src/lib_scoru_wasm/wasm_vm.ml index 2972f7457e8e..641cd16d60a7 100644 --- a/src/lib_scoru_wasm/wasm_vm.ml +++ b/src/lib_scoru_wasm/wasm_vm.ml @@ -178,12 +178,20 @@ let unsafe_next_tick_state ({buffers; durable; tick_state; _} as pvm_state) = Wasm.Instance.ModuleMap.get Constants.wasm_main_module_name module_reg in let* extern = - Wasm.Instance.NameMap.get - Constants.wasm_entrypoint - module_inst.Wasm.Instance.exports + Lwt.catch + (fun () -> + let+ extern = + Wasm.Instance.NameMap.get + Constants.wasm_entrypoint + module_inst.Wasm.Instance.exports + in + Some extern) + (function + | Tezos_lazy_containers.Lazy_map.UnexpectedAccess -> return_none + | exn -> raise exn) in match extern with - | Wasm.Instance.ExternFunc main_func -> + | Some (Wasm.Instance.ExternFunc main_func) -> let admin_instr' = Wasm.Eval.Invoke main_func in let admin_instr = Wasm.Source.{it = admin_instr'; at = no_region} in (* Clear the values and the locals in the frame. *) @@ -206,7 +214,9 @@ let unsafe_next_tick_state ({buffers; durable; tick_state; _} as pvm_state) = ~status:Failing (Stuck (Wasm_pvm_errors.invalid_state - "Invalid_module: no `main` function exported"))) + (Format.sprintf + "Invalid_module: no `%s` function exported" + Constants.wasm_entrypoint)))) | Init {self; ast_module; init_kont; module_reg} -> let* init_kont = Wasm.Eval.init_step -- GitLab