Skip to content

Commit

Permalink
Fix unaligned access
Browse files Browse the repository at this point in the history
Rust 1.70.0 added alignment checks in debug builds:

PANIC!
Panicked at 'misaligned pointer dereference: address must be a multiple of 0x4 but is 0xffff8000ffffff6e', src/bios.rs:203:23
PANIC!

Fix the access using core::ptr::read_unaligned().

Signed-off-by: Claudio Carvalho <[email protected]>
  • Loading branch information
cclaudio committed Jun 13, 2023
1 parent 2f60189 commit d400c9b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/bios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn find_bios_guid_entry(bios_info: &mut BiosInfo, guid: &str) -> Option<u64> {
}

unsafe fn __find_snp_section(bios_info: &mut BiosInfo, stype: u32, p: u64) -> Option<SnpSection> {
let offset: u64 = *(p as *const u32) as u64;
let offset: u64 = core::ptr::read_unaligned(p as *const u32) as u64;
if offset > bios_info.size {
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion src/vtpm/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ pub fn handle_tpm2_crb_request(addr: u32, val: u64) {
crb_regs.loc_state.modify(LocState::LocAssigned.val(1));
let sts = (TPM_CRB_BASE + 0xc) as *const u64;
let loc_sts = crb_regs.loc_sts.get();
let sts_val = unsafe { *sts };
let sts_val = unsafe { core::ptr::read_unaligned(sts) };
println!("sts {} read_back val {}", sts_val, loc_sts);
}
}
Expand Down

0 comments on commit d400c9b

Please sign in to comment.