Path: blob/main/crates/fuzzing/wasm-spec-interpreter/ocaml/interpret.ml
1692 views
(* This module exposes an [interpret] function to Rust. It wraps several1different calls from the WebAssembly specification interpreter in a way that we2can access across the FFI boundary. To understand this better, see:3- the OCaml manual documentation re: calling OCaml from C,4https://ocaml.org/manual/intfc.html#s%3Ac-advexample5- the [ocaml-interop] example,6https://github.com/tezedge/ocaml-interop/blob/master/testing/rust-caller/ocaml/callable.ml7*)89(** Enumerate the types of values we pass across the FFI boundary. This must10match `Value` in `src/lib.rs` *)11type ffi_value =12| I32 of int3213| I64 of int6414| F32 of int3215| F64 of int6416| V128 of Bytes.t1718(** Enumerate the kinds of exported values the interpreter can retrieve. *)19type ffi_export_value =20| Global of ffi_value21| Memory of Bytes.t2223(* Here we access the WebAssembly specification interpreter; this must be linked24in. *)25open Wasm26open Wasm.WasmRef_Isa_m.WasmRef_Isa2728type spec_instance = (unit module_export_ext list * ((unit s_m_ext) ref))2930(** Helper for converting the FFI values to their spec interpreter type. *)31let convert_to_wasm (v: ffi_value) : v = match v with32| I32 n -> V_num (ConstInt32 (I32_impl_abs n))33| I64 n -> V_num (ConstInt64 (I64_impl_abs n))34| F32 n -> V_num (ConstFloat32 (F32.of_bits n))35| F64 n -> V_num (ConstFloat64 (F64.of_bits n))36| V128 n -> V_vec (ConstVec128 (V128.of_bits (Bytes.to_string n)))3738(** Helper for converting the spec interpreter values to their FFI type. *)39let convert_from_wasm (v: v) : ffi_value = match v with40| V_num ((ConstInt32 (I32_impl_abs n))) -> I32 n41| V_num ((ConstInt64 (I64_impl_abs n))) -> I64 n42| V_num ((ConstFloat32 n)) -> F32 (F32.to_bits n)43| V_num ((ConstFloat64 n)) -> F64 (F64.to_bits n)44| V_vec ((ConstVec128 n)) -> V128 (Bytes.of_string (V128.to_bits n))4546(** Parse the given WebAssembly module binary into an Ast.module_. At some point47in the future this should also be able to parse the textual form (TODO). *)48let parse bytes =49(* Optionally, use Bytes.unsafe_to_string here to avoid the copy *)50let bytes_as_str = Bytes.to_string bytes in51(Decode.decode "default" bytes_as_str)5253(** Construct an instance from a sequence of WebAssembly bytes. This clears the54previous contents of the global store *)55let instantiate_exn module_bytes : spec_instance =56let s = (make_empty_store_m ()) in57let module_ = parse module_bytes in58let m_isa = Ast_convert.convert_module (module_.it) in59(match interp_instantiate_init_m s m_isa [] () with60| (s', (RI_res_m(inst,v_exps,_))) -> (v_exps, ref s')61| (s', (RI_trap_m str)) -> raise (Eval.Trap (Source.no_region, "(Isabelle) trap: " ^ str))62| (s', (RI_crash_m (Error_exhaustion str))) -> raise (Eval.Exhaustion (Source.no_region, "(Isabelle) call stack exhausted"))63| (s', (RI_crash_m (Error_invalid str))) -> raise (Eval.Crash (Source.no_region, "(Isabelle) error: " ^ str))64| (s', (RI_crash_m (Error_invariant str))) -> raise (Eval.Crash (Source.no_region, "(Isabelle) error: " ^ str))65)6667let instantiate module_bytes =68try Ok(instantiate_exn module_bytes) with69| _ as e -> Error(Printexc.to_string e)7071(** Retrieve the value of an export by name from a WebAssembly instance. *)72let export_exn (inst_s : spec_instance) (name : string) : ffi_export_value =73let (inst, s_ref) = inst_s in74match (e_desc (List.find (fun exp -> String.equal (e_name exp) name) inst)) with75Ext_func _ -> raise Not_found76| Ext_tab _ -> raise Not_found77| Ext_mem i -> Memory (fst (Array.get (mems (!s_ref)) (Z.to_int (integer_of_nat i))))78| Ext_glob i -> Global (convert_from_wasm (g_val (Array.get (globs (!s_ref)) (Z.to_int (integer_of_nat i)))))7980let export inst name =81try Ok(export_exn inst name) with82| _ as e -> Error(Printexc.to_string e)8384(** Interpret the first exported function and return the result. Use provided85parameters if they exist, otherwise use default (zeroed) values. *)86let interpret_legacy_exn module_bytes opt_params =87let opt_params_ = Option.map (List.rev_map convert_to_wasm) opt_params in88let module_ = parse module_bytes in89let m_isa = Ast_convert.convert_module (module_.it) in90let fuel = Z.of_string "4611686018427387904" in91let max_call_depth = Z.of_string "300" in92(match run_fuzz (nat_of_integer fuel) (nat_of_integer max_call_depth) (make_empty_store_m ()) m_isa [] opt_params_ () with93| (s', RValue vs_isa') -> List.rev_map convert_from_wasm vs_isa'94| (s', RTrap str) -> raise (Eval.Trap (Source.no_region, "(Isabelle) trap: " ^ str))95| (s', (RCrash (Error_exhaustion str))) -> raise (Eval.Exhaustion (Source.no_region, "(Isabelle) call stack exhausted"))96| (s', (RCrash (Error_invalid str))) -> raise (Eval.Crash (Source.no_region, "(Isabelle) error: " ^ str))97| (s', (RCrash (Error_invariant str))) -> raise (Eval.Crash (Source.no_region, "(Isabelle) error: " ^ str))98)99100let interpret_legacy module_bytes opt_params =101try Ok(interpret_legacy_exn module_bytes opt_params) with102| _ as e -> Error(Printexc.to_string e)103104(* process an optional list of params, generating default params if necessary *)105(* TODO: this should be done in the Isabelle model *)106let get_param_vs s_ref (vs_opt :(ffi_value list) option) i =107(match vs_opt with108| None -> (match cl_m_type ((array_nth heap_cl_m (funcs !s_ref) i) ()) with Tf (t1, _) -> map bitzero t1)109| Some vs -> List.map convert_to_wasm vs)110111(** Interpret the function exported at name. Use provided112parameters if they exist, otherwise use default (zeroed) values. *)113let interpret_exn (inst_s : spec_instance) (name : string) opt_params =114(let fuel = Z.of_string "4611686018427387904" in115let max_call_depth = Z.of_string "300" in116let (inst, s_ref) = inst_s in117match (e_desc (List.find (fun exp -> String.equal (e_name exp) name) inst)) with118| Ext_func i ->119(let params = get_param_vs s_ref opt_params i in120let (s', res) = run_invoke_v_m (nat_of_integer fuel) (nat_of_integer max_call_depth) ((!s_ref), (params, i)) () in121s_ref := s';122(match res with123| RValue vs_isa' -> List.rev_map convert_from_wasm vs_isa'124| RTrap str -> raise (Eval.Trap (Source.no_region, "(Isabelle) trap: " ^ str))125| (RCrash (Error_exhaustion str)) -> raise (Eval.Exhaustion (Source.no_region, "(Isabelle) call stack exhausted"))126| (RCrash (Error_invalid str)) -> raise (Eval.Crash (Source.no_region, "(Isabelle) error: " ^ str))127| (RCrash (Error_invariant str)) -> raise (Eval.Crash (Source.no_region, "(Isabelle) error: " ^ str))128))129| _ -> raise Not_found)130131let interpret inst name opt_params =132try Ok(interpret_exn inst name opt_params) with133| _ as e -> Error(Printexc.to_string e)134135let () =136Callback.register "instantiate" instantiate;137Callback.register "interpret_legacy" interpret_legacy;138Callback.register "interpret" interpret;139Callback.register "export" export;140141142