Skip to content

Commit

Permalink
compiler: move MetadataHeader to wasmer-types
Browse files Browse the repository at this point in the history
  • Loading branch information
epilys committed Jul 27, 2022
1 parent a4f943d commit 038101e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 73 deletions.
2 changes: 1 addition & 1 deletion lib/compiler/src/artifact_builders/artifact_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#[cfg(feature = "engine_compilation")]
use super::trampoline::{libcall_trampoline_len, make_libcall_trampolines};
use crate::Features;
use crate::MetadataHeader;
use crate::{ArtifactCreate, EngineBuilder};
#[cfg(feature = "engine_compilation")]
use crate::{ModuleEnvironment, ModuleMiddlewareChain};
Expand All @@ -13,6 +12,7 @@ use std::mem;
use wasmer_types::entity::PrimaryMap;
#[cfg(feature = "engine_compilation")]
use wasmer_types::CompileModuleInfo;
use wasmer_types::MetadataHeader;
use wasmer_types::SerializeError;
use wasmer_types::{
CompileError, CpuFeature, CustomSection, Dwarf, FunctionIndex, LocalFunctionIndex, MemoryIndex,
Expand Down
3 changes: 2 additions & 1 deletion lib/compiler/src/engine/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use crate::Features;
use crate::ModuleEnvironment;
use crate::{
register_frame_info, resolve_imports, FunctionExtent, GlobalFrameInfoRegistration,
InstantiationError, MetadataHeader, RuntimeError, Tunables,
InstantiationError, RuntimeError, Tunables,
};
use enumset::EnumSet;
use std::sync::Arc;
use std::sync::Mutex;
use wasmer_types::entity::{BoxedSlice, PrimaryMap};
use wasmer_types::MetadataHeader;
use wasmer_types::{
CompileError, CpuFeature, DataInitializer, DeserializeError, FunctionIndex, LocalFunctionIndex,
MemoryIndex, ModuleInfo, OwnedDataInitializer, SerializableModule, SerializeError,
Expand Down
73 changes: 2 additions & 71 deletions lib/compiler/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
use crate::Features;
use enumset::EnumSet;
use std::any::Any;
use std::convert::TryInto;
use std::fs;
use std::path::Path;
use std::{fs, mem};
use wasmer_types::entity::PrimaryMap;
use wasmer_types::SerializeError;
use wasmer_types::{
CpuFeature, MemoryIndex, MemoryStyle, ModuleInfo, OwnedDataInitializer, TableIndex, TableStyle,
};
use wasmer_types::{DeserializeError, SerializeError};

/// An `Artifact` is the product that the `Engine`
/// implementation produce and use.
Expand Down Expand Up @@ -87,71 +86,3 @@ impl dyn ArtifactCreate + 'static {
self.upcast_any_mut().downcast_mut::<T>()
}
}

/// Metadata header which holds an ABI version and the length of the remaining
/// metadata.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct MetadataHeader {
magic: [u8; 8],
version: u32,
len: u32,
}

impl MetadataHeader {
/// Current ABI version. Increment this any time breaking changes are made
/// to the format of the serialized data.
const CURRENT_VERSION: u32 = 1;

/// Magic number to identify wasmer metadata.
const MAGIC: [u8; 8] = *b"WASMER\0\0";

/// Length of the metadata header.
pub const LEN: usize = 16;

/// Alignment of the metadata.
pub const ALIGN: usize = 16;

/// Creates a new header for metadata of the given length.
pub fn new(len: usize) -> Self {
Self {
magic: Self::MAGIC,
version: Self::CURRENT_VERSION,
len: len.try_into().expect("metadata exceeds maximum length"),
}
}

/// Convert the header into its bytes representation.
pub fn into_bytes(self) -> [u8; 16] {
unsafe { mem::transmute(self) }
}

/// Parses the header and returns the length of the metadata following it.
pub fn parse(bytes: &[u8]) -> Result<usize, DeserializeError> {
if bytes.as_ptr() as usize % 16 != 0 {
return Err(DeserializeError::CorruptedBinary(
"misaligned metadata".to_string(),
));
}
let bytes: [u8; 16] = bytes
.get(..16)
.ok_or_else(|| {
DeserializeError::CorruptedBinary("invalid metadata header".to_string())
})?
.try_into()
.unwrap();
let header: Self = unsafe { mem::transmute(bytes) };
if header.magic != Self::MAGIC {
return Err(DeserializeError::Incompatible(
"The provided bytes were not serialized by Wasmer".to_string(),
));
}
if header.version != Self::CURRENT_VERSION {
return Err(DeserializeError::Incompatible(
"The provided bytes were serialized by an incompatible version of Wasmer"
.to_string(),
));
}
Ok(header.len as usize)
}
}

0 comments on commit 038101e

Please sign in to comment.