Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support import memory/global #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ impl ModuleInstance {
}

if let Some(memory_ref) = module_ref.memory_by_index(DEFAULT_MEMORY_INDEX) {
tracer.push_init_memory(memory_ref)
tracer.push_init_memory(current_module_id, memory_ref)
}
}
}
Expand Down
88 changes: 33 additions & 55 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl Interpreter {
let raw_address = <_>::from_value_internal(*self.value_stack.top());
let address =
effective_address(offset, raw_address).map_or(None, |addr| Some(addr));
let mmid = tracer.lookup_memory_instance(
let origin_mmid = tracer.lookup_memory_instance_origin(
&function_context
.memory
.clone()
Expand All @@ -509,7 +509,7 @@ impl Interpreter {
effective_address: address,
vtype: parity_wasm::elements::ValueType::I32,
load_size,
mmid: mmid as u64,
origin_mmid,
})
}
isa::Instruction::I64Load(offset) | isa::Instruction::I64Load8U(offset) => {
Expand All @@ -521,7 +521,7 @@ impl Interpreter {
let raw_address = <_>::from_value_internal(*self.value_stack.top());
let address =
effective_address(offset, raw_address).map_or(None, |addr| Some(addr));
let mmid = tracer.lookup_memory_instance(
let origin_mmid = tracer.lookup_memory_instance_origin(
&function_context
.memory
.clone()
Expand All @@ -534,7 +534,7 @@ impl Interpreter {
effective_address: address,
vtype: parity_wasm::elements::ValueType::I64,
load_size,
mmid: mmid as u64,
origin_mmid,
})
}
isa::Instruction::I32Store(offset) | isa::Instruction::I32Store8(offset) => {
Expand All @@ -548,7 +548,8 @@ impl Interpreter {
let raw_address = <_>::from_value_internal(*self.value_stack.pick(2));
let address =
effective_address(offset, raw_address).map_or(None, |addr| Some(addr));
let mmid = tracer.lookup_memory_instance(&function_context.memory.clone().unwrap());
let origin_mmid =
tracer.lookup_memory_instance_origin(&function_context.memory.clone().unwrap());

let pre_block_value = address.map(|address| {
let mut buf = [0u8; 8];
Expand All @@ -568,7 +569,7 @@ impl Interpreter {
value: value as u64,
vtype: parity_wasm::elements::ValueType::I32,
store_size,
mmid: mmid as u64,
origin_mmid,
pre_block_value,
})
}
Expand All @@ -583,7 +584,8 @@ impl Interpreter {
let raw_address = <_>::from_value_internal(*self.value_stack.pick(2));
let address =
effective_address(offset, raw_address).map_or(None, |addr| Some(addr));
let mmid = tracer.lookup_memory_instance(&function_context.memory.clone().unwrap());
let origin_mmid =
tracer.lookup_memory_instance_origin(&function_context.memory.clone().unwrap());

let pre_block_value = address.map(|address| {
let mut buf = [0u8; 8];
Expand All @@ -603,7 +605,7 @@ impl Interpreter {
value,
vtype: parity_wasm::elements::ValueType::I64,
store_size,
mmid: mmid as u64,
origin_mmid,
pre_block_value,
})
}
Expand Down Expand Up @@ -720,7 +722,7 @@ impl Interpreter {
value: from_value_internal_to_u64_with_typ(vtype.into(), *self.value_stack.top()),
vtype: vtype.into(),
},
isa::Instruction::GetGlobal(idx) => {
isa::Instruction::GetGlobal(idx) | isa::Instruction::SetGlobal(idx) => {
let tracer = self.tracer.as_ref().unwrap().borrow();

let global_ref = context.module().global_by_index(idx).unwrap();
Expand All @@ -733,49 +735,25 @@ impl Interpreter {

let (origin_module, origin_idx) =
tracer.lookup_global_instance(&global_ref).unwrap();
let moid = tracer.lookup_module_instance(&context.module);
/*
* TODO: imported global is not support yet.
*/
assert_eq!(origin_module, moid);
assert_eq!(origin_idx, idx as u16);

StepInfo::GetGlobal {
idx,
origin_module: moid,
origin_idx: idx as u16,
vtype,
is_mutable,
value,
}
}
isa::Instruction::SetGlobal(idx) => {
let tracer = self.tracer.as_ref().unwrap().borrow();

let global_ref = context.module().global_by_index(idx).unwrap();
let is_mutable = global_ref.is_mutable();
let vtype: VarType = global_ref.value_type().into_elements().into();
let value = from_value_internal_to_u64_with_typ(
vtype.into(),
ValueInternal::from(global_ref.get()),
);

let (origin_module, origin_idx) =
tracer.lookup_global_instance(&global_ref).unwrap();
let moid = tracer.lookup_module_instance(&context.module);
/*
* TODO: imported global is not support yet.
*/
assert_eq!(origin_module, moid);
assert_eq!(origin_idx, idx as u16);

StepInfo::SetGlobal {
idx,
origin_module: moid,
origin_idx: idx as u16,
vtype,
is_mutable,
value,
match *instructions {
isa::Instruction::GetGlobal(_) => StepInfo::GetGlobal {
idx,
origin_module,
origin_idx,
vtype,
is_mutable,
value,
},
isa::Instruction::SetGlobal(_) => StepInfo::SetGlobal {
idx,
origin_module,
origin_idx,
vtype,
is_mutable,
value,
},
_ => unreachable!(),
}
}

Expand Down Expand Up @@ -935,7 +913,7 @@ impl Interpreter {
effective_address,
vtype,
load_size,
mmid,
origin_mmid,
} = pre_status.unwrap()
{
let block_value = {
Expand All @@ -960,7 +938,7 @@ impl Interpreter {
*self.value_stack.top(),
),
block_value,
mmid,
origin_mmid,
}
} else {
unreachable!()
Expand All @@ -977,7 +955,7 @@ impl Interpreter {
value,
vtype,
store_size,
mmid,
origin_mmid,
pre_block_value,
} = pre_status.unwrap()
{
Expand All @@ -999,7 +977,7 @@ impl Interpreter {
raw_address,
effective_address: effective_address.unwrap(),
value: value as u64,
mmid,
origin_mmid,
pre_block_value: pre_block_value.unwrap(),
updated_block_value,
}
Expand Down
4 changes: 2 additions & 2 deletions src/tracer/etable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum RunInstructionTracePre {
effective_address: Option<u32>, // use option in case of memory out of bound
vtype: ValueType,
load_size: MemoryReadSize,
mmid: u64,
origin_mmid: u16,
},
Store {
offset: u32,
Expand All @@ -47,7 +47,7 @@ pub enum RunInstructionTracePre {
value: u64,
vtype: ValueType,
store_size: MemoryStoreSize,
mmid: u64,
origin_mmid: u16,
pre_block_value: Option<u64>,
},

Expand Down
55 changes: 0 additions & 55 deletions src/tracer/imtable.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/tracer/itable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Into<InstructionTableEntry> for IEntry {
fn into(self) -> InstructionTableEntry {
InstructionTableEntry {
moid: self.module_instance_index,
mmid: self.module_instance_index,
// mmid: self.module_instance_index,
fid: self.func_index,
iid: self.pc,
opcode: self.opcode,
Expand Down
Loading