Skip to content

Commit 354ac3a

Browse files
committed
tmp 2
Signed-off-by: danbugs <[email protected]>
1 parent 86f0bab commit 354ac3a

File tree

9 files changed

+78
-53
lines changed

9 files changed

+78
-53
lines changed

Justfile

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ build target=default-target:
3131
guests: build-and-move-rust-guests build-and-move-c-guests
3232

3333
build-rust-guests target=default-target:
34-
cd src/tests/rust_guests/callbackguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
35-
cd src/tests/rust_guests/callbackguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }} --target=x86_64-pc-windows-msvc
36-
cd src/tests/rust_guests/simpleguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
34+
# cd src/tests/rust_guests/callbackguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
35+
# cd src/tests/rust_guests/callbackguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }} --target=x86_64-pc-windows-msvc
36+
cd src/tests/rust_guests/simpleguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
3737
cd src/tests/rust_guests/simpleguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }} --target=x86_64-pc-windows-msvc
38-
cd src/tests/rust_guests/dummyguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
38+
# cd src/tests/rust_guests/dummyguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
3939

4040
@move-rust-guests target=default-target:
41-
cp {{ callbackguest_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
42-
cp {{ callbackguest_msvc_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
41+
# cp {{ callbackguest_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
42+
# cp {{ callbackguest_msvc_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
4343
cp {{ simpleguest_source }}/{{ target }}/simpleguest* {{ rust_guests_bin_dir }}/{{ target }}/
4444
cp {{ simpleguest_msvc_source }}/{{ target }}/simpleguest* {{ rust_guests_bin_dir }}/{{ target }}/
45-
cp {{ dummyguest_source }}/{{ target }}/dummyguest* {{ rust_guests_bin_dir }}/{{ target }}/
45+
# cp {{ dummyguest_source }}/{{ target }}/dummyguest* {{ rust_guests_bin_dir }}/{{ target }}/
4646

4747
build-and-move-rust-guests: (build-rust-guests "debug") (move-rust-guests "debug") (build-rust-guests "release") (move-rust-guests "release")
4848
build-and-move-c-guests: (build-c-guests "debug") (move-c-guests "debug") (build-c-guests "release") (move-c-guests "release")

src/hyperlight_common/src/peb.rs

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct MemoryRegion {
2727
#[repr(C)]
2828
#[derive(Clone, Default)]
2929
pub struct HyperlightPEB {
30+
pub min_stack_address: u64,
3031
// - Host configured fields
3132
/// Hyperlight supports two primary modes:
3233
/// 1. Hypervisor mode
@@ -86,6 +87,7 @@ impl HyperlightPEB {
8687
/// Creates a new HyperlightPEB with the basic configuration based on the provided guest memory
8788
/// layout and default guest heap/stack sizes. The guest can later fill additional fields.
8889
pub fn new(
90+
min_stack_address: u64,
8991
run_mode: RunMode,
9092
guest_heap_size: u64,
9193
guest_stack_size: u64,
@@ -94,6 +96,7 @@ impl HyperlightPEB {
9496
guest_memory_size: u64,
9597
) -> Self {
9698
Self {
99+
min_stack_address,
97100
run_mode,
98101
outb_ptr: 0,
99102
outb_ptr_ctx: 0,
@@ -222,6 +225,16 @@ impl HyperlightPEB {
222225
region.offset.unwrap() + self.guest_memory_base_address + region.size
223226
}
224227

228+
/// Calculate the minimum guest stack address (start of guest stack data region in the guest
229+
/// address space).
230+
pub fn calculate_min_stack_address(&self) -> u64 {
231+
let region = self
232+
.guest_stack_data
233+
.as_ref()
234+
.expect("Guest stack data region not set");
235+
region.offset.unwrap() + self.guest_memory_base_address
236+
}
237+
225238
/// Sets the guest heap data region.
226239
/// - HyperlightPEB is always set with a default size for heap from the guest binary, there's an
227240
/// option to override this size with the `size_override` parameter.

src/hyperlight_guest/src/chkstk.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ limitations under the License.
1515
*/
1616

1717
use core::arch::global_asm;
18-
use core::mem::size_of;
18+
use core::mem::{offset_of, size_of};
1919

20-
use hyperlight_common::peb::RunMode;
21-
use hyperlight_common::RUNNING_MODE;
20+
use hyperlight_common::peb::{HyperlightPEB, RunMode};
21+
use hyperlight_common::{PEB, RUNNING_MODE};
2222

2323
use crate::guest_error::{set_invalid_runmode_error, set_stack_allocate_error};
24-
use crate::MIN_STACK_ADDRESS;
2524

2625
extern "win64" {
2726
fn __chkstk();
@@ -52,20 +51,22 @@ global_asm!(
5251
5352
handle_hypervisor:
5453
/* Load the minimum stack address from the PEB */
55-
mov r11, [rip+{min_stack_addr}]
54+
/* min_stack_address is offset 0x0 in the PEB struct */
55+
mov r11, [rip+{peb_ptr}]
56+
mov r11, qword ptr [r11]
5657
5758
/* Get the current stack pointer */
58-
lea r10, [rsp+0x18]
59+
lea r10, [rsp+0x18]
5960
6061
/* Calculate what the new stack pointer will be */
6162
sub r10, rax
62-
63+
6364
/* If result is negative, cause StackOverflow */
6465
js call_set_error
65-
66+
6667
/* Compare the new stack pointer with the minimum stack address */
67-
cmp r10, r11
68-
/* If the new stack pointer is greater or equal to the minimum stack address,
68+
cmp r10, r11
69+
/* If the new stack pointer is greater or equal to the minimum stack address,
6970
then we are good. Otherwise set the error code to 9 (stack overflow) call set_error and halt */
7071
jae cs_ret
7172
@@ -90,7 +91,7 @@ global_asm!(
9091
cmp r10, r11
9192
jne csip_stackprobe
9293
cs_ret:
93-
/* Restore RAX, R11 */
94+
/* Restore R10, R11 */
9495
pop r11
9596
pop r10
9697
ret
@@ -104,7 +105,7 @@ global_asm!(
104105
handle_invalid:
105106
call {invalid_runmode}",
106107
run_mode = sym RUNNING_MODE,
107-
min_stack_addr = sym MIN_STACK_ADDRESS,
108+
peb_ptr = sym PEB,
108109
set_error = sym set_stack_allocate_error,
109110
invalid_runmode = sym set_invalid_runmode_error
110111
);
@@ -118,4 +119,5 @@ const _: () = {
118119
assert!(RunMode::InProcessWindows as u64 == 2);
119120
assert!(RunMode::InProcessLinux as u64 == 3);
120121
assert!(RunMode::Invalid as u64 == 4);
122+
assert!(offset_of!(HyperlightPEB, min_stack_address) == 0x0);
121123
};

src/hyperlight_guest/src/entrypoint.rs

+3-7
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, HEAP_ALLOCATOR, MIN_STACK_ADDRESS};
30+
use crate::{__security_cookie, panic, HEAP_ALLOCATOR};
3131

3232
#[inline(never)]
3333
pub fn halt() {
@@ -111,12 +111,6 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, max_log_level: u64
111111

112112
match RUNNING_MODE {
113113
RunMode::Hypervisor => {
114-
// This static is to make it easier to implement the __chkstk function in assembly.
115-
// It also means that, should we change the layout of the struct in the future, we
116-
// don't have to change the assembly code. Plus, while this could be accessible via
117-
// the PEB, we don't want to expose it entirely to user code.
118-
MIN_STACK_ADDRESS = (*PEB).get_stack_data_address();
119-
120114
// Setup GDT and IDT
121115
load_gdt();
122116
load_idt();
@@ -141,6 +135,8 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, max_log_level: u64
141135
_ => panic!("Invalid runmode in PEB"),
142136
}
143137

138+
panic(
139+
144140
hyperlight_main();
145141
});
146142

src/hyperlight_guest/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ pub(crate) static _fltused: i32 = 0;
6565
#[allow(clippy::panic)]
6666
// to satisfy the clippy when cfg == test
6767
#[allow(dead_code)]
68-
fn panic(info: &core::panic::PanicInfo) -> ! {
68+
pub fn panic(info: &core::panic::PanicInfo) -> ! {
69+
loop {}
6970
unsafe {
7071
copy_nonoverlapping(
7172
info.to_string().as_ptr(),
@@ -85,7 +86,5 @@ pub(crate) static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::empty();
8586
#[no_mangle]
8687
pub(crate) static mut __security_cookie: u64 = 0;
8788

88-
pub static mut MIN_STACK_ADDRESS: u64 = 0;
89-
9089
pub(crate) static mut REGISTERED_GUEST_FUNCTIONS: GuestFunctionRegister =
9190
GuestFunctionRegister::new();

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -489,13 +489,17 @@ impl Hypervisor for HypervLinuxDriver {
489489
)?;
490490

491491
// 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()?;
492+
let mut hyperlight_peb = self.mem_sections.read_hyperlight_peb()?;
493493

494494
if let Some(guest_stack_data) = &hyperlight_peb.get_guest_stack_data_region() {
495495
if guest_stack_data.offset.is_some() {
496496
// If we got here, it means the guest has set up a new stack
497497
let rsp = hyperlight_peb.get_top_of_guest_stack_data();
498498
self.orig_rsp = GuestPtr::try_from(RawPtr::from(rsp))?;
499+
500+
// Need to update the min stack address from tmp_stack address to the new stack
501+
hyperlight_peb.min_stack_address = hyperlight_peb.calculate_min_stack_address();
502+
self.mem_sections.write_hyperlight_peb(hyperlight_peb)?;
499503
}
500504
}
501505

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,17 @@ impl Hypervisor for HypervWindowsDriver {
339339
)?;
340340

341341
// 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()?;
342+
let mut hyperlight_peb = self.mem_sections.read_hyperlight_peb()?;
343343

344344
if let Some(guest_stack_data) = &hyperlight_peb.get_guest_stack_data_region() {
345345
if guest_stack_data.offset.is_some() {
346346
// If we got here, it means the guest has set up a new stack
347347
let rsp = hyperlight_peb.get_top_of_guest_stack_data();
348348
self.orig_rsp = GuestPtr::try_from(RawPtr::from(rsp))?;
349+
350+
// Need to update the min stack address from tmp_stack address to the new stack
351+
hyperlight_peb.min_stack_address = hyperlight_peb.calculate_min_stack_address();
352+
self.mem_sections.write_hyperlight_peb(hyperlight_peb)?;
349353
}
350354
}
351355

src/hyperlight_host/src/sandbox/sandbox_builder.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,15 @@ impl SandboxMemorySections {
190190
Ok(())
191191
}
192192

193-
pub(crate) fn sections(&self) -> impl Iterator<Item = &SandboxMemorySection> {
193+
pub(crate) fn sections(&self) -> impl Iterator<Item=&SandboxMemorySection> {
194194
self.sections.values()
195195
}
196196

197197
pub(crate) fn insert(&mut self, offset: usize, section: SandboxMemorySection) {
198198
self.sections.insert(offset, section);
199199
}
200200

201-
pub(crate) fn iter(&self) -> impl Iterator<Item = (&usize, &SandboxMemorySection)> {
201+
pub(crate) fn iter(&self) -> impl Iterator<Item=(&usize, &SandboxMemorySection)> {
202202
self.sections.iter()
203203
}
204204
}
@@ -776,6 +776,9 @@ impl SandboxBuilder {
776776
sandbox_builder.map_host_addresses(exclusive_shared_memory.base_addr());
777777

778778
let hyperlight_peb = HyperlightPEB::new(
779+
sandbox_builder
780+
.memory_sections
781+
.get_tmp_stack_section_offset().unwrap() as u64,
779782
run_mode,
780783
guest_heap_size,
781784
guest_stack_size,
@@ -913,6 +916,8 @@ mod tests {
913916

914917
#[test]
915918
fn test_sandbox_builder() -> Result<()> {
919+
env_logger::init();
920+
916921
// Tests building an uninitialized sandbox w/ the sandbox builder
917922
let sandbox_builder =
918923
SandboxBuilder::new(GuestBinary::FilePath(simple_guest_as_string()?))?;
@@ -929,21 +934,23 @@ mod tests {
929934
host_function.register(&mut uninitialized_sandbox, "HostAdd")?;
930935

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

934-
let result = multi_use_sandbox.call_guest_function_by_name(
935-
"Add",
936-
ReturnType::Int,
937-
Some(vec![ParameterValue::Int(1), ParameterValue::Int(41)]),
938-
)?;
939-
940-
assert_eq!(result, ReturnValue::Int(42));
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));
941946

942947
Ok(())
943948
}
944949

945950
#[test]
946951
fn test_sandbox_builder_with_exe() -> Result<()> {
952+
env_logger::init();
953+
947954
// Tests building an uninitialized sandbox w/ the sandbox builder
948955
let sandbox_builder =
949956
SandboxBuilder::new(GuestBinary::FilePath(simple_guest_exe_as_string()?))?;
@@ -958,15 +965,15 @@ mod tests {
958965
host_function.register(&mut uninitialized_sandbox, "HostAdd")?;
959966

960967
// Tests evolving to a multi-use sandbox
961-
let mut multi_use_sandbox = uninitialized_sandbox.evolve(Noop::default())?;
962-
963-
let result = multi_use_sandbox.call_guest_function_by_name(
964-
"Add",
965-
ReturnType::Int,
966-
Some(vec![ParameterValue::Int(1), ParameterValue::Int(41)]),
967-
)?;
968-
969-
assert_eq!(result, ReturnValue::Int(42));
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));
970977

971978
Ok(())
972979
}

src/tests/rust_guests/simpleguest/src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
4141
use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
4242
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
4343
use hyperlight_common::host_calling::{call_host_function, get_host_return_value, print};
44-
use hyperlight_common::PAGE_SIZE;
44+
use hyperlight_common::{PAGE_SIZE, PEB};
4545
use hyperlight_guest::entrypoint::{abort_with_code, abort_with_code_and_message};
4646
use hyperlight_guest::error::{HyperlightGuestError, Result};
4747
use hyperlight_guest::guest_function_definition::GuestFunctionDefinition;
4848
use hyperlight_guest::guest_function_register::register_function;
4949
use hyperlight_guest::memory::malloc;
50-
use hyperlight_guest::{logging, MIN_STACK_ADDRESS};
50+
use hyperlight_guest::logging;
5151
use log::{error, LevelFilter};
5252

5353
extern crate hyperlight_guest;
@@ -551,7 +551,7 @@ fn test_guest_panic(function_call: &FunctionCall) -> Result<Vec<u8>> {
551551

552552
fn test_write_raw_ptr(function_call: &FunctionCall) -> Result<Vec<u8>> {
553553
if let ParameterValue::Long(offset) = function_call.parameters.clone().unwrap()[0].clone() {
554-
let min_stack_addr = unsafe { MIN_STACK_ADDRESS };
554+
let min_stack_addr = unsafe { (*PEB).min_stack_address };
555555
let page_guard_start = min_stack_addr - PAGE_SIZE as u64;
556556
let addr = {
557557
let abs = u64::try_from(offset.abs())

0 commit comments

Comments
 (0)