Skip to content

Commit 0f91d6c

Browse files
committed
[guest/{input,output}stacks] make input/output stacks portable
If we use usize and the guest and host are compiled for different architectures, there will be a mismatch in sizing between the guest and host, so we should use u64s to address that (tested w/ Nanvix already). Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 49942bb commit 0f91d6c

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/hyperlight_guest/src/shared_input_data.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ where
4343
}
4444

4545
// get relative offset to next free address
46-
let stack_ptr_rel: usize =
47-
usize::from_le_bytes(idb[..8].try_into().expect("Shared input buffer too small"));
46+
let stack_ptr_rel: u64 =
47+
u64::from_le_bytes(idb[..8].try_into().expect("Shared input buffer too small"));
4848

49-
if stack_ptr_rel > shared_buffer_size || stack_ptr_rel < 16 {
49+
if stack_ptr_rel as usize > shared_buffer_size || stack_ptr_rel < 16 {
5050
return Err(HyperlightGuestError::new(
5151
ErrorCode::GuestError,
5252
format!(
@@ -57,13 +57,13 @@ where
5757
}
5858

5959
// go back 8 bytes and read. This is the offset to the element on top of stack
60-
let last_element_offset_rel = usize::from_le_bytes(
61-
idb[stack_ptr_rel - 8..stack_ptr_rel]
60+
let last_element_offset_rel = u64::from_le_bytes(
61+
idb[stack_ptr_rel as usize - 8..stack_ptr_rel as usize]
6262
.try_into()
6363
.expect("Invalid stack pointer in pop_shared_input_data_into"),
6464
);
6565

66-
let buffer = &idb[last_element_offset_rel..];
66+
let buffer = &idb[last_element_offset_rel as usize..];
6767

6868
// convert the buffer to T
6969
let type_t = match T::try_from(buffer) {
@@ -80,7 +80,7 @@ where
8080
idb[..8].copy_from_slice(&last_element_offset_rel.to_le_bytes());
8181

8282
// zero out popped off buffer
83-
idb[last_element_offset_rel..stack_ptr_rel].fill(0);
83+
idb[last_element_offset_rel as usize..stack_ptr_rel as usize].fill(0);
8484

8585
type_t
8686
}

src/hyperlight_guest/src/shared_output_data.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ pub fn push_shared_output_data(data: Vec<u8>) -> Result<()> {
3838
}
3939

4040
// get offset to next free address on the stack
41-
let stack_ptr_rel: usize =
42-
usize::from_le_bytes(odb[..8].try_into().expect("Shared output buffer too small"));
41+
let stack_ptr_rel: u64 =
42+
u64::from_le_bytes(odb[..8].try_into().expect("Shared output buffer too small"));
4343

4444
// check if the stack pointer is within the bounds of the buffer.
4545
// It can be equal to the size, but never greater
4646
// It can never be less than 8. An empty buffer's stack pointer is 8
47-
if stack_ptr_rel > shared_buffer_size || stack_ptr_rel < 8 {
47+
if stack_ptr_rel as usize > shared_buffer_size || stack_ptr_rel < 8 {
4848
return Err(HyperlightGuestError::new(
4949
ErrorCode::GuestError,
5050
format!(
@@ -56,7 +56,7 @@ pub fn push_shared_output_data(data: Vec<u8>) -> Result<()> {
5656

5757
// check if there is enough space in the buffer
5858
let size_required = data.len() + 8; // the data plus the pointer pointing to the data
59-
let size_available = shared_buffer_size - stack_ptr_rel;
59+
let size_available = shared_buffer_size - stack_ptr_rel as usize;
6060
if size_required > size_available {
6161
return Err(HyperlightGuestError::new(
6262
ErrorCode::GuestError,
@@ -68,14 +68,15 @@ pub fn push_shared_output_data(data: Vec<u8>) -> Result<()> {
6868
}
6969

7070
// write the actual data
71-
odb[stack_ptr_rel..stack_ptr_rel + data.len()].copy_from_slice(&data);
71+
odb[stack_ptr_rel as usize..stack_ptr_rel as usize + data.len()].copy_from_slice(&data);
7272

7373
// write the offset to the newly written data, to the top of the stack
74-
let bytes = stack_ptr_rel.to_le_bytes();
75-
odb[stack_ptr_rel + data.len()..stack_ptr_rel + data.len() + 8].copy_from_slice(&bytes);
74+
let bytes: [u8; 8] = stack_ptr_rel.to_le_bytes();
75+
odb[stack_ptr_rel as usize + data.len()..stack_ptr_rel as usize + data.len() + 8]
76+
.copy_from_slice(&bytes);
7677

7778
// update stack pointer to point to next free address
78-
let new_stack_ptr_rel = stack_ptr_rel + data.len() + 8;
79+
let new_stack_ptr_rel: u64 = (stack_ptr_rel as usize + data.len() + 8) as u64;
7980
odb[0..8].copy_from_slice(&(new_stack_ptr_rel).to_le_bytes());
8081

8182
Ok(())

0 commit comments

Comments
 (0)