Skip to content

Commit 1cf50a2

Browse files
fix: intruction enum size regression
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent e2001eb commit 1cf50a2

File tree

8 files changed

+35
-28
lines changed

8 files changed

+35
-28
lines changed

Cargo.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ version="0.9.0-alpha.0"
2222
rust-version="1.90"
2323
edition="2024"
2424
license="MIT OR Apache-2.0"
25-
authors=["Henry Gressmann <mail@henrygressmann.de>"]
25+
authors=["Henry Gressmann <crates@henrygressmann.de>"]
2626
repository="https://github.com/explodingcamera/tinywasm"
2727
categories=["wasm", "no-std"]
2828
keywords=["tinywasm"]

crates/parser/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub use tinywasm_types::TinyWasmModule;
4141

4242
/// Parser optimization and lowering options.
4343
#[non_exhaustive]
44-
#[derive(Debug, Clone)]
44+
#[derive(Debug, Clone, Default)]
4545
pub struct ParserOptions {
4646
// /// Enable control-flow graph cleanup rewrites.
4747
// pub cfg_cleanup: bool,
@@ -51,12 +51,6 @@ pub struct ParserOptions {
5151
// pub tailcall_rewrite: bool,
5252
}
5353

54-
impl Default for ParserOptions {
55-
fn default() -> Self {
56-
Self {}
57-
}
58-
}
59-
6054
/// A WebAssembly parser
6155
#[derive(Debug, Default)]
6256
pub struct Parser {

crates/parser/src/visit.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
431431
let addr = self.instructions[len - 2];
432432
let value = self.instructions[len - 1];
433433
if let (Instruction::LocalGet32(addr_local), Instruction::LocalGet32(value_local)) = (addr, value) {
434+
let (Ok(addr_local), Ok(value_local)) = (u8::try_from(addr_local), u8::try_from(value_local)) else {
435+
self.instructions.push(Instruction::I32Store(memarg));
436+
return;
437+
};
434438
self.instructions.pop();
435439
self.instructions.pop();
436440
self.instructions.push(Instruction::I32StoreLocalLocal(memarg, addr_local, value_local));
@@ -626,6 +630,11 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
626630
if let (Instruction::LocalGet32(addr_local), Instruction::I32Load(memarg)) = (addr, load) {
627631
match self.validator.get_operand_type(0) {
628632
Some(Some(wasmparser::ValType::I32)) | Some(Some(wasmparser::ValType::F32)) => {
633+
let (Ok(addr_local), Ok(resolved_idx)) = (u8::try_from(addr_local), u8::try_from(resolved_idx))
634+
else {
635+
self.instructions.push(Instruction::LocalTee32(resolved_idx));
636+
return;
637+
};
629638
self.instructions.pop();
630639
self.instructions.pop();
631640
self.instructions.push(Instruction::I32LoadLocalTee(memarg, addr_local, resolved_idx));

crates/tinywasm/src/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub enum ExecProgress<T> {
1313
Suspended,
1414
}
1515

16-
#[derive(Clone, Copy)]
16+
#[derive(Clone)]
1717
#[cfg_attr(feature = "debug", derive(Debug))]
1818
pub(crate) struct ExecutionState {
1919
pub(crate) callframe: CallFrame,

crates/tinywasm/src/interpreter/executor.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,17 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
174174
I64AddConst(c) => stack_op!(unary i64, |v| v.wrapping_add(*c)),
175175
I32StoreLocalLocal(m, addr_local, value_local) => {
176176
let mem = self.store.state.get_mem_mut(self.module.resolve_mem_addr(m.mem_addr()));
177-
let addr = u64::from(self.store.stack.values.local_get::<u32>(&self.cf, *addr_local));
178-
let value = self.store.stack.values.local_get::<u32>(&self.cf, *value_local).to_mem_bytes();
177+
let addr_local = u16::from(*addr_local);
178+
let value_local = u16::from(*value_local);
179+
let addr = u64::from(self.store.stack.values.local_get::<u32>(&self.cf, addr_local));
180+
let value = self.store.stack.values.local_get::<u32>(&self.cf, value_local).to_mem_bytes();
179181
mem.store((m.offset() + addr) as usize, value.len(), &value)?;
180182
}
181183
I32LoadLocalTee(m, addr_local, dst_local) => {
182184
let mem = self.store.state.get_mem(self.module.resolve_mem_addr(m.mem_addr()));
183-
let addr = u64::from(self.store.stack.values.local_get::<u32>(&self.cf, *addr_local));
185+
let addr_local = u16::from(*addr_local);
186+
let dst_local = u16::from(*dst_local);
187+
let addr = u64::from(self.store.stack.values.local_get::<u32>(&self.cf, addr_local));
184188
let Some(Ok(addr)) = m.offset().checked_add(addr).map(|a| a.try_into()) else {
185189
return Err(Error::Trap(Trap::MemoryOutOfBounds {
186190
offset: addr as usize,
@@ -189,7 +193,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
189193
}));
190194
};
191195
let value = mem.load_as::<4, i32>(addr)?;
192-
self.store.stack.values.local_set(&self.cf, *dst_local, value);
196+
self.store.stack.values.local_set(&self.cf, dst_local, value);
193197
self.store.stack.values.push(value)?;
194198
}
195199
I64XorRotlConst(c) => stack_op!(binary i64, |lhs, rhs| (lhs ^ rhs).rotate_left(*c as u32)),

crates/tinywasm/src/interpreter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{Result, Store, interpreter::stack::CallFrame};
1111
pub(crate) use value128::*;
1212
pub(crate) use values::*;
1313

14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
#[cfg_attr(feature = "debug", derive(Debug))]
1616
pub(crate) enum ExecState {
1717
Completed,

crates/types/src/instructions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub enum Instruction {
5656
LocalCopy32(LocalAddr, LocalAddr), LocalCopy64(LocalAddr, LocalAddr), LocalCopy128(LocalAddr, LocalAddr), LocalCopyRef(LocalAddr, LocalAddr),
5757
I32AddLocals(LocalAddr, LocalAddr), I64AddLocals(LocalAddr, LocalAddr),
5858
I32AddConst(i32), I64AddConst(i64),
59-
I32StoreLocalLocal(MemoryArg, LocalAddr, LocalAddr),
60-
I32LoadLocalTee(MemoryArg, LocalAddr, LocalAddr),
59+
I32StoreLocalLocal(MemoryArg, u8, u8),
60+
I32LoadLocalTee(MemoryArg, u8, u8),
6161
I64XorRotlConst(i64),
6262
I64XorRotlConstTee(i64, LocalAddr),
6363
// > Control Instructions (jump-oriented, lowered from structured control during parsing)

0 commit comments

Comments
 (0)