Skip to content

Commit

Permalink
fix: ref address offset from const expr execution
Browse files Browse the repository at this point in the history
Signed-off-by: nerodesu017 <[email protected]>
  • Loading branch information
nerodesu017 committed Dec 9, 2024
1 parent e5c1ec3 commit afdf88b
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,24 @@ where
run_const_span(validation_info.wasm, expr, ()).unwrap_validated(),
)
})
.collect(),
.collect::<Vec<Option<u32>>>(),
ElemItems::RefFuncs(indicies) => {
// This branch gets taken when the elements are direct function references (i32 values), so we just return the indices
indicies.clone()
indicies
.iter()
.map(|el| Some(*el))
.collect::<Vec<Option<u32>>>()
}
};

let references: Vec<Ref> = offsets
.iter()
.map(|offset| match elem.ty() {
RefType::FuncRef => Ref::Func(FuncAddr::new(Some(*offset as usize))),
RefType::ExternRef => Ref::Extern(ExternAddr::new(Some(*offset as usize))),
.map(|offset| {
let offset = offset.as_ref().map(|offset| *offset as usize);
match elem.ty() {
RefType::FuncRef => Ref::Func(FuncAddr::new(offset)),
RefType::ExternRef => Ref::Extern(ExternAddr::new(offset)),
}
})
.collect();

Expand Down Expand Up @@ -411,7 +417,8 @@ where
let offset = get_address_offset(
run_const_span(validation_info.wasm, &active_elem.init_expr, ())
.unwrap_validated(),
) as usize;
)
.unwrap() as usize;

let table = &mut tables[table_idx];
// This can't be verified at validation-time because we don't keep track of actual values when validating expressions
Expand Down Expand Up @@ -529,13 +536,19 @@ where
/// they can only be an i32 (which can be understood from either a [`Value::I32`] - but
/// since we don't unbox the address of the reference, for us also a [`Value::Ref`] -
/// or from a Global)
fn get_address_offset(value: Value) -> u32 {
fn get_address_offset(value: Value) -> Option<u32> {
match value {
Value::I32(val) => val,
Value::I32(val) => Some(val),
Value::Ref(rref) => match rref {
Ref::Extern(_) => todo!("Not yet implemented"),
// TODO: fix
Ref::Func(func_addr) => func_addr.addr.unwrap_or(u32::MAX as usize) as u32,
Ref::Func(func_addr) => {
if func_addr.is_null() {
None
} else {
Some(func_addr.addr.unwrap() as u32)
}
}
},
// INFO: from wasmtime - implement only global
_ => unreachable!(),
Expand Down

0 comments on commit afdf88b

Please sign in to comment.