wasmer_compiler/
serialize.rs1#![allow(missing_docs)]
6
7use crate::types::{
8 function::{CompiledFunctionFrameInfo, Dwarf, FunctionBody},
9 module::CompileModuleInfo,
10 relocation::Relocation,
11 section::{CustomSection, SectionIndex},
12 target::CpuFeature,
13};
14use enumset::EnumSet;
15use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
16use wasmer_types::{
17 entity::PrimaryMap, DeserializeError, Features, FunctionIndex, LocalFunctionIndex, MemoryIndex,
18 MemoryStyle, ModuleInfo, OwnedDataInitializer, SerializeError, SignatureIndex, TableIndex,
19 TableStyle,
20};
21
22pub use wasmer_types::MetadataHeader;
23
24#[derive(Archive, Default, RkyvDeserialize, RkyvSerialize)]
26#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
27#[allow(missing_docs)]
28#[rkyv(derive(Debug))]
29pub struct SerializableCompilation {
30 pub function_bodies: PrimaryMap<LocalFunctionIndex, FunctionBody>,
31 pub function_relocations: PrimaryMap<LocalFunctionIndex, Vec<Relocation>>,
32 pub function_frame_info: PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo>,
33 pub function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
34 pub dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
35 pub custom_sections: PrimaryMap<SectionIndex, CustomSection>,
36 pub custom_section_relocations: PrimaryMap<SectionIndex, Vec<Relocation>>,
37 pub debug: Option<Dwarf>,
39 pub libcall_trampolines: SectionIndex,
41 pub libcall_trampoline_len: u32,
43}
44
45impl SerializableCompilation {
46 pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
50 rkyv::to_bytes::<rkyv::rancor::Error>(self)
51 .map(|v| v.into_vec())
52 .map_err(|e| SerializeError::Generic(e.to_string()))
53 }
54}
55
56#[derive(Archive, RkyvDeserialize, RkyvSerialize)]
58#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
59#[allow(missing_docs)]
60#[rkyv(derive(Debug))]
61pub struct SerializableModule {
62 pub compilation: SerializableCompilation,
64 pub compile_info: CompileModuleInfo,
66 pub data_initializers: Box<[OwnedDataInitializer]>,
68 pub cpu_features: u64,
70}
71
72impl SerializableModule {
73 pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
77 rkyv::to_bytes::<rkyv::rancor::Error>(self)
78 .map(|v| v.into_vec())
79 .map_err(|e| SerializeError::Generic(e.to_string()))
80 }
81
82 pub unsafe fn deserialize_unchecked(metadata_slice: &[u8]) -> Result<Self, DeserializeError> {
94 let archived = Self::archive_from_slice(metadata_slice)?;
95 Self::deserialize_from_archive(archived)
96 }
97
98 pub unsafe fn deserialize(metadata_slice: &[u8]) -> Result<Self, DeserializeError> {
108 let archived = Self::archive_from_slice_checked(metadata_slice)?;
109 Self::deserialize_from_archive(archived)
110 }
111
112 pub unsafe fn archive_from_slice(
117 metadata_slice: &[u8],
118 ) -> Result<&ArchivedSerializableModule, DeserializeError> {
119 Ok(rkyv::access_unchecked(metadata_slice))
120 }
121
122 pub fn archive_from_slice_checked(
127 metadata_slice: &[u8],
128 ) -> Result<&ArchivedSerializableModule, DeserializeError> {
129 rkyv::access::<_, rkyv::rancor::Error>(metadata_slice)
130 .map_err(|e| DeserializeError::CorruptedBinary(e.to_string()))
131 }
132
133 pub fn deserialize_from_archive(
135 archived: &ArchivedSerializableModule,
136 ) -> Result<Self, DeserializeError> {
137 rkyv::deserialize::<_, rkyv::rancor::Error>(archived)
138 .map_err(|e| DeserializeError::CorruptedBinary(e.to_string()))
139 }
140
141 pub fn create_module_info(&self) -> ModuleInfo {
143 self.compile_info.module.as_ref().clone()
144 }
145
146 pub fn module_info(&self) -> &ModuleInfo {
148 &self.compile_info.module
149 }
150
151 pub fn features(&self) -> &Features {
153 &self.compile_info.features
154 }
155
156 pub fn cpu_features(&self) -> EnumSet<CpuFeature> {
158 EnumSet::from_u64(self.cpu_features)
159 }
160
161 pub fn data_initializers(&self) -> &[OwnedDataInitializer] {
163 &self.data_initializers
164 }
165
166 pub fn memory_styles(&self) -> &PrimaryMap<MemoryIndex, MemoryStyle> {
168 &self.compile_info.memory_styles
169 }
170
171 pub fn table_styles(&self) -> &PrimaryMap<TableIndex, TableStyle> {
173 &self.compile_info.table_styles
174 }
175}