Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! #k23VM - k23 WebAssembly Virtual Machine
9
10mod builtins;
11mod code_registry;
12mod compile;
13mod cranelift;
14mod engine;
15mod func;
16mod global;
17mod indices;
18mod instance;
19mod linker;
20mod memory;
21mod module;
22mod store;
23mod table;
24mod tag;
25mod translate;
26mod trap;
27pub mod trap_handler;
28mod type_registry;
29mod types;
30mod utils;
31mod values;
32mod vm;
33
34use crate::wasm::store::StoreOpaque;
35use crate::wasm::utils::{enum_accessors, owned_enum_accessors};
36
37pub use engine::Engine;
38pub use func::Func;
39pub use global::Global;
40pub use instance::Instance;
41#[cfg(test)]
42pub use linker::Linker;
43pub use memory::Memory;
44pub use module::Module;
45pub use store::Store;
46pub use table::Table;
47pub use tag::Tag;
48pub use trap::TrapKind;
49#[cfg(test)]
50pub use values::Val;
51#[cfg(test)]
52pub use vm::{ConstExprEvaluator, PlaceholderAllocatorDontUse};
53
54/// The number of pages (for 32-bit modules) we can have before we run out of
55/// byte index space.
56pub const WASM32_MAX_PAGES: u64 = 1 << 16;
57/// The number of pages (for 64-bit modules) we can have before we run out of
58/// byte index space.
59pub const WASM64_MAX_PAGES: u64 = 1 << 48;
60/// Maximum size, in bytes, of 32-bit memories (4G).
61pub const WASM32_MAX_SIZE: u64 = 1 << 32;
62/// Maximum size, in bytes of WebAssembly stacks.
63pub const MAX_WASM_STACK: usize = 512 * 1024;
64
65/***************** Settings *******************************************/
66/// Whether lowerings for relaxed simd instructions are forced to
67/// be deterministic.
68pub const RELAXED_SIMD_DETERMINISTIC: bool = false;
69/// 2 GiB of guard pages
70/// TODO why does this help to eliminate bounds checks?
71pub const DEFAULT_OFFSET_GUARD_SIZE: u64 = 0x8000_0000;
72/// The absolute maximum size of a memory in bytes
73pub const MEMORY_MAX: usize = 1 << 32;
74/// The absolute maximum size of a table in elements
75pub const TABLE_MAX: usize = 1 << 10;
76
77/// A WebAssembly external value which is just any type that can be imported or exported between modules.
78#[derive(Clone, Debug)]
79pub enum Extern {
80 Func(Func),
81 Table(Table),
82 Memory(Memory),
83 Global(Global),
84 Tag(Tag),
85}
86
87impl From<Func> for Extern {
88 fn from(f: Func) -> Self {
89 Extern::Func(f)
90 }
91}
92
93impl From<Table> for Extern {
94 fn from(t: Table) -> Self {
95 Extern::Table(t)
96 }
97}
98
99impl From<Memory> for Extern {
100 fn from(m: Memory) -> Self {
101 Extern::Memory(m)
102 }
103}
104
105impl From<Global> for Extern {
106 fn from(g: Global) -> Self {
107 Extern::Global(g)
108 }
109}
110
111impl From<Tag> for Extern {
112 fn from(t: Tag) -> Self {
113 Extern::Tag(t)
114 }
115}
116
117impl Extern {
118 /// # Safety
119 ///
120 /// The caller must ensure `export` is a valid export within `store`.
121 pub(crate) unsafe fn from_export(export: vm::Export, store: &mut StoreOpaque) -> Self {
122 // Safety: ensured by caller
123 unsafe {
124 match export {
125 vm::Export::Function(e) => Extern::Func(Func::from_exported_function(store, e)),
126 vm::Export::Table(e) => Extern::Table(Table::from_exported_table(store, e)),
127 vm::Export::Memory(e) => Extern::Memory(Memory::from_exported_memory(store, e)),
128 vm::Export::Global(e) => Extern::Global(Global::from_exported_global(store, e)),
129 vm::Export::Tag(e) => Extern::Tag(Tag::from_exported_tag(store, e)),
130 }
131 }
132 }
133
134 enum_accessors! {
135 e
136 (Func(&Func) is_func get_func unwrap_func e)
137 (Table(&Table) is_table get_table unwrap_table e)
138 (Memory(&Memory) is_memory get_memory unwrap_memory e)
139 (Global(&Global) is_global get_global unwrap_global e)
140 }
141
142 owned_enum_accessors! {
143 e
144 (Func(Func) into_func e)
145 (Table(Table) into_table e)
146 (Memory(Memory) into_memory e)
147 (Global(Global) into_global e)
148 }
149}