Skip to content

Commit 36c23da

Browse files
committed
PR review comments
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 0767f2b commit 36c23da

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/hyperlight_host/src/mem/shared_mem.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,14 @@ impl HostSharedMemory {
819819
}
820820

821821
// Read aligned u128 chunks
822-
// SAFETY: After processing head_len bytes, base.add(i) is u128-aligned
822+
// SAFETY: After processing head_len bytes, base.add(i) is u128-aligned.
823+
// We use write_unaligned for the destination since the slice may not be u128-aligned.
824+
let dst = slice.as_mut_ptr();
823825
while i + CHUNK <= len {
824-
let value = unsafe { (base.add(i) as *const u128).read_volatile() };
825-
slice[i..i + CHUNK].copy_from_slice(&value.to_ne_bytes());
826+
unsafe {
827+
let value = (base.add(i) as *const u128).read_volatile();
828+
std::ptr::write_unaligned(dst.add(i) as *mut u128, value);
829+
}
826830
i += CHUNK;
827831
}
828832

@@ -866,20 +870,12 @@ impl HostSharedMemory {
866870
}
867871

868872
// Write aligned u128 chunks
869-
// SAFETY: After processing head_len bytes, base.add(i) is u128-aligned
873+
// SAFETY: After processing head_len bytes, base.add(i) is u128-aligned.
874+
// We use read_unaligned for the source since the slice may not be u128-aligned.
875+
let src = slice.as_ptr();
870876
while i + CHUNK <= len {
871-
let chunk: [u8; CHUNK] = slice[i..i + CHUNK].try_into().map_err(|_| {
872-
new_error!(
873-
"Failed to convert slice to fixed-size array for u128 chunk: \
874-
expected length {}, got {} (total slice len {}, offset {})",
875-
CHUNK,
876-
slice[i..i + CHUNK].len(),
877-
len,
878-
i,
879-
)
880-
})?;
881-
let value = u128::from_ne_bytes(chunk);
882877
unsafe {
878+
let value = std::ptr::read_unaligned(src.add(i) as *const u128);
883879
(base.add(i) as *mut u128).write_volatile(value);
884880
}
885881
i += CHUNK;

0 commit comments

Comments
 (0)