Skip to content

Commit fae56e1

Browse files
Rebase on current master of upstream
1 parent 11a726b commit fae56e1

File tree

11 files changed

+81
-251
lines changed

11 files changed

+81
-251
lines changed

crates/ir/src/enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ macro_rules! define_enum {
4040
/// The documentation of each [`Instruction`] describes its encoding in the
4141
/// `#Encoding` section of its documentation if it requires more than a single
4242
/// instruction for its encoding.
43-
#[derive(Debug)]
43+
#[derive(Debug, PartialEq, Eq)]
4444
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
4545
#[cfg_attr(feature = "deserialization", derive(serde::Deserialize))]
4646
#[non_exhaustive]

crates/wasmi/src/engine/code_map.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ use spin::Mutex;
2525
#[cfg(feature = "parser")]
2626
use super::{FuncTranslationDriver, FuncTranslator, TranslationError, ValidatingFuncTranslator};
2727
#[cfg(feature = "parser")]
28-
use crate::{
29-
core::{FuelCostsProvider, FuelError},
30-
engine::ResumableOutOfFuelError,
31-
module::{FuncIdx, ModuleHeader},
32-
};
28+
use crate::module::{FuncIdx, ModuleHeader};
3329
#[cfg(feature = "parser")]
3430
use core::{fmt, mem::MaybeUninit};
3531
#[cfg(feature = "parser")]

crates/wasmi/src/engine/config.rs

Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -82,68 +82,6 @@ impl Config {
8282
features
8383
}
8484

85-
/// Sets the maximum recursion depth of the [`Engine`]'s stack during execution.
86-
///
87-
/// # Note
88-
///
89-
/// An execution traps if it exceeds this limits.
90-
///
91-
/// [`Engine`]: [`crate::Engine`]
92-
pub fn set_max_recursion_depth(&mut self, value: usize) -> &mut Self {
93-
self.stack.set_max_recursion_depth(value);
94-
self
95-
}
96-
97-
/// Sets the minimum (or initial) height of the [`Engine`]'s value stack in bytes.
98-
///
99-
/// # Note
100-
///
101-
/// - Lower initial heights may improve memory consumption.
102-
/// - Higher initial heights may improve cold start times.
103-
///
104-
/// # Panics
105-
///
106-
/// If `value` is greater than the current maximum height of the value stack.
107-
///
108-
/// [`Engine`]: [`crate::Engine`]
109-
pub fn set_min_stack_height(&mut self, value: usize) -> &mut Self {
110-
if self.stack.set_min_stack_height(value).is_err() {
111-
let max = self.stack.max_stack_height();
112-
panic!("minimum stack height exceeds maximum: min={value}, max={max}");
113-
}
114-
self
115-
}
116-
117-
/// Sets the maximum height of the [`Engine`]'s value stack in bytes.
118-
///
119-
/// # Note
120-
///
121-
/// An execution traps if it exceeds this limits.
122-
///
123-
/// # Panics
124-
///
125-
/// If `value` is less than the current minimum height of the value stack.
126-
///
127-
/// [`Engine`]: [`crate::Engine`]
128-
pub fn set_max_stack_height(&mut self, value: usize) -> &mut Self {
129-
if self.stack.set_max_stack_height(value).is_err() {
130-
let max = self.stack.min_stack_height();
131-
panic!("maximum stack height is lower than minimum: min={value}, max={max}");
132-
}
133-
self
134-
}
135-
136-
/// Sets the maximum number of cached stacks for reuse for the [`Config`].
137-
///
138-
/// # Note
139-
///
140-
/// - A higher value may improve execution performance.
141-
/// - A lower value may improve memory consumption.
142-
pub fn set_max_cached_stacks(&mut self, value: usize) -> &mut Self {
143-
self.stack.set_max_cached_stacks(value);
144-
self
145-
}
146-
14785
/// Enable or disable the [`mutable-global`] Wasm proposal for the [`Config`].
14886
///
14987
/// # Note
@@ -326,30 +264,66 @@ impl Config {
326264
}
327265

328266
impl Config {
329-
/// Sets the [`StackLimits`] for the [`Config`].
330-
pub fn set_stack_limits(&mut self, stack_limits: StackLimits) -> &mut Self {
331-
self.stack_limits = stack_limits;
267+
/// Sets the maximum recursion depth of the [`Engine`]'s stack during execution.
268+
///
269+
/// # Note
270+
///
271+
/// An execution traps if it exceeds this limits.
272+
///
273+
/// [`Engine`]: [`crate::Engine`]
274+
pub fn set_max_recursion_depth(&mut self, value: usize) -> &mut Self {
275+
self.stack.set_max_recursion_depth(value);
332276
self
333277
}
334278

335-
/// Returns the [`StackLimits`] of the [`Config`].
336-
pub(super) fn stack_limits(&self) -> StackLimits {
337-
self.stack_limits
279+
/// Sets the minimum (or initial) height of the [`Engine`]'s value stack in bytes.
280+
///
281+
/// # Note
282+
///
283+
/// - Lower initial heights may improve memory consumption.
284+
/// - Higher initial heights may improve cold start times.
285+
///
286+
/// # Panics
287+
///
288+
/// If `value` is greater than the current maximum height of the value stack.
289+
///
290+
/// [`Engine`]: [`crate::Engine`]
291+
pub fn set_min_stack_height(&mut self, value: usize) -> &mut Self {
292+
if self.stack.set_min_stack_height(value).is_err() {
293+
let max = self.stack.max_stack_height();
294+
panic!("minimum stack height exceeds maximum: min={value}, max={max}");
295+
}
296+
self
338297
}
339298

340-
/// Sets the maximum amount of cached stacks for reuse for the [`Config`].
299+
/// Sets the maximum height of the [`Engine`]'s value stack in bytes.
341300
///
342301
/// # Note
343302
///
344-
/// Defaults to 2.
345-
pub fn set_cached_stacks(&mut self, amount: usize) -> &mut Self {
346-
self.cached_stacks = amount;
303+
/// An execution traps if it exceeds this limits.
304+
///
305+
/// # Panics
306+
///
307+
/// If `value` is less than the current minimum height of the value stack.
308+
///
309+
/// [`Engine`]: [`crate::Engine`]
310+
pub fn set_max_stack_height(&mut self, value: usize) -> &mut Self {
311+
if self.stack.set_max_stack_height(value).is_err() {
312+
let max = self.stack.min_stack_height();
313+
panic!("maximum stack height is lower than minimum: min={value}, max={max}");
314+
}
347315
self
348316
}
349317

350-
/// Returns the maximum amount of cached stacks for reuse of the [`Config`].
351-
pub(super) fn cached_stacks(&self) -> usize {
352-
self.cached_stacks
318+
/// Sets the maximum number of cached stacks for reuse for the [`Config`].
319+
///
320+
/// # Note
321+
///
322+
/// - A higher value may improve execution performance.
323+
/// - A lower value may improve memory consumption.
324+
pub fn set_max_cached_stacks(&mut self, value: usize) -> &mut Self {
325+
self.stack.set_max_cached_stacks(value);
326+
self
353327
}
354328

355329
/// Configures whether Wasmi will consume fuel during execution to either halt execution as desired.

crates/wasmi/src/engine/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ mod block_type;
1414
#[cfg(feature = "parser")]
1515
mod translator;
1616

17-
#[cfg(all(test, feature = "parser"))]
18-
mod tests;
19-
2017
#[cfg(feature = "parser")]
2118
pub(crate) use self::translator::TranslationError;
2219

crates/wasmi/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::errors::{
22
EnforcedLimitsError, FuncError, GlobalError, InstantiationError, IrError, LinkerError,
33
};
44
use crate::{
5-
engine::{ResumableError, ResumableHostTrapError, ResumableOutOfFuelError, TranslationError},
5+
engine::{ResumableError, ResumableHostTrapError, ResumableOutOfFuelError},
66
module::ReadError,
77
TrapCode,
88
};

crates/wasmi/src/module/init_expr.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -251,48 +251,6 @@ impl ConstExprStack {
251251
}
252252
}
253253

254-
/// Stack to translate [`ConstExpr`].
255-
#[derive(Debug, Default)]
256-
pub struct ConstExprStack {
257-
/// The top-most [`Op`] on the stack.
258-
///
259-
/// # Note
260-
/// This is an optimization so that the [`ConstExprStack`] does not
261-
/// require heap allocations for the common case where only a single
262-
/// stack slot is needed.
263-
top: Option<Op>,
264-
/// The remaining ops on the stack.
265-
ops: Vec<Op>,
266-
}
267-
268-
impl ConstExprStack {
269-
/// Returns `true` if [`ConstExprStack`] is empty.
270-
pub fn is_empty(&self) -> bool {
271-
self.ops.is_empty()
272-
}
273-
274-
/// Pushes an [`Op`] to the [`ConstExprStack`].
275-
pub fn push(&mut self, op: Op) {
276-
let old_top = self.top.replace(op);
277-
if let Some(old_top) = old_top {
278-
self.ops.push(old_top);
279-
}
280-
}
281-
282-
/// Pops the top-most [`Op`] from the [`ConstExprStack`] if any.
283-
pub fn pop(&mut self) -> Option<Op> {
284-
let new_top = self.ops.pop();
285-
mem::replace(&mut self.top, new_top)
286-
}
287-
288-
/// Pops the 2 top-most [`Op`]s from the [`ConstExprStack`] if any.
289-
pub fn pop2(&mut self) -> Option<(Op, Op)> {
290-
let rhs = self.pop()?;
291-
let lhs = self.pop()?;
292-
Some((lhs, rhs))
293-
}
294-
}
295-
296254
#[cfg(feature = "parser")]
297255
impl ConstExpr {
298256
/// Creates a new [`ConstExpr`] from the given Wasm [`ConstExpr`].

crates/wasmi/src/module/mod.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -214,32 +214,6 @@ impl ModuleImports {
214214

215215
#[cfg(feature = "parser")]
216216
impl Module {
217-
/// Creates a new Wasm [`Module`] from the given Wasm bytecode stream.
218-
///
219-
/// # Note
220-
///
221-
/// This parses, validates and translates the Wasm bytecode yielded by `stream`.
222-
///
223-
/// # Errors
224-
///
225-
/// - If the Wasm bytecode is malformed or fails to validate.
226-
/// - If the Wasm bytecode violates restrictions
227-
/// set in the [`Config`] used by the `engine`.
228-
/// - If Wasmi cannot translate the Wasm bytecode.
229-
///
230-
/// [`Config`]: crate::Config
231-
#[deprecated(
232-
since = "0.48.0",
233-
note = "\
234-
This API has been deprecated because it is inefficient and unserused. \
235-
Please use the `Module::new` API instead if possible. \
236-
If you have an urgent need for this API, please tell us at: https://github.com/wasmi-labs/wasmi \
237-
"
238-
)]
239-
pub fn new_streaming(engine: &Engine, stream: impl Read) -> Result<Self, Error> {
240-
ModuleParser::new(engine).parse_streaming(stream)
241-
}
242-
243217
/// Creates a new Wasm [`Module`] from the given Wasm bytecode buffer.
244218
///
245219
/// # Note

crates/wasmi/src/preparsed/deserialization/tests.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ fn imports_roundtrip() {
1919
let engine = Engine::default();
2020
let module = Module::new(&engine, &wasm_bytes).expect("Failed to create Module");
2121
// Serialize
22-
let ser =
23-
crate::preparsed::SerializedModule::from_module(&module, &engine).expect("serialize");
22+
let ser = crate::preparsed::SerializedModule::from_module(&module, &engine).expect("serialize");
2423
let bytes = postcard::to_allocvec(&ser).expect("postcard serialize");
2524
// Deserialize
2625
let (deser_mod, _other_engine) = deserialize_module(&bytes).expect("failed to deserialize");
@@ -64,8 +63,7 @@ fn addition_module_serialize_deserialize_and_run() {
6463
let engine = Engine::default();
6564
let module = Module::new(&engine, &wasm_bytes).expect("Failed to create Module");
6665
// Serialize
67-
let ser =
68-
crate::preparsed::SerializedModule::from_module(&module, &engine).expect("serialize");
66+
let ser = crate::preparsed::SerializedModule::from_module(&module, &engine).expect("serialize");
6967
let bytes = postcard::to_allocvec(&ser).expect("postcard serialize");
7068
// Deserialize with a new engine
7169
let (deser_mod, other_engine) =
@@ -74,10 +72,9 @@ fn addition_module_serialize_deserialize_and_run() {
7472

7573
let mut store = Store::new(&other_engine, ());
7674
let linker = <Linker<()>>::new(&other_engine);
77-
let instantiated = linker
78-
.instantiate(&mut store, &deser_mod)
75+
let started = linker
76+
.instantiate_and_start(&mut store, &deser_mod)
7977
.expect("failed to instantiate");
80-
let started = instantiated.start(&mut store).expect("failed to start");
8178
let add = started
8279
.get_typed_func::<(i32, i32), i32>(&mut store, "add")
8380
.expect("failed to get function");
@@ -109,10 +106,9 @@ fn memory_export_roundtrip() {
109106
// Instantiate and check that memory is exported
110107
let mut store = Store::new(&other_engine, ());
111108
let linker = <Linker<()>>::new(&other_engine);
112-
let instantiated = linker
113-
.instantiate(&mut store, &deser_mod)
109+
let started = linker
110+
.instantiate_and_start(&mut store, &deser_mod)
114111
.expect("failed to instantiate");
115-
let started = instantiated.start(&mut store).expect("failed to start");
116112

117113
// Check that memory is exported
118114
let memory = started
@@ -176,12 +172,10 @@ fn module_with_host_import_roundtrip() {
176172
.expect("Failed to define host function");
177173

178174
// Instantiate the module
179-
let instance = linker
180-
.instantiate(&mut store, &deserialized_module)
175+
let started = linker
176+
.instantiate_and_start(&mut store, &deserialized_module)
181177
.expect("Failed to instantiate module");
182178

183-
let started = instance.start(&mut store).expect("failed to start");
184-
185179
// Get the exported function
186180
let add_reduced = started
187181
.get_typed_func::<(i32, i32), i32>(&store, "add_reduced")

crates/wasmi/src/preparsed/serialization/tests.rs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,6 @@ use crate::{
55
};
66
use wat::parse_str as parse_wat;
77

8-
#[test]
9-
fn serialization_succeeds() {
10-
// Minimal valid Wasm module: (module (func))
11-
let wat = "(module (func))";
12-
let wasm_bytes = parse_wat(wat).expect("Failed to parse WAT");
13-
let engine = Engine::default();
14-
let module = Module::new(&engine, &wasm_bytes).expect("Failed to create Module");
15-
let result = SerializedModule::from_module(&module, &engine);
16-
assert!(
17-
result.is_ok(),
18-
"from_module should succeed for minimal module"
19-
);
20-
let serialized = result.unwrap();
21-
assert_eq!(serialized.version, SERIALIZATION_VERSION);
22-
// Check that instructions match engine's instructions for internal functions
23-
let mut expected_instrs = Vec::new();
24-
for (_dedup_fn, engine_func) in module.internal_funcs() {
25-
let mut instrs = Vec::new();
26-
let mut idx = 0;
27-
loop {
28-
match engine.resolve_instr(engine_func, idx) {
29-
Ok(Some(instr)) => instrs.push(instr),
30-
Ok(None) => break,
31-
Err(e) => panic!("Failed to extract instructions: {e}"),
32-
}
33-
idx += 1;
34-
}
35-
expected_instrs.push(instrs);
36-
}
37-
assert_eq!(
38-
serialized.internal_functions.len(),
39-
expected_instrs.len(),
40-
"Number of serialized functions should match"
41-
);
42-
for (serialized_func, expected_instrs) in serialized
43-
.internal_functions
44-
.iter()
45-
.zip(expected_instrs.iter())
46-
{
47-
assert_eq!(
48-
serialized_func.instructions, *expected_instrs,
49-
"Serialized instructions should match engine instructions"
50-
);
51-
}
52-
}
53-
548
#[test]
559
fn functions_extraction() {
5610
// WAT with three functions: (func) (func (param i32)) (func (param i32) (result i64))

0 commit comments

Comments
 (0)