Skip to main content

hyperlight_common/
mem.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025 The Hyperlight Authors.
3
4/// A memory region in the guest address space
5#[derive(Debug, Clone, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
6#[repr(C)]
7pub struct GuestMemoryRegion {
8    /// The size of the memory region
9    pub size: u64,
10    /// The address of the memory region
11    pub ptr: u64,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
15#[repr(C)]
16pub struct HyperlightPEB {
17    pub input_stack: GuestMemoryRegion,
18    pub output_stack: GuestMemoryRegion,
19    pub init_data: GuestMemoryRegion,
20    pub guest_heap: GuestMemoryRegion,
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn peb_round_trip() {
29        let peb = HyperlightPEB {
30            input_stack: GuestMemoryRegion {
31                size: 0x1111,
32                ptr: 0x2222,
33            },
34            output_stack: GuestMemoryRegion {
35                size: 0x3333,
36                ptr: 0x4444,
37            },
38            init_data: GuestMemoryRegion {
39                size: 0x5555,
40                ptr: 0x6666,
41            },
42            guest_heap: GuestMemoryRegion {
43                size: 0x7777,
44                ptr: 0x8888,
45            },
46        };
47        let bytes = bytemuck::bytes_of(&peb);
48        let peb2 = *bytemuck::from_bytes::<HyperlightPEB>(bytes);
49        let peb2_bytes = bytemuck::bytes_of(&peb2);
50        assert_eq!(peb, peb2);
51        assert_eq!(bytes, peb2_bytes);
52    }
53}