Skip to content

Commit 7b16d5b

Browse files
authored
Refactor SandboxMemoryLayout (#1455)
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent bc0cb0f commit 7b16d5b

7 files changed

Lines changed: 187 additions & 272 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hyperlight_common/src/mem.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub const PAGE_SIZE: u64 = 1 << 12;
1919
pub const PAGE_SIZE_USIZE: usize = 1 << 12;
2020

2121
/// A memory region in the guest address space
22-
#[derive(Debug, Clone, Copy)]
22+
#[derive(Debug, Clone, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
2323
#[repr(C)]
2424
pub struct GuestMemoryRegion {
2525
/// The size of the memory region
@@ -65,7 +65,7 @@ impl Default for FileMappingInfo {
6565
}
6666
}
6767

68-
#[derive(Debug, Clone, Copy)]
68+
#[derive(Debug, Clone, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
6969
#[repr(C)]
7070
pub struct HyperlightPEB {
7171
pub input_stack: GuestMemoryRegion,
@@ -80,3 +80,40 @@ pub struct HyperlightPEB {
8080
#[cfg(feature = "nanvix-unstable")]
8181
pub file_mappings: GuestMemoryRegion,
8282
}
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
#[test]
89+
fn peb_round_trip() {
90+
let peb = HyperlightPEB {
91+
input_stack: GuestMemoryRegion {
92+
size: 0x1111,
93+
ptr: 0x2222,
94+
},
95+
output_stack: GuestMemoryRegion {
96+
size: 0x3333,
97+
ptr: 0x4444,
98+
},
99+
init_data: GuestMemoryRegion {
100+
size: 0x5555,
101+
ptr: 0x6666,
102+
},
103+
guest_heap: GuestMemoryRegion {
104+
size: 0x7777,
105+
ptr: 0x8888,
106+
},
107+
#[cfg(feature = "nanvix-unstable")]
108+
file_mappings: GuestMemoryRegion {
109+
size: 0x9999,
110+
ptr: 0xaaaa,
111+
},
112+
};
113+
let bytes = bytemuck::bytes_of(&peb);
114+
let peb2 = *bytemuck::from_bytes::<HyperlightPEB>(bytes);
115+
let peb2_bytes = bytemuck::bytes_of(&peb2);
116+
assert_eq!(peb, peb2);
117+
assert_eq!(bytes, peb2_bytes);
118+
}
119+
}

src/hyperlight_host/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ flatbuffers = "25.12.19"
3131
framehop = { version = "0.16.0", optional = true }
3232
fallible-iterator = { version = "0.3.0", optional = true }
3333
blake3 = "1.8.5"
34+
bytemuck = { version = "1.24", features = ["derive"] }
3435
page_size = "0.6.0"
3536
termcolor = "1.2.0"
3637
bitflags = "2.11.1"

src/hyperlight_host/src/hypervisor/hyperlight_vm/x86_64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ mod tests {
14891489

14901490
let (mut hshm, gshm) = mem_mgr.build().unwrap();
14911491

1492-
let peb_address = gshm.layout.peb_address;
1492+
let peb_address = gshm.layout.peb_address();
14931493
let stack_top_gva = hyperlight_common::layout::MAX_GVA as u64
14941494
- hyperlight_common::layout::SCRATCH_TOP_EXN_STACK_OFFSET
14951495
+ 1;

0 commit comments

Comments
 (0)