123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 TriliTech <contact@trili.tech> *)(* Copyright (c) 2022 Marigold <contact@marigold.dev> *)(* *)(* 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. *)(* *)(*****************************************************************************)openTezos_scoru_wasmmoduleWasmer=Tezos_wasmerinclude(Wasm_vm:Wasm_vm_sig.S)letcompiler_env_variable="OCTEZ_WASMER_COMPILER"letget_compiler()=letcompiler_choice=Option.mapString.lowercase_ascii(Sys.getenv_optcompiler_env_variable)inmatchcompiler_choicewith|Some"singlepass"->Wasmer.Config.SINGLEPASS|Some"cranelift"->Wasmer.Config.CRANELIFT|Somecompiler->Format.sprintf"Unknown Wasmer compiler %S (selected via %s environment variable)"compilercompiler_env_variable|>Stdlib.failwith|None->Wasmer.Config.CRANELIFTletstore=Lazy.from_fun@@fun()->letengine=Wasmer.Engine.createWasmer.Config.{compiler=get_compiler()}inWasmer.Store.createengineletload_kerneldurable=letstore=Lazy.forcestoreinModule_cache.load_kernelstoredurableletcompute~version~reveal_builtins~write_debugdurablebuffers=letopenLwt.Syntaxinlet*module_=load_kerneldurableinletmain_mem:(unit->Wasmer.Memory.t)optionref=refNoneinletretrieve_mem()=match!main_memwithSomex->x()|None->assertfalseinlethost_state=Funcs.{retrieve_mem;buffers;durable}inlethost_funcs=Funcs.make~version~reveal_builtins~write_debughost_stateinletwith_durablef=let+durable=fhost_state.durableinhost_state.durable<-durableinletstore=Lazy.forcestoreinlet*instance=Wasmer.Instance.createstoremodule_host_funcsinlet*()=(* At this point we know that the kernel is valid because we parsed and
instantiated it. It is now safe to set it as the fallback kernel. *)with_durableWasm_vm.save_fallback_kernelinletexports=Wasmer.Exports.from_instanceinstanceinletkernel_run=Wasmer.(Exports.fnexports"kernel_run"(producernothing))inmain_mem:=Some(fun()->Wasmer.Exports.mem0exports);let*()=Lwt.finalizekernel_run(fun()->(* Make sure that the instance is deleted regardless of whether
[kernel_run] succeeds or not. *)Wasmer.Instance.deleteinstance;Lwt.return_unit)inlet*durable=Wasm_vm.patch_flags_on_eval_successfulhost_state.durablein(* TODO: #4283
The module is cached, but the cash is never cleaned.
This is the point where it was scrubed before.*)Lwt.returndurable