Skip to content

Commit e63a0cc

Browse files
authored
Remove in-process mode from hyperlight-guest and remaining clean up in hyperlight-host (#496)
Signed-off-by: Simon Davies <[email protected]>
1 parent cca0c78 commit e63a0cc

File tree

37 files changed

+90
-531
lines changed

37 files changed

+90
-531
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ fn main() -> hyperlight_host::Result<()> {
4242
// Create an uninitialized sandbox with a guest binary
4343
let mut uninitialized_sandbox = UninitializedSandbox::new(
4444
hyperlight_host::GuestBinary::FilePath(hyperlight_testing::simple_guest_as_string().unwrap()),
45-
None, // default configuration
46-
None, // default run options
45+
None // default configuration
4746
)?;
4847

4948
// Registering a host function makes it available to be called by the guest

fuzz/fuzz_targets/guest_call.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ fuzz_target!(
3535
let u_sbox = UninitializedSandbox::new(
3636
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
3737
None,
38-
None,
3938
)
4039
.unwrap();
4140

fuzz/fuzz_targets/host_call.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ fuzz_target!(
3333
init: {
3434
let u_sbox = UninitializedSandbox::new(
3535
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
36-
None,
37-
None,
36+
None
3837
)
3938
.unwrap();
4039

fuzz/fuzz_targets/host_print.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ fuzz_target!(
2121
let u_sbox = UninitializedSandbox::new(
2222
GuestBinary::FilePath(simple_guest_for_fuzzing_as_string().expect("Guest Binary Missing")),
2323
None,
24-
None,
2524
)
2625
.unwrap();
2726

src/hyperlight_common/src/mem.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ pub const PAGE_SIZE_USIZE: usize = 1 << 12;
2222

2323
use core::ffi::{c_char, c_void};
2424

25-
#[repr(u64)]
26-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
27-
pub enum RunMode {
28-
None = 0,
29-
Hypervisor = 1,
30-
InProcessWindows = 2,
31-
InProcessLinux = 3,
32-
Invalid = 4,
33-
}
34-
3525
#[repr(C)]
3626
pub struct InputData {
3727
pub inputDataSize: u64,
@@ -67,9 +57,6 @@ pub struct HyperlightPEB {
6757
pub security_cookie_seed: u64,
6858
pub guest_function_dispatch_ptr: u64,
6959
pub pCode: *mut c_char,
70-
pub pOutb: *mut c_void,
71-
pub pOutbContext: *mut c_void,
72-
pub runMode: RunMode,
7360
pub inputdata: InputData,
7461
pub outputdata: OutputData,
7562
pub guestheapData: GuestHeapData,

src/hyperlight_guest/src/chkstk.rs

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ limitations under the License.
1515
*/
1616

1717
use core::arch::global_asm;
18-
use core::mem::size_of;
1918

20-
use hyperlight_common::mem::RunMode;
21-
22-
use crate::guest_error::{set_invalid_runmode_error, set_stack_allocate_error};
23-
use crate::{MIN_STACK_ADDRESS, RUNNING_MODE};
19+
use crate::guest_error::set_stack_allocate_error;
20+
use crate::MIN_STACK_ADDRESS;
2421

2522
extern "win64" {
2623
fn __chkstk();
@@ -35,21 +32,6 @@ global_asm!(
3532
push r10
3633
push r11
3734
38-
/* Load run_mode into r10 */
39-
mov r10, qword ptr [rip+{run_mode}]
40-
41-
cmp r10, 0
42-
je handle_none
43-
cmp r10, 1
44-
je handle_hypervisor
45-
cmp r10, 2
46-
je handle_inproc_windows
47-
cmp r10, 3
48-
je handle_inproc_linux
49-
/* run_mode > 3 (invalid), so treat like handle_none */
50-
jmp handle_invalid
51-
52-
handle_hypervisor:
5335
/* Load the minimum stack address from the PEB */
5436
mov r11, [rip+{min_stack_addr}]
5537
@@ -72,49 +54,11 @@ global_asm!(
7254
call {set_error}
7355
hlt
7456
75-
handle_inproc_windows:
76-
/* Get the current stack pointer */
77-
lea r10, [rsp + 0x18]
78-
79-
/* Calculate what the new stack pointer will be */
80-
sub r10, rax
81-
cmovb r10, r11
82-
mov r11, qword ptr gs:[0x0000000000000010]
83-
cmp r10, r11
84-
jae cs_ret
85-
and r10w, 0x0F000
86-
csip_stackprobe:
87-
lea r11, [r11 + 0x0FFFFFFFFFFFFF000]
88-
mov byte ptr [r11], 0
89-
cmp r10, r11
90-
jne csip_stackprobe
9157
cs_ret:
9258
/* Restore RAX, R11 */
9359
pop r11
9460
pop r10
95-
ret
96-
handle_inproc_linux:
97-
/* no-op */
98-
jmp cs_ret
99-
handle_none:
100-
/* no-op. This can entrypoint has a large stack allocation
101-
before RunMode variable is set */
102-
jmp cs_ret
103-
handle_invalid:
104-
call {invalid_runmode}",
105-
run_mode = sym RUNNING_MODE,
61+
ret",
10662
min_stack_addr = sym MIN_STACK_ADDRESS,
10763
set_error = sym set_stack_allocate_error,
108-
invalid_runmode = sym set_invalid_runmode_error
10964
);
110-
111-
// Assumptions made in implementation above. If these are no longer true, compilation will fail
112-
// and the developer will need to update the assembly code.
113-
const _: () = {
114-
assert!(size_of::<RunMode>() == size_of::<u64>());
115-
assert!(RunMode::None as u64 == 0);
116-
assert!(RunMode::Hypervisor as u64 == 1);
117-
assert!(RunMode::InProcessWindows as u64 == 2);
118-
assert!(RunMode::InProcessLinux as u64 == 3);
119-
assert!(RunMode::Invalid as u64 == 4);
120-
};

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ limitations under the License.
1515
*/
1616

1717
use core::arch::asm;
18-
use core::ffi::{c_char, c_void, CStr};
18+
use core::ffi::{c_char, CStr};
1919

20-
use hyperlight_common::mem::{HyperlightPEB, RunMode};
20+
use hyperlight_common::mem::HyperlightPEB;
2121
use hyperlight_common::outb::OutBAction;
2222
use log::LevelFilter;
2323
use spin::Once;
@@ -27,18 +27,11 @@ use crate::guest_function_call::dispatch_function;
2727
use crate::guest_logger::init_logger;
2828
use crate::host_function_call::outb;
2929
use crate::idtr::load_idt;
30-
use crate::{
31-
__security_cookie, HEAP_ALLOCATOR, MIN_STACK_ADDRESS, OS_PAGE_SIZE, OUTB_PTR,
32-
OUTB_PTR_WITH_CONTEXT, P_PEB, RUNNING_MODE,
33-
};
30+
use crate::{__security_cookie, HEAP_ALLOCATOR, MIN_STACK_ADDRESS, OS_PAGE_SIZE, P_PEB};
3431

3532
#[inline(never)]
3633
pub fn halt() {
37-
unsafe {
38-
if RUNNING_MODE == RunMode::Hypervisor {
39-
asm!("hlt", options(nostack))
40-
}
41-
}
34+
unsafe { asm!("hlt", options(nostack)) }
4235
}
4336

4437
#[no_mangle]
@@ -105,45 +98,14 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_
10598
.expect("Invalid log level");
10699
init_logger(max_log_level);
107100

108-
match (*peb_ptr).runMode {
109-
RunMode::Hypervisor => {
110-
RUNNING_MODE = RunMode::Hypervisor;
111-
// This static is to make it easier to implement the __chkstk function in assembly.
112-
// It also means that should we change the layout of the struct in the future, we
113-
// don't have to change the assembly code.
114-
MIN_STACK_ADDRESS = (*peb_ptr).gueststackData.minUserStackAddress;
115-
116-
// Setup GDT and IDT
117-
load_gdt();
118-
load_idt();
119-
}
120-
RunMode::InProcessLinux | RunMode::InProcessWindows => {
121-
RUNNING_MODE = (*peb_ptr).runMode;
122-
123-
OUTB_PTR = {
124-
let outb_ptr: extern "win64" fn(u16, *const u8, u64) =
125-
core::mem::transmute((*peb_ptr).pOutb);
126-
Some(outb_ptr)
127-
};
128-
129-
if (*peb_ptr).pOutbContext.is_null() {
130-
panic!("OutbContext is null");
131-
}
132-
133-
OUTB_PTR_WITH_CONTEXT = {
134-
let outb_ptr_with_context: extern "win64" fn(
135-
*mut c_void,
136-
u16,
137-
*const u8,
138-
u64,
139-
) = core::mem::transmute((*peb_ptr).pOutb);
140-
Some(outb_ptr_with_context)
141-
};
142-
}
143-
_ => {
144-
panic!("Invalid runmode in PEB");
145-
}
146-
}
101+
// This static is to make it easier to implement the __chkstk function in assembly.
102+
// It also means that should we change the layout of the struct in the future, we
103+
// don't have to change the assembly code.
104+
MIN_STACK_ADDRESS = (*peb_ptr).gueststackData.minUserStackAddress;
105+
106+
// Setup GDT and IDT
107+
load_gdt();
108+
load_idt();
147109

148110
let heap_start = (*peb_ptr).guestheapData.guestHeapBuffer as usize;
149111
let heap_size = (*peb_ptr).guestheapData.guestHeapSize as usize;

src/hyperlight_guest/src/guest_error.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ pub(crate) extern "win64" fn set_stack_allocate_error() {
5252
outb(OutBAction::Abort as u16, &[ErrorCode::StackOverflow as u8]);
5353
}
5454

55-
#[no_mangle]
56-
pub(crate) extern "win64" fn set_invalid_runmode_error() {
57-
panic!("Invalid run mode in __chkstk");
58-
}
59-
6055
/// Exposes a C API to allow the guest to set an error
6156
///
6257
/// # Safety

src/hyperlight_guest/src/host_function_call.rs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
2525
};
2626
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2727
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
28-
use hyperlight_common::mem::RunMode;
2928
use hyperlight_common::outb::OutBAction;
3029

3130
use crate::error::{HyperlightGuestError, Result};
3231
use crate::shared_input_data::try_pop_shared_input_data_into;
3332
use crate::shared_output_data::push_shared_output_data;
34-
use crate::{OUTB_PTR, OUTB_PTR_WITH_CONTEXT, P_PEB, RUNNING_MODE};
3533

3634
/// Get a return value from a host function call.
3735
/// This usually requires a host function to be called first using `call_host_function`.
@@ -77,39 +75,16 @@ pub fn call_host_function(
7775

7876
pub fn outb(port: u16, data: &[u8]) {
7977
unsafe {
80-
match RUNNING_MODE {
81-
RunMode::Hypervisor => {
82-
let mut i = 0;
83-
while i < data.len() {
84-
let remaining = data.len() - i;
85-
let chunk_len = remaining.min(3);
86-
let mut chunk = [0u8; 4];
87-
chunk[0] = chunk_len as u8;
88-
chunk[1..1 + chunk_len].copy_from_slice(&data[i..i + chunk_len]);
89-
let val = u32::from_le_bytes(chunk);
90-
out32(port, val);
91-
i += chunk_len;
92-
}
93-
}
94-
RunMode::InProcessLinux | RunMode::InProcessWindows => {
95-
if let Some(outb_func) = OUTB_PTR_WITH_CONTEXT {
96-
if let Some(peb_ptr) = P_PEB {
97-
outb_func(
98-
(*peb_ptr).pOutbContext,
99-
port,
100-
data.as_ptr(),
101-
data.len() as u64,
102-
);
103-
}
104-
} else if let Some(outb_func) = OUTB_PTR {
105-
outb_func(port, data.as_ptr(), data.len() as u64);
106-
} else {
107-
panic!("Tried to call outb without hypervisor and without outb function ptrs");
108-
}
109-
}
110-
_ => {
111-
panic!("Tried to call outb in invalid runmode");
112-
}
78+
let mut i = 0;
79+
while i < data.len() {
80+
let remaining = data.len() - i;
81+
let chunk_len = remaining.min(3);
82+
let mut chunk = [0u8; 4];
83+
chunk[0] = chunk_len as u8;
84+
chunk[1..1 + chunk_len].copy_from_slice(&data[i..i + chunk_len]);
85+
let val = u32::from_le_bytes(chunk);
86+
out32(port, val);
87+
i += chunk_len;
11388
}
11489
}
11590
}

src/hyperlight_guest/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use alloc::string::ToString;
2121
use buddy_system_allocator::LockedHeap;
2222
use guest_function_register::GuestFunctionRegister;
2323
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
24-
use hyperlight_common::mem::{HyperlightPEB, RunMode};
24+
use hyperlight_common::mem::HyperlightPEB;
2525

2626
use crate::entrypoint::abort_with_code_and_message;
2727
extern crate alloc;
@@ -59,6 +59,7 @@ pub mod logging;
5959
pub(crate) extern "C" fn __CxxFrameHandler3() {}
6060
///cbindgen:ignore
6161
#[no_mangle]
62+
#[clippy::allow(clippy::non_upper_case_globals)]
6263
pub(crate) static _fltused: i32 = 0;
6364

6465
// It looks like rust-analyzer doesn't correctly manage no_std crates,
@@ -83,17 +84,13 @@ pub(crate) static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::empty();
8384

8485
///cbindgen:ignore
8586
#[no_mangle]
87+
#[clippy::allow(clippy::non_upper_case_globals)]
8688
pub(crate) static mut __security_cookie: u64 = 0;
8789

8890
pub(crate) static mut P_PEB: Option<*mut HyperlightPEB> = None;
8991
pub static mut MIN_STACK_ADDRESS: u64 = 0;
9092

9193
pub static mut OS_PAGE_SIZE: u32 = 0;
92-
pub(crate) static mut OUTB_PTR: Option<extern "win64" fn(u16, *const u8, u64)> = None;
93-
pub(crate) static mut OUTB_PTR_WITH_CONTEXT: Option<
94-
extern "win64" fn(*mut core::ffi::c_void, u16, *const u8, u64),
95-
> = None;
96-
pub static mut RUNNING_MODE: RunMode = RunMode::None;
9794

9895
pub(crate) static mut REGISTERED_GUEST_FUNCTIONS: GuestFunctionRegister =
9996
GuestFunctionRegister::new();

0 commit comments

Comments
 (0)