package octez-l2-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file vm.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 TriliTech <contact@trili.tech>                         *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Tezos_scoru_wasm
open Wasm_pvm_state.Internal_state

include (Wasm_vm : Wasm_vm_sig.S)

let compute_until_snapshot ~wasm_entrypoint ~max_steps ?write_debug pvm_state =
  Wasm_vm.compute_step_many_until
    ~wasm_entrypoint
    ~max_steps
    ?write_debug
    (fun pvm_state ->
      Lwt.return
      @@
      match pvm_state.tick_state with
      | Snapshot -> false
      | _ -> Wasm_vm.should_compute pvm_state)
    pvm_state

let compute_fast ~wasm_entrypoint ~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
      ~wasm_entrypoint
      ~version
      ~reveal_builtins
      ~write_debug
      pvm_state.durable
      pvm_state.buffers
  in
  (* The WASM PVM does several maintenance operations at the end of
     each [kernel_run] call, using the very last [Padding]
     tick. Instead of replicating the same logic in the Fast VM, we
     reuse it by crafting the PVM state just before that critical
     tick. *)
  let ticks = pvm_state.max_nb_ticks in
  let current_tick = Z.(pred @@ add pvm_state.last_top_level_call ticks) in
  let pvm_state =
    {pvm_state with durable; current_tick; tick_state = Padding}
  in
  (* Here, we don't reuse the [write_debug] argument, because we are
     in the [Padding] stage of the WASM PVM execution, so there is no
     WASM execution during this tick.

     Calling [compute_step_with_debug ~write_debug] has a significant
     performance cost, because the host functions registry is
     reconstructed.

     As a consequence of these two facts, we call [compute_step] to
     avoid this penalty.*)
  let* pvm_state = Wasm_vm.compute_step ~wasm_entrypoint pvm_state in

  Lwt.return pvm_state

let rec compute_step_many accum_ticks ?reveal_builtins
    ?(write_debug = Builtins.Noop) ?(after_fast_exec = fun () -> ())
    ?(stop_at_snapshot = false) ~wasm_entrypoint ~max_steps pvm_state =
  let open Lwt.Syntax in
  assert (max_steps > 0L) ;
  let eligible_for_fast_exec =
    Z.Compare.(pvm_state.max_nb_ticks <= Z.of_int64 max_steps)
  in
  let inbox_snapshot =
    Tezos_webassembly_interpreter.Input_buffer.snapshot pvm_state.buffers.input
  in
  let* outbox_snapshot =
    Tezos_webassembly_interpreter.Output_buffer.snapshot
      pvm_state.buffers.output
  in
  let backup pvm_state =
    let+ pvm_state, ticks =
      Wasm_vm.compute_step_many
        ~wasm_entrypoint
        ?reveal_builtins
        ~write_debug
        ~stop_at_snapshot
        ~max_steps
        {
          pvm_state with
          buffers = {input = inbox_snapshot; output = outbox_snapshot};
        }
    in
    (pvm_state, Int64.add accum_ticks ticks)
  in
  match reveal_builtins with
  | Some reveal_builtins when eligible_for_fast_exec -> (
      let goto_snapshot_and_retry () =
        let* pvm_state, ticks =
          compute_until_snapshot
            ~wasm_entrypoint
            ~write_debug
            ~max_steps
            pvm_state
        in
        match pvm_state.tick_state with
        | Snapshot when not stop_at_snapshot ->
            let max_steps = Int64.sub max_steps ticks in
            let accum_ticks = Int64.add accum_ticks ticks in
            let may_compute_more = Wasm_vm.should_compute pvm_state in
            if may_compute_more && max_steps > 0L then
              (compute_step_many [@tailcall])
                ~wasm_entrypoint
                accum_ticks
                ~reveal_builtins
                ~write_debug
                ~stop_at_snapshot
                ~after_fast_exec
                ~max_steps
                pvm_state
            else Lwt.return (pvm_state, accum_ticks)
        | _ -> Lwt.return (pvm_state, ticks)
      in
      let go_like_the_wind () =
        let* pvm_state =
          compute_fast ~wasm_entrypoint ~write_debug ~reveal_builtins pvm_state
        in
        let accum_ticks =
          Int64.add accum_ticks (Z.to_int64 pvm_state.max_nb_ticks)
        in
        after_fast_exec () ;
        let max_steps =
          Int64.sub max_steps (Z.to_int64 pvm_state.max_nb_ticks)
        in
        if
          max_steps > 0L
          && Wasm_vm.should_compute pvm_state
          && not stop_at_snapshot
        then
          (compute_step_many [@tailcall])
            ~wasm_entrypoint
            accum_ticks
            ~max_steps
            ~reveal_builtins
            ~write_debug
            ~stop_at_snapshot
            ~after_fast_exec
            pvm_state
        else Lwt.return (pvm_state, accum_ticks)
      in
      match pvm_state.tick_state with
      | Snapshot -> Lwt.catch go_like_the_wind (fun _ -> backup pvm_state)
      | _ -> goto_snapshot_and_retry ())
  | _ ->
      (* The number of ticks we're asked to do is lower than the maximum number
         of ticks for a top-level cycle or no builtins were supplied. Fast
         Execution cannot be applied in this case. *)
      backup pvm_state

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
end

let compute_step_many = compute_step_many ?after_fast_exec:None
OCaml

Innovation. Community. Security.