From 6a360fb2646783e528480992edb719361b8b197d Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Wed, 10 Feb 2021 17:34:29 -0800 Subject: [PATCH 1/9] add borsh serialize deserialize for jit artifact --- Cargo.lock | 55 +++++++++++++++++ lib/compiler/Cargo.toml | 6 +- lib/compiler/src/address_map.rs | 4 ++ lib/compiler/src/function.rs | 5 ++ lib/compiler/src/jump_table.rs | 3 + lib/compiler/src/module.rs | 26 ++++++++ lib/compiler/src/relocation.rs | 5 ++ lib/compiler/src/section.rs | 6 ++ lib/compiler/src/sourceloc.rs | 3 + lib/compiler/src/trap.rs | 2 + lib/compiler/src/unwind.rs | 3 + lib/engine-jit/Cargo.toml | 1 + lib/engine-jit/src/artifact.rs | 1 + lib/engine-jit/src/serialize.rs | 53 +++++++++++++++- lib/engine/Cargo.toml | 1 + lib/engine/src/serialize.rs | 21 ++++++- lib/vm/Cargo.toml | 1 + lib/vm/src/libcalls.rs | 3 +- lib/vm/src/memory.rs | 3 +- lib/vm/src/module.rs | 90 ++++++++++++++++++++++++++++ lib/vm/src/table.rs | 3 +- lib/vm/src/trap/trapcode.rs | 3 +- lib/wasmer-types/Cargo.toml | 4 +- lib/wasmer-types/src/features.rs | 3 + lib/wasmer-types/src/indexes.rs | 14 +++++ lib/wasmer-types/src/initializers.rs | 6 +- lib/wasmer-types/src/types.rs | 11 ++++ lib/wasmer-types/src/units.rs | 3 + 28 files changed, 329 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 685bab05cd9..4c44901f0cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -161,6 +161,47 @@ dependencies = [ "digest", ] +[[package]] +name = "borsh" +version = "0.8.1" +source = "git+https://github.com/near/borsh-rs?rev=c62cdfbd10d4a17fc877809eba4ccb65e866d5f8#c62cdfbd10d4a17fc877809eba4ccb65e866d5f8" +dependencies = [ + "borsh-derive", + "hashbrown 0.9.1", +] + +[[package]] +name = "borsh-derive" +version = "0.8.1" +source = "git+https://github.com/near/borsh-rs?rev=c62cdfbd10d4a17fc877809eba4ccb65e866d5f8#c62cdfbd10d4a17fc877809eba4ccb65e866d5f8" +dependencies = [ + "borsh-derive-internal", + "borsh-schema-derive-internal", + "proc-macro-crate", + "proc-macro2", + "syn", +] + +[[package]] +name = "borsh-derive-internal" +version = "0.8.1" +source = "git+https://github.com/near/borsh-rs?rev=c62cdfbd10d4a17fc877809eba4ccb65e866d5f8#c62cdfbd10d4a17fc877809eba4ccb65e866d5f8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "borsh-schema-derive-internal" +version = "0.8.1" +source = "git+https://github.com/near/borsh-rs?rev=c62cdfbd10d4a17fc877809eba4ccb65e866d5f8#c62cdfbd10d4a17fc877809eba4ccb65e866d5f8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "bstr" version = "0.2.14" @@ -1445,6 +1486,15 @@ dependencies = [ "treeline", ] +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -2390,6 +2440,7 @@ dependencies = [ name = "wasmer-compiler" version = "1.0.2" dependencies = [ + "borsh", "enumset", "hashbrown 0.9.1", "serde", @@ -2495,6 +2546,7 @@ version = "1.0.2" dependencies = [ "backtrace", "bincode", + "borsh", "lazy_static", "memmap2", "more-asserts", @@ -2526,6 +2578,7 @@ name = "wasmer-engine-jit" version = "1.0.2" dependencies = [ "bincode", + "borsh", "cfg-if 0.1.10", "region", "serde", @@ -2605,6 +2658,7 @@ dependencies = [ name = "wasmer-types" version = "1.0.2" dependencies = [ + "borsh", "cranelift-entity 0.68.0", "serde", "thiserror", @@ -2615,6 +2669,7 @@ name = "wasmer-vm" version = "1.0.2" dependencies = [ "backtrace", + "borsh", "cc", "cfg-if 0.1.10", "indexmap", diff --git a/lib/compiler/Cargo.toml b/lib/compiler/Cargo.toml index 92625b684b6..83dbe2b0a3c 100644 --- a/lib/compiler/Cargo.toml +++ b/lib/compiler/Cargo.toml @@ -20,10 +20,11 @@ hashbrown = { version = "0.9", optional = true } serde = { version = "1.0", features = ["derive"], optional = true } thiserror = "1.0" serde_bytes = { version = "0.11", optional = true } -smallvec = "1.6" +smallvec = "1.6" +borsh = { git = "https://github.com/near/borsh-rs", rev = "c62cdfbd10d4a17fc877809eba4ccb65e866d5f8", optional = true } [features] -default = ["std", "enable-serde"] +default = ["std", "enable-serde", "enable-borsh"] # This feature is for compiler implementors, it enables using `Compiler` and # `CompilerConfig`, as well as the included wasmparser. # Disable this feature if you just want a headless engine. @@ -31,6 +32,7 @@ translator = ["wasmparser"] std = ["wasmer-types/std"] core = ["hashbrown", "wasmer-types/core"] enable-serde = ["serde", "serde_bytes", "wasmer-types/enable-serde"] +enable-borsh = ["borsh"] [badges] maintenance = { status = "experimental" } diff --git a/lib/compiler/src/address_map.rs b/lib/compiler/src/address_map.rs index 40d37a073f6..5af281e9852 100644 --- a/lib/compiler/src/address_map.rs +++ b/lib/compiler/src/address_map.rs @@ -5,9 +5,12 @@ use crate::lib::std::vec::Vec; use crate::sourceloc::SourceLoc; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; /// Single source location to generated address mapping. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq)] pub struct InstructionAddressMap { /// Original source location. @@ -22,6 +25,7 @@ pub struct InstructionAddressMap { /// Function and its instructions addresses mappings. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct FunctionAddressMap { /// Instructions maps. diff --git a/lib/compiler/src/function.rs b/lib/compiler/src/function.rs index 6be0ae9ac52..cf66d672a4f 100644 --- a/lib/compiler/src/function.rs +++ b/lib/compiler/src/function.rs @@ -14,6 +14,8 @@ use crate::trap::TrapInformation; use crate::{CompiledFunctionUnwindInfo, FunctionAddressMap, JumpTableOffsets, Relocation}; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; use wasmer_types::entity::PrimaryMap; use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex}; @@ -22,6 +24,7 @@ use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex}; /// This structure is only used for reconstructing /// the frame information after a `Trap`. #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct CompiledFunctionFrameInfo { /// The traps (in the function body). @@ -35,6 +38,7 @@ pub struct CompiledFunctionFrameInfo { /// The function body. #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq)] pub struct FunctionBody { /// The function body bytes. @@ -79,6 +83,7 @@ pub type CustomSections = PrimaryMap; /// In the future this structure may also hold other information useful /// for debugging. #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, PartialEq, Eq, Clone)] pub struct Dwarf { /// The section index in the [`Compilation`] that corresponds to the exception frames. diff --git a/lib/compiler/src/jump_table.rs b/lib/compiler/src/jump_table.rs index 19da678c77a..36fba843103 100644 --- a/lib/compiler/src/jump_table.rs +++ b/lib/compiler/src/jump_table.rs @@ -7,6 +7,8 @@ use super::CodeOffset; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; use wasmer_types::entity::{entity_impl, SecondaryMap}; /// An opaque reference to a [jump table](https://en.wikipedia.org/wiki/Branch_table). @@ -14,6 +16,7 @@ use wasmer_types::entity::{entity_impl, SecondaryMap}; /// `JumpTable`s are used for indirect branching and are specialized for dense, /// 0-based jump offsets. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct JumpTable(u32); diff --git a/lib/compiler/src/module.rs b/lib/compiler/src/module.rs index dacbdf3560a..67d05551598 100644 --- a/lib/compiler/src/module.rs +++ b/lib/compiler/src/module.rs @@ -1,6 +1,9 @@ use crate::lib::std::sync::Arc; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; + use wasmer_types::entity::PrimaryMap; use wasmer_types::{Features, MemoryIndex, TableIndex}; use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle}; @@ -25,3 +28,26 @@ pub struct CompileModuleInfo { /// The table plans used for compiling. pub table_styles: PrimaryMap, } + +#[cfg(feature = "enable-borsh")] +impl BorshSerialize for CompileModuleInfo { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { + BorshSerialize::serialize(&self.features, writer)?; + BorshSerialize::serialize(&self.module, writer)?; + BorshSerialize::serialize(&self.memory_styles.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.table_styles.into_iter().collect::>(), writer) + } +} + +#[cfg(feature = "enable-borsh")] +impl BorshDeserialize for CompileModuleInfo { + fn deserialize(buf: &mut &[u8]) -> std::io::Result { + let features: Features = BorshDeserialize::deserialize(buf)?; + let module: Arc = BorshDeserialize::deserialize(buf)?; + let memory_styles: Vec = BorshDeserialize::deserialize(buf)?; + let memory_styles: PrimaryMap = PrimaryMap::from_iter(memory_styles.into_iter()); + let table_styles: Vec = BorshDeserialize::deserialize(buf)?; + let table_styles: PrimaryMap = PrimaryMap::from_iter(table_styles.into_iter()); + Self { features, module, memory_styles, table_styles } + } +} \ No newline at end of file diff --git a/lib/compiler/src/relocation.rs b/lib/compiler/src/relocation.rs index c0e6306ed6e..a880c50332e 100644 --- a/lib/compiler/src/relocation.rs +++ b/lib/compiler/src/relocation.rs @@ -15,12 +15,15 @@ use crate::section::SectionIndex; use crate::{Addend, CodeOffset, JumpTable}; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; use wasmer_types::entity::PrimaryMap; use wasmer_types::LocalFunctionIndex; use wasmer_vm::libcalls::LibCall; /// Relocation kinds for every ISA. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum RelocationKind { /// absolute 4-byte @@ -79,6 +82,7 @@ impl fmt::Display for RelocationKind { /// A record of a relocation to perform. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq)] pub struct Relocation { /// The relocation kind. @@ -93,6 +97,7 @@ pub struct Relocation { /// Destination function. Can be either user function or some special one, like `memory.grow`. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum RelocationTarget { /// A relocation to a function defined locally in the wasm (not an imported one). diff --git a/lib/compiler/src/section.rs b/lib/compiler/src/section.rs index 096cbcc8cf7..73c6a313053 100644 --- a/lib/compiler/src/section.rs +++ b/lib/compiler/src/section.rs @@ -9,10 +9,13 @@ use crate::lib::std::vec::Vec; use crate::Relocation; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; use wasmer_types::entity::entity_impl; /// Index type of a Section defined inside a WebAssembly `Compilation`. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] pub struct SectionIndex(u32); @@ -22,6 +25,7 @@ entity_impl!(SectionIndex); /// /// Determines how a custom section may be used. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq)] pub enum CustomSectionProtection { /// A custom section with read permission. @@ -36,6 +40,7 @@ pub enum CustomSectionProtection { /// This is used so compilers can store arbitrary information /// in the emitted module. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq)] pub struct CustomSection { /// Memory protection that applies to this section. @@ -55,6 +60,7 @@ pub struct CustomSection { /// The bytes in the section. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct SectionBody(#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))] Vec); diff --git a/lib/compiler/src/sourceloc.rs b/lib/compiler/src/sourceloc.rs index 2964e144d47..5299619d514 100644 --- a/lib/compiler/src/sourceloc.rs +++ b/lib/compiler/src/sourceloc.rs @@ -11,6 +11,8 @@ use crate::lib::std::fmt; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; /// A source location. /// @@ -21,6 +23,7 @@ use serde::{Deserialize, Serialize}; derive(Serialize, Deserialize), serde(transparent) )] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[repr(transparent)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct SourceLoc(u32); diff --git a/lib/compiler/src/trap.rs b/lib/compiler/src/trap.rs index 058f7e21c2e..ea540fe2715 100644 --- a/lib/compiler/src/trap.rs +++ b/lib/compiler/src/trap.rs @@ -1,10 +1,12 @@ use crate::CodeOffset; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +use borsh::{BorshSerialize, BorshDeserialize}; use wasmer_vm::TrapCode; /// Information about trap. #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Clone, Debug, PartialEq, Eq)] pub struct TrapInformation { /// The offset of the trapping instruction in native code. It is relative to the beginning of the function. diff --git a/lib/compiler/src/unwind.rs b/lib/compiler/src/unwind.rs index 30949281477..d227ef082ed 100644 --- a/lib/compiler/src/unwind.rs +++ b/lib/compiler/src/unwind.rs @@ -8,6 +8,8 @@ use crate::lib::std::vec::Vec; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; /// Compiled function unwind information. /// @@ -17,6 +19,7 @@ use serde::{Deserialize, Serialize}; /// /// [unwind info]: https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=vs-2019 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] #[derive(Debug, Clone, PartialEq, Eq)] pub enum CompiledFunctionUnwindInfo { /// Windows UNWIND_INFO. diff --git a/lib/engine-jit/Cargo.toml b/lib/engine-jit/Cargo.toml index a4d1c6f90d0..482f177d236 100644 --- a/lib/engine-jit/Cargo.toml +++ b/lib/engine-jit/Cargo.toml @@ -21,6 +21,7 @@ serde = { version = "1.0", features = ["derive", "rc"] } serde_bytes = { version = "0.11" } bincode = "1.3" cfg-if = "0.1" +borsh = { git = "https://github.com/near/borsh-rs", rev = "c62cdfbd10d4a17fc877809eba4ccb65e866d5f8" } [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3", features = ["winnt", "impl-default"] } diff --git a/lib/engine-jit/src/artifact.rs b/lib/engine-jit/src/artifact.rs index e9d91e10bdc..03550096c1d 100644 --- a/lib/engine-jit/src/artifact.rs +++ b/lib/engine-jit/src/artifact.rs @@ -7,6 +7,7 @@ use crate::link::link_module; use crate::serialize::SerializableCompilation; use crate::serialize::SerializableModule; use std::sync::{Arc, Mutex}; +use borsh::{BorshSerialize, BorshDeserialize}; use wasmer_compiler::{CompileError, Features, Triple}; #[cfg(feature = "compiler")] use wasmer_compiler::{CompileModuleInfo, ModuleEnvironment}; diff --git a/lib/engine-jit/src/serialize.rs b/lib/engine-jit/src/serialize.rs index 92596ff3362..97abedbe43f 100644 --- a/lib/engine-jit/src/serialize.rs +++ b/lib/engine-jit/src/serialize.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use borsh::{BorshSerialize, BorshDeserialize}; use wasmer_compiler::{ CompileModuleInfo, CustomSection, Dwarf, FunctionBody, JumpTableOffsets, Relocation, SectionIndex, @@ -6,6 +7,7 @@ use wasmer_compiler::{ use wasmer_engine::SerializableFunctionFrameInfo; use wasmer_types::entity::PrimaryMap; use wasmer_types::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex}; +use std::iter::FromIterator; // /// The serializable function data // #[derive(Serialize, Deserialize)] @@ -37,9 +39,58 @@ pub struct SerializableCompilation { /// Serializable struct that is able to serialize from and to /// a `JITArtifactInfo`. -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, BorshSerialize, BorshDeserialize)] pub struct SerializableModule { pub compilation: SerializableCompilation, pub compile_info: CompileModuleInfo, pub data_initializers: Box<[OwnedDataInitializer]>, } + +impl BorshSerialize for SerializableCompilation { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { + BorshSerialize::serialize(&self.function_bodies.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.function_relocations.into_iter().collect::>(), writer)?; + // JumpTableOffsets is a SecondaryMap, non trivial to impl borsh + let v = bincode::serialize(&self.function_jt_offsets)?; + BorshSerialize::serialize(&v, writer)?; + BorshSerialize::serialize(&self.function_frame_info.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.function_call_trampolines.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.dynamic_function_trampolines.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.custom_sections.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.custom_section_relocations.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.debug, writer)?; + } +} + +impl BorshDeserialize for SerializableCompilation { + fn deserialize(buf: &mut &[u8]) -> std::io::Result { + let function_bodies: Vec = BorshDeserialize::deserialize(buf)?; + let function_bodies = PrimaryMap::from_iter(function_bodies.into_iter()); + let function_relocations: Vec> = BorshDeserialize::deserialize(buf)?; + let function_relocations = PrimaryMap::from_iter(function_relocations.into_iter()); + let v: Vec = BorshDeserialize::deserialize(buf)?; + let function_jt_offsets = bincode::deserialize(&v)?; + let function_frame_info: Vec = BorshDeserialize::deserialize(buf)?; + let function_frame_info = PrimaryMap::from_iter(function_frame_info.into_iter()); + let function_call_trampolines: Vec = BorshDeserialize::deserialize(buf)?; + let function_call_trampolines = PrimaryMap::from_iter(function_call_trampolines.into_iter()); + let dynamic_function_trampolines: Vec = BorshDeserialize::deserialize(buf)?; + let dynamic_function_trampolines = PrimaryMap::from_iter(dynamic_function_trampolines.into_iter()); + let custom_sections: Vec = BorshDeserialize::deserialize(buf)?; + let custom_sections = PrimaryMap::from_iter(custom_sections.into_iter()); + let custom_section_relocations: Vec> = BorshDeserialize::deserialize(buf)?; + let custom_section_relocations = PrimaryMap::from_iter(custom_section_relocations.into_iter()); + let debug = BorshDeserialize::deserialize(buf)?; + Self { + function_bodies, + function_relocations, + function_jt_offsets, + function_frame_info, + function_call_trampolines, + dynamic_function_trampolines, + custom_sections, + custom_section_relocations, + debug + } + } +} \ No newline at end of file diff --git a/lib/engine/Cargo.toml b/lib/engine/Cargo.toml index eab531b0f6b..9b85572b682 100644 --- a/lib/engine/Cargo.toml +++ b/lib/engine/Cargo.toml @@ -25,6 +25,7 @@ serde = { version = "1.0", features = ["derive", "rc"] } serde_bytes = { version = "0.11" } bincode = "1.3" lazy_static = "1.4" +borsh = { git = "https://github.com/near/borsh-rs", rev = "c62cdfbd10d4a17fc877809eba4ccb65e866d5f8" } [badges] maintenance = { status = "actively-developed" } diff --git a/lib/engine/src/serialize.rs b/lib/engine/src/serialize.rs index d501332c931..d640dd401ba 100644 --- a/lib/engine/src/serialize.rs +++ b/lib/engine/src/serialize.rs @@ -1,11 +1,12 @@ use serde::de::{Deserializer, Visitor}; use serde::ser::Serializer; use serde::{Deserialize, Serialize}; +use borsh::{BorshSerialize, BorshDeserialize}; use std::fmt; use wasmer_compiler::CompiledFunctionFrameInfo; /// This is the unserialized verison of `CompiledFunctionFrameInfo`. -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] #[serde(transparent)] #[repr(transparent)] pub struct UnprocessedFunctionFrameInfo { @@ -76,6 +77,16 @@ impl Serialize for SerializableFunctionFrameInfo { } } +impl BorshSerialize for SerializableFunctionFrameInfo { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { + let unprocessed = match self { + Self::Processed(processed) => UnprocessedFunctionFrameInfo::serialize(processed), + Self::Unprocessed(unprocessed) => unprocessed.clone(), + }; + BorshSerialize::serialize(&unprocessed.bytes, writer) + } +} + struct FunctionFrameInfoVisitor; impl<'de> Visitor<'de> for FunctionFrameInfoVisitor { @@ -102,3 +113,11 @@ impl<'de> Deserialize<'de> for SerializableFunctionFrameInfo { )) } } + +impl BorshDeserialize for SerializableFunctionFrameInfo { + fn deserialize(buf: &mut &[u8]) -> std::io::Result { + Ok(Self::Unprocesed( + BorshDeserialize::deserialize(buf)? + )) + } +} \ No newline at end of file diff --git a/lib/vm/Cargo.toml b/lib/vm/Cargo.toml index 5f1c88d104c..a0c6f4ec090 100644 --- a/lib/vm/Cargo.toml +++ b/lib/vm/Cargo.toml @@ -21,6 +21,7 @@ more-asserts = "0.2" cfg-if = "0.1" backtrace = "0.3" serde = { version = "1.0", features = ["derive", "rc"] } +borsh = { git = "https://github.com/near/borsh-rs", rev = "c62cdfbd10d4a17fc877809eba4ccb65e866d5f8" } [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3", features = ["winbase", "memoryapi", "errhandlingapi"] } diff --git a/lib/vm/src/libcalls.rs b/lib/vm/src/libcalls.rs index 5267d35a6e5..38846277d5c 100644 --- a/lib/vm/src/libcalls.rs +++ b/lib/vm/src/libcalls.rs @@ -39,6 +39,7 @@ use crate::probestack::PROBESTACK; use crate::trap::{raise_lib_trap, Trap, TrapCode}; use crate::vmcontext::VMContext; use serde::{Deserialize, Serialize}; +use borsh::{BorshSerialize, BorshDeserialize}; use std::fmt; use wasmer_types::{DataIndex, ElemIndex, LocalMemoryIndex, MemoryIndex, TableIndex}; @@ -405,7 +406,7 @@ pub static wasmer_probestack: unsafe extern "C" fn() = PROBESTACK; /// The name of a runtime library routine. /// /// This list is likely to grow over time. -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] pub enum LibCall { /// ceil.f32 CeilF32, diff --git a/lib/vm/src/memory.rs b/lib/vm/src/memory.rs index 8ab78560584..24d7d211bf6 100644 --- a/lib/vm/src/memory.rs +++ b/lib/vm/src/memory.rs @@ -9,6 +9,7 @@ use crate::mmap::Mmap; use crate::vmcontext::VMMemoryDefinition; use more_asserts::assert_ge; use serde::{Deserialize, Serialize}; +use borsh::{BorshDeserialize, BorshSerialize}; use std::borrow::BorrowMut; use std::cell::UnsafeCell; use std::convert::TryInto; @@ -61,7 +62,7 @@ pub enum MemoryError { } /// Implementation styles for WebAssembly linear memory. -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] pub enum MemoryStyle { /// The actual memory can be resized and moved. Dynamic { diff --git a/lib/vm/src/module.rs b/lib/vm/src/module.rs index 79bb1b41829..f96d4259660 100644 --- a/lib/vm/src/module.rs +++ b/lib/vm/src/module.rs @@ -6,6 +6,7 @@ use indexmap::IndexMap; use serde::{Deserialize, Serialize}; +use borsh::{BorshSerialize, BorshDeserialize}; use std::collections::HashMap; use std::fmt; use std::iter::ExactSizeIterator; @@ -18,6 +19,7 @@ use wasmer_types::{ LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryIndex, MemoryType, SignatureIndex, TableIndex, TableInitializer, TableType, }; +use std::iter::FromIterator; #[derive(Debug, Clone)] pub struct ModuleId { @@ -115,6 +117,94 @@ pub struct ModuleInfo { pub num_imported_globals: usize, } +impl BorshSerialize for ModuleInfo { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { + BorshSerialize::serialize(&self.name, writer)?; + BorshSerialize::serialize(&self.imports.len(), writer)?; + for (k, v) in &self.imports { + BorshSerialize::serialize(&k, writer)?; + BorshSerialize::serialize(&v, writer)?; + } + BorshSerialize::serialize(&self.exports.len(), writer)?; + for (k, v) in &self.exports { + BorshSerialize::serialize(&k, writer)?; + BorshSerialize::serialize(&v, writer)?; + } + BorshSerialize::serialize(&self.start_function, writer)?; + BorshSerialize::serialize(&self.table_initializers, writer)?; + BorshSerialize::serialize(&self.passive_elements, writer)?; + let passive_data: Vec<_> = self.passive_data.iter().collect(); + BorshSerialize::serialize(&self.passive_data.len(), writer)?; + for (k, v) in passive_data { + BorshSerialize::serialize(&k, writer)?; + BorshSerialize::serialize(&v.as_ref(), writer)?; + } + BorshSerialize::serialize(&self.global_initializers.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.function_names, writer)?; + BorshSerialize::serialize(&self.signatures.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.functions.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.tables.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.memories.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.globals.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.custom_sections.len(), writer)?; + for (k, v) in &self.custom_sections { + BorshSerialize::serialize(&k, writer)?; + BorshSerialize::serialize(&v, writer)?; + } + BorshSerialize::serialize(&self.custom_sections_data.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.num_imported_functions, writer)?; + BorshSerialize::serialize(&self.num_imported_tables, writer)?; + BorshSerialize::serialize(&self.num_imported_memories, writer)?; + BorshSerialize::serialize(&self.num_imported_globals, writer) + } +} + +impl BorshDeserialize for ModuleInfo { + fn deserialize(buf: &mut &[u8]) -> std::io::Result { + let mut ret = Self::new(); + ret.name = BorshDeserialize::deserialize(buf)?; + let len: usize = BorshDeserialize::deserialize(buf)?; + ret.imports = IndexMap::with_capacity(len); + for _ in 0..len { + ret.imports.insert(BorshDeserialize::deserialize(buf)?, BorshDeserialize::deserialize(buf)?); + } + let len: usize = BorshDeserialize::deserialize(buf)?; + ret.exports = IndexMap::with_capacity(len); + for _ in 0..len { + ret.exports.insert(BorshDeserialize::deserialize(buf)?, BorshDeserialize::deserialize(buf)?); + } + ret.start_function = BorshDeserialize::deserialize(buf)?; + ret.table_initializers = BorshDeserialize::deserialize(buf)?; + ret.passive_elements = BorshDeserialize::deserialize(buf)?; + ret.passive_data = BorshDeserialize::deserialize(buf)?; + let global_initializers: Vec = BorshDeserialize::deserialize(buf)?; + ret.global_initializers = PrimaryMap::from_iter(global_initializers.into_iter()); + ret.function_names = BorshDeserialize::deserialize(buf)?; + let signatures: Vec = BorshDeserialize::deserialize(buf)?; + ret.signatures = PrimaryMap::from_iter(signatures.into_iter()); + let functions: Vec = BorshDeserialize::deserialize(buf)?; + ret.functions = PrimaryMap::from_iter(functions.into_iter()); + let tables: Vec = BorshDeserialize::deserialize(buf)?; + ret.tables = PrimaryMap::from_iter(tables.into_iter()); + let memories: Vec = BorshDeserialize::deserialize(buf)?; + ret.memories = PrimaryMap::from_iter(memories.into_iter()); + let globals: Vec = BorshDeserialize::deserialize(buf)?; + ret.globals = PrimaryMap::from_iter(globals.into_iter()); + let len: usize = BorshDeserialize::deserialize(buf)?; + ret.custom_sections = IndexMap::with_capacity(len); + for _ in 0..len { + ret.custom_sections.insert(BorshDeserialize::deserialize(buf)?, BorshDeserialize::deserialize(buf)?); + } + let custom_sections_data: Vec = BorshDeserialize::deserialize(buf)?; + ret.custom_sections_data = PrimaryMap::from_iter(custom_sections_data.into_iter()); + ret.num_imported_functions = BorshDeserialize::deserialize(buf)?; + ret.num_imported_tables = BorshDeserialize::deserialize(buf)?; + ret.num_imported_memories = BorshDeserialize::deserialize(buf)?; + ret.num_imported_globals = BorshDeserialize::deserialize(buf)?; + Ok(ret) + } +} + impl ModuleInfo { /// Allocates the module data structures. pub fn new() -> Self { diff --git a/lib/vm/src/table.rs b/lib/vm/src/table.rs index 66fb0e8a128..4bd7d0db8b9 100644 --- a/lib/vm/src/table.rs +++ b/lib/vm/src/table.rs @@ -8,6 +8,7 @@ use crate::trap::{Trap, TrapCode}; use crate::vmcontext::{VMCallerCheckedAnyfunc, VMTableDefinition}; use serde::{Deserialize, Serialize}; +use borsh::{BorshDeserialize, BorshSerialize}; use std::borrow::{Borrow, BorrowMut}; use std::cell::UnsafeCell; use std::convert::TryFrom; @@ -17,7 +18,7 @@ use std::sync::Mutex; use wasmer_types::{TableType, Type as ValType}; /// Implementation styles for WebAssembly tables. -#[derive(Debug, Clone, Hash, Serialize, Deserialize)] +#[derive(Debug, Clone, Hash, Serialize, Deserialize, BorshDeserialize, BorshSerialize)] pub enum TableStyle { /// Signatures are stored in the table and checked in the caller. CallerChecksSignature, diff --git a/lib/vm/src/trap/trapcode.rs b/lib/vm/src/trap/trapcode.rs index 32121fbd08e..5867d7c701f 100644 --- a/lib/vm/src/trap/trapcode.rs +++ b/lib/vm/src/trap/trapcode.rs @@ -6,12 +6,13 @@ use core::fmt::{self, Display, Formatter}; use core::str::FromStr; use serde::{Deserialize, Serialize}; +use borsh::{BorshSerialize, BorshDeserialize}; use thiserror::Error; /// A trap code describing the reason for a trap. /// /// All trap instructions have an explicit trap code. -#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Error)] +#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Error, BorshSerialize, BorshDeserialize)] #[repr(u32)] pub enum TrapCode { /// The current stack space was exhausted. diff --git a/lib/wasmer-types/Cargo.toml b/lib/wasmer-types/Cargo.toml index b211d2db48e..7aa750b83e1 100644 --- a/lib/wasmer-types/Cargo.toml +++ b/lib/wasmer-types/Cargo.toml @@ -16,9 +16,11 @@ edition = "2018" cranelift-entity = "0.68" serde = { version = "1.0", features = ["derive"], optional = true, default-features = false } thiserror = "1.0" +borsh = { git = "https://github.com/near/borsh-rs", rev = "c62cdfbd10d4a17fc877809eba4ccb65e866d5f8", optional = true } [features] -default = ["std", "enable-serde"] +default = ["std", "enable-serde", "enable-borsh"] std = ["serde/std"] core = [] enable-serde = ["serde", "cranelift-entity/enable-serde"] +enable-borsh = ["borsh"] \ No newline at end of file diff --git a/lib/wasmer-types/src/features.rs b/lib/wasmer-types/src/features.rs index 557c4e6323d..2379da3967f 100644 --- a/lib/wasmer-types/src/features.rs +++ b/lib/wasmer-types/src/features.rs @@ -1,5 +1,7 @@ #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; /// Controls which experimental features will be enabled. /// Features usually have a corresponding [WebAssembly proposal]. @@ -7,6 +9,7 @@ use serde::{Deserialize, Serialize}; /// [WebAssembly proposal]: https://github.com/WebAssembly/proposals #[derive(Clone, Debug, Eq, PartialEq)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct Features { /// Threads proposal should be enabled pub threads: bool, diff --git a/lib/wasmer-types/src/indexes.rs b/lib/wasmer-types/src/indexes.rs index 7c2247f8eb6..55e46fc0166 100644 --- a/lib/wasmer-types/src/indexes.rs +++ b/lib/wasmer-types/src/indexes.rs @@ -3,10 +3,13 @@ use crate::entity::entity_impl; use core::u32; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; /// Index type of a function defined locally inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct LocalFunctionIndex(u32); entity_impl!(LocalFunctionIndex); @@ -25,60 +28,70 @@ entity_impl!(LocalMemoryIndex); /// Index type of a global defined locally inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct LocalGlobalIndex(u32); entity_impl!(LocalGlobalIndex); /// Index type of a function (imported or local) inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct FunctionIndex(u32); entity_impl!(FunctionIndex); /// Index type of a table (imported or local) inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct TableIndex(u32); entity_impl!(TableIndex); /// Index type of a global variable (imported or local) inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct GlobalIndex(u32); entity_impl!(GlobalIndex); /// Index type of a linear memory (imported or local) inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct MemoryIndex(u32); entity_impl!(MemoryIndex); /// Index type of a signature (imported or local) inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct SignatureIndex(u32); entity_impl!(SignatureIndex); /// Index type of a passive data segment inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct DataIndex(u32); entity_impl!(DataIndex); /// Index type of a passive element segment inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct ElemIndex(u32); entity_impl!(ElemIndex); /// Index type of a custom section inside a WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct CustomSectionIndex(u32); entity_impl!(CustomSectionIndex); /// An entity to export. #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub enum ExportIndex { /// Function export. Function(FunctionIndex), @@ -93,6 +106,7 @@ pub enum ExportIndex { /// An entity to import. #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub enum ImportIndex { /// Function import. Function(FunctionIndex), diff --git a/lib/wasmer-types/src/initializers.rs b/lib/wasmer-types/src/initializers.rs index 0d47554c502..76b5b7e0d58 100644 --- a/lib/wasmer-types/src/initializers.rs +++ b/lib/wasmer-types/src/initializers.rs @@ -3,9 +3,11 @@ use crate::lib::std::boxed::Box; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; /// A WebAssembly table initializer. -#[derive(Clone, Debug, Hash, Serialize, Deserialize)] +#[derive(Clone, Debug, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] pub struct TableInitializer { /// The index of a table to initialize. pub table_index: TableIndex, @@ -21,6 +23,7 @@ pub struct TableInitializer { /// should be performed. #[derive(Clone, Debug)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct DataInitializerLocation { /// The index of the memory to initialize. pub memory_index: MemoryIndex, @@ -47,6 +50,7 @@ pub struct DataInitializer<'data> { /// holding a reference to it #[derive(Debug, Clone)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct OwnedDataInitializer { /// The location where the initialization is to be performed. pub location: DataInitializerLocation, diff --git a/lib/wasmer-types/src/types.rs b/lib/wasmer-types/src/types.rs index 6ed80e73ce5..a0778c36157 100644 --- a/lib/wasmer-types/src/types.rs +++ b/lib/wasmer-types/src/types.rs @@ -9,6 +9,9 @@ use crate::values::Value; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; + // Type Representations @@ -17,6 +20,7 @@ use serde::{Deserialize, Serialize}; /// A list of all possible value types in WebAssembly. #[derive(Copy, Debug, Clone, Eq, PartialEq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub enum Type { /// Signed 32 bit integer. I32, @@ -58,6 +62,7 @@ impl fmt::Display for Type { #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] /// The WebAssembly V128 type pub struct V128(pub(crate) [u8; 16]); @@ -227,6 +232,7 @@ impl ExternType { /// WebAssembly functions can have 0 or more parameters and results. #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct FunctionType { /// The parameters of the function params: Box<[Type]>, @@ -312,6 +318,7 @@ impl From<&FunctionType> for FunctionType { /// Indicator of whether a global is mutable or not #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub enum Mutability { /// The global is constant and its value does not change Const, @@ -348,6 +355,7 @@ impl From for bool { /// WebAssembly global. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct GlobalType { /// The type of the value stored in the global. pub ty: Type, @@ -391,6 +399,7 @@ impl fmt::Display for GlobalType { /// Globals are initialized via the `const` operators or by referring to another import. #[derive(Debug, Clone, Copy)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub enum GlobalInit { /// An `i32.const`. I32Const(i32), @@ -442,6 +451,7 @@ impl GlobalInit { /// which `call_indirect` can invoke other functions. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct TableType { /// The type of data stored in elements of the table. pub ty: Type, @@ -481,6 +491,7 @@ impl fmt::Display for TableType { /// chunks of addressable memory. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct MemoryType { /// The minimum number of pages in the memory. pub minimum: Pages, diff --git a/lib/wasmer-types/src/units.rs b/lib/wasmer-types/src/units.rs index 93e9f04d473..55e30927f8b 100644 --- a/lib/wasmer-types/src/units.rs +++ b/lib/wasmer-types/src/units.rs @@ -3,6 +3,8 @@ use crate::lib::std::fmt; use crate::lib::std::ops::{Add, Sub}; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; use std::convert::TryInto; use thiserror::Error; @@ -21,6 +23,7 @@ pub const WASM_MIN_PAGES: u32 = 0x100; /// Units of WebAssembly pages (as specified to be 65,536 bytes). #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "enable-borsh", derive(BorshSerialize, BorshDeserialize))] pub struct Pages(pub u32); impl Pages { From c0879c69b2a1e71e02f9bc6344134d5234e6937c Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Thu, 11 Feb 2021 17:45:45 -0800 Subject: [PATCH 2/9] fix all compilation errors --- lib/compiler/src/module.rs | 16 +++++++----- lib/engine-jit/src/artifact.rs | 11 ++++---- lib/engine-jit/src/serialize.rs | 38 +++++++++++++-------------- lib/engine/src/serialize.rs | 2 +- lib/vm/src/module.rs | 46 +++++++++++++++++++++------------ 5 files changed, 64 insertions(+), 49 deletions(-) diff --git a/lib/compiler/src/module.rs b/lib/compiler/src/module.rs index 67d05551598..f2a3640ae36 100644 --- a/lib/compiler/src/module.rs +++ b/lib/compiler/src/module.rs @@ -1,4 +1,5 @@ use crate::lib::std::sync::Arc; +use std::iter::FromIterator; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] @@ -33,9 +34,9 @@ pub struct CompileModuleInfo { impl BorshSerialize for CompileModuleInfo { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { BorshSerialize::serialize(&self.features, writer)?; - BorshSerialize::serialize(&self.module, writer)?; - BorshSerialize::serialize(&self.memory_styles.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.table_styles.into_iter().collect::>(), writer) + BorshSerialize::serialize(&self.module.as_ref(), writer)?; + BorshSerialize::serialize(&self.memory_styles.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.table_styles.values().collect::>(), writer) } } @@ -43,11 +44,12 @@ impl BorshSerialize for CompileModuleInfo { impl BorshDeserialize for CompileModuleInfo { fn deserialize(buf: &mut &[u8]) -> std::io::Result { let features: Features = BorshDeserialize::deserialize(buf)?; - let module: Arc = BorshDeserialize::deserialize(buf)?; + let module: ModuleInfo = BorshDeserialize::deserialize(buf)?; + let module = Arc::new(module); let memory_styles: Vec = BorshDeserialize::deserialize(buf)?; - let memory_styles: PrimaryMap = PrimaryMap::from_iter(memory_styles.into_iter()); + let memory_styles: PrimaryMap = PrimaryMap::from_iter(memory_styles); let table_styles: Vec = BorshDeserialize::deserialize(buf)?; - let table_styles: PrimaryMap = PrimaryMap::from_iter(table_styles.into_iter()); - Self { features, module, memory_styles, table_styles } + let table_styles: PrimaryMap = PrimaryMap::from_iter(table_styles); + Ok(Self { features, module, memory_styles, table_styles }) } } \ No newline at end of file diff --git a/lib/engine-jit/src/artifact.rs b/lib/engine-jit/src/artifact.rs index 03550096c1d..17811089128 100644 --- a/lib/engine-jit/src/artifact.rs +++ b/lib/engine-jit/src/artifact.rs @@ -141,12 +141,11 @@ impl JITArtifact { )); } - let inner_bytes = &bytes[Self::MAGIC_HEADER.len()..]; - + let mut inner_bytes = &bytes[Self::MAGIC_HEADER.len()..]; + println!("deserialize with borsh"); // let r = flexbuffers::Reader::get_root(bytes).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?; // let serializable = SerializableModule::deserialize(r).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?; - - let serializable: SerializableModule = bincode::deserialize(inner_bytes) + let serializable: SerializableModule = BorshDeserialize::deserialize(&mut inner_bytes) .map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?; Self::from_parts(&mut jit.inner_mut(), serializable).map_err(DeserializeError::Compiler) @@ -316,9 +315,9 @@ impl Artifact for JITArtifact { // let mut s = flexbuffers::FlexbufferSerializer::new(); // self.serializable.serialize(&mut s).map_err(|e| SerializeError::Generic(format!("{:?}", e))); // Ok(s.take_buffer()) - let bytes = bincode::serialize(&self.serializable) + let bytes = BorshSerialize::try_to_vec(&self.serializable) .map_err(|e| SerializeError::Generic(format!("{:?}", e)))?; - + println!("serialize with borsh"); // Prepend the header. let mut serialized = Self::MAGIC_HEADER.to_vec(); serialized.extend(bytes); diff --git a/lib/engine-jit/src/serialize.rs b/lib/engine-jit/src/serialize.rs index 97abedbe43f..5c0f095b01c 100644 --- a/lib/engine-jit/src/serialize.rs +++ b/lib/engine-jit/src/serialize.rs @@ -48,40 +48,40 @@ pub struct SerializableModule { impl BorshSerialize for SerializableCompilation { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - BorshSerialize::serialize(&self.function_bodies.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.function_relocations.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.function_bodies.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.function_relocations.values().collect::>(), writer)?; // JumpTableOffsets is a SecondaryMap, non trivial to impl borsh - let v = bincode::serialize(&self.function_jt_offsets)?; + let v = bincode::serialize(&self.function_jt_offsets).map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid function_jt_offsets"))?; BorshSerialize::serialize(&v, writer)?; - BorshSerialize::serialize(&self.function_frame_info.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.function_call_trampolines.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.dynamic_function_trampolines.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.custom_sections.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.custom_section_relocations.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.debug, writer)?; + BorshSerialize::serialize(&self.function_frame_info.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.function_call_trampolines.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.dynamic_function_trampolines.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.custom_sections.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.custom_section_relocations.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.debug, writer) } } impl BorshDeserialize for SerializableCompilation { fn deserialize(buf: &mut &[u8]) -> std::io::Result { let function_bodies: Vec = BorshDeserialize::deserialize(buf)?; - let function_bodies = PrimaryMap::from_iter(function_bodies.into_iter()); + let function_bodies = PrimaryMap::from_iter(function_bodies); let function_relocations: Vec> = BorshDeserialize::deserialize(buf)?; - let function_relocations = PrimaryMap::from_iter(function_relocations.into_iter()); + let function_relocations = PrimaryMap::from_iter(function_relocations); let v: Vec = BorshDeserialize::deserialize(buf)?; - let function_jt_offsets = bincode::deserialize(&v)?; + let function_jt_offsets = bincode::deserialize(&v).map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid function_jt_offsets"))?; let function_frame_info: Vec = BorshDeserialize::deserialize(buf)?; - let function_frame_info = PrimaryMap::from_iter(function_frame_info.into_iter()); + let function_frame_info = PrimaryMap::from_iter(function_frame_info); let function_call_trampolines: Vec = BorshDeserialize::deserialize(buf)?; - let function_call_trampolines = PrimaryMap::from_iter(function_call_trampolines.into_iter()); + let function_call_trampolines = PrimaryMap::from_iter(function_call_trampolines); let dynamic_function_trampolines: Vec = BorshDeserialize::deserialize(buf)?; - let dynamic_function_trampolines = PrimaryMap::from_iter(dynamic_function_trampolines.into_iter()); + let dynamic_function_trampolines = PrimaryMap::from_iter(dynamic_function_trampolines); let custom_sections: Vec = BorshDeserialize::deserialize(buf)?; - let custom_sections = PrimaryMap::from_iter(custom_sections.into_iter()); + let custom_sections = PrimaryMap::from_iter(custom_sections); let custom_section_relocations: Vec> = BorshDeserialize::deserialize(buf)?; - let custom_section_relocations = PrimaryMap::from_iter(custom_section_relocations.into_iter()); + let custom_section_relocations = PrimaryMap::from_iter(custom_section_relocations); let debug = BorshDeserialize::deserialize(buf)?; - Self { + Ok(Self { function_bodies, function_relocations, function_jt_offsets, @@ -91,6 +91,6 @@ impl BorshDeserialize for SerializableCompilation { custom_sections, custom_section_relocations, debug - } + }) } } \ No newline at end of file diff --git a/lib/engine/src/serialize.rs b/lib/engine/src/serialize.rs index d640dd401ba..d92c5163a93 100644 --- a/lib/engine/src/serialize.rs +++ b/lib/engine/src/serialize.rs @@ -116,7 +116,7 @@ impl<'de> Deserialize<'de> for SerializableFunctionFrameInfo { impl BorshDeserialize for SerializableFunctionFrameInfo { fn deserialize(buf: &mut &[u8]) -> std::io::Result { - Ok(Self::Unprocesed( + Ok(Self::Unprocessed( BorshDeserialize::deserialize(buf)? )) } diff --git a/lib/vm/src/module.rs b/lib/vm/src/module.rs index f96d4259660..caf9b472b81 100644 --- a/lib/vm/src/module.rs +++ b/lib/vm/src/module.rs @@ -139,19 +139,23 @@ impl BorshSerialize for ModuleInfo { BorshSerialize::serialize(&k, writer)?; BorshSerialize::serialize(&v.as_ref(), writer)?; } - BorshSerialize::serialize(&self.global_initializers.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.global_initializers.values().collect::>(), writer)?; BorshSerialize::serialize(&self.function_names, writer)?; - BorshSerialize::serialize(&self.signatures.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.functions.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.tables.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.memories.into_iter().collect::>(), writer)?; - BorshSerialize::serialize(&self.globals.into_iter().collect::>(), writer)?; + BorshSerialize::serialize(&self.signatures.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.functions.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.tables.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.memories.values().collect::>(), writer)?; + BorshSerialize::serialize(&self.globals.values().collect::>(), writer)?; BorshSerialize::serialize(&self.custom_sections.len(), writer)?; for (k, v) in &self.custom_sections { BorshSerialize::serialize(&k, writer)?; BorshSerialize::serialize(&v, writer)?; } - BorshSerialize::serialize(&self.custom_sections_data.into_iter().collect::>(), writer)?; + let custom_sections_data: Vec<_> = self.custom_sections_data.values().collect(); + BorshSerialize::serialize(&self.custom_sections_data.len(), writer)?; + for v in &custom_sections_data { + BorshSerialize::serialize(&v.as_ref(), writer)?; + } BorshSerialize::serialize(&self.num_imported_functions, writer)?; BorshSerialize::serialize(&self.num_imported_tables, writer)?; BorshSerialize::serialize(&self.num_imported_memories, writer)?; @@ -176,27 +180,37 @@ impl BorshDeserialize for ModuleInfo { ret.start_function = BorshDeserialize::deserialize(buf)?; ret.table_initializers = BorshDeserialize::deserialize(buf)?; ret.passive_elements = BorshDeserialize::deserialize(buf)?; - ret.passive_data = BorshDeserialize::deserialize(buf)?; + let len: usize = BorshDeserialize::deserialize(buf)?; + ret.passive_data = HashMap::with_capacity(len); + for _ in 0..len { + let kv: (DataIndex, Vec) = BorshDeserialize::deserialize(buf)?; + ret.passive_data.insert(kv.0, Arc::from(kv.1.as_ref())); + } let global_initializers: Vec = BorshDeserialize::deserialize(buf)?; - ret.global_initializers = PrimaryMap::from_iter(global_initializers.into_iter()); + ret.global_initializers = PrimaryMap::from_iter(global_initializers); ret.function_names = BorshDeserialize::deserialize(buf)?; let signatures: Vec = BorshDeserialize::deserialize(buf)?; - ret.signatures = PrimaryMap::from_iter(signatures.into_iter()); + ret.signatures = PrimaryMap::from_iter(signatures); let functions: Vec = BorshDeserialize::deserialize(buf)?; - ret.functions = PrimaryMap::from_iter(functions.into_iter()); + ret.functions = PrimaryMap::from_iter(functions); let tables: Vec = BorshDeserialize::deserialize(buf)?; - ret.tables = PrimaryMap::from_iter(tables.into_iter()); + ret.tables = PrimaryMap::from_iter(tables); let memories: Vec = BorshDeserialize::deserialize(buf)?; - ret.memories = PrimaryMap::from_iter(memories.into_iter()); + ret.memories = PrimaryMap::from_iter(memories); let globals: Vec = BorshDeserialize::deserialize(buf)?; - ret.globals = PrimaryMap::from_iter(globals.into_iter()); + ret.globals = PrimaryMap::from_iter(globals); let len: usize = BorshDeserialize::deserialize(buf)?; ret.custom_sections = IndexMap::with_capacity(len); for _ in 0..len { ret.custom_sections.insert(BorshDeserialize::deserialize(buf)?, BorshDeserialize::deserialize(buf)?); } - let custom_sections_data: Vec = BorshDeserialize::deserialize(buf)?; - ret.custom_sections_data = PrimaryMap::from_iter(custom_sections_data.into_iter()); + let len : usize = BorshDeserialize::deserialize(buf)?; + let mut custom_sections_data = Vec::with_capacity(len); + for _ in 0..len { + let v: Vec = BorshDeserialize::deserialize(buf)?; + custom_sections_data.push(Arc::from(v.as_ref())); + } + ret.custom_sections_data = PrimaryMap::from_iter(custom_sections_data); ret.num_imported_functions = BorshDeserialize::deserialize(buf)?; ret.num_imported_tables = BorshDeserialize::deserialize(buf)?; ret.num_imported_memories = BorshDeserialize::deserialize(buf)?; From 74832f6fd21d20a0237f430d958f95d0abb37639 Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Thu, 11 Feb 2021 17:54:43 -0800 Subject: [PATCH 3/9] replace bincode also in UnprocessedFunctionFrameInfo --- lib/engine/src/serialize.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/engine/src/serialize.rs b/lib/engine/src/serialize.rs index d92c5163a93..1efe6222a04 100644 --- a/lib/engine/src/serialize.rs +++ b/lib/engine/src/serialize.rs @@ -19,7 +19,7 @@ impl UnprocessedFunctionFrameInfo { pub fn deserialize(&self) -> CompiledFunctionFrameInfo { // let r = flexbuffers::Reader::get_root(&self.bytes).expect("Can't deserialize the info"); // CompiledFunctionFrameInfo::deserialize(r).expect("Can't deserialize the info") - bincode::deserialize(&self.bytes).expect("Can't deserialize the info") + BorshDeserialize::deserialize(&mut self.bytes.as_ref()).expect("Can't deserialize the info") } /// Converts the `CompiledFunctionFrameInfo` to a `UnprocessedFunctionFrameInfo` @@ -29,7 +29,7 @@ impl UnprocessedFunctionFrameInfo { // .serialize(&mut s) // .expect("Can't serialize the info"); // let bytes = s.take_buffer(); - let bytes = bincode::serialize(&processed).expect("Can't serialize the info"); + let bytes = BorshSerialize::try_to_vec(&processed).expect("Can't serialize the info"); Self { bytes } } } From 0260c1df52ce6de531ce4bfbc35b5fc8cc6e76a5 Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Tue, 16 Feb 2021 14:28:30 -0800 Subject: [PATCH 4/9] Update lib/engine-jit/src/artifact.rs Co-authored-by: nlewycky --- lib/engine-jit/src/artifact.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/engine-jit/src/artifact.rs b/lib/engine-jit/src/artifact.rs index 17811089128..1a596c58463 100644 --- a/lib/engine-jit/src/artifact.rs +++ b/lib/engine-jit/src/artifact.rs @@ -142,7 +142,6 @@ impl JITArtifact { } let mut inner_bytes = &bytes[Self::MAGIC_HEADER.len()..]; - println!("deserialize with borsh"); // let r = flexbuffers::Reader::get_root(bytes).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?; // let serializable = SerializableModule::deserialize(r).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?; let serializable: SerializableModule = BorshDeserialize::deserialize(&mut inner_bytes) From 790299cd6ffd85cda052d548e277f4e74e0011be Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Tue, 16 Feb 2021 14:28:37 -0800 Subject: [PATCH 5/9] Update lib/engine-jit/src/artifact.rs Co-authored-by: nlewycky --- lib/engine-jit/src/artifact.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/engine-jit/src/artifact.rs b/lib/engine-jit/src/artifact.rs index 1a596c58463..f10e11c074d 100644 --- a/lib/engine-jit/src/artifact.rs +++ b/lib/engine-jit/src/artifact.rs @@ -316,7 +316,6 @@ impl Artifact for JITArtifact { // Ok(s.take_buffer()) let bytes = BorshSerialize::try_to_vec(&self.serializable) .map_err(|e| SerializeError::Generic(format!("{:?}", e)))?; - println!("serialize with borsh"); // Prepend the header. let mut serialized = Self::MAGIC_HEADER.to_vec(); serialized.extend(bytes); From 524e5b3f11615de3afa1fdcaae01cd3778b375e4 Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Tue, 16 Feb 2021 14:45:10 -0800 Subject: [PATCH 6/9] newline eof --- lib/compiler/src/module.rs | 2 +- lib/engine/src/serialize.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compiler/src/module.rs b/lib/compiler/src/module.rs index f2a3640ae36..4ec26f37550 100644 --- a/lib/compiler/src/module.rs +++ b/lib/compiler/src/module.rs @@ -52,4 +52,4 @@ impl BorshDeserialize for CompileModuleInfo { let table_styles: PrimaryMap = PrimaryMap::from_iter(table_styles); Ok(Self { features, module, memory_styles, table_styles }) } -} \ No newline at end of file +} diff --git a/lib/engine/src/serialize.rs b/lib/engine/src/serialize.rs index 1efe6222a04..ef77d42e34a 100644 --- a/lib/engine/src/serialize.rs +++ b/lib/engine/src/serialize.rs @@ -120,4 +120,4 @@ impl BorshDeserialize for SerializableFunctionFrameInfo { BorshDeserialize::deserialize(buf)? )) } -} \ No newline at end of file +} From 4c28d8678a26eae823246a50e303085c8d62ab3e Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Tue, 16 Feb 2021 15:32:13 -0800 Subject: [PATCH 7/9] factor ser/de indexmap into fn, cargo fmt --- lib/compiler/src/address_map.rs | 4 +- lib/compiler/src/function.rs | 4 +- lib/compiler/src/jump_table.rs | 4 +- lib/compiler/src/module.rs | 16 ++++-- lib/compiler/src/relocation.rs | 4 +- lib/compiler/src/section.rs | 4 +- lib/compiler/src/sourceloc.rs | 4 +- lib/compiler/src/trap.rs | 2 +- lib/compiler/src/unwind.rs | 4 +- lib/engine-jit/src/artifact.rs | 4 +- lib/engine-jit/src/serialize.rs | 53 +++++++++++++++----- lib/engine/src/serialize.rs | 6 +-- lib/vm/src/libcalls.rs | 15 +++++- lib/vm/src/memory.rs | 6 ++- lib/vm/src/module.rs | 75 ++++++++++++++++------------ lib/vm/src/table.rs | 2 +- lib/vm/src/trap/trapcode.rs | 16 +++++- lib/wasmer-types/src/features.rs | 4 +- lib/wasmer-types/src/indexes.rs | 4 +- lib/wasmer-types/src/initializers.rs | 4 +- lib/wasmer-types/src/types.rs | 5 +- lib/wasmer-types/src/units.rs | 4 +- 22 files changed, 155 insertions(+), 89 deletions(-) diff --git a/lib/compiler/src/address_map.rs b/lib/compiler/src/address_map.rs index 5af281e9852..07b959e3e74 100644 --- a/lib/compiler/src/address_map.rs +++ b/lib/compiler/src/address_map.rs @@ -3,10 +3,10 @@ use crate::lib::std::vec::Vec; use crate::sourceloc::SourceLoc; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// Single source location to generated address mapping. #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] diff --git a/lib/compiler/src/function.rs b/lib/compiler/src/function.rs index cf66d672a4f..1b3ac3a30a9 100644 --- a/lib/compiler/src/function.rs +++ b/lib/compiler/src/function.rs @@ -12,10 +12,10 @@ use crate::lib::std::vec::Vec; use crate::section::{CustomSection, SectionIndex}; use crate::trap::TrapInformation; use crate::{CompiledFunctionUnwindInfo, FunctionAddressMap, JumpTableOffsets, Relocation}; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; use wasmer_types::entity::PrimaryMap; use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex}; diff --git a/lib/compiler/src/jump_table.rs b/lib/compiler/src/jump_table.rs index 36fba843103..9a848cff96b 100644 --- a/lib/compiler/src/jump_table.rs +++ b/lib/compiler/src/jump_table.rs @@ -5,10 +5,10 @@ //! [Learn more](https://en.wikipedia.org/wiki/Branch_table). use super::CodeOffset; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; use wasmer_types::entity::{entity_impl, SecondaryMap}; /// An opaque reference to a [jump table](https://en.wikipedia.org/wiki/Branch_table). diff --git a/lib/compiler/src/module.rs b/lib/compiler/src/module.rs index 4ec26f37550..ab1d511cdad 100644 --- a/lib/compiler/src/module.rs +++ b/lib/compiler/src/module.rs @@ -1,9 +1,9 @@ use crate::lib::std::sync::Arc; -use std::iter::FromIterator; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; +use std::iter::FromIterator; use wasmer_types::entity::PrimaryMap; use wasmer_types::{Features, MemoryIndex, TableIndex}; @@ -47,9 +47,15 @@ impl BorshDeserialize for CompileModuleInfo { let module: ModuleInfo = BorshDeserialize::deserialize(buf)?; let module = Arc::new(module); let memory_styles: Vec = BorshDeserialize::deserialize(buf)?; - let memory_styles: PrimaryMap = PrimaryMap::from_iter(memory_styles); + let memory_styles: PrimaryMap = + PrimaryMap::from_iter(memory_styles); let table_styles: Vec = BorshDeserialize::deserialize(buf)?; let table_styles: PrimaryMap = PrimaryMap::from_iter(table_styles); - Ok(Self { features, module, memory_styles, table_styles }) + Ok(Self { + features, + module, + memory_styles, + table_styles, + }) } } diff --git a/lib/compiler/src/relocation.rs b/lib/compiler/src/relocation.rs index a880c50332e..6116eafc7f4 100644 --- a/lib/compiler/src/relocation.rs +++ b/lib/compiler/src/relocation.rs @@ -13,10 +13,10 @@ use crate::lib::std::fmt; use crate::lib::std::vec::Vec; use crate::section::SectionIndex; use crate::{Addend, CodeOffset, JumpTable}; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; use wasmer_types::entity::PrimaryMap; use wasmer_types::LocalFunctionIndex; use wasmer_vm::libcalls::LibCall; diff --git a/lib/compiler/src/section.rs b/lib/compiler/src/section.rs index 73c6a313053..d9187a399d5 100644 --- a/lib/compiler/src/section.rs +++ b/lib/compiler/src/section.rs @@ -7,10 +7,10 @@ use crate::lib::std::vec::Vec; use crate::Relocation; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; use wasmer_types::entity::entity_impl; /// Index type of a Section defined inside a WebAssembly `Compilation`. diff --git a/lib/compiler/src/sourceloc.rs b/lib/compiler/src/sourceloc.rs index 5299619d514..ecd99f98fb7 100644 --- a/lib/compiler/src/sourceloc.rs +++ b/lib/compiler/src/sourceloc.rs @@ -9,10 +9,10 @@ use crate::lib::std::fmt; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// A source location. /// diff --git a/lib/compiler/src/trap.rs b/lib/compiler/src/trap.rs index ea540fe2715..6dc742226df 100644 --- a/lib/compiler/src/trap.rs +++ b/lib/compiler/src/trap.rs @@ -1,7 +1,7 @@ use crate::CodeOffset; +use borsh::{BorshDeserialize, BorshSerialize}; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; -use borsh::{BorshSerialize, BorshDeserialize}; use wasmer_vm::TrapCode; /// Information about trap. diff --git a/lib/compiler/src/unwind.rs b/lib/compiler/src/unwind.rs index d227ef082ed..deddded35cd 100644 --- a/lib/compiler/src/unwind.rs +++ b/lib/compiler/src/unwind.rs @@ -6,10 +6,10 @@ //! //! [Learn more](https://en.wikipedia.org/wiki/Call_stack). use crate::lib::std::vec::Vec; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// Compiled function unwind information. /// diff --git a/lib/engine-jit/src/artifact.rs b/lib/engine-jit/src/artifact.rs index f10e11c074d..8433b800efb 100644 --- a/lib/engine-jit/src/artifact.rs +++ b/lib/engine-jit/src/artifact.rs @@ -6,8 +6,8 @@ use crate::link::link_module; #[cfg(feature = "compiler")] use crate::serialize::SerializableCompilation; use crate::serialize::SerializableModule; +use borsh::{BorshDeserialize, BorshSerialize}; use std::sync::{Arc, Mutex}; -use borsh::{BorshSerialize, BorshDeserialize}; use wasmer_compiler::{CompileError, Features, Triple}; #[cfg(feature = "compiler")] use wasmer_compiler::{CompileModuleInfo, ModuleEnvironment}; @@ -141,7 +141,7 @@ impl JITArtifact { )); } - let mut inner_bytes = &bytes[Self::MAGIC_HEADER.len()..]; + let mut inner_bytes = &bytes[Self::MAGIC_HEADER.len()..]; // let r = flexbuffers::Reader::get_root(bytes).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?; // let serializable = SerializableModule::deserialize(r).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?; let serializable: SerializableModule = BorshDeserialize::deserialize(&mut inner_bytes) diff --git a/lib/engine-jit/src/serialize.rs b/lib/engine-jit/src/serialize.rs index 5c0f095b01c..23cd3cd6307 100644 --- a/lib/engine-jit/src/serialize.rs +++ b/lib/engine-jit/src/serialize.rs @@ -1,5 +1,6 @@ +use borsh::{BorshDeserialize, BorshSerialize}; use serde::{Deserialize, Serialize}; -use borsh::{BorshSerialize, BorshDeserialize}; +use std::iter::FromIterator; use wasmer_compiler::{ CompileModuleInfo, CustomSection, Dwarf, FunctionBody, JumpTableOffsets, Relocation, SectionIndex, @@ -7,7 +8,6 @@ use wasmer_compiler::{ use wasmer_engine::SerializableFunctionFrameInfo; use wasmer_types::entity::PrimaryMap; use wasmer_types::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex}; -use std::iter::FromIterator; // /// The serializable function data // #[derive(Serialize, Deserialize)] @@ -49,15 +49,38 @@ pub struct SerializableModule { impl BorshSerialize for SerializableCompilation { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { BorshSerialize::serialize(&self.function_bodies.values().collect::>(), writer)?; - BorshSerialize::serialize(&self.function_relocations.values().collect::>(), writer)?; + BorshSerialize::serialize( + &self.function_relocations.values().collect::>(), + writer, + )?; // JumpTableOffsets is a SecondaryMap, non trivial to impl borsh - let v = bincode::serialize(&self.function_jt_offsets).map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid function_jt_offsets"))?; + let v = bincode::serialize(&self.function_jt_offsets).map_err(|_| { + std::io::Error::new( + std::io::ErrorKind::InvalidData, + "invalid function_jt_offsets", + ) + })?; BorshSerialize::serialize(&v, writer)?; - BorshSerialize::serialize(&self.function_frame_info.values().collect::>(), writer)?; - BorshSerialize::serialize(&self.function_call_trampolines.values().collect::>(), writer)?; - BorshSerialize::serialize(&self.dynamic_function_trampolines.values().collect::>(), writer)?; + BorshSerialize::serialize( + &self.function_frame_info.values().collect::>(), + writer, + )?; + BorshSerialize::serialize( + &self.function_call_trampolines.values().collect::>(), + writer, + )?; + BorshSerialize::serialize( + &self + .dynamic_function_trampolines + .values() + .collect::>(), + writer, + )?; BorshSerialize::serialize(&self.custom_sections.values().collect::>(), writer)?; - BorshSerialize::serialize(&self.custom_section_relocations.values().collect::>(), writer)?; + BorshSerialize::serialize( + &self.custom_section_relocations.values().collect::>(), + writer, + )?; BorshSerialize::serialize(&self.debug, writer) } } @@ -69,8 +92,14 @@ impl BorshDeserialize for SerializableCompilation { let function_relocations: Vec> = BorshDeserialize::deserialize(buf)?; let function_relocations = PrimaryMap::from_iter(function_relocations); let v: Vec = BorshDeserialize::deserialize(buf)?; - let function_jt_offsets = bincode::deserialize(&v).map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid function_jt_offsets"))?; - let function_frame_info: Vec = BorshDeserialize::deserialize(buf)?; + let function_jt_offsets = bincode::deserialize(&v).map_err(|_| { + std::io::Error::new( + std::io::ErrorKind::InvalidData, + "invalid function_jt_offsets", + ) + })?; + let function_frame_info: Vec = + BorshDeserialize::deserialize(buf)?; let function_frame_info = PrimaryMap::from_iter(function_frame_info); let function_call_trampolines: Vec = BorshDeserialize::deserialize(buf)?; let function_call_trampolines = PrimaryMap::from_iter(function_call_trampolines); @@ -90,7 +119,7 @@ impl BorshDeserialize for SerializableCompilation { dynamic_function_trampolines, custom_sections, custom_section_relocations, - debug + debug, }) } -} \ No newline at end of file +} diff --git a/lib/engine/src/serialize.rs b/lib/engine/src/serialize.rs index ef77d42e34a..0fb719b479b 100644 --- a/lib/engine/src/serialize.rs +++ b/lib/engine/src/serialize.rs @@ -1,7 +1,7 @@ +use borsh::{BorshDeserialize, BorshSerialize}; use serde::de::{Deserializer, Visitor}; use serde::ser::Serializer; use serde::{Deserialize, Serialize}; -use borsh::{BorshSerialize, BorshDeserialize}; use std::fmt; use wasmer_compiler::CompiledFunctionFrameInfo; @@ -116,8 +116,6 @@ impl<'de> Deserialize<'de> for SerializableFunctionFrameInfo { impl BorshDeserialize for SerializableFunctionFrameInfo { fn deserialize(buf: &mut &[u8]) -> std::io::Result { - Ok(Self::Unprocessed( - BorshDeserialize::deserialize(buf)? - )) + Ok(Self::Unprocessed(BorshDeserialize::deserialize(buf)?)) } } diff --git a/lib/vm/src/libcalls.rs b/lib/vm/src/libcalls.rs index 38846277d5c..200cfe34d9d 100644 --- a/lib/vm/src/libcalls.rs +++ b/lib/vm/src/libcalls.rs @@ -38,8 +38,8 @@ use crate::probestack::PROBESTACK; use crate::trap::{raise_lib_trap, Trap, TrapCode}; use crate::vmcontext::VMContext; +use borsh::{BorshDeserialize, BorshSerialize}; use serde::{Deserialize, Serialize}; -use borsh::{BorshSerialize, BorshDeserialize}; use std::fmt; use wasmer_types::{DataIndex, ElemIndex, LocalMemoryIndex, MemoryIndex, TableIndex}; @@ -406,7 +406,18 @@ pub static wasmer_probestack: unsafe extern "C" fn() = PROBESTACK; /// The name of a runtime library routine. /// /// This list is likely to grow over time. -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] +#[derive( + Copy, + Clone, + Debug, + PartialEq, + Eq, + Hash, + Serialize, + Deserialize, + BorshSerialize, + BorshDeserialize, +)] pub enum LibCall { /// ceil.f32 CeilF32, diff --git a/lib/vm/src/memory.rs b/lib/vm/src/memory.rs index 24d7d211bf6..0e57b4263fd 100644 --- a/lib/vm/src/memory.rs +++ b/lib/vm/src/memory.rs @@ -7,9 +7,9 @@ use crate::mmap::Mmap; use crate::vmcontext::VMMemoryDefinition; +use borsh::{BorshDeserialize, BorshSerialize}; use more_asserts::assert_ge; use serde::{Deserialize, Serialize}; -use borsh::{BorshDeserialize, BorshSerialize}; use std::borrow::BorrowMut; use std::cell::UnsafeCell; use std::convert::TryInto; @@ -62,7 +62,9 @@ pub enum MemoryError { } /// Implementation styles for WebAssembly linear memory. -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] +#[derive( + Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize, +)] pub enum MemoryStyle { /// The actual memory can be resized and moved. Dynamic { diff --git a/lib/vm/src/module.rs b/lib/vm/src/module.rs index caf9b472b81..806b01f7665 100644 --- a/lib/vm/src/module.rs +++ b/lib/vm/src/module.rs @@ -4,12 +4,13 @@ //! Data structure for representing WebAssembly modules in a //! `wasmer::Module`. +use borsh::{BorshDeserialize, BorshSerialize}; use indexmap::IndexMap; use serde::{Deserialize, Serialize}; -use borsh::{BorshSerialize, BorshDeserialize}; use std::collections::HashMap; use std::fmt; use std::iter::ExactSizeIterator; +use std::iter::FromIterator; use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; use std::sync::Arc; use wasmer_types::entity::{EntityRef, PrimaryMap}; @@ -19,7 +20,6 @@ use wasmer_types::{ LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryIndex, MemoryType, SignatureIndex, TableIndex, TableInitializer, TableType, }; -use std::iter::FromIterator; #[derive(Debug, Clone)] pub struct ModuleId { @@ -117,19 +117,27 @@ pub struct ModuleInfo { pub num_imported_globals: usize, } +fn borsh_serialize_index_map< + K: BorshSerialize + core::hash::Hash, + V: BorshSerialize, + W: std::io::Write, +>( + index_map: &IndexMap, + writer: &mut W, +) -> std::io::Result<()> { + BorshSerialize::serialize(&index_map.len(), writer)?; + for (k, v) in index_map { + BorshSerialize::serialize(&k, writer)?; + BorshSerialize::serialize(&v, writer)?; + } + Ok(()) +} + impl BorshSerialize for ModuleInfo { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { BorshSerialize::serialize(&self.name, writer)?; - BorshSerialize::serialize(&self.imports.len(), writer)?; - for (k, v) in &self.imports { - BorshSerialize::serialize(&k, writer)?; - BorshSerialize::serialize(&v, writer)?; - } - BorshSerialize::serialize(&self.exports.len(), writer)?; - for (k, v) in &self.exports { - BorshSerialize::serialize(&k, writer)?; - BorshSerialize::serialize(&v, writer)?; - } + borsh_serialize_index_map(&self.imports, writer)?; + borsh_serialize_index_map(&self.exports, writer)?; BorshSerialize::serialize(&self.start_function, writer)?; BorshSerialize::serialize(&self.table_initializers, writer)?; BorshSerialize::serialize(&self.passive_elements, writer)?; @@ -139,18 +147,17 @@ impl BorshSerialize for ModuleInfo { BorshSerialize::serialize(&k, writer)?; BorshSerialize::serialize(&v.as_ref(), writer)?; } - BorshSerialize::serialize(&self.global_initializers.values().collect::>(), writer)?; + BorshSerialize::serialize( + &self.global_initializers.values().collect::>(), + writer, + )?; BorshSerialize::serialize(&self.function_names, writer)?; BorshSerialize::serialize(&self.signatures.values().collect::>(), writer)?; BorshSerialize::serialize(&self.functions.values().collect::>(), writer)?; BorshSerialize::serialize(&self.tables.values().collect::>(), writer)?; BorshSerialize::serialize(&self.memories.values().collect::>(), writer)?; BorshSerialize::serialize(&self.globals.values().collect::>(), writer)?; - BorshSerialize::serialize(&self.custom_sections.len(), writer)?; - for (k, v) in &self.custom_sections { - BorshSerialize::serialize(&k, writer)?; - BorshSerialize::serialize(&v, writer)?; - } + borsh_serialize_index_map(&self.custom_sections, writer)?; let custom_sections_data: Vec<_> = self.custom_sections_data.values().collect(); BorshSerialize::serialize(&self.custom_sections_data.len(), writer)?; for v in &custom_sections_data { @@ -163,20 +170,26 @@ impl BorshSerialize for ModuleInfo { } } +fn borsh_deserialize_index_map( + buf: &mut &[u8], +) -> std::io::Result> { + let len: usize = BorshDeserialize::deserialize(buf)?; + let mut ret = IndexMap::with_capacity(len); + for _ in 0..len { + ret.insert( + BorshDeserialize::deserialize(buf)?, + BorshDeserialize::deserialize(buf)?, + ); + } + Ok(ret) +} + impl BorshDeserialize for ModuleInfo { fn deserialize(buf: &mut &[u8]) -> std::io::Result { let mut ret = Self::new(); ret.name = BorshDeserialize::deserialize(buf)?; - let len: usize = BorshDeserialize::deserialize(buf)?; - ret.imports = IndexMap::with_capacity(len); - for _ in 0..len { - ret.imports.insert(BorshDeserialize::deserialize(buf)?, BorshDeserialize::deserialize(buf)?); - } - let len: usize = BorshDeserialize::deserialize(buf)?; - ret.exports = IndexMap::with_capacity(len); - for _ in 0..len { - ret.exports.insert(BorshDeserialize::deserialize(buf)?, BorshDeserialize::deserialize(buf)?); - } + ret.imports = borsh_deserialize_index_map(buf)?; + ret.exports = borsh_deserialize_index_map(buf)?; ret.start_function = BorshDeserialize::deserialize(buf)?; ret.table_initializers = BorshDeserialize::deserialize(buf)?; ret.passive_elements = BorshDeserialize::deserialize(buf)?; @@ -199,12 +212,8 @@ impl BorshDeserialize for ModuleInfo { ret.memories = PrimaryMap::from_iter(memories); let globals: Vec = BorshDeserialize::deserialize(buf)?; ret.globals = PrimaryMap::from_iter(globals); + ret.custom_sections = borsh_deserialize_index_map(buf)?; let len: usize = BorshDeserialize::deserialize(buf)?; - ret.custom_sections = IndexMap::with_capacity(len); - for _ in 0..len { - ret.custom_sections.insert(BorshDeserialize::deserialize(buf)?, BorshDeserialize::deserialize(buf)?); - } - let len : usize = BorshDeserialize::deserialize(buf)?; let mut custom_sections_data = Vec::with_capacity(len); for _ in 0..len { let v: Vec = BorshDeserialize::deserialize(buf)?; diff --git a/lib/vm/src/table.rs b/lib/vm/src/table.rs index 4bd7d0db8b9..032b332e03c 100644 --- a/lib/vm/src/table.rs +++ b/lib/vm/src/table.rs @@ -7,8 +7,8 @@ use crate::trap::{Trap, TrapCode}; use crate::vmcontext::{VMCallerCheckedAnyfunc, VMTableDefinition}; -use serde::{Deserialize, Serialize}; use borsh::{BorshDeserialize, BorshSerialize}; +use serde::{Deserialize, Serialize}; use std::borrow::{Borrow, BorrowMut}; use std::cell::UnsafeCell; use std::convert::TryFrom; diff --git a/lib/vm/src/trap/trapcode.rs b/lib/vm/src/trap/trapcode.rs index 5867d7c701f..36b36a75814 100644 --- a/lib/vm/src/trap/trapcode.rs +++ b/lib/vm/src/trap/trapcode.rs @@ -3,16 +3,28 @@ //! Trap codes describing the reason for a trap. +use borsh::{BorshDeserialize, BorshSerialize}; use core::fmt::{self, Display, Formatter}; use core::str::FromStr; use serde::{Deserialize, Serialize}; -use borsh::{BorshSerialize, BorshDeserialize}; use thiserror::Error; /// A trap code describing the reason for a trap. /// /// All trap instructions have an explicit trap code. -#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Error, BorshSerialize, BorshDeserialize)] +#[derive( + Clone, + Copy, + PartialEq, + Eq, + Debug, + Hash, + Serialize, + Deserialize, + Error, + BorshSerialize, + BorshDeserialize, +)] #[repr(u32)] pub enum TrapCode { /// The current stack space was exhausted. diff --git a/lib/wasmer-types/src/features.rs b/lib/wasmer-types/src/features.rs index 2379da3967f..13ddb4d8bf0 100644 --- a/lib/wasmer-types/src/features.rs +++ b/lib/wasmer-types/src/features.rs @@ -1,7 +1,7 @@ -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// Controls which experimental features will be enabled. /// Features usually have a corresponding [WebAssembly proposal]. diff --git a/lib/wasmer-types/src/indexes.rs b/lib/wasmer-types/src/indexes.rs index 55e46fc0166..9626a39491f 100644 --- a/lib/wasmer-types/src/indexes.rs +++ b/lib/wasmer-types/src/indexes.rs @@ -1,10 +1,10 @@ //! Helper functions and structures for the translation. use crate::entity::entity_impl; +#[cfg(feature = "enable-borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; use core::u32; #[cfg(feature = "enable-serde")] use serde::{Deserialize, Serialize}; -#[cfg(feature = "enable-borsh")] -use borsh::{BorshDeserialize, BorshSerialize}; /// Index type of a function defined locally inside the WebAssembly module. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] diff --git a/lib/wasmer-types/src/initializers.rs b/lib/wasmer-types/src/initializers.rs index 76b5b7e0d58..e49b6ec10af 100644 --- a/lib/wasmer-types/src/initializers.rs +++ b/lib/wasmer-types/src/initializers.rs @@ -1,10 +1,10 @@ use crate::indexes::{FunctionIndex, GlobalIndex, MemoryIndex, TableIndex}; use crate::lib::std::boxed::Box; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; /// A WebAssembly table initializer. #[derive(Clone, Debug, Hash, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] diff --git a/lib/wasmer-types/src/types.rs b/lib/wasmer-types/src/types.rs index a0778c36157..c6c3c416f32 100644 --- a/lib/wasmer-types/src/types.rs +++ b/lib/wasmer-types/src/types.rs @@ -7,11 +7,10 @@ use crate::lib::std::vec::Vec; use crate::units::Pages; use crate::values::Value; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; - +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; // Type Representations diff --git a/lib/wasmer-types/src/units.rs b/lib/wasmer-types/src/units.rs index 55e30927f8b..70f29072c3f 100644 --- a/lib/wasmer-types/src/units.rs +++ b/lib/wasmer-types/src/units.rs @@ -1,10 +1,10 @@ use crate::lib::std::convert::TryFrom; use crate::lib::std::fmt; use crate::lib::std::ops::{Add, Sub}; -#[cfg(feature = "enable-serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "enable-borsh")] use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "enable-serde")] +use serde::{Deserialize, Serialize}; use std::convert::TryInto; use thiserror::Error; From bf4ddb905530323fe08229d3b82272ef33a51806 Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Tue, 23 Feb 2021 18:51:45 -0800 Subject: [PATCH 8/9] bench store module in cache --- Cargo.lock | 66 +++++++++++++++++++-- lib/cache/Cargo.toml | 11 ++++ lib/cache/benches/bench_filesystem_cache.rs | 35 +++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 lib/cache/benches/bench_filesystem_cache.rs diff --git a/Cargo.lock b/Cargo.lock index 9bb7953a873..a5a8a1d0d1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -800,6 +800,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "generational-arena" version = "0.2.8" @@ -1514,6 +1520,19 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + [[package]] name = "rand" version = "0.8.3" @@ -1522,7 +1541,7 @@ checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ "libc", "rand_chacha", - "rand_core", + "rand_core 0.6.2", "rand_hc", ] @@ -1533,9 +1552,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.2", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", ] +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.6.2" @@ -1551,7 +1585,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ - "rand_core", + "rand_core 0.6.2", ] [[package]] @@ -1588,6 +1622,15 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -1984,6 +2027,16 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ee5a98e506fb7231a304c3a1bd7c132a55016cf65001e0282480665870dfcb9" +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +dependencies = [ + "rand 0.4.6", + "remove_dir_all", +] + [[package]] name = "tempfile" version = "3.2.0" @@ -1992,7 +2045,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ "cfg-if 1.0.0", "libc", - "rand", + "rand 0.8.3", "redox_syscall 0.2.5", "remove_dir_all", "winapi", @@ -2377,9 +2430,14 @@ name = "wasmer-cache" version = "1.0.2" dependencies = [ "blake3", + "criterion", "hex", + "rand 0.8.3", + "tempdir", "thiserror", "wasmer", + "wasmer-compiler-singlepass", + "wasmer-engine-jit", ] [[package]] diff --git a/lib/cache/Cargo.toml b/lib/cache/Cargo.toml index 932fbb71459..dea167f40db 100644 --- a/lib/cache/Cargo.toml +++ b/lib/cache/Cargo.toml @@ -15,3 +15,14 @@ wasmer = { path = "../api", version = "1.0.2", default-features = false } hex = "0.4" thiserror = "1" blake3 = "0.3" + +[dev-dependencies] +criterion = "0.3" +tempdir = "0.3.7" +rand = "0.8.3" +wasmer-compiler-singlepass = { path = "../compiler-singlepass", version = "1.0.2" } +wasmer-engine-jit = { path = "../engine-jit", version = "1.0.2" } + +[[bench]] +name = "bench_filesystem_cache" +harness = false \ No newline at end of file diff --git a/lib/cache/benches/bench_filesystem_cache.rs b/lib/cache/benches/bench_filesystem_cache.rs new file mode 100644 index 00000000000..8488725ce19 --- /dev/null +++ b/lib/cache/benches/bench_filesystem_cache.rs @@ -0,0 +1,35 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use tempdir::TempDir; +use wasmer_cache::{FileSystemCache, Hash}; +use rand::{thread_rng, Rng}; +use rand::distributions::Alphanumeric; +use wasmer_compiler_singlepass::Singlepass; +use wasmer::{Module, Store}; +use wasmer_cache::Cache; +use wasmer_engine_jit::JIT; + +fn random_key() -> Hash { + Hash::new(rand::thread_rng().gen::<[u8; 32]>()) +} + +pub fn store_cache(c: &mut Criterion) { + let tmp_dir = TempDir::new("wasmer-cache-bench").unwrap(); + let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap(); + let compiler = Singlepass::default(); + let store = Store::new(&JIT::new(compiler).engine()); + let module = Module::new(&store, std::fs::read("../../lib/c-api/tests/assets/qjs.wasm").unwrap()).unwrap(); + + c.bench_function("store module in filesystem cache", |b| { + b.iter(|| { + let key = random_key(); + fs_cache.store(key, &module).unwrap() + }) + }); +} + +pub fn load_cache(c: &mut Criterion) { + +} + +criterion_group!(benches, store_cache); +criterion_main!(benches); \ No newline at end of file From 2e3b70931cd9d7283144bce035b060a35ef95412 Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Wed, 24 Feb 2021 11:58:39 -0800 Subject: [PATCH 9/9] load cache bench --- lib/cache/benches/bench_filesystem_cache.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/cache/benches/bench_filesystem_cache.rs b/lib/cache/benches/bench_filesystem_cache.rs index 8488725ce19..90187e4c2f9 100644 --- a/lib/cache/benches/bench_filesystem_cache.rs +++ b/lib/cache/benches/bench_filesystem_cache.rs @@ -28,8 +28,24 @@ pub fn store_cache(c: &mut Criterion) { } pub fn load_cache(c: &mut Criterion) { + let tmp_dir = TempDir::new("wasmer-cache-bench").unwrap(); + let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap(); + let compiler = Singlepass::default(); + let store = Store::new(&JIT::new(compiler).engine()); + let module = Module::new(&store, std::fs::read("../../lib/c-api/tests/assets/qjs.wasm").unwrap()).unwrap(); + let key = Hash::new([0u8; 32]); + fs_cache.store(key, &module).unwrap(); + c.bench_function("load module in filesystem cache", |b| { + b.iter(|| { + unsafe { fs_cache.load(&store, key.clone()).unwrap() } + }) + }); } -criterion_group!(benches, store_cache); +criterion_group! { + name = benches; + config = Criterion::default().sample_size(300); + targets = store_cache, load_cache +} criterion_main!(benches); \ No newline at end of file