wasm_interp: tests for store instructions

This commit is contained in:
Brian Carroll 2022-11-25 16:29:37 +00:00
parent 65fc079393
commit 06f3726e35
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
2 changed files with 197 additions and 21 deletions

View file

@ -19,9 +19,7 @@ pub enum Action {
#[derive(Debug)] #[derive(Debug)]
pub struct ExecutionState<'a> { pub struct ExecutionState<'a> {
#[allow(dead_code)] pub memory: Vec<'a, u8>,
memory: Vec<'a, u8>,
pub call_stack: CallStack<'a>, pub call_stack: CallStack<'a>,
pub value_stack: ValueStack<'a>, pub value_stack: ValueStack<'a>,
pub globals: Vec<'a, Value>, pub globals: Vec<'a, Value>,
@ -168,6 +166,9 @@ impl<'a> ExecutionState<'a> {
let offset = self.fetch_immediate_u32(module); let offset = self.fetch_immediate_u32(module);
let value = self.value_stack.pop(); let value = self.value_stack.pop();
let base_addr = self.value_stack.pop_u32(); let base_addr = self.value_stack.pop_u32();
dbg!(base_addr, offset);
let addr = (base_addr + offset) as usize; let addr = (base_addr + offset) as usize;
(addr, value) (addr, value)
} }

View file

@ -460,32 +460,207 @@ fn test_i64load32u() {
); );
} }
// #[test] fn test_store<'a>(
// fn test_i32store() {} arena: &'a Bump,
module: &mut WasmModule<'a>,
addr: u32,
store_op: OpCode,
offset: u32,
value: Value,
) -> Vec<'a, u8> {
let is_debug_mode = false;
let start_fn_name = "test";
// #[test] module.memory = MemorySection::new(arena, MemorySection::PAGE_SIZE);
// fn test_i64store() {}
// #[test] let signature = Signature {
// fn test_f32store() {} param_types: bumpalo::vec![in arena],
ret_type: None,
};
// #[test] create_exported_function_no_locals(module, start_fn_name, signature, |buf| {
// fn test_f64store() {} buf.append_u8(OpCode::I32CONST as u8);
buf.encode_u32(addr);
match value {
Value::I32(x) => {
buf.append_u8(OpCode::I32CONST as u8);
buf.encode_i32(x);
}
Value::I64(x) => {
buf.append_u8(OpCode::I64CONST as u8);
buf.encode_i64(x);
}
Value::F32(x) => {
buf.append_u8(OpCode::F32CONST as u8);
buf.encode_f32(x);
}
Value::F64(x) => {
buf.append_u8(OpCode::F64CONST as u8);
buf.encode_f64(x);
}
}
buf.append_u8(store_op as u8);
buf.encode_u32(0); // align
buf.encode_u32(offset);
buf.append_u8(OpCode::END as u8);
});
// #[test] let mut state =
// fn test_i32store8() {} ExecutionState::for_module(&arena, &module, start_fn_name, is_debug_mode).unwrap();
// #[test] while let Action::Continue = state.execute_next_instruction(&module) {}
// fn test_i32store16() {}
// #[test] state.memory
// fn test_i64store8() {} }
// #[test] #[test]
// fn test_i64store16() {} fn test_i32store() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
// #[test] let addr: u32 = 0x11;
// fn test_i64store32() {} let store_op = OpCode::I32STORE;
let offset = 1;
let value = Value::I32(0x12345678);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..4], &[0x78, 0x56, 0x34, 0x12]);
}
#[test]
fn test_i64store() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let addr: u32 = 0x11;
let store_op = OpCode::I64STORE;
let offset = 1;
let value = Value::I64(0x123456789abcdef0);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(
&memory[index..][..8],
&[0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12]
);
}
#[test]
fn test_f32store() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let addr: u32 = 0x11;
let store_op = OpCode::F32STORE;
let offset = 1;
let inner: f32 = 1.23456;
let value = Value::F32(inner);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..4], &inner.to_le_bytes());
}
#[test]
fn test_f64store() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let addr: u32 = 0x11;
let store_op = OpCode::F64STORE;
let offset = 1;
let inner: f64 = 1.23456;
let value = Value::F64(inner);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..8], &inner.to_le_bytes());
}
#[test]
fn test_i32store8() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let addr: u32 = 0x11;
let store_op = OpCode::I32STORE8;
let offset = 1;
let value = Value::I32(0x12345678);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..4], &[0x78, 0x00, 0x00, 0x00]);
}
#[test]
fn test_i32store16() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let addr: u32 = 0x11;
let store_op = OpCode::I32STORE16;
let offset = 1;
let value = Value::I32(0x12345678);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(&memory[index..][..4], &[0x78, 0x56, 0x00, 0x00]);
}
#[test]
fn test_i64store8() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let addr: u32 = 0x11;
let store_op = OpCode::I64STORE8;
let offset = 1;
let value = Value::I64(0x123456789abcdef0);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(
&memory[index..][..8],
&[0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
);
}
#[test]
fn test_i64store16() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let addr: u32 = 0x11;
let store_op = OpCode::I64STORE16;
let offset = 1;
let value = Value::I64(0x123456789abcdef0);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(
&memory[index..][..8],
&[0xf0, 0xde, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
);
}
#[test]
fn test_i64store32() {
let arena = Bump::new();
let mut module = WasmModule::new(&arena);
let addr: u32 = 0x11;
let store_op = OpCode::I64STORE32;
let offset = 1;
let value = Value::I64(0x123456789abcdef0);
let memory = test_store(&arena, &mut module, addr, store_op, offset, value);
let index = (addr + offset) as usize;
assert_eq!(
&memory[index..][..8],
&[0xf0, 0xde, 0xbc, 0x9a, 0x00, 0x00, 0x00, 0x00]
);
}
// #[test] // #[test]
// fn test_currentmemory() {} // fn test_currentmemory() {}