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

bugfix: fix rkyv alignment #1780

Draft
wants to merge 4 commits into
base: bing/rkyv-access
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion examples/wallet/core-logic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate alloc;

use mozak_sdk::common::types::{Poseidon2Hash, ProgramIdentifier, StateObject};
use rkyv::rancor::{Failure, Panic, Strategy};
use rkyv::util::AlignedVec;
use rkyv::{Archive, Deserialize, Serialize};

/// A generic private key used by the wallet.
Expand Down Expand Up @@ -60,7 +61,9 @@ pub struct TokenObject {

impl From<StateObject> for TokenObject {
fn from(value: StateObject) -> Self {
let archived = rkyv::access::<TokenObject, Failure>(&value.data[..]).unwrap();
let mut aligned_data = AlignedVec::with_capacity(value.data.len());
aligned_data.extend_from_slice(&value.data);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems suspicious. I suspect we want our StateObject's data to be an AlignedVec in the first place, then we don't need to copy here. (Assuming this code also gets executed on guest.)

let archived = rkyv::access::<TokenObject, Failure>(&aligned_data).unwrap();
let token_object: TokenObject = archived
.deserialize(Strategy::<_, Panic>::wrap(&mut ()))
.unwrap();
Expand Down
18 changes: 10 additions & 8 deletions examples/wallet/native/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ use mozak_sdk::common::types::ProgramIdentifier;
use wallet_core_logic::{dispatch, BlackBox, MethodArgs, PrivateKey, PublicKey, TokenObject};

fn main() {
let wallet_program = ProgramIdentifier::new_from_rand_seed(1);
let remitter_program = ProgramIdentifier::new_from_rand_seed(2);
let remittee_program = ProgramIdentifier::new_from_rand_seed(3);
let private_key = PrivateKey::new_from_rand_seed(4);
let public_key = PublicKey(mozak_sdk::native::poseidon::poseidon2_hash_no_pad(
&private_key.0,
let remitter_private_key = PrivateKey::new_from_rand_seed(4);
let remitter_public_key = PublicKey(mozak_sdk::native::poseidon::poseidon2_hash_no_pad(
&remitter_private_key.0,
));
mozak_sdk::add_identity(remitter_program); // Manual override for `IdentityStack`
let _ = mozak_sdk::write(&mozak_sdk::InputTapeType::PrivateTape, &private_key.0[..]);
let _ = mozak_sdk::write(
&mozak_sdk::InputTapeType::PrivateTape,
&remitter_private_key.0[..],
);
mozak_sdk::rm_identity(); // Manual override for `IdentityStack`

let token_object = TokenObject {
pub_key: public_key.clone(),
pub_key: remitter_public_key.clone(),
amount: 10.into(),
};

Expand All @@ -44,8 +46,8 @@ fn main() {
};

mozak_sdk::call_send(
wallet_program,
MethodArgs::ApproveSignature(public_key, black_box.clone()),
remitter_program,
MethodArgs::ApproveSignature(remitter_public_key, black_box.clone()),
dispatch,
);

Expand Down
11 changes: 9 additions & 2 deletions sdk/src/common/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
call_tape_read, event_tape_read, ioread_private, ioread_public, self_prog_id_tape_read,
},
rkyv::rancor::{Panic, Strategy},
rkyv::util::AlignedVec,
std::collections::BTreeSet,
};
#[cfg(not(target_os = "mozakvm"))]
Expand Down Expand Up @@ -102,7 +103,10 @@ fn populate_call_tape(self_prog_id: ProgramIdentifier) -> CallTapeType {
let mut len_bytes = [0; 4];
call_tape_read(&mut len_bytes);
let len: usize = u32::from_le_bytes(len_bytes).try_into().unwrap();
let buf: &'static mut Vec<u8> = Box::leak(Box::new(vec![0; len]));
let buf: &'static mut AlignedVec = Box::leak(Box::new(AlignedVec::with_capacity(len)));
unsafe {
buf.set_len(len);
}
call_tape_read(buf);

let archived_cpc_messages = rkyv::access::<Vec<CrossProgramCall>, Panic>(buf).unwrap();
Expand Down Expand Up @@ -139,7 +143,10 @@ fn populate_event_tape(self_prog_id: ProgramIdentifier) -> EventTapeType {
event_tape_read(&mut len_bytes);

let len: usize = u32::from_le_bytes(len_bytes).try_into().unwrap();
let buf: &'static mut Vec<u8> = Box::leak(Box::new(vec![0; len]));
let buf: &'static mut AlignedVec = Box::leak(Box::new(AlignedVec::with_capacity(len)));
unsafe {
buf.set_len(len);
}
event_tape_read(buf);

let canonical_ordered_temporal_hints =
Expand Down
Loading