Skip to content

Commit 59388c2

Browse files
committed
tmp (x4)
Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 78256e0 commit 59388c2

File tree

19 files changed

+598
-424
lines changed

19 files changed

+598
-424
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ members = [
1111
"src/hyperlight_host",
1212
"src/hyperlight_guest_capi",
1313
"src/hyperlight_testing",
14-
"fuzz",
14+
"src/hyperlight_host/fuzz",
15+
# "src/tests/rust_guests/simpleguest",
1516
]
1617
# Because hyperlight-guest has custom linker flags,
1718
# we exclude it from the default-members list
@@ -20,7 +21,7 @@ members = [
2021
exclude = [
2122
"src/tests/rust_guests/callbackguest",
2223
"src/tests/rust_guests/dummyguest",
23-
"src/tests/rust_guests/simpleguest",
24+
"src/tests/rust_guests/simpleguest",
2425
# TODO(danbugs:297): This is a temporary guest to test
2526
# the new memory layout. Once I'm done w/
2627
# the work, I'll delete it. `simpleguest`

Justfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ build-rust-guests target=default-target:
4646
# cd src/tests/rust_guests/customguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
4747

4848
@move-rust-guests target=default-target:
49-
cp {{ callbackguest_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
50-
cp {{ callbackguest_msvc_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
5149
cp {{ simpleguest_source }}/{{ target }}/simpleguest* {{ rust_guests_bin_dir }}/{{ target }}/
5250
cp {{ simpleguest_msvc_source }}/{{ target }}/simpleguest* {{ rust_guests_bin_dir }}/{{ target }}/
53-
cp {{ dummyguest_source }}/{{ target }}/dummyguest* {{ rust_guests_bin_dir }}/{{ target }}/
54-
# TODO(danbugs:297): Delete
55-
cp {{ customguest_source }}/{{ target }}/customguest* {{ rust_guests_bin_dir }}/{{ target }}/
51+
# TODO(danbugs:297): Delete
52+
# cp {{ callbackguest_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
53+
# cp {{ callbackguest_msvc_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
54+
# cp {{ dummyguest_source }}/{{ target }}/dummyguest* {{ rust_guests_bin_dir }}/{{ target }}/
55+
# cp {{ customguest_source }}/{{ target }}/customguest* {{ rust_guests_bin_dir }}/{{ target }}/
5656

5757
build-and-move-rust-guests: (build-rust-guests "debug") (move-rust-guests "debug") (build-rust-guests "release") (move-rust-guests "release")
5858
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/flatbuffer_wrappers/function_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub struct FunctionCall {
4747
pub function_name: String,
4848
/// The parameters for the function call.
4949
pub parameters: Option<Vec<ParameterValue>>,
50-
function_call_type: FunctionCallType,
50+
pub function_call_type: FunctionCallType,
5151
/// The return type of the function call
5252
pub expected_return_type: ReturnType,
5353
}

src/hyperlight_common/src/flatbuffer_wrappers/hyperlight_peb.rs

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use flatbuffers::{size_prefixed_root, FlatBufferBuilder};
55
use crate::flatbuffers::hyperlight::generated::{HyperlightPEB as FbHyperlightPEB, HyperlightPEBArgs as FbHyperlightPEBArgs};
66

77
/// Hyperlight supports 2 primary modes:
8-
/// (1) Hypervisor mode
9-
/// (2) In-process mode
8+
/// 1. Hypervisor mode
9+
/// 2. In-process mode
1010
///
1111
/// When running in process, there's no hypervisor isolation.
1212
/// In-process mode is primarily used for debugging and testing.
1313
#[repr(u64)]
14-
#[derive(Clone, PartialEq)]
14+
#[derive(Clone, Debug, PartialEq)]
1515
pub enum RunMode {
1616
None = 0,
1717
Hypervisor = 1,
@@ -48,8 +48,8 @@ pub struct HyperlightPEB {
4848
pub outb_ptr: Option<u64>,
4949

5050
/// Hyperlight supports two primary modes:
51-
/// (1) Hypervisor mode
52-
/// (2) In-process mode
51+
/// 1. Hypervisor mode
52+
/// 2. In-process mode
5353
///
5454
/// When running in process, there's no hypervisor isolation.
5555
/// It's a mode primarily used for debugging and testing.
@@ -119,6 +119,44 @@ impl TryFrom<&[u8]> for HyperlightPEB {
119119
}
120120
}
121121

122+
impl TryFrom<&mut [u8]> for HyperlightPEB {
123+
type Error = anyhow::Error;
124+
125+
fn try_from(value: &mut [u8]) -> Result<Self> {
126+
let peb_fb = size_prefixed_root::<FbHyperlightPEB>(value)
127+
.map_err(|e| anyhow::anyhow!("Error reading HyperlightPEB buffer: {:?}", e))?;
128+
129+
let run_mode = match peb_fb.run_mode() {
130+
0 => Some(RunMode::None),
131+
1 => Some(RunMode::Hypervisor),
132+
2 => Some(RunMode::InProcessWindows),
133+
3 => Some(RunMode::InProcessLinux),
134+
4 => Some(RunMode::Invalid),
135+
_ => None, // Handles unexpected values gracefully
136+
};
137+
138+
Ok(Self {
139+
guest_function_dispatch_ptr: Some(peb_fb.guest_function_dispatch_ptr()).filter(|&v| v != 0),
140+
host_function_details_ptr: Some(peb_fb.host_function_details_ptr()).filter(|&v| v != 0),
141+
host_function_details_size: Some(peb_fb.host_function_details_size()).filter(|&v| v != 0),
142+
guest_error_data_ptr: Some(peb_fb.guest_error_data_ptr()).filter(|&v| v != 0),
143+
guest_error_data_size: Some(peb_fb.guest_error_data_size()).filter(|&v| v != 0),
144+
outb_ptr: Some(peb_fb.outb_ptr()).filter(|&v| v != 0),
145+
run_mode,
146+
input_data_ptr: Some(peb_fb.input_data_ptr()).filter(|&v| v != 0),
147+
input_data_size: Some(peb_fb.input_data_size()).filter(|&v| v != 0),
148+
output_data_ptr: Some(peb_fb.output_data_ptr()).filter(|&v| v != 0),
149+
output_data_size: Some(peb_fb.output_data_size()).filter(|&v| v != 0),
150+
guest_panic_context_ptr: Some(peb_fb.guest_panic_context_ptr()).filter(|&v| v != 0),
151+
guest_panic_context_size: Some(peb_fb.guest_panic_context_size()).filter(|&v| v != 0),
152+
guest_heap_data_ptr: Some(peb_fb.guest_heap_data_ptr()).filter(|&v| v != 0),
153+
guest_heap_data_size: Some(peb_fb.guest_heap_data_size()).filter(|&v| v != 0),
154+
guest_stack_data_ptr: Some(peb_fb.guest_stack_data_ptr()).filter(|&v| v != 0),
155+
guest_stack_data_size: Some(peb_fb.guest_stack_data_size()).filter(|&v| v != 0),
156+
})
157+
}
158+
}
159+
122160
impl TryFrom<HyperlightPEB> for Vec<u8> {
123161
type Error = anyhow::Error;
124162

@@ -164,3 +202,56 @@ impl TryFrom<HyperlightPEB> for Vec<u8> {
164202
Ok(res)
165203
}
166204
}
205+
206+
#[cfg(test)]
207+
mod tests {
208+
use alloc::vec::Vec;
209+
use anyhow::Result;
210+
use crate::flatbuffer_wrappers::hyperlight_peb::{HyperlightPEB, RunMode};
211+
212+
#[test]
213+
fn test_hyperlight_peb_to_vec_u8() -> Result<()> {
214+
let peb = HyperlightPEB {
215+
guest_function_dispatch_ptr: Some(0x1000),
216+
host_function_details_ptr: Some(0x2000),
217+
host_function_details_size: Some(64),
218+
guest_error_data_ptr: Some(0x3000),
219+
guest_error_data_size: Some(128),
220+
outb_ptr: Some(0x4000),
221+
run_mode: Some(RunMode::Hypervisor),
222+
input_data_ptr: Some(0x5000),
223+
input_data_size: Some(256),
224+
output_data_ptr: Some(0x6000),
225+
output_data_size: Some(512),
226+
guest_panic_context_ptr: Some(0x7000),
227+
guest_panic_context_size: Some(32),
228+
guest_heap_data_ptr: Some(0x8000),
229+
guest_heap_data_size: Some(1024),
230+
guest_stack_data_ptr: Some(0x9000),
231+
guest_stack_data_size: Some(2048),
232+
};
233+
234+
let buffer: Vec<u8> = peb.try_into()?;
235+
let parsed_peb = HyperlightPEB::try_from(buffer.as_slice())?;
236+
237+
assert_eq!(parsed_peb.guest_function_dispatch_ptr, Some(0x1000));
238+
assert_eq!(parsed_peb.host_function_details_ptr, Some(0x2000));
239+
assert_eq!(parsed_peb.host_function_details_size, Some(64));
240+
assert_eq!(parsed_peb.guest_error_data_ptr, Some(0x3000));
241+
assert_eq!(parsed_peb.guest_error_data_size, Some(128));
242+
assert_eq!(parsed_peb.outb_ptr, Some(0x4000));
243+
assert_eq!(parsed_peb.run_mode, Some(RunMode::Hypervisor));
244+
assert_eq!(parsed_peb.input_data_ptr, Some(0x5000));
245+
assert_eq!(parsed_peb.input_data_size, Some(256));
246+
assert_eq!(parsed_peb.output_data_ptr, Some(0x6000));
247+
assert_eq!(parsed_peb.output_data_size, Some(512));
248+
assert_eq!(parsed_peb.guest_panic_context_ptr, Some(0x7000));
249+
assert_eq!(parsed_peb.guest_panic_context_size, Some(32));
250+
assert_eq!(parsed_peb.guest_heap_data_ptr, Some(0x8000));
251+
assert_eq!(parsed_peb.guest_heap_data_size, Some(1024));
252+
assert_eq!(parsed_peb.guest_stack_data_ptr, Some(0x9000));
253+
assert_eq!(parsed_peb.guest_stack_data_size, Some(2048));
254+
255+
Ok(())
256+
}
257+
}

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 90 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
16+
use alloc::vec::Vec;
1717
use core::arch::asm;
1818

1919
use spin::Once;
20-
use hyperlight_common::flatbuffer_wrappers::hyperlight_peb::RunMode;
21-
use crate::{
22-
__security_cookie, PEB, RUNNING_MODE,
23-
};
20+
use hyperlight_common::flatbuffer_wrappers::hyperlight_peb::{HyperlightPEB, RunMode};
21+
use crate::{__security_cookie, HEAP_ALLOCATOR, PEB, RUNNING_MODE};
22+
use crate::guest_function_call::dispatch_function;
2423

2524
#[inline(never)]
2625
pub fn halt() {
@@ -32,7 +31,6 @@ pub fn halt() {
3231
}
3332

3433
// TODO(danbugs:297): delete
35-
pub fn dummy() {}
3634
#[no_mangle]
3735
pub fn __chkstk() {}
3836

@@ -61,11 +59,11 @@ pub fn __chkstk() {}
6159
// outb(OutBAction::Abort as u16, code as u8);
6260
// unreachable!()
6361
// }
64-
//
65-
// extern "C" {
66-
// fn hyperlight_main();
67-
// fn srand(seed: u32);
68-
// }
62+
63+
extern "C" {
64+
fn hyperlight_main();
65+
// fn srand(seed: u32);
66+
}
6967

7068
static INIT: Once = Once::new();
7169

@@ -80,75 +78,91 @@ pub extern "win64" fn entrypoint(
8078
) {
8179
INIT.call_once(|| {
8280
unsafe {
83-
let peb_slice: &[u8] = core::slice::from_raw_parts(
84-
hyperlight_peb_ptr as *const u8,
81+
let peb_slice: &mut [u8] = core::slice::from_raw_parts_mut(
82+
hyperlight_peb_ptr as *mut u8,
8583
hyperlight_peb_size as usize,
8684
);
87-
PEB = Some(peb_slice.try_into().unwrap());
88-
let val = PEB.clone().unwrap().run_mode.unwrap();
89-
90-
__security_cookie = hyperlight_peb_ptr ^ seed;
91-
// let srand_seed = ((hyperlight_peb_ptr << 8 ^ seed >> 4) >> 32) as u32;
92-
// // Set the seed for the random number generator for C code using rand;
93-
// srand(srand_seed);
94-
//
95-
// // set up the logger
96-
// let max_log_level = LevelFilter::iter()
97-
// .nth(max_log_level as usize)
98-
// .expect("Invalid log level");
99-
// init_logger(max_log_level);
100-
//
101-
// match (*peb_ptr).runMode {
102-
// RunMode::Hypervisor => {
103-
RUNNING_MODE = RunMode::Hypervisor;
104-
// // This static is to make it easier to implement the __chkstk function in assembly.
105-
// // It also means that should we change the layout of the struct in the future, we
106-
// // don't have to change the assembly code.
107-
// MIN_STACK_ADDRESS = (*peb_ptr).gueststackData.minUserStackAddress;
108-
//
109-
// // Setup GDT and IDT
110-
// load_gdt();
111-
// load_idt();
112-
// }
113-
// RunMode::InProcessLinux | RunMode::InProcessWindows => {
114-
// RUNNING_MODE = (*peb_ptr).runMode;
115-
//
116-
// OUTB_PTR = {
117-
// let outb_ptr: extern "win64" fn(u16, u8) =
118-
// core::mem::transmute((*peb_ptr).pOutb);
119-
// Some(outb_ptr)
120-
// };
121-
//
122-
// if (*peb_ptr).pOutbContext.is_null() {
123-
// panic!("OutbContext is null");
124-
// }
125-
//
126-
// OUTB_PTR_WITH_CONTEXT = {
127-
// let outb_ptr_with_context: extern "win64" fn(*mut c_void, u16, u8) =
128-
// core::mem::transmute((*peb_ptr).pOutb);
129-
// Some(outb_ptr_with_context)
130-
// };
131-
// }
132-
// _ => {
133-
// panic!("Invalid runmode in PEB");
134-
// }
135-
// }
136-
//
137-
// let heap_start = (*peb_ptr).guestheapData.guestHeapBuffer as usize;
138-
// let heap_size = (*peb_ptr).guestheapData.guestHeapSize as usize;
139-
// HEAP_ALLOCATOR
140-
// .try_lock()
141-
// .expect("Failed to access HEAP_ALLOCATOR")
142-
// .init(heap_start, heap_size);
143-
//
144-
//
145-
// (*peb_ptr).guest_function_dispatch_ptr = dispatch_function as usize as u64;
146-
//
147-
// reset_error();
148-
//
149-
// hyperlight_main();
85+
let mut peb: HyperlightPEB = peb_slice.try_into().unwrap();
86+
let run_mode = peb.run_mode.clone().unwrap();
87+
88+
__security_cookie = seed;
89+
90+
let heap_start = peb.guest_heap_data_ptr.unwrap() as usize;
91+
let heap_size = peb.guest_heap_data_size.unwrap() as usize;
92+
HEAP_ALLOCATOR
93+
.try_lock()
94+
.expect("Failed to access HEAP_ALLOCATOR")
95+
.init(heap_start, heap_size);
96+
97+
match run_mode {
98+
RunMode::Hypervisor => {
99+
RUNNING_MODE = RunMode::Hypervisor;
100+
}
101+
RunMode::InProcessLinux | RunMode::InProcessWindows => {
102+
RUNNING_MODE = run_mode;
103+
}
104+
_ => {
105+
panic!("Invalid runmode in PEB");
106+
}
107+
}
108+
109+
dispatch_function();
110+
111+
peb.guest_function_dispatch_ptr = Some(dispatch_function as usize as u64);
112+
PEB = Some(peb.clone());
113+
let peb_serialized: Vec<u8> = peb.try_into().unwrap();
114+
core::slice::from_raw_parts_mut(hyperlight_peb_ptr as *mut u8, peb_serialized.len())
115+
.copy_from_slice(&peb_serialized);
116+
117+
hyperlight_main();
150118
}
151119
});
152120

153121
halt();
154122
}
123+
124+
// let srand_seed = ((hyperlight_peb_ptr << 8 ^ seed >> 4) >> 32) as u32;
125+
// // Set the seed for the random number generator for C code using rand;
126+
// srand(srand_seed);
127+
//
128+
// // set up the logger
129+
// let max_log_level = LevelFilter::iter()
130+
// .nth(max_log_level as usize)
131+
// .expect("Invalid log level");
132+
// init_logger(max_log_level);
133+
134+
// // This static is to make it easier to implement the __chkstk function in assembly.
135+
// // It also means that should we change the layout of the struct in the future, we
136+
// // don't have to change the assembly code.
137+
// MIN_STACK_ADDRESS = (*peb_ptr).gueststackData.minUserStackAddress;
138+
//
139+
// // Setup GDT and IDT
140+
// load_gdt();
141+
// load_idt();
142+
143+
//
144+
// OUTB_PTR = {
145+
// let outb_ptr: extern "win64" fn(u16, u8) =
146+
// core::mem::transmute((*peb_ptr).pOutb);
147+
// Some(outb_ptr)
148+
// };
149+
//
150+
// if (*peb_ptr).pOutbContext.is_null() {
151+
// panic!("OutbContext is null");
152+
// }
153+
//
154+
// OUTB_PTR_WITH_CONTEXT = {
155+
// let outb_ptr_with_context: extern "win64" fn(*mut c_void, u16, u8) =
156+
// core::mem::transmute((*peb_ptr).pOutb);
157+
// Some(outb_ptr_with_context)
158+
// };
159+
//
160+
// let heap_start = (*peb_ptr).guestheapData.guestHeapBuffer as usize;
161+
// let heap_size = (*peb_ptr).guestheapData.guestHeapSize as usize;
162+
// HEAP_ALLOCATOR
163+
// .try_lock()
164+
// .expect("Failed to access HEAP_ALLOCATOR")
165+
// .init(heap_start, heap_size);
166+
//
167+
//
168+
// reset_error();

0 commit comments

Comments
 (0)