Skip to content

Commit 8acc9b6

Browse files
committed
fixup! tmp 2
1 parent e3a5ccd commit 8acc9b6

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

src/hyperlight_guest/src/entrypoint.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::gdt::load_gdt;
2727
use crate::guest_function_call::dispatch_function;
2828
use crate::guest_logger::init_logger;
2929
use crate::idtr::load_idt;
30-
use crate::{__security_cookie, panic, HEAP_ALLOCATOR};
30+
use crate::{__security_cookie, HEAP_ALLOCATOR};
3131

3232
#[inline(never)]
3333
pub fn halt() {
@@ -135,8 +135,6 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, max_log_level: u64
135135
_ => panic!("Invalid runmode in PEB"),
136136
}
137137

138-
panic!("ok");
139-
140138
hyperlight_main();
141139
});
142140

src/hyperlight_guest/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ pub(crate) static _fltused: i32 = 0;
6666
// to satisfy the clippy when cfg == test
6767
#[allow(dead_code)]
6868
fn panic(info: &core::panic::PanicInfo) -> ! {
69-
loop {}
7069
unsafe {
7170
copy_nonoverlapping(
7271
info.to_string().as_ptr(),

src/hyperlight_host/src/hypervisor/kvm.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,19 @@ impl Hypervisor for KVMDriver {
418418

419419
let regs = kvm_regs {
420420
rip: self.entrypoint,
421-
rsp: self.orig_rsp.absolute()?,
421+
// For MSVC, move rsp down by 0x28. This gives the called 'main'
422+
// function the appearance that rsp was 16 byte aligned before
423+
// the 'call' that calls main (note we don't really have a return value
424+
// on the stack but some assembly instructions are expecting rsp have
425+
// started 0x8 bytes off of 16 byte alignment when 'main' is invoked.
426+
// We do 0x28 instead of 0x8 because MSVC can expect that there are
427+
// 0x20 bytes of space to write to by the called function.
428+
// I am not sure if this happens with the 'main' method, but we do this
429+
// just in case.
430+
//
431+
// NOTE: We do this also for GCC freestanding binaries because we
432+
// specify __attribute__((ms_abi)) on the start method
433+
rsp: self.orig_rsp.absolute()? - 0x28,
422434

423435
// function args
424436
rcx: hyperlight_peb_guest_memory_region_address,
@@ -439,13 +451,17 @@ impl Hypervisor for KVMDriver {
439451
)?;
440452

441453
// 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()?;
454+
let mut hyperlight_peb = self.mem_sections.read_hyperlight_peb()?;
443455

444456
if let Some(guest_stack_data) = &hyperlight_peb.get_guest_stack_data_region() {
445457
if guest_stack_data.offset.is_some() {
446458
// If we got here, it means the guest has set up a new stack
447459
let rsp = hyperlight_peb.get_top_of_guest_stack_data();
448-
self.orig_rsp = GuestPtr::try_from(RawPtr::from(rsp))?;
460+
self.orig_rsp = GuestPtr::try_from(RawPtr::from(rsp - 0x28))?;
461+
462+
// Need to update the min stack address from tmp_stack address to the new stack
463+
hyperlight_peb.min_stack_address = hyperlight_peb.calculate_min_stack_address();
464+
self.mem_sections.write_hyperlight_peb(hyperlight_peb)?;
449465
}
450466
}
451467

@@ -579,9 +595,13 @@ impl Hypervisor for KVMDriver {
579595
log_then_return!("Error running VCPU {:?}", e);
580596
}
581597
},
582-
Ok(other) => {
583-
crate::debug!("KVM Other Exit {:?}", other);
584-
HyperlightExit::Unknown(format!("Unexpected KVM Exit {:?}", other))
598+
Ok(_) => {
599+
// get registers
600+
let regs = self.vcpu_fd.get_regs()?;
601+
dbg!(regs.clone());
602+
603+
// crate::debug!("KVM Other Exit {:?}", other);
604+
HyperlightExit::Unknown(format!("Unexpected KVM Exit {:?}", VcpuExit::Unknown))
585605
}
586606
};
587607
Ok(result)

src/hyperlight_host/src/sandbox/sandbox_builder.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl SandboxBuilder {
543543
const DEFAULT_TMP_STACK_GUARD_PAGE_NAME: &str = "tmp stack guard page";
544544
const DEFAULT_CUSTOM_GUEST_MEMORY_GUARD_PAGE_NAME: &str = "custom guest memory guard page";
545545

546-
let tmp_stack_size = 0x200_000;
546+
let tmp_stack_size = 0x20_000;
547547
let guest_memory_size = self.guest_memory_size;
548548

549549
// (a) guest code added on `new`
@@ -775,6 +775,8 @@ impl SandboxBuilder {
775775
// Map host addresses to guest addresses
776776
sandbox_builder.map_host_addresses(exclusive_shared_memory.base_addr());
777777

778+
dbg!(sandbox_builder.memory_sections.clone());
779+
778780
let hyperlight_peb = HyperlightPEB::new(
779781
sandbox_builder
780782
.memory_sections
@@ -934,15 +936,15 @@ mod tests {
934936
host_function.register(&mut uninitialized_sandbox, "HostAdd")?;
935937

936938
// Tests evolving to a multi-use sandbox
937-
let multi_use_sandbox = uninitialized_sandbox.evolve(Noop::default())?;
939+
let mut multi_use_sandbox = uninitialized_sandbox.evolve(Noop::default())?;
938940

939-
// let result = multi_use_sandbox.call_guest_function_by_name(
940-
// "Add",
941-
// ReturnType::Int,
942-
// Some(vec![ParameterValue::Int(1), ParameterValue::Int(41)]),
943-
// )?;
944-
//
945-
// assert_eq!(result, ReturnValue::Int(42));
941+
let result = multi_use_sandbox.call_guest_function_by_name(
942+
"Add",
943+
ReturnType::Int,
944+
Some(vec![ParameterValue::Int(1), ParameterValue::Int(41)]),
945+
)?;
946+
947+
assert_eq!(result, ReturnValue::Int(42));
946948

947949
Ok(())
948950
}
@@ -965,15 +967,15 @@ mod tests {
965967
host_function.register(&mut uninitialized_sandbox, "HostAdd")?;
966968

967969
// Tests evolving to a multi-use sandbox
968-
let multi_use_sandbox = uninitialized_sandbox.evolve(Noop::default())?;
969-
970-
// let result = multi_use_sandbox.call_guest_function_by_name(
971-
// "Add",
972-
// ReturnType::Int,
973-
// Some(vec![ParameterValue::Int(1), ParameterValue::Int(41)]),
974-
// )?;
975-
//
976-
// assert_eq!(result, ReturnValue::Int(42));
970+
let mut multi_use_sandbox = uninitialized_sandbox.evolve(Noop::default())?;
971+
972+
let result = multi_use_sandbox.call_guest_function_by_name(
973+
"Add",
974+
ReturnType::Int,
975+
Some(vec![ParameterValue::Int(1), ParameterValue::Int(41)]),
976+
)?;
977+
978+
assert_eq!(result, ReturnValue::Int(42));
977979

978980
Ok(())
979981
}

0 commit comments

Comments
 (0)