From a8d2dfd648cc760b82bf47a8743694541c778ee4 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Thu, 16 Mar 2023 15:48:04 +0100 Subject: [PATCH] WASM: Have the Fast Exec enable host functions per WASM PVM revision --- src/lib_scoru_wasm/fast/exec.ml | 6 ++++-- src/lib_scoru_wasm/fast/exec.mli | 1 + src/lib_scoru_wasm/fast/funcs.ml | 10 +++++++--- src/lib_scoru_wasm/fast/funcs.mli | 1 + src/lib_scoru_wasm/fast/vm.ml | 6 ++++-- src/lib_scoru_wasm/test/test_protocol_migration.ml | 6 +++--- src/lib_scoru_wasm/wasm_pvm.ml | 10 +++++----- src/lib_scoru_wasm/wasm_vm.ml | 2 -- src/lib_scoru_wasm/wasm_vm_sig.ml | 6 ++++-- 9 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/lib_scoru_wasm/fast/exec.ml b/src/lib_scoru_wasm/fast/exec.ml index 6d6e7dfa69e0..1d72df6e4e93 100644 --- a/src/lib_scoru_wasm/fast/exec.ml +++ b/src/lib_scoru_wasm/fast/exec.ml @@ -57,7 +57,7 @@ let load_kernel durable = let store = Lazy.force store in Module_cache.load_kernel store durable -let compute ~reveal_builtins ~write_debug durable buffers = +let compute ~version ~reveal_builtins ~write_debug durable buffers = let open Lwt.Syntax in let* module_ = load_kernel durable in @@ -67,7 +67,9 @@ let compute ~reveal_builtins ~write_debug durable buffers = in let host_state = Funcs.{retrieve_mem; buffers; durable} in - let host_funcs = Funcs.make ~reveal_builtins ~write_debug host_state in + let host_funcs = + Funcs.make ~version ~reveal_builtins ~write_debug host_state + in let with_durable f = let+ durable = f host_state.durable in diff --git a/src/lib_scoru_wasm/fast/exec.mli b/src/lib_scoru_wasm/fast/exec.mli index cf793f0d745f..b7d7399d60af 100644 --- a/src/lib_scoru_wasm/fast/exec.mli +++ b/src/lib_scoru_wasm/fast/exec.mli @@ -28,6 +28,7 @@ open Tezos_webassembly_interpreter (** [compute durable buffers] applies one call to [kernel_run]. *) val compute : + version:Wasm_pvm_state.version -> reveal_builtins:Builtins.reveals -> write_debug:Builtins.write_debug -> Durable.t -> diff --git a/src/lib_scoru_wasm/fast/funcs.ml b/src/lib_scoru_wasm/fast/funcs.ml index 4de508b19fbb..a10b46c8ece8 100644 --- a/src/lib_scoru_wasm/fast/funcs.ml +++ b/src/lib_scoru_wasm/fast/funcs.ml @@ -38,7 +38,7 @@ type host_state = { mutable durable : Durable.t; } -let make ~reveal_builtins ~write_debug state = +let make ~version ~reveal_builtins ~write_debug state = let open Wasmer in let open Lwt.Syntax in let with_mem f = @@ -244,8 +244,7 @@ let make ~reveal_builtins ~write_debug state = ~payload:(Bytes.of_string payload)) in - List.map - (fun (name, impl) -> (Constants.wasm_host_funcs_virual_module, name, impl)) + let base = [ ("read_input", read_input); ("write_output", write_output); @@ -262,3 +261,8 @@ let make ~reveal_builtins ~write_debug state = ("reveal_preimage", reveal_preimage); ("reveal_metadata", reveal_metadata); ] + in + let extra = match version with Wasm_pvm_state.V0 | V1 -> [] in + List.map + (fun (name, impl) -> (Constants.wasm_host_funcs_virual_module, name, impl)) + (base @ extra) diff --git a/src/lib_scoru_wasm/fast/funcs.mli b/src/lib_scoru_wasm/fast/funcs.mli index 0b2520861375..39ed83addb02 100644 --- a/src/lib_scoru_wasm/fast/funcs.mli +++ b/src/lib_scoru_wasm/fast/funcs.mli @@ -39,6 +39,7 @@ type host_state = { implementation for [write_debug] will be picked that prints to the standard output. *) val make : + version:Wasm_pvm_state.version -> reveal_builtins:Builtins.reveals -> write_debug:Builtins.write_debug -> host_state -> diff --git a/src/lib_scoru_wasm/fast/vm.ml b/src/lib_scoru_wasm/fast/vm.ml index 80a474334f67..e7094881123a 100644 --- a/src/lib_scoru_wasm/fast/vm.ml +++ b/src/lib_scoru_wasm/fast/vm.ml @@ -42,9 +42,11 @@ let compute_until_snapshot ~max_steps ?write_debug pvm_state = let compute_fast ~reveal_builtins ~write_debug pvm_state = let open Lwt.Syntax in + let* version = Wasm_vm.get_wasm_version pvm_state in (* Execute! *) let* durable = Exec.compute + ~version ~reveal_builtins ~write_debug pvm_state.durable @@ -161,10 +163,10 @@ let rec compute_step_many accum_ticks ?reveal_builtins let compute_step_many = compute_step_many 0L +let get_wasm_version = Wasm_vm.get_wasm_version + module Internal_for_tests = struct let compute_step_many_with_hooks = compute_step_many - - let get_wasm_version = Wasm_vm.Internal_for_tests.get_wasm_version end let compute_step_many = compute_step_many ?after_fast_exec:None diff --git a/src/lib_scoru_wasm/test/test_protocol_migration.ml b/src/lib_scoru_wasm/test/test_protocol_migration.ml index bf1e98fe1454..ede388112a1c 100644 --- a/src/lib_scoru_wasm/test/test_protocol_migration.ml +++ b/src/lib_scoru_wasm/test/test_protocol_migration.ml @@ -47,15 +47,15 @@ let test_protocol_migration_message () = let open Lwt_syntax in let* tree = initial_tree ~version:V0 noop_module in let* tree = eval_until_input_requested tree in - let* version = Wasm.Internal_for_tests.get_wasm_version tree in + let* version = Wasm.get_wasm_version tree in assert (version = V0) ; let* tree = set_empty_inbox_step 0l tree in let* tree = eval_until_input_requested tree in - let* version = Wasm.Internal_for_tests.get_wasm_version tree in + let* version = Wasm.get_wasm_version tree in assert (version = V0) ; let* tree = set_empty_inbox_step ~migrate_to:Proto_alpha 0l tree in let* tree = eval_until_input_requested tree in - let* version = Wasm.Internal_for_tests.get_wasm_version tree in + let* version = Wasm.get_wasm_version tree in assert (version = V1) ; Lwt_result_syntax.return_unit diff --git a/src/lib_scoru_wasm/wasm_pvm.ml b/src/lib_scoru_wasm/wasm_pvm.ml index ddeb0a0da41c..e1b6f0bd6483 100644 --- a/src/lib_scoru_wasm/wasm_pvm.ml +++ b/src/lib_scoru_wasm/wasm_pvm.ml @@ -341,6 +341,11 @@ module Make_pvm (Wasm_vm : Wasm_vm_sig.S) (T : Tezos_tree_encoding.TREE) : let* pvm_state = Wasm_vm.reveal_step payload pvm_state in encode pvm_state tree + let get_wasm_version tree = + let open Lwt.Syntax in + let* pvm = Tree_encoding_runner.decode pvm_state_encoding tree in + Wasm_vm.get_wasm_version pvm + module Internal_for_tests = struct let get_tick_state tree = let open Lwt_syntax in @@ -409,11 +414,6 @@ module Make_pvm (Wasm_vm : Wasm_vm_sig.S) (T : Tezos_tree_encoding.TREE) : let+ pvm = Tree_encoding_runner.decode pvm_state_encoding tree in pvm.buffers.input - let get_wasm_version tree = - let open Lwt.Syntax in - let* pvm = Tree_encoding_runner.decode pvm_state_encoding tree in - Wasm_vm.Internal_for_tests.get_wasm_version pvm - let compute_step_many_with_hooks ?reveal_builtins ?write_debug ?after_fast_exec ?stop_at_snapshot ~max_steps tree = let open Lwt.Syntax in diff --git a/src/lib_scoru_wasm/wasm_vm.ml b/src/lib_scoru_wasm/wasm_vm.ml index a12d92712c56..0df600753f17 100644 --- a/src/lib_scoru_wasm/wasm_vm.ml +++ b/src/lib_scoru_wasm/wasm_vm.ml @@ -711,6 +711,4 @@ module Internal_for_tests = struct let compute_step_many_with_hooks ?reveal_builtins ?write_debug ?after_fast_exec:_ = compute_step_many ?reveal_builtins ?write_debug - - let get_wasm_version = get_wasm_version end diff --git a/src/lib_scoru_wasm/wasm_vm_sig.ml b/src/lib_scoru_wasm/wasm_vm_sig.ml index faa756a6c22e..cb41e36f95fe 100644 --- a/src/lib_scoru_wasm/wasm_vm_sig.ml +++ b/src/lib_scoru_wasm/wasm_vm_sig.ml @@ -37,8 +37,6 @@ module type Internal_for_tests = sig max_steps:int64 -> state -> (state * int64) Lwt.t - - val get_wasm_version : state -> version Lwt.t end module type Generic = sig @@ -88,6 +86,10 @@ module type Generic = sig Should not raise. *) val get_info : state -> info Lwt.t + (** [get_wasm_version pvm_state] returns the current version at + which the WASM PVM operates. *) + val get_wasm_version : state -> version Lwt.t + module Internal_for_tests : Internal_for_tests with type state := state end -- GitLab