Skip to content

Commit 49942bb

Browse files
committed
[common,guest,host] refactor PEB-related structs
- InputData, OutputData, and GuestHeapData were functionally the same and so they were combined into a single GuestMemoryRegion struct. - removed the allow_non_snake_case attribute. - general housekeeping. Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 5d728d9 commit 49942bb

File tree

5 files changed

+36
-56
lines changed

5 files changed

+36
-56
lines changed

src/hyperlight_common/src/mem.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,38 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
#![allow(non_snake_case)]
18-
1917
pub const PAGE_SHIFT: u64 = 12;
2018
pub const PAGE_SIZE: u64 = 1 << 12;
2119
pub const PAGE_SIZE_USIZE: usize = 1 << 12;
2220

21+
/// A memory region in the guest address space
2322
#[derive(Debug, Clone, Copy)]
2423
#[repr(C)]
25-
pub struct InputData {
26-
pub inputDataSize: u64,
27-
pub inputDataBuffer: u64,
28-
}
29-
30-
#[derive(Debug, Clone, Copy)]
31-
#[repr(C)]
32-
pub struct OutputData {
33-
pub outputDataSize: u64,
34-
pub outputDataBuffer: u64,
35-
}
36-
37-
#[derive(Debug, Clone, Copy)]
38-
#[repr(C)]
39-
pub struct GuestHeapData {
40-
pub guestHeapSize: u64,
41-
pub guestHeapBuffer: u64,
24+
pub struct GuestMemoryRegion {
25+
/// The size of the memory region
26+
pub size: u64,
27+
/// The address of the memory region
28+
pub ptr: u64,
4229
}
4330

31+
/// A memory region in the guest address space that is used for the stack
4432
#[derive(Debug, Clone, Copy)]
4533
#[repr(C)]
46-
pub struct GuestStackData {
47-
/// This is the top of the user stack
48-
pub minUserStackAddress: u64,
49-
/// This is the user stack pointer
50-
pub userStackAddress: u64,
34+
pub struct GuestStack {
35+
/// The top of the user stack
36+
pub min_user_stack_address: u64,
37+
/// The user stack pointer
38+
pub user_stack_address: u64,
5139
}
5240

5341
#[derive(Debug, Clone, Copy)]
5442
#[repr(C)]
5543
pub struct HyperlightPEB {
5644
pub security_cookie_seed: u64,
5745
pub guest_function_dispatch_ptr: u64,
58-
pub pCode: u64,
59-
pub inputdata: InputData,
60-
pub outputdata: OutputData,
61-
pub guestheapData: GuestHeapData,
62-
pub gueststackData: GuestStackData,
46+
pub code_ptr: u64,
47+
pub input_stack: GuestMemoryRegion,
48+
pub output_stack: GuestMemoryRegion,
49+
pub guest_heap: GuestMemoryRegion,
50+
pub guest_stack: GuestStack,
6351
}

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub extern "C" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_leve
9898
// This static is to make it easier to implement the __chkstk function in assembly.
9999
// It also means that should we change the layout of the struct in the future, we
100100
// don't have to change the assembly code.
101-
MIN_STACK_ADDRESS = (*peb_ptr).gueststackData.minUserStackAddress;
101+
MIN_STACK_ADDRESS = (*peb_ptr).guest_stack.min_user_stack_address;
102102

103103
#[cfg(target_arch = "x86_64")]
104104
{
@@ -107,8 +107,8 @@ pub extern "C" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_leve
107107
load_idt();
108108
}
109109

110-
let heap_start = (*peb_ptr).guestheapData.guestHeapBuffer as usize;
111-
let heap_size = (*peb_ptr).guestheapData.guestHeapSize as usize;
110+
let heap_start = (*peb_ptr).guest_heap.ptr as usize;
111+
let heap_size = (*peb_ptr).guest_heap.size as usize;
112112
HEAP_ALLOCATOR
113113
.try_lock()
114114
.expect("Failed to access HEAP_ALLOCATOR")

src/hyperlight_guest/src/shared_input_data.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,10 @@ where
3030
T: for<'a> TryFrom<&'a [u8]>,
3131
{
3232
let peb_ptr = unsafe { P_PEB.unwrap() };
33-
let shared_buffer_size = unsafe { (*peb_ptr).inputdata.inputDataSize as usize };
33+
let shared_buffer_size = unsafe { (*peb_ptr).input_stack.size as usize };
3434

35-
let idb = unsafe {
36-
from_raw_parts_mut(
37-
(*peb_ptr).inputdata.inputDataBuffer as *mut u8,
38-
shared_buffer_size,
39-
)
40-
};
35+
let idb =
36+
unsafe { from_raw_parts_mut((*peb_ptr).input_stack.ptr as *mut u8, shared_buffer_size) };
4137

4238
if idb.is_empty() {
4339
return Err(HyperlightGuestError::new(

src/hyperlight_guest/src/shared_output_data.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@ use crate::P_PEB;
2626

2727
pub fn push_shared_output_data(data: Vec<u8>) -> Result<()> {
2828
let peb_ptr = unsafe { P_PEB.unwrap() };
29-
let shared_buffer_size = unsafe { (*peb_ptr).outputdata.outputDataSize as usize };
30-
let odb = unsafe {
31-
from_raw_parts_mut(
32-
(*peb_ptr).outputdata.outputDataBuffer as *mut u8,
33-
shared_buffer_size,
34-
)
35-
};
29+
let shared_buffer_size = unsafe { (*peb_ptr).output_stack.size as usize };
30+
let odb =
31+
unsafe { from_raw_parts_mut((*peb_ptr).output_stack.ptr as *mut u8, shared_buffer_size) };
3632

3733
if odb.is_empty() {
3834
return Err(HyperlightGuestError::new(

src/hyperlight_host/src/mem/layout.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616
use std::fmt::Debug;
1717
use std::mem::{offset_of, size_of};
1818

19-
use hyperlight_common::mem::{GuestStackData, HyperlightPEB, PAGE_SIZE_USIZE};
19+
use hyperlight_common::mem::{GuestStack, HyperlightPEB, PAGE_SIZE_USIZE};
2020
use rand::{rng, RngCore};
2121
use tracing::{instrument, Span};
2222

@@ -225,17 +225,17 @@ impl SandboxMemoryLayout {
225225
peb_offset + offset_of!(HyperlightPEB, security_cookie_seed);
226226
let peb_guest_dispatch_function_ptr_offset =
227227
peb_offset + offset_of!(HyperlightPEB, guest_function_dispatch_ptr);
228-
let peb_code_pointer_offset = peb_offset + offset_of!(HyperlightPEB, pCode);
229-
let peb_input_data_offset = peb_offset + offset_of!(HyperlightPEB, inputdata);
230-
let peb_output_data_offset = peb_offset + offset_of!(HyperlightPEB, outputdata);
231-
let peb_heap_data_offset = peb_offset + offset_of!(HyperlightPEB, guestheapData);
232-
let peb_guest_stack_data_offset = peb_offset + offset_of!(HyperlightPEB, gueststackData);
228+
let peb_code_pointer_offset = peb_offset + offset_of!(HyperlightPEB, code_ptr);
229+
let peb_input_data_offset = peb_offset + offset_of!(HyperlightPEB, input_stack);
230+
let peb_output_data_offset = peb_offset + offset_of!(HyperlightPEB, output_stack);
231+
let peb_heap_data_offset = peb_offset + offset_of!(HyperlightPEB, guest_heap);
232+
let peb_guest_stack_data_offset = peb_offset + offset_of!(HyperlightPEB, guest_stack);
233233

234234
// The following offsets are the actual values that relate to memory layout,
235235
// which are written to PEB struct
236236
let peb_address = Self::BASE_ADDRESS + peb_offset;
237237
let input_data_buffer_offset = round_up_to(
238-
peb_guest_stack_data_offset + size_of::<GuestStackData>(),
238+
peb_guest_stack_data_offset + size_of::<GuestStack>(),
239239
PAGE_SIZE_USIZE,
240240
);
241241
let output_data_buffer_offset = round_up_to(
@@ -332,15 +332,15 @@ impl SandboxMemoryLayout {
332332
/// Get the offset in guest memory to the input data size.
333333
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
334334
pub(super) fn get_input_data_size_offset(&self) -> usize {
335-
// The input data size is the first field in the `InputData` struct
335+
// The input data size is the first field in the input stack's `GuestMemoryRegion` struct
336336
self.peb_input_data_offset
337337
}
338338

339339
/// Get the offset in guest memory to the input data pointer.
340340
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
341341
fn get_input_data_pointer_offset(&self) -> usize {
342342
// The input data pointer is immediately after the input
343-
// data size field in the `InputData` struct which is a `u64`.
343+
// data size field in the input data `GuestMemoryRegion` struct which is a `u64`.
344344
self.get_input_data_size_offset() + size_of::<u64>()
345345
}
346346

@@ -375,7 +375,7 @@ impl SandboxMemoryLayout {
375375
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
376376
fn get_heap_pointer_offset(&self) -> usize {
377377
// The heap pointer is immediately after the
378-
// heap size field in the `GuestHeap` struct which is a `u64`.
378+
// heap size field in the guest heap's `GuestMemoryRegion` struct which is a `u64`.
379379
self.get_heap_size_offset() + size_of::<u64>()
380380
}
381381

0 commit comments

Comments
 (0)