2019-01-08 17:09:47 +00:00
|
|
|
use crate::{
|
2019-04-12 01:01:54 +00:00
|
|
|
backend::{Backend, RunnableModule},
|
2019-02-22 22:07:03 +00:00
|
|
|
cache::{Artifact, Error as CacheError},
|
2019-02-19 23:36:22 +00:00
|
|
|
error,
|
2019-01-21 22:43:04 +00:00
|
|
|
import::ImportObject,
|
2019-02-07 00:26:45 +00:00
|
|
|
structures::{Map, TypedIndex},
|
2019-01-08 17:09:47 +00:00
|
|
|
types::{
|
2019-02-07 00:26:45 +00:00
|
|
|
FuncIndex, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, ImportedFuncIndex,
|
2019-01-29 21:04:42 +00:00
|
|
|
ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex, Initializer,
|
|
|
|
LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryDescriptor, MemoryIndex,
|
|
|
|
SigIndex, TableDescriptor, TableIndex,
|
2019-01-08 17:09:47 +00:00
|
|
|
},
|
2019-01-11 03:59:57 +00:00
|
|
|
Instance,
|
2019-01-08 17:09:47 +00:00
|
|
|
};
|
2019-02-19 23:36:22 +00:00
|
|
|
|
|
|
|
use crate::backend::CacheGen;
|
2019-02-07 00:26:45 +00:00
|
|
|
use indexmap::IndexMap;
|
2019-08-01 06:51:12 +00:00
|
|
|
use std::collections::HashMap;
|
2019-01-28 19:55:44 +00:00
|
|
|
use std::sync::Arc;
|
2019-01-08 17:09:47 +00:00
|
|
|
|
2019-01-19 07:03:07 +00:00
|
|
|
/// This is used to instantiate a new WebAssembly module.
|
2019-01-13 03:02:19 +00:00
|
|
|
#[doc(hidden)]
|
2019-01-08 17:09:47 +00:00
|
|
|
pub struct ModuleInner {
|
2019-04-12 01:01:54 +00:00
|
|
|
pub runnable_module: Box<dyn RunnableModule>,
|
2019-02-19 17:58:01 +00:00
|
|
|
pub cache_gen: Box<dyn CacheGen>,
|
2019-01-18 22:30:15 +00:00
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
pub info: ModuleInfo,
|
|
|
|
}
|
|
|
|
|
2019-05-22 04:44:31 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-02-07 00:26:45 +00:00
|
|
|
pub struct ModuleInfo {
|
2019-01-16 18:26:10 +00:00
|
|
|
// This are strictly local and the typsystem ensures that.
|
2019-01-29 21:04:42 +00:00
|
|
|
pub memories: Map<LocalMemoryIndex, MemoryDescriptor>,
|
2019-01-28 19:55:44 +00:00
|
|
|
pub globals: Map<LocalGlobalIndex, GlobalInit>,
|
2019-01-29 21:04:42 +00:00
|
|
|
pub tables: Map<LocalTableIndex, TableDescriptor>,
|
2019-01-08 17:09:47 +00:00
|
|
|
|
2019-01-16 18:26:10 +00:00
|
|
|
// These are strictly imported and the typesystem ensures that.
|
|
|
|
pub imported_functions: Map<ImportedFuncIndex, ImportName>,
|
2019-01-29 21:04:42 +00:00
|
|
|
pub imported_memories: Map<ImportedMemoryIndex, (ImportName, MemoryDescriptor)>,
|
|
|
|
pub imported_tables: Map<ImportedTableIndex, (ImportName, TableDescriptor)>,
|
|
|
|
pub imported_globals: Map<ImportedGlobalIndex, (ImportName, GlobalDescriptor)>,
|
2019-01-08 17:09:47 +00:00
|
|
|
|
2019-08-01 08:27:21 +00:00
|
|
|
pub exports: IndexMap<String, ExportIndex>,
|
2019-01-08 17:09:47 +00:00
|
|
|
|
|
|
|
pub data_initializers: Vec<DataInitializer>,
|
2019-01-17 01:59:12 +00:00
|
|
|
pub elem_initializers: Vec<TableInitializer>,
|
|
|
|
|
2019-01-08 17:09:47 +00:00
|
|
|
pub start_func: Option<FuncIndex>,
|
|
|
|
|
|
|
|
pub func_assoc: Map<FuncIndex, SigIndex>,
|
2019-02-08 22:19:58 +00:00
|
|
|
pub signatures: Map<SigIndex, FuncSig>,
|
2019-02-07 00:26:45 +00:00
|
|
|
pub backend: Backend,
|
|
|
|
|
|
|
|
pub namespace_table: StringTable<NamespaceIndex>,
|
|
|
|
pub name_table: StringTable<NameIndex>,
|
2019-03-12 17:45:44 +00:00
|
|
|
|
2019-03-27 21:08:17 +00:00
|
|
|
/// Symbol information from emscripten
|
2019-03-27 21:01:27 +00:00
|
|
|
pub em_symbol_map: Option<HashMap<u32, String>>,
|
2019-03-28 18:41:45 +00:00
|
|
|
|
2019-03-12 17:45:44 +00:00
|
|
|
pub custom_sections: HashMap<String, Vec<u8>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleInfo {
|
|
|
|
pub fn import_custom_sections(&mut self, wasm: &[u8]) -> crate::error::ParseResult<()> {
|
|
|
|
let mut parser = wasmparser::ModuleReader::new(wasm)?;
|
|
|
|
while !parser.eof() {
|
|
|
|
let section = parser.read()?;
|
|
|
|
if let wasmparser::SectionCode::Custom { name, kind: _ } = section.code {
|
|
|
|
let mut reader = section.get_binary_reader();
|
|
|
|
let len = reader.bytes_remaining();
|
|
|
|
let bytes = reader.read_bytes(len)?;
|
|
|
|
let data = bytes.to_vec();
|
2019-04-04 00:21:57 +00:00
|
|
|
let name = name.to_string();
|
2019-03-12 17:45:44 +00:00
|
|
|
self.custom_sections.insert(name, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:34:15 +00:00
|
|
|
/// A compiled WebAssembly module.
|
|
|
|
///
|
|
|
|
/// `Module` is returned by the [`compile`] and
|
|
|
|
/// [`compile_with`] functions.
|
|
|
|
///
|
|
|
|
/// [`compile`]: fn.compile.html
|
|
|
|
/// [`compile_with`]: fn.compile_with.html
|
2019-02-18 19:56:20 +00:00
|
|
|
pub struct Module {
|
|
|
|
inner: Arc<ModuleInner>,
|
|
|
|
}
|
2019-01-08 17:09:47 +00:00
|
|
|
|
|
|
|
impl Module {
|
2019-01-28 19:55:44 +00:00
|
|
|
pub(crate) fn new(inner: Arc<ModuleInner>) -> Self {
|
2019-02-18 19:56:20 +00:00
|
|
|
Module { inner }
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:34:15 +00:00
|
|
|
/// Instantiate a WebAssembly module with the provided [`ImportObject`].
|
|
|
|
///
|
|
|
|
/// [`ImportObject`]: struct.ImportObject.html
|
|
|
|
///
|
|
|
|
/// # Note:
|
|
|
|
/// Instantiating a `Module` will also call the function designated as `start`
|
|
|
|
/// in the WebAssembly module, if there is one.
|
|
|
|
///
|
|
|
|
/// # Usage:
|
|
|
|
/// ```
|
|
|
|
/// # use wasmer_runtime_core::error::Result;
|
|
|
|
/// # use wasmer_runtime_core::Module;
|
|
|
|
/// # use wasmer_runtime_core::imports;
|
|
|
|
/// # fn instantiate(module: &Module) -> Result<()> {
|
|
|
|
/// let import_object = imports! {
|
|
|
|
/// // ...
|
|
|
|
/// };
|
2019-02-03 00:02:28 +00:00
|
|
|
/// let instance = module.instantiate(&import_object)?;
|
2019-01-23 20:34:15 +00:00
|
|
|
/// // ...
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2019-02-19 23:36:22 +00:00
|
|
|
pub fn instantiate(&self, import_object: &ImportObject) -> error::Result<Instance> {
|
2019-02-18 19:56:20 +00:00
|
|
|
Instance::new(Arc::clone(&self.inner), import_object)
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
2019-02-19 23:36:22 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
pub fn cache(&self) -> Result<Artifact, CacheError> {
|
2019-04-19 20:54:48 +00:00
|
|
|
let (backend_metadata, code) = self.inner.cache_gen.generate_cache()?;
|
|
|
|
Ok(Artifact::from_parts(
|
|
|
|
Box::new(self.inner.info.clone()),
|
|
|
|
backend_metadata,
|
|
|
|
code,
|
|
|
|
))
|
2019-02-21 00:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn info(&self) -> &ModuleInfo {
|
|
|
|
&self.inner.info
|
2019-02-19 23:36:22 +00:00
|
|
|
}
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:42:30 +00:00
|
|
|
impl Clone for Module {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: Arc::clone(&self.inner),
|
|
|
|
}
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 18:26:10 +00:00
|
|
|
impl ModuleInner {}
|
2019-01-08 17:09:47 +00:00
|
|
|
|
2019-01-13 03:02:19 +00:00
|
|
|
#[doc(hidden)]
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
2019-01-08 17:09:47 +00:00
|
|
|
pub struct ImportName {
|
2019-02-07 00:26:45 +00:00
|
|
|
pub namespace_index: NamespaceIndex,
|
|
|
|
pub name_index: NameIndex,
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
|
2019-01-11 03:59:57 +00:00
|
|
|
pub enum ExportIndex {
|
2019-01-08 17:09:47 +00:00
|
|
|
Func(FuncIndex),
|
|
|
|
Memory(MemoryIndex),
|
|
|
|
Global(GlobalIndex),
|
|
|
|
Table(TableIndex),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A data initializer for linear memory.
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
2019-01-08 17:09:47 +00:00
|
|
|
pub struct DataInitializer {
|
|
|
|
/// The index of the memory to initialize.
|
|
|
|
pub memory_index: MemoryIndex,
|
2019-01-17 01:59:12 +00:00
|
|
|
/// Either a constant offset or a `get_global`
|
|
|
|
pub base: Initializer,
|
2019-01-08 17:09:47 +00:00
|
|
|
/// The initialization data.
|
2019-02-07 00:26:45 +00:00
|
|
|
#[cfg_attr(feature = "cache", serde(with = "serde_bytes"))]
|
2019-01-08 17:09:47 +00:00
|
|
|
pub data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A WebAssembly table initializer.
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
2019-01-08 17:09:47 +00:00
|
|
|
pub struct TableInitializer {
|
|
|
|
/// The index of a table to initialize.
|
|
|
|
pub table_index: TableIndex,
|
2019-01-16 18:26:10 +00:00
|
|
|
/// Either a constant offset or a `get_global`
|
|
|
|
pub base: Initializer,
|
2019-01-08 17:09:47 +00:00
|
|
|
/// The values to write into the table elements.
|
|
|
|
pub elements: Vec<FuncIndex>,
|
|
|
|
}
|
2019-02-07 00:26:45 +00:00
|
|
|
|
|
|
|
pub struct StringTableBuilder<K: TypedIndex> {
|
|
|
|
map: IndexMap<String, (K, u32, u32)>,
|
|
|
|
buffer: String,
|
|
|
|
count: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: TypedIndex> StringTableBuilder<K> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
map: IndexMap::new(),
|
|
|
|
buffer: String::new(),
|
|
|
|
count: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register<S>(&mut self, s: S) -> K
|
|
|
|
where
|
|
|
|
S: Into<String> + AsRef<str>,
|
|
|
|
{
|
|
|
|
let s_str = s.as_ref();
|
|
|
|
|
|
|
|
if self.map.contains_key(s_str) {
|
|
|
|
self.map[s_str].0
|
|
|
|
} else {
|
|
|
|
let offset = self.buffer.len();
|
|
|
|
let length = s_str.len();
|
|
|
|
let index = TypedIndex::new(self.count as _);
|
|
|
|
|
|
|
|
self.buffer.push_str(s_str);
|
|
|
|
self.map
|
|
|
|
.insert(s.into(), (index, offset as u32, length as u32));
|
|
|
|
self.count += 1;
|
|
|
|
|
|
|
|
index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish(self) -> StringTable<K> {
|
|
|
|
let table = self
|
|
|
|
.map
|
|
|
|
.values()
|
|
|
|
.map(|(_, offset, length)| (*offset, *length))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
StringTable {
|
|
|
|
table,
|
|
|
|
buffer: self.buffer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
2019-02-07 00:26:45 +00:00
|
|
|
pub struct StringTable<K: TypedIndex> {
|
|
|
|
table: Map<K, (u32, u32)>,
|
|
|
|
buffer: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: TypedIndex> StringTable<K> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
table: Map::new(),
|
|
|
|
buffer: String::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, index: K) -> &str {
|
|
|
|
let (offset, length) = self.table[index];
|
|
|
|
let offset = offset as usize;
|
|
|
|
let length = length as usize;
|
|
|
|
|
|
|
|
&self.buffer[offset..offset + length]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2019-02-07 00:26:45 +00:00
|
|
|
pub struct NamespaceIndex(u32);
|
|
|
|
|
|
|
|
impl TypedIndex for NamespaceIndex {
|
|
|
|
#[doc(hidden)]
|
|
|
|
fn new(index: usize) -> Self {
|
|
|
|
NamespaceIndex(index as _)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
fn index(&self) -> usize {
|
|
|
|
self.0 as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2019-02-07 00:26:45 +00:00
|
|
|
pub struct NameIndex(u32);
|
|
|
|
|
|
|
|
impl TypedIndex for NameIndex {
|
|
|
|
#[doc(hidden)]
|
|
|
|
fn new(index: usize) -> Self {
|
|
|
|
NameIndex(index as _)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
fn index(&self) -> usize {
|
|
|
|
self.0 as usize
|
|
|
|
}
|
|
|
|
}
|