Skip to content

Commit 4cae8b5

Browse files
committed
Add back tests that were deleted
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 354d276 commit 4cae8b5

File tree

4 files changed

+282
-4
lines changed

4 files changed

+282
-4
lines changed

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test-unit target=default-target features="":
8383
test-isolated target=default-target features="":
8484
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::uninitialized::tests::test_trace_trace --exact --ignored
8585
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::uninitialized::tests::test_log_trace --exact --ignored
86-
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- hypervisor::hypervisor_handler::tests::create_1000_sandboxes --exact --ignored
86+
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::initialized_multi_use::tests::create_1000_sandboxes --exact --ignored
8787
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- sandbox::outb::tests::test_log_outb_log --exact --ignored
8888
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --lib -- mem::shared_mem::tests::test_drop --exact --ignored
8989
cargo test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} -p hyperlight-host --test integration_test -- log_message --exact --ignored

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,76 @@ impl InterruptHandle for LinuxInterruptHandle {
363363
self.dropped.load(Ordering::Relaxed)
364364
}
365365
}
366+
367+
#[cfg(all(test, any(target_os = "windows", kvm)))]
368+
pub(crate) mod tests {
369+
use std::sync::{Arc, Mutex};
370+
371+
use hyperlight_testing::dummy_guest_as_string;
372+
373+
use super::handlers::{MemAccessHandler, OutBHandler};
374+
#[cfg(gdb)]
375+
use crate::hypervisor::DbgMemAccessHandlerCaller;
376+
use crate::mem::ptr::RawPtr;
377+
use crate::sandbox::uninitialized::GuestBinary;
378+
use crate::sandbox::uninitialized_evolve::set_up_hypervisor_partition;
379+
use crate::sandbox::UninitializedSandbox;
380+
use crate::{is_hypervisor_present, new_error, Result};
381+
382+
#[cfg(gdb)]
383+
struct DbgMemAccessHandler {}
384+
385+
#[cfg(gdb)]
386+
impl DbgMemAccessHandlerCaller for DbgMemAccessHandler {
387+
fn read(&mut self, _offset: usize, _data: &mut [u8]) -> Result<()> {
388+
Ok(())
389+
}
390+
391+
fn write(&mut self, _offset: usize, _data: &[u8]) -> Result<()> {
392+
Ok(())
393+
}
394+
395+
fn get_code_offset(&mut self) -> Result<usize> {
396+
Ok(0)
397+
}
398+
}
399+
400+
#[test]
401+
fn test_initialise() -> Result<()> {
402+
if !is_hypervisor_present() {
403+
return Ok(());
404+
}
405+
406+
let outb_handler: Arc<Mutex<OutBHandler>> = {
407+
let func: Box<dyn FnMut(u16, u32) -> Result<()> + Send> =
408+
Box::new(|_, _| -> Result<()> { Ok(()) });
409+
Arc::new(Mutex::new(OutBHandler::from(func)))
410+
};
411+
let mem_access_handler = {
412+
let func: Box<dyn FnMut() -> Result<()> + Send> = Box::new(|| -> Result<()> { Ok(()) });
413+
Arc::new(Mutex::new(MemAccessHandler::from(func)))
414+
};
415+
#[cfg(gdb)]
416+
let dbg_mem_access_handler = Arc::new(Mutex::new(DbgMemAccessHandler {}));
417+
418+
let filename = dummy_guest_as_string().map_err(|e| new_error!("{}", e))?;
419+
420+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(filename.clone()), None)?;
421+
let (_hshm, mut gshm) = sandbox.mgr.build();
422+
let mut vm = set_up_hypervisor_partition(
423+
&mut gshm,
424+
#[cfg(gdb)]
425+
&sandbox.debug_info,
426+
)?;
427+
vm.initialise(
428+
RawPtr::from(0x230000),
429+
1234567890,
430+
4096,
431+
outb_handler,
432+
mem_access_handler,
433+
None,
434+
#[cfg(gdb)]
435+
dbg_mem_access_handler,
436+
)
437+
}
438+
}

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 207 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,22 @@ where
295295

296296
#[cfg(test)]
297297
mod tests {
298+
use std::sync::{Arc, Barrier};
299+
use std::thread;
300+
298301
use hyperlight_common::flatbuffer_wrappers::function_types::{
299302
ParameterValue, ReturnType, ReturnValue,
300303
};
301-
use hyperlight_testing::simple_guest_as_string;
304+
use hyperlight_testing::{callback_guest_as_string, simple_guest_as_string};
302305

303306
use crate::func::call_ctx::MultiUseGuestCallContext;
304307
use crate::sandbox::SandboxConfiguration;
305308
use crate::sandbox_state::sandbox::{DevolvableSandbox, EvolvableSandbox};
306309
use crate::sandbox_state::transition::{MultiUseContextCallback, Noop};
307-
use crate::{GuestBinary, MultiUseSandbox, UninitializedSandbox};
310+
use crate::{
311+
is_hypervisor_present, GuestBinary, HyperlightError, MultiUseSandbox, Result,
312+
UninitializedSandbox,
313+
};
308314

309315
// Tests to ensure that many (1000) function calls can be made in a call context with a small stack (1K) and heap(14K).
310316
// This test effectively ensures that the stack is being properly reset after each call and we are not leaking memory in the Guest.
@@ -384,4 +390,203 @@ mod tests {
384390
.unwrap();
385391
assert_eq!(res, ReturnValue::Int(0));
386392
}
393+
394+
#[test]
395+
// TODO: Investigate why this test fails with an incorrect error when run alongside other tests
396+
#[ignore]
397+
#[cfg(target_os = "linux")]
398+
fn test_violate_seccomp_filters() -> Result<()> {
399+
if !is_hypervisor_present() {
400+
panic!("Panic on create_multi_use_sandbox because no hypervisor is present");
401+
}
402+
403+
fn make_get_pid_syscall() -> Result<u64> {
404+
let pid = unsafe { libc::syscall(libc::SYS_getpid) };
405+
Ok(pid as u64)
406+
}
407+
408+
// First, run to make sure it fails.
409+
{
410+
let mut usbox = UninitializedSandbox::new(
411+
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
412+
None,
413+
)
414+
.unwrap();
415+
416+
usbox.register("MakeGetpidSyscall", make_get_pid_syscall)?;
417+
418+
let mut sbox: MultiUseSandbox = usbox.evolve(Noop::default())?;
419+
420+
let res =
421+
sbox.call_guest_function_by_name("ViolateSeccompFilters", ReturnType::ULong, None);
422+
423+
#[cfg(feature = "seccomp")]
424+
match res {
425+
Ok(_) => panic!("Expected to fail due to seccomp violation"),
426+
Err(e) => match e {
427+
HyperlightError::DisallowedSyscall => {}
428+
_ => panic!("Expected DisallowedSyscall error: {}", e),
429+
},
430+
}
431+
432+
#[cfg(not(feature = "seccomp"))]
433+
match res {
434+
Ok(_) => (),
435+
Err(e) => panic!("Expected to succeed without seccomp: {}", e),
436+
}
437+
}
438+
439+
// Second, run with allowing `SYS_getpid`
440+
#[cfg(feature = "seccomp")]
441+
{
442+
let mut usbox = UninitializedSandbox::new(
443+
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
444+
None,
445+
)
446+
.unwrap();
447+
448+
usbox.register_with_extra_allowed_syscalls(
449+
"MakeGetpidSyscall",
450+
make_get_pid_syscall,
451+
vec![libc::SYS_getpid],
452+
)?;
453+
// ^^^ note, we are allowing SYS_getpid
454+
455+
let mut sbox: MultiUseSandbox = usbox.evolve(Noop::default())?;
456+
457+
let res =
458+
sbox.call_guest_function_by_name("ViolateSeccompFilters", ReturnType::ULong, None);
459+
460+
match res {
461+
Ok(_) => {}
462+
Err(e) => panic!("Expected to succeed due to seccomp violation: {}", e),
463+
}
464+
}
465+
466+
Ok(())
467+
}
468+
469+
// This test is to capture the case where the guest execution is running a host function when cancelled and that host function
470+
// is never going to return.
471+
// The host function that is called will end after 5 seconds, but by this time the cancellation will have given up
472+
// (using default timeout settings) , so this tests looks for the error "Failed to cancel guest execution".
473+
#[test]
474+
#[ignore = "We cannot cancel host functions. TODO reenable this test when it's enabled"]
475+
fn test_terminate_vcpu_calling_host_spinning_cpu() {
476+
// This test relies upon a Hypervisor being present so for now
477+
// we will skip it if there isn't one.
478+
if !is_hypervisor_present() {
479+
println!("Skipping test_call_guest_function_by_name because no hypervisor is present");
480+
return;
481+
}
482+
let mut usbox = UninitializedSandbox::new(
483+
GuestBinary::FilePath(callback_guest_as_string().expect("Guest Binary Missing")),
484+
None,
485+
)
486+
.unwrap();
487+
488+
// Make this host call run for 5 seconds
489+
490+
fn spin() -> Result<()> {
491+
thread::sleep(std::time::Duration::from_secs(5));
492+
Ok(())
493+
}
494+
495+
#[cfg(any(target_os = "windows", not(feature = "seccomp")))]
496+
usbox.register("Spin", spin).unwrap();
497+
498+
#[cfg(all(target_os = "linux", feature = "seccomp"))]
499+
usbox
500+
.register_with_extra_allowed_syscalls("Spin", spin, vec![libc::SYS_clock_nanosleep])
501+
.unwrap();
502+
503+
let sandbox: MultiUseSandbox = usbox.evolve(Noop::default()).unwrap();
504+
let mut ctx = sandbox.new_call_context();
505+
let result = ctx.call("CallHostSpin", ReturnType::Void, None);
506+
507+
assert!(result.is_err());
508+
match result.unwrap_err() {
509+
HyperlightError::GuestExecutionHungOnHostFunctionCall() => {}
510+
e => panic!(
511+
"Expected HyperlightError::GuestExecutionHungOnHostFunctionCall but got {:?}",
512+
e
513+
),
514+
}
515+
}
516+
517+
#[test]
518+
fn test_trigger_exception_on_guest() {
519+
let usbox = UninitializedSandbox::new(
520+
GuestBinary::FilePath(simple_guest_as_string().expect("Guest Binary Missing")),
521+
None,
522+
)
523+
.unwrap();
524+
525+
let mut multi_use_sandbox: MultiUseSandbox = usbox.evolve(Noop::default()).unwrap();
526+
527+
let res = multi_use_sandbox.call_guest_function_by_name(
528+
"TriggerException",
529+
ReturnType::Void,
530+
None,
531+
);
532+
533+
assert!(res.is_err());
534+
535+
match res.unwrap_err() {
536+
HyperlightError::GuestAborted(_, msg) => {
537+
// msg should indicate we got an invalid opcode exception
538+
assert!(msg.contains("InvalidOpcode"));
539+
}
540+
e => panic!(
541+
"Expected HyperlightError::GuestExecutionError but got {:?}",
542+
e
543+
),
544+
}
545+
}
546+
547+
#[test]
548+
#[ignore] // this test runs by itself because it uses a lot of system resources
549+
fn create_1000_sandboxes() {
550+
let barrier = Arc::new(Barrier::new(21));
551+
552+
let mut handles = vec![];
553+
554+
for _ in 0..20 {
555+
let c = barrier.clone();
556+
557+
let handle = thread::spawn(move || {
558+
c.wait();
559+
560+
for _ in 0..50 {
561+
let usbox = UninitializedSandbox::new(
562+
GuestBinary::FilePath(
563+
simple_guest_as_string().expect("Guest Binary Missing"),
564+
),
565+
None,
566+
)
567+
.unwrap();
568+
569+
let mut multi_use_sandbox: MultiUseSandbox =
570+
usbox.evolve(Noop::default()).unwrap();
571+
572+
let res = multi_use_sandbox.call_guest_function_by_name(
573+
"GetStatic",
574+
ReturnType::Int,
575+
None,
576+
);
577+
578+
assert!(res.is_ok());
579+
assert_eq!(res.unwrap(), ReturnValue::Int(0));
580+
}
581+
});
582+
583+
handles.push(handle);
584+
}
585+
586+
barrier.wait();
587+
588+
for handle in handles {
589+
handle.join().unwrap();
590+
}
591+
}
387592
}

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub(super) fn evolve_impl_multi_use(u_sbox: UninitializedSandbox) -> Result<Mult
141141
)
142142
}
143143

144-
fn set_up_hypervisor_partition(
144+
pub(crate) fn set_up_hypervisor_partition(
145145
mgr: &mut SandboxMemoryManager<GuestSharedMemory>,
146146
#[cfg(gdb)] debug_info: &Option<DebugInfo>,
147147
) -> Result<Box<dyn Hypervisor>> {

0 commit comments

Comments
 (0)