Skip to content

Commit

Permalink
Fixed Notify helper funciton and opcode (for #3155 and #3158)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Nov 22, 2022
1 parent 9472576 commit bb69903
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
23 changes: 17 additions & 6 deletions lib/compiler-singlepass/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6542,19 +6542,20 @@ impl<'a, M: Machine> FuncGen<'a, M> {
)?;
}
Operator::MemoryAtomicNotify { ref memarg } => {
let cnt = self.value_stack.pop().unwrap();
let dst = self.value_stack.pop().unwrap();
self.release_locations_only_regs(&[dst])?;
self.release_locations_only_regs(&[cnt, dst])?;

let memory_index = MemoryIndex::new(memarg.memory as usize);
let (memory_atomic_wait32, memory_index) =
let (memory_atomic_notify, memory_index) =
if self.module.local_memory_index(memory_index).is_some() {
(
VMBuiltinFunctionIndex::get_memory_atomic_wait32_index(),
VMBuiltinFunctionIndex::get_memory_atomic_notify_index(),
memory_index,
)
} else {
(
VMBuiltinFunctionIndex::get_imported_memory_atomic_wait32_index(),
VMBuiltinFunctionIndex::get_imported_memory_atomic_notify_index(),
memory_index,
)
};
Expand All @@ -6563,7 +6564,7 @@ impl<'a, M: Machine> FuncGen<'a, M> {
Size::S64,
Location::Memory(
self.machine.get_vmctx_reg(),
self.vmoffsets.vmctx_builtin_function(memory_atomic_wait32) as i32,
self.vmoffsets.vmctx_builtin_function(memory_atomic_notify) as i32,
),
Location::GPR(self.machine.get_grp_for_call()),
)?;
Expand All @@ -6582,7 +6583,17 @@ impl<'a, M: Machine> FuncGen<'a, M> {
.cloned(),
[WpType::I32, WpType::I32].iter().cloned(),
)?;
self.release_locations_only_stack(&[dst])?;
self.release_locations_only_stack(&[dst, cnt])?;
let ret = self.acquire_locations(
&[(WpType::I32, MachineValue::WasmStack(self.value_stack.len()))],
false,
)?[0];
self.value_stack.push(ret);
self.machine.move_location(
Size::S32,
Location::GPR(self.machine.get_gpr_for_ret()),
ret,
)?;
}
_ => {
return Err(CompileError::Codegen(format!(
Expand Down
26 changes: 18 additions & 8 deletions lib/vm/src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,8 @@ impl Instance {
&mut self,
memory_index: LocalMemoryIndex,
dst: u32,
) -> Result<(), Trap> {
count: u32,
) -> Result<u32, Trap> {
//let memory = self.memory(memory_index);
//if ! memory.shared {
// We should trap according to spec, but official test rely on not trapping...
Expand All @@ -938,21 +939,26 @@ impl Instance {
// fetch the notifier
let key = (memory_index.as_u32(), dst);
let mut conds = self.conditions.lock().unwrap();
let mut cnt = 0u32;
if conds.contains_key(&key) {
let v = conds.get_mut(&key).unwrap();
for (t, b) in v {
*b = true; // mark as was waiked up
t.unpark(); // wakeup!
if cnt < count {
*b = true; // mark as was waiked up
t.unpark(); // wakeup!
cnt += 1;
}
}
}
Ok(())
Ok(cnt)
}
/// Perform an Atomic.Notify
pub(crate) fn imported_memory_notify(
&mut self,
memory_index: MemoryIndex,
dst: u32,
) -> Result<(), Trap> {
count: u32,
) -> Result<u32, Trap> {
//let import = self.imported_memory(memory_index);
//let memory = unsafe { import.definition.as_ref() };
//if ! memory.shared {
Expand All @@ -962,14 +968,18 @@ impl Instance {
// fetch the notifier
let key = (memory_index.as_u32(), dst);
let mut conds = self.conditions.lock().unwrap();
let mut cnt = 0u32;
if conds.contains_key(&key) {
let v = conds.get_mut(&key).unwrap();
for (t, b) in v {
*b = true; // mark as was waiked up
t.unpark(); // wakeup!
if cnt < count {
*b = true; // mark as was waiked up
t.unpark(); // wakeup!
cnt += 1;
}
}
}
Ok(())
Ok(cnt)
}
}

Expand Down
11 changes: 7 additions & 4 deletions lib/vm/src/libcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,16 +777,18 @@ pub unsafe extern "C" fn wasmer_vm_memory32_atomic_notify(
vmctx: *mut VMContext,
memory_index: u32,
dst: u32,
) {
cnt: u32,
) -> u32 {
let result = {
let instance = (*vmctx).instance_mut();
let memory_index = LocalMemoryIndex::from_u32(memory_index);

instance.local_memory_notify(memory_index, dst)
instance.local_memory_notify(memory_index, dst, cnt)
};
if let Err(trap) = result {
raise_lib_trap(trap);
}
result.unwrap()
}

/// Implementation of memory.notfy for imported 32-bit memories.
Expand All @@ -799,12 +801,13 @@ pub unsafe extern "C" fn wasmer_vm_imported_memory32_atomic_notify(
vmctx: *mut VMContext,
memory_index: u32,
dst: u32,
) {
cnt: u32,
) -> u32 {
let result = {
let instance = (*vmctx).instance_mut();
let memory_index = MemoryIndex::from_u32(memory_index);

instance.imported_memory_notify(memory_index, dst)
instance.imported_memory_notify(memory_index, dst, cnt)
};
if let Err(trap) = result {
raise_lib_trap(trap);
Expand Down

0 comments on commit bb69903

Please sign in to comment.