Skip to content

Commit 032b1f2

Browse files
committed
[common,guest,host] modified HyperlightPEB API + added rsp mod in hosts
- cleaned up the HyperlightPEB API in the common library. -- added MemoryRegion struct to better group related offsets and sizes. -- removed pub fields from HyperlightPEB struct making fields accessible only via getters/setters. -- cleaned up, commented, and re-organized existing fxns for HyperlightPEB struct. - now we modify the rsp in the host if the guest sets up a new stack region (i.e., essentially dropping the tmp stack). Signed-off-by: danbugs <[email protected]>
1 parent bcb9246 commit 032b1f2

File tree

14 files changed

+485
-293
lines changed

14 files changed

+485
-293
lines changed

src/hyperlight_common/src/outb.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ pub fn outb(port: u16, value: u8) {
4545
}
4646
RunMode::InProcessLinux | RunMode::InProcessWindows => {
4747
if let Some(outb_func) = OUTB_HANDLER_CTX {
48-
outb_func((*PEB).outb_ptr_ctx as *mut core::ffi::c_void, port, value);
48+
outb_func(
49+
(*PEB).get_outb_ptr_ctx() as *mut core::ffi::c_void,
50+
port,
51+
value,
52+
);
4953
} else if let Some(outb_func) = OUTB_HANDLER {
5054
outb_func(port, value);
5155
} else {

src/hyperlight_common/src/peb.rs

Lines changed: 333 additions & 187 deletions
Large diffs are not rendered by default.

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static INIT: Once = Once::new();
7676
pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, max_log_level: u64) {
7777
INIT.call_once(|| unsafe {
7878
PEB = peb_address as *mut HyperlightPEB;
79-
RUNNING_MODE = (*PEB).clone().run_mode;
79+
RUNNING_MODE = (*PEB).clone().get_run_mode();
8080

8181
// The guest receives an undifferentiated block of memory that it can address as it sees fit.
8282
// This 'addressing' is done by writing to the PEB the guest's memory layout via this function,
@@ -87,15 +87,15 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, max_log_level: u64
8787

8888
// The guest sets the address to a "guest function dispatch" function, which is a function
8989
// that is called by the host to dispatch calls to guest functions.
90-
(*PEB).guest_function_dispatch_ptr = dispatch_function as usize as u64;
90+
(*PEB).set_guest_function_dispatch_ptr(dispatch_function as u64);
9191

9292
// Set up the guest heap
9393
HEAP_ALLOCATOR
9494
.try_lock()
9595
.expect("Failed to access HEAP_ALLOCATOR")
9696
.init(
9797
(*PEB).get_heap_data_address() as usize,
98-
(*PEB).guest_heap_data_size as usize,
98+
(*PEB).get_guest_heap_data_size() as usize,
9999
);
100100

101101
__security_cookie = peb_address ^ seed;
@@ -125,17 +125,17 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, max_log_level: u64
125125
RunMode::InProcessLinux | RunMode::InProcessWindows => {
126126
OUTB_HANDLER = {
127127
let outb_handler: extern "C" fn(u16, u8) =
128-
core::mem::transmute((*PEB).outb_ptr);
128+
core::mem::transmute((*PEB).get_outb_ptr());
129129
Some(outb_handler)
130130
};
131131

132-
if (*PEB).outb_ptr_ctx == 0 {
132+
if (*PEB).get_outb_ptr_ctx() == 0 {
133133
panic!("outb_ptr_ctx is null");
134134
}
135135

136136
OUTB_HANDLER_CTX = {
137137
let outb_handler_ctx: extern "C" fn(*mut core::ffi::c_void, u16, u8) =
138-
core::mem::transmute((*PEB).outb_ptr);
138+
core::mem::transmute((*PEB).get_outb_ptr());
139139
Some(outb_handler_ctx)
140140
};
141141
}

src/hyperlight_guest/src/guest_error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ pub(crate) fn write_error(error_code: ErrorCode, message: Option<&str>) {
3838
unsafe {
3939
assert_ne!(!peb.get_guest_error_data_address(), 0);
4040
let len = guest_error_buffer.len();
41-
if guest_error_buffer.len() > peb.guest_error_data_size as usize {
41+
if guest_error_buffer.len() > peb.get_guest_error_data_size() as usize {
4242
error!(
4343
"Guest error buffer is too small to hold the error message: size {} buffer size {} message may be truncated",
4444
guest_error_buffer.len(),
45-
peb.guest_error_data_size as usize
45+
peb.get_guest_error_data_size() as usize
4646
);
4747
// get the length of the message
4848
let message_len = message.map_or("".to_string(), |m| m.to_string()).len();
4949
// message is too long, truncate it
5050
let truncate_len =
51-
message_len - (guest_error_buffer.len() - peb.guest_error_data_size as usize);
51+
message_len - (guest_error_buffer.len() - peb.get_guest_error_data_size() as usize);
5252
let truncated_message = message
5353
.map_or("".to_string(), |m| m.to_string())
5454
.chars()
@@ -77,7 +77,7 @@ pub(crate) fn reset_error() {
7777
core::ptr::write_bytes(
7878
(*PEB).get_guest_error_data_address() as *mut u8,
7979
0,
80-
(*PEB).guest_error_data_size as usize,
80+
(*PEB).get_guest_error_data_size() as usize,
8181
);
8282
}
8383
}

src/hyperlight_guest/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
7272
copy_nonoverlapping(
7373
info.to_string().as_ptr(),
7474
(*PEB).get_guest_panic_context_address() as *mut u8,
75-
(*PEB).guest_panic_context_size as usize,
75+
(*PEB).get_guest_panic_context_size() as usize,
7676
);
7777
}
7878
outb(OutBAction::Abort as u16, ErrorCode::UnknownError as u8);

src/hyperlight_host/src/func/guest_dispatch.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ pub(crate) fn call_function_on_guest(
5454
.try_into()
5555
.map_err(|_| HyperlightError::Error("Failed to serialize FunctionCall".to_string()))?;
5656

57-
let input_data_region = mem_mgr.read_hyperlight_peb()?.get_input_data_guest_region();
57+
let input_data_region = mem_mgr
58+
.memory_sections
59+
.read_hyperlight_peb()?
60+
.get_input_data_guest_region();
5861

5962
mem_mgr.write_guest_function_call(input_data_region, &buffer)?;
6063

@@ -81,6 +84,7 @@ pub(crate) fn call_function_on_guest(
8184
check_for_guest_error(mem_mgr)?;
8285

8386
let output_data_region = mem_mgr
87+
.memory_sections
8488
.read_hyperlight_peb()?
8589
.get_output_data_guest_region();
8690

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,16 @@ impl Hypervisor for HypervLinuxDriver {
488488
dbg_mem_access_fn,
489489
)?;
490490

491-
// TODO(danbugs:297): here, we should update the rsp to what the guest configured.
491+
// The guest may have chosen a different stack region. If so, we drop usage of our tmp stack.
492+
let hyperlight_peb = self.mem_sections.read_hyperlight_peb()?;
493+
494+
if let Some(guest_stack_data) = &hyperlight_peb.get_guest_stack_data_region() {
495+
if guest_stack_data.offset.is_some() {
496+
// If we got here, it means the guest has set up a new stack
497+
let rsp = hyperlight_peb.get_top_of_guest_stack_data();
498+
self.orig_rsp = GuestPtr::try_from(RawPtr::from(rsp))?;
499+
}
500+
}
492501

493502
Ok(())
494503
}

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ impl Hypervisor for HypervWindowsDriver {
338338
dbg_mem_access_hdl,
339339
)?;
340340

341+
// The guest may have chosen a different stack region. If so, we drop usage of our tmp stack.
342+
let hyperlight_peb = self.mem_sections.read_hyperlight_peb()?;
343+
344+
if let Some(guest_stack_data) = &hyperlight_peb.get_guest_stack_data_region() {
345+
if guest_stack_data.offset.is_some() {
346+
// If we got here, it means the guest has set up a new stack
347+
let rsp = hyperlight_peb.get_top_of_guest_stack_data();
348+
self.orig_rsp = GuestPtr::try_from(RawPtr::from(rsp))?;
349+
}
350+
}
351+
341352
Ok(())
342353
}
343354

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl HypervisorHandler {
399399
.as_mut()
400400
.ok_or_else(|| {
401401
new_error!("guest shm lock: {}:{}", file!(), line!())
402-
})?.read_hyperlight_peb()?.guest_function_dispatch_ptr);
402+
})?.memory_sections.read_hyperlight_peb()?.get_guest_function_dispatch_ptr());
403403

404404
if dispatch_function_addr == RawPtr(0) {
405405
log_then_return!(

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,16 @@ impl Hypervisor for KVMDriver {
438438
dbg_mem_access_fn,
439439
)?;
440440

441-
// TODO(danbugs:297): here, we should update the rsp to what the guest configured.
441+
// The guest may have chosen a different stack region. If so, we drop usage of our tmp stack.
442+
let hyperlight_peb = self.mem_sections.read_hyperlight_peb()?;
443+
444+
if let Some(guest_stack_data) = &hyperlight_peb.get_guest_stack_data_region() {
445+
if guest_stack_data.offset.is_some() {
446+
// If we got here, it means the guest has set up a new stack
447+
let rsp = hyperlight_peb.get_top_of_guest_stack_data();
448+
self.orig_rsp = GuestPtr::try_from(RawPtr::from(rsp))?;
449+
}
450+
}
442451

443452
Ok(())
444453
}

0 commit comments

Comments
 (0)