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 init_data: GuestMemoryRegion,
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn peb_round_trip() {
26        let peb = HyperlightPEB {
27            init_data: GuestMemoryRegion {
28                size: 0x1111,
29                ptr: 0x2222,
30            },
31        };
32        let bytes = bytemuck::bytes_of(&peb);
33        let peb2 = *bytemuck::from_bytes::<HyperlightPEB>(bytes);
34        let peb2_bytes = bytemuck::bytes_of(&peb2);
35        assert_eq!(peb, peb2);
36        assert_eq!(bytes, peb2_bytes);
37    }
38}