Skip to content

Commit 41ae84b

Browse files
bors[bot]lachlansneffsyrusakbary
committed
Merge #269
269: docs(runtime) Add some better runtime docs r=syrusakbary a=lachlansneff Co-authored-by: Lachlan Sneff <lachlan.sneff@gmail.com> Co-authored-by: Syrus <me@syrusakbary.com>
2 parents 9aac20e + 02d4aff commit 41ae84b

File tree

9 files changed

+63
-20
lines changed

9 files changed

+63
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Blocks of changes will separated by version increments.
66

77
## **[Unreleased]**
88

9+
- [#269](https://github.com/wasmerio/wasmer/pull/269) Add better runtime docs
910
- [#432](https://github.com/wasmerio/wasmer/pull/432) Fix returned value of `wasmer_last_error_message` in the runtime C API
1011
- [#429](https://github.com/wasmerio/wasmer/pull/429) Get wasi::path_filestat_get working for some programs; misc. minor WASI FS improvements
1112
- [#413](https://github.com/wasmerio/wasmer/pull/413) Update LLVM backend to use new parser codegen traits

lib/clif-backend/src/signal/unix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Installing signal handlers allows us to handle traps and out-of-bounds memory
2-
//! accesses that occur when runniing webassembly.
2+
//! accesses that occur when running WebAssembly.
33
//!
44
//! This code is inspired by: https://github.com/pepyakin/wasmtime/commit/625a2b6c0815b21996e111da51b9664feb174622
55
//!

lib/runtime-core/src/backing.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,31 @@ use crate::{
1717
};
1818
use std::slice;
1919

20+
/// The `LocalBacking` "owns" the memory used by all the local resources of an Instance.
21+
/// That is, local memories, tables, and globals (as well as some additional
22+
/// data for the virtual call machinery).
2023
#[derive(Debug)]
2124
pub struct LocalBacking {
25+
/// This is a map from the local resource index to actual memory,
26+
/// table, and globals.
2227
pub(crate) memories: BoxedMap<LocalMemoryIndex, Memory>,
2328
pub(crate) tables: BoxedMap<LocalTableIndex, Table>,
2429
pub(crate) globals: BoxedMap<LocalGlobalIndex, Global>,
2530

31+
/// This own the memory containing the pointers to the local memories.
32+
/// While simplifying implementation, this adds indirection and may hurt
33+
/// performance, especially on cache-starved systems.
2634
pub(crate) vm_memories: BoxedMap<LocalMemoryIndex, *mut vm::LocalMemory>,
2735
pub(crate) vm_tables: BoxedMap<LocalTableIndex, *mut vm::LocalTable>,
2836
pub(crate) vm_globals: BoxedMap<LocalGlobalIndex, *mut vm::LocalGlobal>,
2937

38+
/// The dynamic sigindices are used to efficiently support caching and
39+
/// the `call_indirect` wasm instruction. This field (and local_functions
40+
/// as well) are subject to change.
3041
pub(crate) dynamic_sigindices: BoxedMap<SigIndex, vm::SigId>,
3142
pub(crate) local_functions: BoxedMap<LocalFuncIndex, *const vm::Func>,
3243
}
3344

34-
// impl LocalBacking {
35-
// pub fn memory(&mut self, local_memory_index: LocalMemoryIndex) -> &mut Memory {
36-
// &mut self.memories[local_memory_index]
37-
// }
38-
39-
// pub fn table(&mut self, local_table_index: LocalTableIndex) -> &mut TableBacking {
40-
// &mut self.tables[local_table_index]
41-
// }
42-
// }
43-
4445
impl LocalBacking {
4546
pub(crate) fn new(module: &ModuleInner, imports: &ImportBacking, vmctx: *mut vm::Ctx) -> Self {
4647
let mut memories = Self::generate_memories(module);
@@ -102,6 +103,9 @@ impl LocalBacking {
102103
memories.into_boxed_map()
103104
}
104105

106+
/// Initialize each locally-defined memory in the Module.
107+
///
108+
/// This involves copying in the data initializers.
105109
fn finalize_memories(
106110
module: &ModuleInner,
107111
imports: &ImportBacking,
@@ -174,6 +178,9 @@ impl LocalBacking {
174178
tables.into_boxed_map()
175179
}
176180

181+
/// This initializes all of the locally-defined tables in the Module, e.g.
182+
/// putting all the table elements (function pointers)
183+
/// in the right places.
177184
#[allow(clippy::cast_ptr_alignment)]
178185
fn finalize_tables(
179186
module: &ModuleInner,

lib/runtime-core/src/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub type ResolveResult<T> = std::result::Result<T, ResolveError>;
1111
pub type ParseResult<T> = std::result::Result<T, ParseError>;
1212

1313
/// This is returned when the chosen compiler is unable to
14-
/// successfully compile the provided webassembly module into
14+
/// successfully compile the provided WebAssembly module into
1515
/// a `Module`.
1616
///
1717
/// Comparing two `CompileError`s always evaluates to false.
@@ -114,7 +114,7 @@ impl std::fmt::Display for LinkError {
114114
impl std::error::Error for LinkError {}
115115

116116
/// This is the error type returned when calling
117-
/// a webassembly function.
117+
/// a WebAssembly function.
118118
///
119119
/// The main way to do this is `Instance.call`.
120120
///
@@ -270,7 +270,7 @@ impl std::error::Error for CreationError {}
270270

271271
/// The amalgamation of all errors that can occur
272272
/// during the compilation, instantiation, or execution
273-
/// of a webassembly module.
273+
/// of a WebAssembly module.
274274
///
275275
/// Comparing two `Error`s always evaluates to false.
276276
#[derive(Debug)]

lib/runtime-core/src/instance.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,15 @@ impl Instance {
262262
}
263263
}
264264

265-
/// Call an exported webassembly function given the export name.
265+
/// Call an exported WebAssembly function given the export name.
266266
/// Pass arguments by wrapping each one in the [`Value`] enum.
267267
/// The returned values are also each wrapped in a [`Value`].
268268
///
269269
/// [`Value`]: enum.Value.html
270270
///
271271
/// # Note:
272272
/// This returns `CallResult<Vec<Value>>` in order to support
273-
/// the future multi-value returns webassembly feature.
273+
/// the future multi-value returns WebAssembly feature.
274274
///
275275
/// # Usage:
276276
/// ```
@@ -601,7 +601,7 @@ pub struct DynFunc<'a> {
601601
}
602602

603603
impl<'a> DynFunc<'a> {
604-
/// Call an exported webassembly function safely.
604+
/// Call an exported WebAssembly function safely.
605605
///
606606
/// Pass arguments by wrapping each one in the [`Value`] enum.
607607
/// The returned values are also each wrapped in a [`Value`].
@@ -610,7 +610,7 @@ impl<'a> DynFunc<'a> {
610610
///
611611
/// # Note:
612612
/// This returns `CallResult<Vec<Value>>` in order to support
613-
/// the future multi-value returns webassembly feature.
613+
/// the future multi-value returns WebAssembly feature.
614614
///
615615
/// # Usage:
616616
/// ```

lib/runtime-core/src/memory/static_/unshared.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
/// This is an internal-only api.
1212
///
1313
/// A static memory allocates 6GB of *virtual* memory when created
14-
/// in order to allow the webassembly module to contain no bounds-checks.
14+
/// in order to allow the WebAssembly module to contain no bounds-checks.
1515
///
1616
/// Additionally, static memories stay at a single virtual address, so there is no need
1717
/// to reload its address on each use.

lib/runtime-core/src/sig_registry.rs

+12
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ struct GlobalSigRegistry {
2323
sig_assoc: Map<SigIndex, Arc<FuncSig>>,
2424
}
2525

26+
/// The `SigRegistry` represents a process-global map of function signatures
27+
/// to signature indexes and vice versa (the map goes both ways).
28+
///
29+
/// This exists for two reasons:
30+
/// 1. The `call_indirect` wasm instruction can compare two signature indices
31+
/// to do signature validation very quickly.
32+
/// 2. To intern function signatures, which may be expensive to create.
2633
#[derive(Debug)]
2734
pub struct SigRegistry;
2835

2936
impl SigRegistry {
37+
/// Map a `FuncSig` to a global `SigIndex`.
3038
pub fn lookup_sig_index<Sig>(&self, func_sig: Sig) -> SigIndex
3139
where
3240
Sig: Into<Arc<FuncSig>>,
@@ -45,11 +53,15 @@ impl SigRegistry {
4553
sig_index
4654
}
4755

56+
/// Map a global `SigIndex` to an interned `FuncSig`.
4857
pub fn lookup_signature(&self, sig_index: SigIndex) -> Arc<FuncSig> {
4958
let global = (*GLOBAL_SIG_REGISTRY).read();
5059
Arc::clone(&global.sig_assoc[sig_index])
5160
}
5261

62+
/// Register a function signature with the global signature registry.
63+
///
64+
/// This will return an interned `FuncSig`.
5365
pub fn lookup_signature_ref(&self, func_sig: &FuncSig) -> Arc<FuncSig> {
5466
let mut global = (*GLOBAL_SIG_REGISTRY).write();
5567
let global = &mut *global;

lib/runtime-core/src/vm.rs

+23
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ use hashbrown::HashMap;
1111

1212
/// The context of the currently running WebAssembly instance.
1313
///
14+
/// This is implicitly passed to every WebAssembly function.
15+
/// Since this is per-instance, each field has a statically
16+
/// (as in after compiling the wasm) known size, so no
17+
/// runtime checks are necessary.
1418
///
19+
/// While the runtime currently just passes this around
20+
/// as the first, implicit parameter of every function,
21+
/// it may someday be pinned to a register (especially
22+
/// on arm, which has a ton of registers) to reduce
23+
/// register shuffling.
1524
#[derive(Debug)]
1625
#[repr(C)]
1726
pub struct Ctx {
@@ -20,11 +29,25 @@ pub struct Ctx {
2029

2130
pub(crate) local_functions: *const *const Func,
2231

32+
/// These are pointers to things that are known to be owned
33+
/// by the owning `Instance`.
2334
local_backing: *mut LocalBacking,
2435
import_backing: *mut ImportBacking,
2536
pub module: *const ModuleInner,
2637

38+
//// This is intended to be user-supplied, per-instance
39+
/// contextual data. There are currently some issue with it,
40+
/// notably that it cannot be set before running the `start`
41+
/// function in a WebAssembly module.
42+
///
43+
/// [#219](https://github.com/wasmerio/wasmer/pull/219) fixes that
44+
/// issue, as well as allowing the user to have *per-function*
45+
/// context, instead of just per-instance.
2746
pub data: *mut c_void,
47+
48+
/// If there's a function set in this field, it gets called
49+
/// when the context is destructed, e.g. when an `Instance`
50+
/// is dropped.
2851
pub data_finalizer: Option<fn(data: *mut c_void)>,
2952
}
3053

lib/singlepass-backend/src/protect_unix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Installing signal handlers allows us to handle traps and out-of-bounds memory
2-
//! accesses that occur when runniing webassembly.
2+
//! accesses that occur when runniing WebAssembly.
33
//!
44
//! This code is inspired by: https://github.com/pepyakin/wasmtime/commit/625a2b6c0815b21996e111da51b9664feb174622
55
//!

0 commit comments

Comments
 (0)