123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *)(* 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. *)(* *)(*****************************************************************************)openTezos_webassembly_interpretertypetruncated_string=Truncatedofstring[@@ocaml.unboxed]letmessages_maximum_size=128lettruncate_messagemsg=ifString.lengthmsg>messages_maximum_sizethenTruncated(String.submsg0messages_maximum_size)elseTruncatedmsgtypeinterpreter_error={raw_exception:truncated_string;explanation:truncated_stringoption;}typefallback_cause=|Decode_causeofinterpreter_error|Link_causeoftruncated_string|Init_causeofinterpreter_errortypet=|Decode_errorofinterpreter_error|Link_erroroftruncated_string|Init_errorofinterpreter_error|Eval_errorofinterpreter_error|Invalid_stateoftruncated_string|Unknown_erroroftruncated_string|Too_many_ticks|Too_many_reboots|No_fallback_kerneloffallback_causeletdecode_state_to_string_raw=function|Decode.Byte_vector_step->"Byte_vector_step"|Instr_step->"Instr_step"|Instr_block_step->"Instr_block_step"|Block_step->"Block_step"|Name_step->"Name_step"|Func_type_step->"Func_type_step"|Import_step->"Import_step"|Export_step->"Export_step"|Code_step->"Code_step"|Elem_step->"Elem_step"|Data_step->"Data_step"|Module_step->"Module_step"letdecode_state_to_stringexn=decode_state_to_string_rawexn|>truncate_messageletinit_state_to_string_raw=function|Eval.Init_step->"Init_step"|Map_step->"Map_step"|Map_concat_step->"Map_concat_step"|Join_step->"Join_step"|Section_step->"Section_step"|Eval_const->"Eval_const"|Create_global_step->"Create_global_step"|Run_data_step->"Run_data_step"letinit_state_to_stringexn=init_state_to_string_rawexn|>truncate_messageletdurable_exn_explanation_raw=function|Durable.Invalid_keypath->Some("Invalid_key: "^path)|Durable.Value_not_found->Some"Value not found"|Durable.Tree_not_found->Some"Tree not found"|Durable.Durable_empty|Durable_storage.Durable_empty->Some"Empty durable storage"|_->Noneletdurable_exn_explanationexn=durable_exn_explanation_rawexn|>Option.maptruncate_messageleteval_state_to_string_raw=function|Eval.Invoke_stepmsg->"Invoke_step: "^msg|Label_step->"Label_step"|Frame_step->"Frame_step"|Eval_step->"Eval_step"leteval_state_to_stringexn=eval_state_to_string_rawexn|>truncate_messageletreveal_error_to_string_raw=function|Eval.Reveal_step->"Reveal_step"|Reveal_hash_decodingmsg->Format.sprintf"Unknown error during hash decoding: %s"msg|Reveal_payload_decodingmsg->Format.sprintf"Unknown error during payload decoding: %s"msgletreveal_error_to_stringexn=reveal_error_to_string_rawexn|>truncate_messageletextract_interpreter_errorexn=letopenTezos_lazy_containersinletraw_exception=Printexc.to_stringexn|>truncate_messageinmatchexnwith(* The locations are removed during encoding, they won't be usable in practice. *)|Binary_exn.Decode_error.Error(_,explanation)|Binary_exn.Encode_error.Error(_,explanation)|Binary_exn.Floating_point.Error(_,explanation)|Valid.Invalid(_,explanation)|Eval.Link(_,explanation)|Eval.Trap(_,explanation)|Eval.Crash(_,explanation)|Eval.Exhaustion(_,explanation)|Import.Unknown(_,explanation)->`Interpreter{raw_exception;explanation=Some(truncate_messageexplanation)}|Values.TypeError_|Binary_exn.EOS|Binary_exn.Utf8|Lazy_map.UnexpectedAccess|Lazy_vector.Bounds|Lazy_vector.SizeOverflow|Chunked_byte_vector.Bounds|Chunked_byte_vector.SizeOverflow|Table.Type|Table.SizeLimit|Table.OutOfMemory|Table.Bounds|Table.SizeOverflow|Memory.Type|Memory.SizeLimit|Memory.OutOfMemory|Memory.Bounds|Memory.SizeOverflow|Global.Type|Global.NotMutable|Ixx.Overflow|Ixx.DivideByZero|Ixx.InvalidConversion|Input_buffer.Bounds|Input_buffer.SizeOverflow|Input_buffer.Cannot_store_an_earlier_message|Input_buffer.Dequeue_from_empty_queue->`Interpreter{raw_exception;explanation=None}|Decode.Step_errorstate->`Interpreter{raw_exception;explanation=Some(decode_state_to_stringstate)}|Eval.Init_step_errorstate->`Interpreter{raw_exception;explanation=Some(init_state_to_stringstate)}|Eval.Evaluation_step_errorstate->`Interpreter{raw_exception;explanation=Some(eval_state_to_stringstate)}|Eval.Reveal_errorstate->`Interpreter{raw_exception;explanation=Some(reveal_error_to_stringstate)}|Eval.Missing_memory_0_export->`Interpreter{raw_exception;explanation=Some(truncate_message"Module must export memory 0");}|Durable.Invalid_key_|Durable.Value_not_found|Durable.Tree_not_found|Durable.Durable_empty|Durable_storage.Durable_empty->`Interpreter{raw_exception;explanation=durable_exn_explanationexn}|_->`Unknownraw_exceptionletinvalid_statem=Invalid_state(truncate_messagem)lettruncated_string_encoding=Data_encoding.(conv(fun(Truncateds)->s)(funs->Truncateds)string)letinterpreter_error_encodingprefix=letopenData_encodinginconv(fun{raw_exception;explanation}->(raw_exception,explanation))(fun(raw_exception,explanation)->{raw_exception;explanation})(obj2(req(prefix^"_raw_exception")truncated_string_encoding)(req(prefix^"_explanation")(optiontruncated_string_encoding)))letfallback_cause_encoding=letopenData_encodinginunion[case(Tag0)~title:"Decode_cause"(interpreter_error_encoding"decode")(functionDecode_causecause->Somecause|_->None)(funcause->Decode_causecause);case(Tag1)~title:"Link_cause"(obj1(req"link"truncated_string_encoding))(functionLink_causecause->Somecause|_->None)(funcause->Link_causecause);case(Tag2)~title:"Init_cause"(interpreter_error_encoding"init")(functionInit_causecause->Somecause|_->None)(funcause->Init_causecause);]letencoding=letopenData_encodinginunion[case(Tag0)~title:"Decode_error"(interpreter_error_encoding"decode")(functionDecode_errorerr->Someerr|_->None)(funerr->Decode_errorerr);case(Tag1)~title:"Link_error"(obj1(req"link"truncated_string_encoding))(functionLink_errorerr->Someerr|_->None)(funerr->Link_errorerr);case(Tag2)~title:"Init_error"(interpreter_error_encoding"init")(functionInit_errorerr->Someerr|_->None)(funerr->Init_errorerr);case(Tag3)~title:"Eval_error"(interpreter_error_encoding"eval")(functionEval_errorerr->Someerr|_->None)(funerr->Eval_errorerr);case(Tag4)~title:"Invalid_state"(obj1(req"invalid_state"truncated_string_encoding))(functionInvalid_statemsg->Somemsg|_->None)(funmsg->Invalid_statemsg);case(Tag5)~title:"Unknown_error"(obj1(req"unknown_error"truncated_string_encoding))(functionUnknown_errorexn->Someexn|_->None)(funexn->Unknown_errorexn);case(Tag6)~title:"Too_many_ticks"(constant"too_many_ticks")(functionToo_many_ticks->Some()|_->None)(fun()->Too_many_ticks);case(Tag7)~title:"Too_many_reboots"(constant"too_many_reboots")(functionToo_many_reboots->Some()|_->None)(fun()->Too_many_reboots);case(Tag8)~title:"No_fallback_kernel"(obj1(req"no_kernel_fallback"fallback_cause_encoding))(functionNo_fallback_kernelcause->Somecause|_->None)(funcause->No_fallback_kernelcause);]letlink_errorkind~module_name~item_name=matchkindwith|`Item->Link_error(Format.sprintf"Unexpected import: %s.%s"module_nameitem_name|>truncate_message)|`Module->Link_error(Format.sprintf"Unexpected module import: %s"module_name|>truncate_message)