Skip to content

Commit af3029b

Browse files
chore: cleanup, make value & block stack fixed size
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 430ea40 commit af3029b

File tree

10 files changed

+332
-327
lines changed

10 files changed

+332
-327
lines changed

crates/parser/src/visit.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ macro_rules! impl_visit_operator {
157157
(@@tail_call $($rest:tt)* ) => {};
158158

159159
(@@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*)) => {
160-
#[cold]
161160
fn $visit(&mut self $($(,_: $argty)*)?) {
162161
self.unsupported(stringify!($visit))
163162
}
@@ -203,7 +202,11 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
203202
wasmparser::ValType::V128 => Instruction::GlobalSet128(global_index),
204203
wasmparser::ValType::Ref(_) => Instruction::GlobalSetRef(global_index),
205204
}),
206-
_ => self.visit_unreachable(),
205+
_ => {
206+
{
207+
self.visit_unreachable();
208+
};
209+
}
207210
}
208211
}
209212

@@ -217,15 +220,17 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
217220
wasmparser::ValType::V128 => Instruction::Drop128,
218221
wasmparser::ValType::Ref(_) => Instruction::DropRef,
219222
}),
220-
_ => self.visit_unreachable(),
223+
_ => {
224+
self.visit_unreachable();
225+
}
221226
}
222227
}
223228

224229
fn visit_select(&mut self) -> Self::Output {
225230
match self.validator.get_operand_type(1) {
226231
Some(Some(t)) => self.visit_typed_select(t),
227232
_ => self.visit_unreachable(),
228-
}
233+
};
229234
}
230235

231236
fn visit_local_get(&mut self, idx: u32) -> Self::Output {
@@ -245,7 +250,9 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
245250
wasmparser::ValType::V128 => Instruction::LocalGet128(resolved_idx),
246251
wasmparser::ValType::Ref(_) => Instruction::LocalGetRef(resolved_idx),
247252
}),
248-
_ => self.visit_unreachable(),
253+
_ => {
254+
self.visit_unreachable();
255+
}
249256
}
250257
}
251258

@@ -276,7 +283,9 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
276283
wasmparser::ValType::V128 => Instruction::LocalCopy128(from, resolved_idx),
277284
wasmparser::ValType::Ref(_) => Instruction::LocalCopyRef(from, resolved_idx),
278285
}),
279-
_ => self.visit_unreachable(),
286+
_ => {
287+
self.visit_unreachable();
288+
}
280289
}
281290
return;
282291
}
@@ -290,7 +299,9 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
290299
wasmparser::ValType::V128 => Instruction::LocalSet128(resolved_idx),
291300
wasmparser::ValType::Ref(_) => Instruction::LocalSetRef(resolved_idx),
292301
}),
293-
_ => self.visit_unreachable(),
302+
_ => {
303+
self.visit_unreachable();
304+
}
294305
}
295306
}
296307

@@ -311,7 +322,9 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
311322
wasmparser::ValType::V128 => Instruction::LocalTee128(resolved_idx),
312323
wasmparser::ValType::Ref(_) => Instruction::LocalTeeRef(resolved_idx),
313324
}),
314-
_ => self.visit_unreachable(),
325+
_ => {
326+
self.visit_unreachable();
327+
}
315328
}
316329
}
317330

@@ -483,7 +496,6 @@ macro_rules! impl_visit_simd_operator {
483496

484497
(@@simd $($rest:tt)* ) => {};
485498
(@@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*)) => {
486-
#[cold]
487499
fn $visit(&mut self $($(,$arg: $argty)*)?) {
488500
self.unsupported(stringify!($visit))
489501
}

crates/tinywasm/src/engine.rs

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -39,102 +39,58 @@ pub(crate) struct EngineInner {
3939
// pub(crate) allocator: Box<dyn Allocator + Send + Sync>,
4040
}
4141

42-
// pub(crate) trait Allocator {}
43-
// pub(crate) struct DefaultAllocator;
44-
// impl Allocator for DefaultAllocator {}
45-
4642
/// Default initial size for the 32-bit value stack (i32, f32 values).
47-
pub const DEFAULT_VALUE_STACK_32_INIT_SIZE: usize = 32 * 1024; // 32KB
43+
pub const DEFAULT_VALUE_STACK_32_SIZE: usize = 64 * 1024; // 64k slots
4844

4945
/// Default initial size for the 64-bit value stack (i64, f64 values).
50-
pub const DEFAULT_VALUE_STACK_64_INIT_SIZE: usize = 16 * 1024; // 16KB
46+
pub const DEFAULT_VALUE_STACK_64_SIZE: usize = 32 * 1024; // 32k slots
5147

5248
/// Default initial size for the 128-bit value stack (v128 values).
53-
pub const DEFAULT_VALUE_STACK_128_INIT_SIZE: usize = 8 * 1024; // 8KB
49+
pub const DEFAULT_VALUE_STACK_128_SIZE: usize = 4 * 1024; // 4k slots
5450

5551
/// Default initial size for the reference value stack (funcref, externref values).
56-
pub const DEFAULT_VALUE_STACK_REF_INIT_SIZE: usize = 1024; // 1KB
52+
pub const DEFAULT_VALUE_STACK_REF_SIZE: usize = 4 * 1024; // 4k slots
5753

58-
/// Default initial size for the block stack.
59-
pub const DEFAULT_BLOCK_STACK_INIT_SIZE: usize = 128;
54+
/// Default initial size for the block stack (control frames).
55+
pub const DEFAULT_BLOCK_STACK_SIZE: usize = 1024; // 1024 frames
6056

61-
/// Default initial size for the call stack.
62-
pub const DEFAULT_CALL_STACK_INIT_SIZE: usize = 128;
57+
/// Default initial size for the call stack (function frames).
58+
pub const DEFAULT_CALL_STACK_SIZE: usize = 1024; // 1024 frames
6359

6460
/// Configuration for the WebAssembly interpreter
6561
#[derive(Debug, Clone)]
6662
#[non_exhaustive]
6763
pub struct Config {
6864
/// Initial size of the 32-bit value stack (i32, f32 values).
69-
pub stack_32_init_size: usize,
65+
pub stack_32_size: usize,
7066
/// Initial size of the 64-bit value stack (i64, f64 values).
71-
pub stack_64_init_size: usize,
67+
pub stack_64_size: usize,
7268
/// Initial size of the 128-bit value stack (v128 values).
73-
pub stack_128_init_size: usize,
69+
pub stack_128_size: usize,
7470
/// Initial size of the reference value stack (funcref, externref values).
75-
pub stack_ref_init_size: usize,
76-
/// Optional maximum sizes for the stacks. If set, the interpreter will enforce these limits and return an error if they are exceeded.
77-
pub stack_32_max_size: Option<usize>,
78-
/// Optional maximum sizes for the stacks. If set, the interpreter will enforce these limits and return an error if they are exceeded.
79-
pub stack_64_max_size: Option<usize>,
80-
/// Optional maximum sizes for the stacks. If set, the interpreter will enforce these limits and return an error if they are exceeded.
81-
pub stack_128_max_size: Option<usize>,
82-
/// Optional maximum sizes for the stacks. If set, the interpreter will enforce these limits and return an error if they are exceeded.
83-
pub stack_ref_max_size: Option<usize>,
84-
71+
pub stack_ref_size: usize,
8572
/// Initial size of the call stack.
86-
pub call_stack_init_size: usize,
87-
/// The maximum size of the call stack. If set, the interpreter will enforce this limit and return an error if it is exceeded.
88-
pub call_stack_max_size: Option<usize>,
89-
73+
pub call_stack_size: usize,
9074
/// Initial size of the control stack (block stack).
91-
pub block_stack_init_size: usize,
92-
/// Optional maximum size for the control stack (block stack). If set, the interpreter will enforce this limit and return an error if it is exceeded.
93-
pub block_stack_max_size: Option<usize>,
75+
pub block_stack_size: usize,
9476
}
9577

9678
impl Config {
9779
/// Create a new stack configuration with default settings.
9880
pub fn new() -> Self {
9981
Self::default()
10082
}
101-
102-
/// Set the same maximum size for all stacks. If set, the interpreter will enforce this limit and return an error if it is exceeded.
103-
pub fn with_max_stack_size(mut self, max_size: usize) -> Self {
104-
self.stack_32_max_size = Some(max_size);
105-
self.stack_64_max_size = Some(max_size);
106-
self.stack_128_max_size = Some(max_size);
107-
self.stack_ref_max_size = Some(max_size);
108-
self.block_stack_max_size = Some(max_size);
109-
self
110-
}
111-
112-
/// Set the same initial size for all stacks.
113-
pub fn with_initial_stack_size(mut self, init_size: usize) -> Self {
114-
self.stack_32_init_size = init_size;
115-
self.stack_64_init_size = init_size;
116-
self.stack_128_init_size = init_size;
117-
self.stack_ref_init_size = init_size;
118-
self.block_stack_init_size = init_size;
119-
self
120-
}
12183
}
12284

12385
impl Default for Config {
12486
fn default() -> Self {
12587
Self {
126-
stack_32_init_size: DEFAULT_VALUE_STACK_32_INIT_SIZE,
127-
stack_64_init_size: DEFAULT_VALUE_STACK_64_INIT_SIZE,
128-
stack_128_init_size: DEFAULT_VALUE_STACK_128_INIT_SIZE,
129-
stack_ref_init_size: DEFAULT_VALUE_STACK_REF_INIT_SIZE,
130-
block_stack_init_size: DEFAULT_BLOCK_STACK_INIT_SIZE,
131-
call_stack_init_size: DEFAULT_CALL_STACK_INIT_SIZE,
132-
call_stack_max_size: None,
133-
stack_32_max_size: None,
134-
stack_64_max_size: None,
135-
stack_128_max_size: None,
136-
stack_ref_max_size: None,
137-
block_stack_max_size: None,
88+
stack_32_size: DEFAULT_VALUE_STACK_32_SIZE,
89+
stack_64_size: DEFAULT_VALUE_STACK_64_SIZE,
90+
stack_128_size: DEFAULT_VALUE_STACK_128_SIZE,
91+
stack_ref_size: DEFAULT_VALUE_STACK_REF_SIZE,
92+
call_stack_size: DEFAULT_CALL_STACK_SIZE,
93+
block_stack_size: DEFAULT_BLOCK_STACK_SIZE,
13894
}
13995
}
14096
}

crates/tinywasm/src/error.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use alloc::string::{String, ToString};
22
use alloc::vec::Vec;
33
use core::{fmt::Display, ops::ControlFlow};
4-
use tinywasm_types::FuncType;
54
use tinywasm_types::archive::TwasmError;
5+
use tinywasm_types::FuncType;
66

77
#[cfg(feature = "parser")]
88
pub use tinywasm_parser::ParseError;
@@ -121,6 +121,12 @@ pub enum Trap {
121121
/// Call stack overflow
122122
CallStackOverflow,
123123

124+
/// Block stack overflow
125+
BlockStackOverflow,
126+
127+
/// Value stack overflow
128+
ValueStackOverflow,
129+
124130
/// An undefined element was encountered
125131
UndefinedElement {
126132
/// The element index
@@ -153,6 +159,8 @@ impl Trap {
153159
Self::InvalidConversionToInt => "invalid conversion to integer",
154160
Self::IntegerOverflow => "integer overflow",
155161
Self::CallStackOverflow => "call stack exhausted",
162+
Self::BlockStackOverflow => "block stack exhausted",
163+
Self::ValueStackOverflow => "value stack exhausted",
156164
Self::UndefinedElement { .. } => "undefined element",
157165
Self::UninitializedElement { .. } => "uninitialized element",
158166
Self::IndirectCallTypeMismatch { .. } => "indirect call type mismatch",
@@ -235,6 +243,8 @@ impl Display for Trap {
235243
Self::InvalidConversionToInt => write!(f, "invalid conversion to integer"),
236244
Self::IntegerOverflow => write!(f, "integer overflow"),
237245
Self::CallStackOverflow => write!(f, "call stack exhausted"),
246+
Self::BlockStackOverflow => write!(f, "block stack exhausted"),
247+
Self::ValueStackOverflow => write!(f, "value stack exhausted"),
238248
Self::UndefinedElement { index } => write!(f, "undefined element: index={index}"),
239249
Self::UninitializedElement { index } => {
240250
write!(f, "uninitialized element: index={index}")

0 commit comments

Comments
 (0)