Skip to content

Commit b7435ae

Browse files
committed
[customguest,host/{hypervisor/{mod,kvm},mem/{layout,memory_region,mgr},sandbox/config},testing/lib,Justfile,Cargo.toml,.gitignore]
added custom guest memory region and tmp custom guest Context: Instead of having a static memory layout set by the host, we want to provide the guest w/ a custom sized allocated undifferentiated memory region for it address in any way it wants. This commit starts that by adding the custom guest memory region. Changes: - added new section to SandboxMemoryLayout, and made getters for its offset and size. - changed get_total_page_table_size calculation to account for custom section. - added new section to builder in get_memory_regions. - wrote address of custom guest memory region to custom guest memory offset. - added new memory section to page table setup (i.e., set_up_shared_memory). - added function to get custom guest memory region as Vec<u8>. - made custom guest memory section size configurable via SandboxConfiguration. - fixed existing tests. - added new guest (customguest) to test custom setup. Modified Justfile to include it for easy builds too. - added new test for custom init with the new customguest. Signed-off-by: danbugs <[email protected]>
1 parent 975d98a commit b7435ae

File tree

14 files changed

+415
-88
lines changed

14 files changed

+415
-88
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,6 @@ hyperlight_guest.h
468468
.mono
469469

470470
!.gitkeep
471+
472+
# gdb
473+
.gdbinit

Diff for: Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ exclude = [
2121
"src/tests/rust_guests/callbackguest",
2222
"src/tests/rust_guests/dummyguest",
2323
"src/tests/rust_guests/simpleguest",
24+
# Note: This is a temporary guest to test
25+
# the new memory layout. Once I'm done w/
26+
# the work, I'll delete it. `simpleguest`
27+
# and `callbackguest` will be demonstrative
28+
# of the work by themselves.
29+
"src/tests/rust_guests/customguest",
2430
]
2531

2632
[workspace.package]

Diff for: Justfile

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ default-target := "debug"
1414
simpleguest_source := "src/tests/rust_guests/simpleguest/target/x86_64-unknown-none"
1515
simpleguest_msvc_source := "src/tests/rust_guests/simpleguest/target/x86_64-pc-windows-msvc"
1616
dummyguest_source := "src/tests/rust_guests/dummyguest/target/x86_64-unknown-none"
17+
customguest_source := "src/tests/rust_guests/customguest/target/x86_64-unknown-none"
1718
callbackguest_source := "src/tests/rust_guests/callbackguest/target/x86_64-unknown-none"
1819
callbackguest_msvc_source := "src/tests/rust_guests/callbackguest/target/x86_64-pc-windows-msvc"
1920
rust_guests_bin_dir := "src/tests/rust_guests/bin"
@@ -39,14 +40,16 @@ build-rust-guests target=default-target:
3940
cd src/tests/rust_guests/callbackguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }} --target=x86_64-pc-windows-msvc
4041
cd src/tests/rust_guests/simpleguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
4142
cd src/tests/rust_guests/simpleguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }} --target=x86_64-pc-windows-msvc
42-
cd src/tests/rust_guests/dummyguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
43+
cd src/tests/rust_guests/dummyguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
44+
cd src/tests/rust_guests/customguest && cargo build --profile={{ if target == "debug" { "dev" } else { target } }}
4345

4446
@move-rust-guests target=default-target:
4547
cp {{ callbackguest_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
4648
cp {{ callbackguest_msvc_source }}/{{ target }}/callbackguest* {{ rust_guests_bin_dir }}/{{ target }}/
4749
cp {{ simpleguest_source }}/{{ target }}/simpleguest* {{ rust_guests_bin_dir }}/{{ target }}/
4850
cp {{ simpleguest_msvc_source }}/{{ target }}/simpleguest* {{ rust_guests_bin_dir }}/{{ target }}/
4951
cp {{ dummyguest_source }}/{{ target }}/dummyguest* {{ rust_guests_bin_dir }}/{{ target }}/
52+
cp {{ customguest_source }}/{{ target }}/customguest* {{ rust_guests_bin_dir }}/{{ target }}/
5053

5154
build-and-move-rust-guests: (build-rust-guests "debug") (move-rust-guests "debug") (build-rust-guests "release") (move-rust-guests "release")
5255
build-and-move-c-guests: (build-c-guests "debug") (move-c-guests "debug") (build-c-guests "release") (move-c-guests "release")
@@ -69,6 +72,7 @@ clean-rust:
6972
cargo clean
7073
cd src/tests/rust_guests/simpleguest && cargo clean
7174
cd src/tests/rust_guests/dummyguest && cargo clean
75+
cd src/tests/rust_guests/customguest && cargo clean
7276
cd src/tests/rust_guests/callbackguest && cargo clean
7377
git clean -fdx src/tests/c_guests/bin src/tests/rust_guests/bin
7478

@@ -126,13 +130,15 @@ fmt-check:
126130
cargo +nightly fmt --manifest-path src/tests/rust_guests/callbackguest/Cargo.toml -- --check
127131
cargo +nightly fmt --manifest-path src/tests/rust_guests/simpleguest/Cargo.toml -- --check
128132
cargo +nightly fmt --manifest-path src/tests/rust_guests/dummyguest/Cargo.toml -- --check
133+
cargo +nightly fmt --manifest-path src/tests/rust_guests/customguest/Cargo.toml -- --check
129134
cargo +nightly fmt --manifest-path src/hyperlight_guest_capi/Cargo.toml -- --check
130135

131136
fmt-apply:
132137
cargo +nightly fmt --all
133138
cargo +nightly fmt --manifest-path src/tests/rust_guests/callbackguest/Cargo.toml
134139
cargo +nightly fmt --manifest-path src/tests/rust_guests/simpleguest/Cargo.toml
135140
cargo +nightly fmt --manifest-path src/tests/rust_guests/dummyguest/Cargo.toml
141+
cargo +nightly fmt --manifest-path src/tests/rust_guests/customguest/Cargo.toml
136142
cargo +nightly fmt --manifest-path src/hyperlight_guest_capi/Cargo.toml
137143

138144
clippy target=default-target:

Diff for: src/hyperlight_host/src/hypervisor/kvm.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ mod tests {
937937
#[cfg(gdb)]
938938
use crate::hypervisor::handlers::DbgMemAccessHandlerCaller;
939939
use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
940-
use crate::hypervisor::tests::test_initialise;
940+
use crate::hypervisor::tests::{test_custom_initialise, test_initialise};
941941
use crate::Result;
942942

943943
#[cfg(gdb)]
@@ -984,4 +984,31 @@ mod tests {
984984
)
985985
.unwrap();
986986
}
987+
988+
#[test]
989+
fn test_custom_init() {
990+
if !super::is_hypervisor_present() {
991+
return;
992+
}
993+
994+
let outb_handler: Arc<Mutex<OutBHandler>> = {
995+
let func: Box<dyn FnMut(u16, u64) -> Result<()> + Send> =
996+
Box::new(|_, _| -> Result<()> { Ok(()) });
997+
Arc::new(Mutex::new(OutBHandler::from(func)))
998+
};
999+
let mem_access_handler = {
1000+
let func: Box<dyn FnMut() -> Result<()> + Send> = Box::new(|| -> Result<()> { Ok(()) });
1001+
Arc::new(Mutex::new(MemAccessHandler::from(func)))
1002+
};
1003+
#[cfg(gdb)]
1004+
let dbg_mem_access_handler = Arc::new(Mutex::new(DbgMemAccessHandler {}));
1005+
1006+
test_custom_initialise(
1007+
outb_handler,
1008+
mem_access_handler,
1009+
#[cfg(gdb)]
1010+
dbg_mem_access_handler,
1011+
)
1012+
.unwrap();
1013+
}
9871014
}

Diff for: src/hyperlight_host/src/hypervisor/mod.rs

+62-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ pub(crate) mod tests {
307307
use std::sync::{Arc, Mutex};
308308
use std::time::Duration;
309309

310-
use hyperlight_testing::dummy_guest_as_string;
310+
use hyperlight_testing::{custom_guest_as_string, dummy_guest_as_string};
311311

312312
#[cfg(gdb)]
313313
use super::handlers::DbgMemAccessHandlerWrapper;
@@ -320,6 +320,67 @@ pub(crate) mod tests {
320320
use crate::sandbox::{SandboxConfiguration, UninitializedSandbox};
321321
use crate::{new_error, Result};
322322

323+
pub(crate) fn test_custom_initialise(
324+
outb_hdl: OutBHandlerWrapper,
325+
mem_access_hdl: MemAccessHandlerWrapper,
326+
#[cfg(gdb)] dbg_mem_access_fn: DbgMemAccessHandlerWrapper,
327+
) -> Result<()> {
328+
let filename = custom_guest_as_string().map_err(|e| new_error!("{}", e))?;
329+
let mut sbox_config = SandboxConfiguration::default();
330+
sbox_config.set_custom_guest_memory_size(0x2_000_000);
331+
let uninitialized_sandbox = UninitializedSandbox::new(
332+
GuestBinary::FilePath(filename.clone()),
333+
Some(sbox_config),
334+
None,
335+
None,
336+
)?;
337+
338+
let (hshm, gshm) = uninitialized_sandbox.mgr.build();
339+
340+
let custom_memory_offset = gshm.layout.get_custom_guest_memory_offset();
341+
let custom_memory_size = gshm.layout.get_custom_guest_memory_size();
342+
dbg!(&custom_memory_offset);
343+
344+
let custom_memory = hshm.as_ref().get_custom_guest_memory()?;
345+
assert_eq!(custom_memory[0], 0);
346+
347+
let hv_handler_config = HvHandlerConfig {
348+
outb_handler: outb_hdl,
349+
mem_access_handler: mem_access_hdl,
350+
#[cfg(gdb)]
351+
dbg_mem_access_handler: dbg_mem_access_fn,
352+
seed: custom_memory_size as u64, // re-using seed as custom_memory_size
353+
page_size: 4096,
354+
peb_addr: RawPtr::from(custom_memory_offset as u64),
355+
dispatch_function_addr: Arc::new(Mutex::new(None)),
356+
max_init_time: Duration::from_millis(
357+
SandboxConfiguration::DEFAULT_MAX_INITIALIZATION_TIME as u64,
358+
),
359+
max_exec_time: Duration::from_millis(
360+
SandboxConfiguration::DEFAULT_MAX_EXECUTION_TIME as u64,
361+
),
362+
max_wait_for_cancellation: Duration::from_millis(
363+
SandboxConfiguration::DEFAULT_MAX_WAIT_FOR_CANCELLATION as u64,
364+
),
365+
};
366+
367+
let mut hv_handler = HypervisorHandler::new(hv_handler_config);
368+
369+
hv_handler.start_hypervisor_handler(
370+
gshm,
371+
#[cfg(gdb)]
372+
None,
373+
)?;
374+
375+
hv_handler.execute_hypervisor_handler_action(HypervisorHandlerAction::Initialise)?;
376+
377+
let custom_memory = hshm.as_ref().get_custom_guest_memory()?;
378+
379+
assert_eq!(custom_memory[0], 1);
380+
381+
Ok(())
382+
}
383+
323384
pub(crate) fn test_initialise(
324385
outb_hdl: OutBHandlerWrapper,
325386
mem_access_hdl: MemAccessHandlerWrapper,

0 commit comments

Comments
 (0)