Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use borsh to deserialize JIT Artifact #2119

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions lib/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ 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 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't merge this while there's a git dependency as it will block us from doing releases on crates.io

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit haven't published, will keep this updated as we publish a new borsh


[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.
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" }
4 changes: 4 additions & 0 deletions lib/compiler/src/address_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions lib/compiler/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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).
Expand All @@ -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.
Expand Down Expand Up @@ -79,6 +83,7 @@ pub type CustomSections = PrimaryMap<SectionIndex, CustomSection>;
/// 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.
Expand Down
3 changes: 3 additions & 0 deletions lib/compiler/src/jump_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
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).
///
/// `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);

Expand Down
28 changes: 28 additions & 0 deletions lib/compiler/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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};

use wasmer_types::entity::PrimaryMap;
use wasmer_types::{Features, MemoryIndex, TableIndex};
use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle};
Expand All @@ -25,3 +29,27 @@ pub struct CompileModuleInfo {
/// The table plans used for compiling.
pub table_styles: PrimaryMap<TableIndex, TableStyle>,
}

#[cfg(feature = "enable-borsh")]
impl BorshSerialize for CompileModuleInfo {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
BorshSerialize::serialize(&self.features, writer)?;
BorshSerialize::serialize(&self.module.as_ref(), writer)?;
BorshSerialize::serialize(&self.memory_styles.values().collect::<Vec<_>>(), writer)?;
BorshSerialize::serialize(&self.table_styles.values().collect::<Vec<_>>(), writer)
}
}

#[cfg(feature = "enable-borsh")]
impl BorshDeserialize for CompileModuleInfo {
fn deserialize(buf: &mut &[u8]) -> std::io::Result<Self> {
let features: Features = BorshDeserialize::deserialize(buf)?;
let module: ModuleInfo = BorshDeserialize::deserialize(buf)?;
let module = Arc::new(module);
let memory_styles: Vec<MemoryStyle> = BorshDeserialize::deserialize(buf)?;
let memory_styles: PrimaryMap<MemoryIndex, MemoryStyle> = PrimaryMap::from_iter(memory_styles);
let table_styles: Vec<TableStyle> = BorshDeserialize::deserialize(buf)?;
let table_styles: PrimaryMap<TableIndex, TableStyle> = PrimaryMap::from_iter(table_styles);
Ok(Self { features, module, memory_styles, table_styles })
}
}
ailisp marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions lib/compiler/src/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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).
Expand Down
6 changes: 6 additions & 0 deletions lib/compiler/src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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<u8>);

Expand Down
3 changes: 3 additions & 0 deletions lib/compiler/src/sourceloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions lib/compiler/src/trap.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/compiler/src/unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/engine-jit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
12 changes: 6 additions & 6 deletions lib/engine-jit/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -140,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");
ailisp marked this conversation as resolved.
Show resolved Hide resolved
// 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)
Expand Down Expand Up @@ -315,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");
ailisp marked this conversation as resolved.
Show resolved Hide resolved
// Prepend the header.
let mut serialized = Self::MAGIC_HEADER.to_vec();
serialized.extend(bytes);
Expand Down
Loading