Skip to content

Commit 1a984d5

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 59bf7a4 commit 1a984d5

File tree

13 files changed

+484
-294
lines changed

13 files changed

+484
-294
lines changed

src/hyperlight_common/src/outb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ 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((*PEB).get_outb_ptr_ctx() as *mut core::ffi::c_void, port, value);
4949
} else if let Some(outb_func) = OUTB_HANDLER {
5050
outb_func(port, value);
5151
} 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: 8 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,17 @@ 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(
91+
dispatch_function as u64,
92+
);
9193

9294
// Set up the guest heap
9395
HEAP_ALLOCATOR
9496
.try_lock()
9597
.expect("Failed to access HEAP_ALLOCATOR")
9698
.init(
9799
(*PEB).get_heap_data_address() as usize,
98-
(*PEB).guest_heap_data_size as usize,
100+
(*PEB).get_guest_heap_data_size() as usize,
99101
);
100102

101103
__security_cookie = peb_address ^ seed;
@@ -125,17 +127,17 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, max_log_level: u64
125127
RunMode::InProcessLinux | RunMode::InProcessWindows => {
126128
OUTB_HANDLER = {
127129
let outb_handler: extern "C" fn(u16, u8) =
128-
core::mem::transmute((*PEB).outb_ptr);
130+
core::mem::transmute((*PEB).get_outb_ptr());
129131
Some(outb_handler)
130132
};
131133

132-
if (*PEB).outb_ptr_ctx == 0 {
134+
if (*PEB).get_outb_ptr_ctx() == 0 {
133135
panic!("outb_ptr_ctx is null");
134136
}
135137

136138
OUTB_HANDLER_CTX = {
137139
let outb_handler_ctx: extern "C" fn(*mut core::ffi::c_void, u16, u8) =
138-
core::mem::transmute((*PEB).outb_ptr);
140+
core::mem::transmute((*PEB).get_outb_ptr());
139141
Some(outb_handler_ctx)
140142
};
141143
}

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
@@ -493,7 +493,16 @@ impl Hypervisor for HypervLinuxDriver {
493493
dbg_mem_access_fn,
494494
)?;
495495

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

498507
Ok(())
499508
}

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl HvHandlerExecVars {
137137
.thread_id
138138
.try_lock()
139139
.map_err(|_| new_error!("Failed to get_thread_id"))?)
140-
.ok_or_else(|| new_error!("thread_id not set"))
140+
.ok_or_else(|| new_error!("thread_id not set"))
141141
}
142142

143143
#[cfg(target_os = "windows")]
@@ -405,7 +405,7 @@ impl HypervisorHandler {
405405
.as_mut()
406406
.ok_or_else(|| {
407407
new_error!("guest shm lock: {}:{}", file!(), line!())
408-
})?.read_hyperlight_peb()?.guest_function_dispatch_ptr);
408+
})?.memory_sections.read_hyperlight_peb()?.get_guest_function_dispatch_ptr());
409409

410410
if dispatch_function_addr == RawPtr(0) {
411411
log_then_return!(
@@ -795,7 +795,7 @@ impl HypervisorHandler {
795795
0,
796796
0,
797797
)
798-
.map_err(|e| new_error!("Failed to cancel guest execution {:?}", e))?;
798+
.map_err(|e| new_error!("Failed to cancel guest execution {:?}", e))?;
799799
}
800800
}
801801
// if running in-process on windows, we currently have no way of cancelling the execution

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)