Skip to content

Commit

Permalink
refactor(executor): swap the ptr and size in u64 return value (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
indirection42 authored Jul 2, 2024
1 parent 7bad98c commit b29b6ec
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions poc/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl<Ctx: XcqExecutorContext> XcqExecutor<Ctx> {
};

let res = instance.call_typed::<(u32, u32), u64>(&mut self.context, method, (input_ptr, input.len() as u32))?;
let res_ptr = (res >> 32) as u32;
let res_size = (res & 0xffffffff) as u32;
let res_size = (res >> 32) as u32;
let res_ptr = (res & 0xffffffff) as u32;
let result = instance
.read_memory_into_vec(res_ptr, res_size)
.map_err(|e| XcqExecutorError::ExecutionError(polkavm::ExecutionError::Trap(e)))?;
Expand Down
4 changes: 2 additions & 2 deletions poc/guests/pass-custom-type/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extern "C" fn main(ptr: u32, _size: u32) -> u64 {
let val = b"test";
let size = core::mem::size_of_val(val);
let val_ptr = val.as_ptr();
(val_ptr as u64) << 32 | size as u64
(size as u64) << 32 | val_ptr as u64
}
1 => {
let val = unsafe { core::ptr::read_volatile((ptr + 1) as *const u8) };
Expand All @@ -67,7 +67,7 @@ extern "C" fn main(ptr: u32, _size: u32) -> u64 {
}
unsafe { core::ptr::write_volatile(ptr as *mut u32, res) };
let size = core::mem::size_of::<u32>();
(ptr as u64) << 32 | size as u64
(size as u64) << 32 | ptr as u64
}
_ => 0,
}
Expand Down
2 changes: 1 addition & 1 deletion poc/guests/query-balance-fungibles/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ extern "C" fn main(ptr: u32, size: u32) -> u64 {
unsafe {
core::ptr::copy_nonoverlapping(sum_bytes.as_ptr(), ptr, sum_bytes.len());
}
(ptr as u64) << 32 | (sum_bytes.len() as u64)
(sum_bytes.len() as u64) << 32 | (ptr as u64)
}
2 changes: 1 addition & 1 deletion poc/guests/query-balance/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ extern "C" fn main(ptr: u32, size: u32) -> u64 {
unsafe {
core::ptr::write_volatile(ptr as *mut u64, sum);
}
(ptr as u64) << 32 | (core::mem::size_of::<u64>() as u64)
(core::mem::size_of::<u64>() as u64) << 32 | (ptr as u64)
}
4 changes: 2 additions & 2 deletions xcq-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl<Ctx: XcqExecutorContext> XcqExecutor<Ctx> {
};

let res = instance.call_typed::<(u32, u32), u64>(&mut self.context, method, (input_ptr, input.len() as u32))?;
let res_ptr = (res >> 32) as u32;
let res_size = (res & 0xffffffff) as u32;
let res_size = (res >> 32) as u32;
let res_ptr = (res & 0xffffffff) as u32;
let result = instance
.read_memory_into_vec(res_ptr, res_size)
.map_err(|e| XcqExecutorError::ExecutionError(polkavm::ExecutionError::Trap(e)))?;
Expand Down
2 changes: 1 addition & 1 deletion xcq-extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<E: ExtensionTuple, P: PermController> XcqExecutorContext for Context<E, P>
caller
.write_memory(res_ptr, &res_bytes)
.map_err(|_| ExtensionError::PolkavmError)?;
Ok(((res_ptr as u64) << 32) | (res_bytes_len as u64))
Ok(((res_bytes_len as u64) << 32) | (res_ptr as u64))
};
let result = func_with_result();
tracing::trace!("(host call): result: {:?}", result);
Expand Down

0 comments on commit b29b6ec

Please sign in to comment.