Skip to content

Commit

Permalink
wip: progress
Browse files Browse the repository at this point in the history
  • Loading branch information
nerodesu017 committed Oct 22, 2024
1 parent 80e7b64 commit 1898f14
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/core/reader/types/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Debug for DataSegment {
/// ```wasm
/// (module
/// (memory 1) ;; memory starting with 1 page
/// (data (i32.const 0) "wtf") ;; writing the array of byte "wtf" in the first memory (0) at offset 0
/// (data (i32.const 0) "abc") ;; writing the array of byte "abc" in the first memory (0) at offset 0
/// ;; for hardcoded offsets, we'll usually use i32.const because of wasm being x86 arch
/// )
/// ```
Expand Down
78 changes: 27 additions & 51 deletions src/execution/interpreter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub(super) fn run<H: HookSet>(
global.value = stack.pop_value(global.global.ty.ty)
}
I32_LOAD => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand All @@ -155,7 +155,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i32.load [{relative_address}] -> [{data}]");
}
I64_LOAD => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated();
Expand All @@ -182,7 +182,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.load [{relative_address}] -> [{data}]");
}
F32_LOAD => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand All @@ -208,7 +208,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: f32.load [{relative_address}] -> [{data}]");
}
F64_LOAD => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated();
Expand All @@ -235,7 +235,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: f64.load [{relative_address}] -> [{data}]");
}
I32_LOAD8_S => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand Down Expand Up @@ -263,7 +263,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i32.load8_s [{relative_address}] -> [{data}]");
}
I32_LOAD8_U => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand Down Expand Up @@ -291,7 +291,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i32.load8_u [{relative_address}] -> [{data}]");
}
I32_LOAD16_S => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand All @@ -318,7 +318,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i32.load16_s [{relative_address}] -> [{data}]");
}
I32_LOAD16_U => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand All @@ -345,7 +345,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i32.load16_u [{relative_address}] -> [{data}]");
}
I64_LOAD8_S => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand Down Expand Up @@ -373,7 +373,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.load8_s [{relative_address}] -> [{data}]");
}
I64_LOAD8_U => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand Down Expand Up @@ -401,7 +401,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.load8_u [{relative_address}] -> [{data}]");
}
I64_LOAD16_S => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand All @@ -428,7 +428,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.load16_s [{relative_address}] -> [{data}]");
}
I64_LOAD16_U => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand All @@ -455,7 +455,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.load16_u [{relative_address}] -> [{data}]");
}
I64_LOAD32_S => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand All @@ -482,7 +482,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.load32_s [{relative_address}] -> [{data}]");
}
I64_LOAD32_U => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

let mem = store.mems.first().unwrap_validated(); // there is only one memory allowed as of now
Expand All @@ -509,7 +509,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.load32_u [{relative_address}] -> [{data}]");
}
I32_STORE => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -530,7 +530,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i32.store [{relative_address} {data_to_store}] -> []");
}
I64_STORE => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: u32 = stack.pop_value(ValType::NumType(NumType::I64)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -551,7 +551,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.store [{relative_address} {data_to_store}] -> []");
}
F32_STORE => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: f32 = stack.pop_value(ValType::NumType(NumType::F32)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -572,7 +572,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: f32.store [{relative_address} {data_to_store}] -> []");
}
F64_STORE => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: f64 = stack.pop_value(ValType::NumType(NumType::F64)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -593,7 +593,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: f64.store [{relative_address} {data_to_store}] -> []");
}
I32_STORE8 => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: i32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -615,7 +615,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i32.store8 [{relative_address} {data_to_store}] -> []");
}
I32_STORE16 => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: i32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -637,7 +637,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i32.store16 [{relative_address} {data_to_store}] -> []");
}
I64_STORE8 => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: i64 = stack.pop_value(ValType::NumType(NumType::I64)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -659,7 +659,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.store8 [{relative_address} {data_to_store}] -> []");
}
I64_STORE16 => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: i64 = stack.pop_value(ValType::NumType(NumType::I64)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -681,7 +681,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.store16 [{relative_address} {data_to_store}] -> []");
}
I64_STORE32 => {
let memarg = MemArg::read_unvalidated(&mut wasm);
let memarg = MemArg::read(&mut wasm).unwrap();

let data_to_store: i64 = stack.pop_value(ValType::NumType(NumType::I64)).into();
let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand All @@ -703,26 +703,14 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: i64.store32 [{relative_address} {data_to_store}] -> []");
}
MEMORY_SIZE => {
let mem_idx =
// if multi_memory_is_enabled {
// wasm.read_var_u32().unwrap_validated() as usize
// } else {
wasm.read_u8().unwrap_validated() as usize
// }
;
let mem_idx = wasm.read_u8().unwrap_validated() as usize;
let mem = store.mems.get(mem_idx).unwrap_validated();
let size = mem.size() as u32;
stack.push_value(Value::I32(size));
trace!("Instruction: memory.size [] -> [{}]", size);
}
MEMORY_GROW => {
let mem_idx =
// if multi_memory_is_enabled {
// wasm.read_var_u32().unwrap_validated() as usize
// } else {
wasm.read_u8().unwrap_validated() as usize
// }
;
let mem_idx = wasm.read_u8().unwrap_validated() as usize;
let mem = store.mems.get_mut(mem_idx).unwrap_validated();
let delta: i32 = stack.pop_value(ValType::NumType(NumType::I32)).into();

Expand Down Expand Up @@ -2038,13 +2026,7 @@ pub(super) fn run<H: HookSet>(
MEMORY_INIT => {
let data_idx = wasm.read_var_u32().unwrap_validated() as DataIdx;
let data = unsafe { store.data.get_unchecked_mut(data_idx) };
let mem_idx =
// if multi_memory_is_enabled {
// wasm.read_var_u32().unwrap_validated() as usize
// } else {
wasm.read_u8().unwrap_validated() as usize
// }
;
let mem_idx = wasm.read_u8().unwrap_validated() as usize;
let mem = store.mems.get(mem_idx).unwrap_validated();
let mut n: i32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
let mut s: i32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand Down Expand Up @@ -2205,13 +2187,7 @@ pub(super) fn run<H: HookSet>(
trace!("Instruction: memory.copy");
}
MEMORY_FILL => {
let mem_idx =
// if multi_memory_is_enabled {
// wasm.read_var_u32().unwrap_validated() as usize
// } else {
wasm.read_u8().unwrap_validated() as usize
// }
;
let mem_idx = wasm.read_u8().unwrap_validated() as usize;
let mem = store.mems.get(mem_idx).unwrap_validated();
let mut n: i32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
let val: i32 = stack.pop_value(ValType::NumType(NumType::I32)).into();
Expand Down
11 changes: 0 additions & 11 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use const_interpreter_loop::run_const;
use function_ref::FunctionRef;
use interpreter_loop::run;
use locals::Locals;
use store::TableInst;
use value_stack::Stack;

use crate::core::reader::types::data::DataSegment;
Expand Down Expand Up @@ -343,15 +342,6 @@ where
.collect()
};

let table_instances = validation_info
.tables
.iter()
.map(|ty| TableInst {
ty: *ty,
elem: Vec::new(),
})
.collect();

let mut memory_instances: Vec<MemInst> = validation_info
.memories
.iter()
Expand Down Expand Up @@ -425,7 +415,6 @@ where

Store {
funcs: function_instances,
tables: table_instances,
mems: memory_instances,
globals: global_instances,
data: data_sections,
Expand Down
1 change: 0 additions & 1 deletion src/execution/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::execution::value::{Ref, Value};
/// <https://webassembly.github.io/spec/core/exec/runtime.html#store>
pub struct Store {
pub funcs: Vec<FuncInst>,
pub tables: Vec<TableInst>,
pub mems: Vec<MemInst>,
pub globals: Vec<GlobalInst>,
pub data: Vec<DataSegment>,
Expand Down
Loading

0 comments on commit 1898f14

Please sign in to comment.