Skip to content

Commit 99f5168

Browse files
chore: cleanup
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 1b08814 commit 99f5168

File tree

9 files changed

+53
-55
lines changed

9 files changed

+53
-55
lines changed

Cargo.lock

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

crates/tinywasm/benches/argon2id.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ use types::TinyWasmModule;
55

66
const WASM: &[u8] = include_bytes!("../../../examples/rust/out/argon2id.opt.wasm");
77

8-
fn init_log() {
9-
let _ = pretty_env_logger::formatted_timed_builder().filter_level(log::LevelFilter::Off).try_init();
10-
}
11-
128
fn argon2id_parse() -> Result<TinyWasmModule> {
139
let parser = tinywasm_parser::Parser::new();
1410
let data = parser.parse_module_bytes(WASM)?;
@@ -34,7 +30,6 @@ fn argon2id_run(module: TinyWasmModule) -> Result<()> {
3430
}
3531

3632
fn criterion_benchmark(c: &mut Criterion) {
37-
init_log();
3833
let module = argon2id_parse().expect("argon2id_parse");
3934
let twasm = argon2id_to_twasm(&module).expect("argon2id_to_twasm");
4035

crates/tinywasm/benches/tinywasm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ fn criterion_benchmark(c: &mut Criterion) {
3535
let module = tinywasm_parse().expect("tinywasm_parse");
3636
let twasm = tinywasm_to_twasm(&module).expect("tinywasm_to_twasm");
3737

38-
// c.bench_function("tinywasm_parse", |b| b.iter(tinywasm_parse));
39-
// c.bench_function("tinywasm_to_twasm", |b| b.iter(|| tinywasm_to_twasm(&module)));
40-
// c.bench_function("tinywasm_from_twasm", |b| b.iter(|| tinywasm_from_twasm(&twasm)));
38+
c.bench_function("tinywasm_parse", |b| b.iter(tinywasm_parse));
39+
c.bench_function("tinywasm_to_twasm", |b| b.iter(|| tinywasm_to_twasm(&module)));
40+
c.bench_function("tinywasm_from_twasm", |b| b.iter(|| tinywasm_from_twasm(&twasm)));
4141
c.bench_function("tinywasm", |b| b.iter(|| tinywasm_run(module.clone())));
4242
}
4343

crates/tinywasm/src/interpreter/executor.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::*;
1717

1818
pub(crate) struct Executor<'store> {
1919
cf: CallFrame,
20-
instructions: ArcSlice<Instruction>,
2120
func: Rc<WasmFunction>,
2221
module: Rc<ModuleInstanceInner>,
2322
store: &'store mut Store,
@@ -27,8 +26,7 @@ impl<'store> Executor<'store> {
2726
pub(crate) fn new(store: &'store mut Store, cf: CallFrame) -> Result<Self> {
2827
let module = store.get_module_instance_raw(cf.module_addr).clone();
2928
let func = store.state.get_wasm_func(cf.func_addr).clone();
30-
let instructions = func.instructions.clone();
31-
Ok(Self { module, store, cf, func, instructions })
29+
Ok(Self { module, store, cf, func })
3230
}
3331

3432
pub(crate) fn run_to_completion(&mut self) -> Result<()> {
@@ -76,12 +74,12 @@ impl<'store> Executor<'store> {
7674
};
7775
}
7876

79-
let next = match self.instructions.0.get(self.cf.instr_ptr) {
77+
let next = match self.func.instructions.0.get(self.cf.instr_ptr) {
8078
Some(instr) => instr,
8179
None => unreachable!(
8280
"Instruction pointer out of bounds: {} ({} instructions)",
8381
self.cf.instr_ptr,
84-
self.instructions.0.len()
82+
self.func.instructions.0.len()
8583
),
8684
};
8785

@@ -116,36 +114,36 @@ impl<'store> Executor<'store> {
116114
return ControlFlow::Continue(());
117115
}
118116
DropKeepSmall { base32, keep32, base64, keep64, base128, keep128, base_ref, keep_ref } => {
119-
let b32 = self.cf.stack_base.s32 as usize + *base32 as usize;
117+
let b32 = self.cf.stack_base.s32 + *base32 as usize;
120118
let k32 = *keep32 as usize;
121119
self.store.stack.values.stack_32.truncate_keep(b32, k32);
122-
let b64 = self.cf.stack_base.s64 as usize + *base64 as usize;
120+
let b64 = self.cf.stack_base.s64 + *base64 as usize;
123121
let k64 = *keep64 as usize;
124122
self.store.stack.values.stack_64.truncate_keep(b64, k64);
125-
let b128 = self.cf.stack_base.s128 as usize + *base128 as usize;
123+
let b128 = self.cf.stack_base.s128 + *base128 as usize;
126124
let k128 = *keep128 as usize;
127125
self.store.stack.values.stack_128.truncate_keep(b128, k128);
128-
let bref = self.cf.stack_base.sref as usize + *base_ref as usize;
126+
let bref = self.cf.stack_base.sref + *base_ref as usize;
129127
let kref = *keep_ref as usize;
130128
self.store.stack.values.stack_ref.truncate_keep(bref, kref);
131129
}
132130
DropKeep32(base, keep) => {
133-
let b = self.cf.stack_base.s32 as usize + *base as usize;
131+
let b = self.cf.stack_base.s32 + *base as usize;
134132
let k = *keep as usize;
135133
self.store.stack.values.stack_32.truncate_keep(b, k);
136134
}
137135
DropKeep64(base, keep) => {
138-
let b = self.cf.stack_base.s64 as usize + *base as usize;
136+
let b = self.cf.stack_base.s64 + *base as usize;
139137
let k = *keep as usize;
140138
self.store.stack.values.stack_64.truncate_keep(b, k);
141139
}
142140
DropKeep128(base, keep) => {
143-
let b = self.cf.stack_base.s128 as usize + *base as usize;
141+
let b = self.cf.stack_base.s128 + *base as usize;
144142
let k = *keep as usize;
145143
self.store.stack.values.stack_128.truncate_keep(b, k);
146144
}
147145
DropKeepRef(base, keep) => {
148-
let b = self.cf.stack_base.sref as usize + *base as usize;
146+
let b = self.cf.stack_base.sref + *base as usize;
149147
let k = *keep as usize;
150148
self.store.stack.values.stack_ref.truncate_keep(b, k);
151149
}
@@ -154,7 +152,7 @@ impl<'store> Executor<'store> {
154152
let start = self.cf.instr_ptr + 1;
155153

156154
let target_ip = if idx >= 0 && (idx as u32) < *len {
157-
match self.instructions.0.get(start + idx as usize) {
155+
match self.func.instructions.0.get(start + idx as usize) {
158156
Some(Instruction::BranchTableTarget(ip)) => *ip,
159157
_ => *default_ip,
160158
}
@@ -629,9 +627,8 @@ impl<'store> Executor<'store> {
629627
func_addr: FuncAddr,
630628
owner: ModuleInstanceAddr,
631629
) -> ControlFlow<Option<Error>> {
632-
if self.func != wasm_func {
630+
if !Rc::ptr_eq(&self.func, &wasm_func) {
633631
self.func = wasm_func.clone();
634-
self.instructions = wasm_func.instructions.clone();
635632
}
636633

637634
if IS_RETURN_CALL {
@@ -716,7 +713,6 @@ impl<'store> Executor<'store> {
716713

717714
if cf.func_addr != self.cf.func_addr {
718715
self.func = self.store.state.get_wasm_func(cf.func_addr).clone();
719-
self.instructions = self.func.instructions.clone();
720716

721717
if cf.module_addr != self.module.idx {
722718
self.module = self.store.get_module_instance_raw(cf.module_addr).clone();
@@ -751,14 +747,14 @@ impl<'store> Executor<'store> {
751747
}
752748
fn exec_memory_grow(&mut self, addr: u32) -> Result<()> {
753749
let mem = self.store.state.get_mem_mut(self.module.resolve_mem_addr(addr));
754-
let prev_size = mem.page_count;
755-
let pages_delta = match mem.is_64bit() {
750+
let is_64bit = mem.is_64bit();
751+
let pages_delta = match is_64bit {
756752
true => self.store.stack.values.pop::<i64>(),
757753
false => i64::from(self.store.stack.values.pop::<i32>()),
758754
};
759755

760-
let size = mem.grow(pages_delta).map(|_| prev_size as i64).unwrap_or(-1);
761-
match mem.is_64bit() {
756+
let size = mem.grow(pages_delta).unwrap_or(-1);
757+
match is_64bit {
762758
true => self.store.stack.values.push::<i64>(size)?,
763759
false => self.store.stack.values.push::<i32>(size as i32)?,
764760
};

crates/tinywasm/src/interpreter/stack/call_stack.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ pub(crate) struct CallFrame {
4444

4545
#[derive(Debug, Clone, Copy, Default)]
4646
pub(crate) struct StackBase {
47-
pub(crate) s32: u32,
48-
pub(crate) s64: u32,
49-
pub(crate) s128: u32,
50-
pub(crate) sref: u32,
47+
pub(crate) s32: usize,
48+
pub(crate) s64: usize,
49+
pub(crate) s128: usize,
50+
pub(crate) sref: usize,
5151
}
5252

5353
#[derive(Debug)]

crates/tinywasm/src/interpreter/stack/value_stack.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ impl<T: Copy + Default> Stack<T> {
8686
}
8787

8888
pub(crate) fn pop_to_locals(&mut self, param_count: usize, local_count: usize) -> Box<[T]> {
89+
if local_count == 0 {
90+
debug_assert!(param_count == 0, "param count exceeds local count");
91+
return Box::new([]);
92+
}
93+
8994
let mut locals = alloc::vec![T::default(); local_count].into_boxed_slice();
9095
let start =
9196
self.len.checked_sub(param_count).unwrap_or_else(|| unreachable!("value stack underflow, this is a bug"));
@@ -120,10 +125,10 @@ impl ValueStack {
120125

121126
pub(crate) fn height(&self) -> StackBase {
122127
StackBase {
123-
s32: u32::try_from(self.stack_32.len()).expect("stack32 height overflow"),
124-
s64: u32::try_from(self.stack_64.len()).expect("stack64 height overflow"),
125-
s128: u32::try_from(self.stack_128.len()).expect("stack128 height overflow"),
126-
sref: u32::try_from(self.stack_ref.len()).expect("stack_ref height overflow"),
128+
s32: self.stack_32.len(),
129+
s64: self.stack_64.len(),
130+
s128: self.stack_128.len(),
131+
sref: self.stack_ref.len(),
127132
}
128133
}
129134

crates/tinywasm/src/store/memory.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ impl MemoryInstance {
5454
Ok(())
5555
}
5656

57-
pub(crate) fn max_pages(&self) -> usize {
58-
self.kind.page_count_max().try_into().unwrap_or(usize::MAX)
59-
}
60-
6157
pub(crate) fn load(&self, addr: usize, len: usize) -> Result<&[u8]> {
6258
let Some(end) = addr.checked_add(len) else {
6359
cold();
@@ -133,9 +129,10 @@ impl MemoryInstance {
133129
let current_pages = self.page_count;
134130
let pages_delta = usize::try_from(pages_delta).ok()?;
135131
let new_pages = current_pages.checked_add(pages_delta)?;
132+
let max_pages = self.kind.page_count_max().try_into().unwrap_or(usize::MAX);
136133

137-
if new_pages > self.max_pages() {
138-
log::debug!("memory.grow failed: new_pages={}, max_pages={}", new_pages, self.max_pages());
134+
if new_pages > max_pages {
135+
log::debug!("memory.grow failed: new_pages={}, max_pages={}", new_pages, max_pages);
139136
return None;
140137
}
141138

@@ -145,7 +142,12 @@ impl MemoryInstance {
145142
return None;
146143
}
147144

148-
self.data.resize(usize::try_from(new_size).ok()?, 0);
145+
let new_size = usize::try_from(new_size).ok()?;
146+
if new_size == self.data.len() {
147+
return i64::try_from(current_pages).ok();
148+
}
149+
150+
self.data.resize(new_size, 0);
149151
self.page_count = new_pages;
150152
i64::try_from(current_pages).ok()
151153
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
0.8.0,57,0,[{"name":"custom-page-sizes-invalid.wast","passed":22,"failed":0},{"name":"custom-page-sizes.wast","passed":35,"failed":0}]
2-
0.9.0-alpha.0,80,0,[{"name":"custom-page-sizes-invalid.wast","passed":23,"failed":0},{"name":"custom-page-sizes.wast","passed":45,"failed":0},{"name":"memory_max.wast","passed":6,"failed":0},{"name":"memory_max_i64.wast","passed":6,"failed":0}]
2+
0.9.0-alpha.0,211,0,[{"name":"binary.wast","passed":127,"failed":0},{"name":"custom-page-sizes-invalid.wast","passed":23,"failed":0},{"name":"custom-page-sizes.wast","passed":45,"failed":0},{"name":"memory_max.wast","passed":8,"failed":0},{"name":"memory_max_i64.wast","passed":8,"failed":0}]

crates/types/src/instructions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ pub enum Instruction {
5959
DropKeep64(u16, u16),
6060
DropKeep128(u16, u16),
6161
DropKeepRef(u16, u16),
62-
BranchTable(u32, u32), // (default_landing_pad_ip, target_count) followed by BranchTableTarget entries
62+
BranchTable(u32, u32), // (default_landing_pad_ip, target_count) - followed by BranchTableTarget entries
6363
BranchTableTarget(u32), // (landing_pad_ip)
6464
Return,
6565
Call(FuncAddr),
6666
CallIndirect(TypeAddr, TableAddr),
6767
ReturnCall(FuncAddr),
6868
ReturnCallIndirect(TypeAddr, TableAddr),
69-
69+
7070
// > Parametric Instructions
7171
// See <https://webassembly.github.io/spec/core/binary/instructions.html#parametric-instructions>
7272
Drop32, Select32,

0 commit comments

Comments
 (0)