diff --git a/CHANGELOG.md b/CHANGELOG.md index e4c94cada..2e3564a68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## [Prerelease] - Unreleased ### Added +* Add `SandboxConfiguration::set_executable_heap` and `SandboxBuilder::executable_heap` to choose heap execution per sandbox. The `executable_heap` Cargo feature sets the default. * Add `HYPERLIGHT_SURROGATE_EXTRACT_DIR` to configure the surrogate extraction directory on Windows. * Add `Sandbox::recreate()`, which rebuilds a sandbox's VM and memory from a snapshot. It recovers unrecoverable sandboxes and keeps configuration, host functions, and interrupt handles by @ludfjig in https://github.com/hyperlight-dev/hyperlight/pull/1751 @@ -17,13 +18,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). * `Snapshot::save` no longer synchronously flushes files to stable storage. Callers requiring power-loss durability must sync the snapshot files and directory hierarchy themselves after saving. -* **Breaking:** Snapshots use ABI 10 and config schema v6. Guests must be rebuilt - and existing snapshots regenerated. Guest entry takes the random seed, host - page size, and log level. +* **Breaking:** Snapshots use ABI 12 and config schema v6. Rebuild guests and + regenerate snapshots. Pass a pointer to `StartupArgs` at guest entry. * **Breaking:** Encode function calls with serde/postcard instead of FlatBuffers. * Stop resetting guest FPU state between guest calls to improve performance. ### Fixed +* Fix snapshot restores overriding the heap execution setting for newly faulted pages. * Fix surrogate process creation for Windows paths containing Unicode or spaces. * Fix restore on WHP not rewinding the guest TSC on hosts with `TSC_ADJUST` support. * HVF sandbox initialization accepts seeds that match Apple hypercall identifiers. diff --git a/Justfile b/Justfile index 624935419..963808113 100644 --- a/Justfile +++ b/Justfile @@ -299,7 +299,7 @@ test-isolated target=default-target features="" : # runs integration tests test-integration target=default-target features="": - @# run execute_on_heap test with feature "executable_heap" on (runs with off during normal tests) + @# Check the default heap permission with the executable_heap feature enabled. {{ cargo-cmd }} test {{ if features =="" {"--features executable_heap"} else if features=="no-default-features" {"--no-default-features --features executable_heap"} else {"--no-default-features -F executable_heap," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} --test integration_test execute_on_heap @# run component-util unit tests and the integration tests that exercise WIT input codegen diff --git a/docs/hyperlight-metrics-logs-and-traces.md b/docs/hyperlight-metrics-logs-and-traces.md index 179e12283..918e8148c 100644 --- a/docs/hyperlight-metrics-logs-and-traces.md +++ b/docs/hyperlight-metrics-logs-and-traces.md @@ -137,9 +137,9 @@ runs `no_std` which `opentelemetry` doesn't know. ##### Guest -When the guest starts executing the `entrypoint` function, it receives a `max_log_level` parameter that tells the guest what kind of logging level is expected from it. +At startup, `StartupArgs::max_log_level` sets the guest log filter. -The `trace_guest` logic takes advantage of this parameter and when the `max_log_level` is `trace`, it allocates a custom made `GuestSubscriber` that implements the `Subscriber` +When this field is `trace`, the `trace_guest` logic allocates a custom made `GuestSubscriber` that implements the `Subscriber` trait from `tracing_core` that allows defining a subscriber for the `tracing` crate to handle new spans and events. This custom subscriber stores the spans and events in a buffer initialized only when tracing is enabled. For each new span and event, a method is called on the custom subscriber which not only stores the data, but also keeps track of the hierarchy and dependencies between the other spans/events. diff --git a/docs/snapshot-versioning.md b/docs/snapshot-versioning.md index cca22ebc2..55304e374 100644 --- a/docs/snapshot-versioning.md +++ b/docs/snapshot-versioning.md @@ -59,6 +59,10 @@ host page size, and log level. Earlier snapshots must be regenerated. ABI 11 encodes control data with postcard and carries its length in a 16-byte message header. Earlier snapshots must be regenerated. +ABI 12 passes a pointer to `StartupArgs` at guest entry and reserves its storage +in scratch-top metadata. Initialized snapshots keep their heap execution +setting in guest memory. Earlier snapshots must be regenerated. + The `OCI_LAYOUT_VERSION` constant is pinned by the OCI image-layout spec at `1.0.0`. diff --git a/src/hyperlight_common/src/layout.rs b/src/hyperlight_common/src/layout.rs index 731f73fe9..2312d1e68 100644 --- a/src/hyperlight_common/src/layout.rs +++ b/src/hyperlight_common/src/layout.rs @@ -21,12 +21,24 @@ const EXN_STACK_ALIGNMENT: usize = 16; /// Pages reserved for the exception stack and scratch-top metadata. pub const SCRATCH_TOP_RESERVED_PAGES: usize = 12; +/// Host-written arguments consumed before guest heap initialization. +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +#[repr(C)] +pub struct StartupArgs { + pub seed: u64, + pub host_page_size: u64, + pub max_log_level: u64, + /// Nonzero if guest heap pages are mapped executable. + pub heap_executable: u64, +} + // Fields are listed in ascending-address order. Public offsets are measured // down from the top of scratch memory. #[repr(C)] struct ScratchTopMetadata { - /// Nonzero if guest heap pages are mapped executable. - heap_executable: u64, + startup_args: StartupArgs, + /// Keeps the exception stack 16-byte aligned. + _alignment_padding: [u8; 8], /// Host-published capacity of each H2G buffer. h2g_buffer_size: u64, /// Number of pages reserved for the H2G pool. @@ -85,8 +97,8 @@ pub const SCRATCH_TOP_LIBC_RNG_SEED_OFFSET: u64 = scratch_top_offset(offset_of!(ScratchTopMetadata, libc_rng_seed)); pub const SCRATCH_TOP_GUEST_LOG_LEVEL_OFFSET: u64 = scratch_top_offset(offset_of!(ScratchTopMetadata, guest_log_level)); -pub const SCRATCH_TOP_HEAP_EXECUTABLE_OFFSET: u64 = - scratch_top_offset(offset_of!(ScratchTopMetadata, heap_executable)); +pub const SCRATCH_TOP_STARTUP_ARGS_OFFSET: u64 = + scratch_top_offset(offset_of!(ScratchTopMetadata, startup_args)); pub const SCRATCH_TOP_EXN_STACK_OFFSET: u64 = size_of::() as u64; const _: () = { @@ -107,8 +119,13 @@ const _: () = { assert!(SCRATCH_TOP_H2G_QUEUE_SIZE_OFFSET == 0x58); assert!(SCRATCH_TOP_H2G_POOL_PAGES_OFFSET == 0x60); assert!(SCRATCH_TOP_H2G_BUFFER_SIZE_OFFSET == 0x68); - assert!(SCRATCH_TOP_HEAP_EXECUTABLE_OFFSET == 0x70); - assert!(SCRATCH_TOP_EXN_STACK_OFFSET == 0x70); + assert!(size_of::() == 0x20); + assert!(offset_of!(StartupArgs, seed) == 0x00); + assert!(offset_of!(StartupArgs, host_page_size) == 0x08); + assert!(offset_of!(StartupArgs, max_log_level) == 0x10); + assert!(offset_of!(StartupArgs, heap_executable) == 0x18); + assert!(SCRATCH_TOP_STARTUP_ARGS_OFFSET == 0x90); + assert!(SCRATCH_TOP_EXN_STACK_OFFSET == 0x90); assert!(VIRTQ_BUFFER_GVA_START < VIRTQ_BUFFER_GVA_END); assert!((VIRTQ_BUFFER_GVA_START as usize).is_multiple_of(crate::vmem::PAGE_SIZE)); diff --git a/src/hyperlight_guest/src/layout.rs b/src/hyperlight_guest/src/layout.rs index 2f8ea911c..a110568e2 100644 --- a/src/hyperlight_guest/src/layout.rs +++ b/src/hyperlight_guest/src/layout.rs @@ -58,9 +58,6 @@ pub fn guest_log_level_gva() -> *mut u64 { use hyperlight_common::layout::{SCRATCH_TOP_GUEST_LOG_LEVEL_OFFSET, SCRATCH_TOP_GVA}; (SCRATCH_TOP_GVA as u64 - SCRATCH_TOP_GUEST_LOG_LEVEL_OFFSET + 1) as *mut u64 } -pub fn heap_executable_gva() -> *mut u64 { - scratch_top_gva(hyperlight_common::layout::SCRATCH_TOP_HEAP_EXECUTABLE_OFFSET) -} pub use arch::{scratch_base_gpa, scratch_base_gva}; /// Exclusive upper GPA for the scratch page allocator: the transport arena, diff --git a/src/hyperlight_guest_bin/src/arch/aarch64/exception/handle.rs b/src/hyperlight_guest_bin/src/arch/aarch64/exception/handle.rs index 58656d02c..a9df10de8 100644 --- a/src/hyperlight_guest_bin/src/arch/aarch64/exception/handle.rs +++ b/src/hyperlight_guest_bin/src/arch/aarch64/exception/handle.rs @@ -106,9 +106,7 @@ fn handle_internal_fault(exn: Exception, far: u64, orig_mapping: Option unsafe { handle_stack_or_heap_fault(far, false) }; true } else if (HEAP_GVA_START..HEAP_GVA_END).contains(&far) { - // SAFETY: the host writes the scratch-top metadata before entry. - let executable = - unsafe { hyperlight_guest::layout::heap_executable_gva().read_volatile() } != 0; + let executable = crate::HEAP_EXECUTABLE.load(Ordering::Relaxed); // SAFETY: a translation fault in the heap region means // the page is unmapped, and exception handling is the // only page table writer. diff --git a/src/hyperlight_guest_bin/src/arch/aarch64/mod.rs b/src/hyperlight_guest_bin/src/arch/aarch64/mod.rs index b97de1840..16826063a 100644 --- a/src/hyperlight_guest_bin/src/arch/aarch64/mod.rs +++ b/src/hyperlight_guest_bin/src/arch/aarch64/mod.rs @@ -55,17 +55,26 @@ unsafe fn init_vbar() { /// Machine-specific initialisation; calls [`crate::generic_init`] /// once VBAR and the main stack have been set up +/// +/// # Safety +/// Call once with exclusive access to guest state. `args` must point to an aligned, +/// initialized `StartupArgs` that stays valid until `generic_init` copies it. #[unsafe(no_mangle)] -pub extern "C" fn entrypoint(seed: u64, ops: u64, max_log_level: u64) -> ! { +pub unsafe extern "C" fn entrypoint(args: *const hyperlight_common::layout::StartupArgs) -> ! { + // SAFETY: the caller guarantees exclusive startup and valid arguments. unsafe { init_vbar(); let stack_top = crate::init::init_stack(); - pivot_stack(seed, ops, max_log_level, stack_top); + pivot_stack(args, stack_top); } } unsafe extern "C" { - unsafe fn pivot_stack(seed: u64, ops: u64, max_log_level: u64, stack_top: u64) -> !; + /// # Safety + /// `args` must satisfy [`crate::generic_init`]'s safety requirements. + /// `stack_top` must be 16-byte aligned with enough writable stack space below it. + unsafe fn pivot_stack(args: *const hyperlight_common::layout::StartupArgs, stack_top: u64) + -> !; } core::arch::global_asm!(" @@ -77,7 +86,7 @@ core::arch::global_asm!(" msr SPSel, #1\n mov sp, x5\n msr SPSel, #0\n - mov sp, x3\n + mov sp, x1\n bl {generic_init}\n ldr x1, ={hlt_addr}\n str x0, [x1]\n diff --git a/src/hyperlight_guest_bin/src/arch/amd64/exception/handle.rs b/src/hyperlight_guest_bin/src/arch/amd64/exception/handle.rs index 2e7a3479a..b6ce9ef91 100644 --- a/src/hyperlight_guest_bin/src/arch/amd64/exception/handle.rs +++ b/src/hyperlight_guest_bin/src/arch/amd64/exception/handle.rs @@ -133,9 +133,7 @@ fn try_handle_internal_pagefault( return true; } if (HEAP_GVA_START..HEAP_GVA_END).contains(&gva) { - // SAFETY: the host writes the scratch-top metadata before entry. - let executable = - unsafe { hyperlight_guest::layout::heap_executable_gva().read_volatile() } != 0; + let executable = crate::HEAP_EXECUTABLE.load(Ordering::Relaxed); // SAFETY: the fault is not-present in the heap region, and // exception handling is the only page table writer. unsafe { handle_stack_or_heap_pagefault(gva, executable) }; diff --git a/src/hyperlight_guest_bin/src/arch/amd64/init.rs b/src/hyperlight_guest_bin/src/arch/amd64/init.rs index 044ad960f..3349cf8e3 100644 --- a/src/hyperlight_guest_bin/src/arch/amd64/init.rs +++ b/src/hyperlight_guest_bin/src/arch/amd64/init.rs @@ -93,8 +93,13 @@ unsafe fn init_tss(pc: *mut ProcCtrl) { /// Machine-specific initialisation; calls [`crate::generic_init`] /// once stack, CoW, etc have been set up. +/// +/// # Safety +/// Call once with exclusive access to guest state. `args` must point to an aligned, +/// initialized `StartupArgs` that stays valid until `generic_init` copies it. #[unsafe(no_mangle)] -pub extern "C" fn entrypoint(seed: u64, ops: u64, max_log_level: u64) { +pub unsafe extern "C" fn entrypoint(args: *const hyperlight_common::layout::StartupArgs) { + // SAFETY: the caller guarantees exclusive startup and valid arguments. unsafe { // Allocate a VA for processor control structures which must // survive snapshotting at the same VA. @@ -108,12 +113,16 @@ pub extern "C" fn entrypoint(seed: u64, ops: u64, max_log_level: u64) { // Architecture early init is complete! We pivot now to // executing on the main stack, and jump into generic // initialisation code in lib.rs - pivot_stack(seed, ops, max_log_level, stack_top); + pivot_stack(args, stack_top); } } unsafe extern "C" { - unsafe fn pivot_stack(seed: u64, ops: u64, max_log_level: u64, stack_top: u64) -> !; + /// # Safety + /// `args` must satisfy [`crate::generic_init`]'s safety requirements. + /// `stack_top` must be 16-byte aligned with enough writable stack space below it. + unsafe fn pivot_stack(args: *const hyperlight_common::layout::StartupArgs, stack_top: u64) + -> !; } // Mark this as the bottom-most frame for debuggers: @@ -129,7 +138,7 @@ core::arch::global_asm!(" pivot_stack:\n .cfi_startproc\n .cfi_undefined rip\n - mov rsp, rcx\n + mov rsp, rsi\n xor ebp, ebp\n call {generic_init}\n mov dx, {halt_port}\n diff --git a/src/hyperlight_guest_bin/src/lib.rs b/src/hyperlight_guest_bin/src/lib.rs index 8fd9cff22..a88c09245 100644 --- a/src/hyperlight_guest_bin/src/lib.rs +++ b/src/hyperlight_guest_bin/src/lib.rs @@ -6,6 +6,7 @@ extern crate alloc; use core::fmt::Write; +use core::sync::atomic::{AtomicBool, Ordering, compiler_fence}; use arch::dispatch::dispatch_function; use guest_function::register::GuestFunctionRegister; @@ -114,6 +115,8 @@ pub(crate) static HEAP_ALLOCATOR: GuestHeap = GuestHeap::new(Manual); #[global_allocator] pub(crate) static HEAP_ALLOCATOR: ProfiledLockedHeap = ProfiledLockedHeap(GuestHeap::new(Manual)); +pub(crate) static HEAP_EXECUTABLE: AtomicBool = AtomicBool::new(false); + pub(crate) static mut REGISTERED_GUEST_FUNCTIONS: GuestFunctionRegister = GuestFunctionRegister::new(); @@ -251,7 +254,19 @@ core::arch::global_asm!( /// Architecture-nonspecific initialisation: set up the heap, /// coordinate some addresses and configuration with the host, and run /// user initialisation -pub(crate) extern "C" fn generic_init(_seed: u64, ops: u64, max_log_level: u64) -> u64 { +/// +/// # Safety +/// Call once with exclusive access to guest state. `args` must point to an aligned, +/// initialized `StartupArgs` that stays valid until copied. +pub(crate) unsafe extern "C" fn generic_init( + args: *const hyperlight_common::layout::StartupArgs, +) -> u64 { + // SAFETY: the caller guarantees an aligned, initialized value at args. + let args = unsafe { args.read() }; + HEAP_EXECUTABLE.store(args.heap_executable != 0, Ordering::Relaxed); + // Heap initialization faults into handlers that read this flag. + compiler_fence(Ordering::SeqCst); + unsafe { #[cfg(not(all(feature = "mem_profile", target_arch = "x86_64")))] let heap_allocator = &HEAP_ALLOCATOR; @@ -276,11 +291,11 @@ pub(crate) extern "C" fn generic_init(_seed: u64, ops: u64, max_log_level: u64) #[cfg(feature = "libc")] unsafe { - srand(_seed as u32); + srand(args.seed as u32); } unsafe { - OS_PAGE_SIZE = ops as u32; + OS_PAGE_SIZE = args.host_page_size as u32; } // Prepare transport before logging or guest initialization code can use it. @@ -288,7 +303,7 @@ pub(crate) extern "C" fn generic_init(_seed: u64, ops: u64, max_log_level: u64) // set up the logger let guest_log_level_filter = - GuestLogFilter::try_from(max_log_level).expect("Invalid log level"); + GuestLogFilter::try_from(args.max_log_level).expect("Invalid log level"); init_logger(guest_log_level_filter.into()); // It is important that all the tracing events are produced after the tracing is initialized. diff --git a/src/hyperlight_host/src/hypervisor/hyperlight_vm/aarch64.rs b/src/hyperlight_host/src/hypervisor/hyperlight_vm/aarch64.rs index feeaf73d1..c547ef239 100644 --- a/src/hyperlight_host/src/hypervisor/hyperlight_vm/aarch64.rs +++ b/src/hyperlight_host/src/hypervisor/hyperlight_vm/aarch64.rs @@ -18,7 +18,6 @@ use crate::hypervisor::LinuxInterruptHandle; use crate::hypervisor::WindowsInterruptHandle; #[cfg(gdb)] use crate::hypervisor::gdb::{DebugCommChannel, DebugMsg, DebugResponse}; -use crate::hypervisor::hyperlight_vm::get_guest_log_filter; use crate::hypervisor::regs::{CommonRegisters, CommonSpecialRegisters}; #[cfg(hvf)] use crate::hypervisor::virtual_machine::hvf::HvfVm; @@ -112,15 +111,13 @@ impl HyperlightVm { seed: u64, mem_mgr: &mut SandboxMemoryManager, host_funcs: &Arc>, - guest_max_log_level: Option, + config: &SandboxConfiguration, ) -> Result<(), InitializeError> { let NextAction::Initialise(initialise) = self.next_action else { return Ok(()); }; let mut x: [u64; 31] = [0; 31]; - x[0] = seed; - x[1] = self.page_size as u64; - x[2] = get_guest_log_filter(guest_max_log_level); + x[0] = self.write_startup_args(seed, mem_mgr, config)?; let regs = CommonRegisters { pc: initialise, sp: self.rsp_gva, diff --git a/src/hyperlight_host/src/hypervisor/hyperlight_vm/mod.rs b/src/hyperlight_host/src/hypervisor/hyperlight_vm/mod.rs index b01b44200..a975f0b4a 100644 --- a/src/hyperlight_host/src/hypervisor/hyperlight_vm/mod.rs +++ b/src/hyperlight_host/src/hypervisor/hyperlight_vm/mod.rs @@ -39,6 +39,7 @@ use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags, MemoryRegionTyp use crate::mem::mgr::SandboxMemoryManager; use crate::mem::shared_mem::{GuestSharedMemory, HostSharedMemory, SharedMemory}; use crate::metrics::{METRIC_ERRONEOUS_VCPU_KICKS, METRIC_GUEST_CANCELLATION}; +use crate::sandbox::SandboxConfiguration; use crate::sandbox::host_funcs::FunctionRegistry; use crate::sandbox::outb::{HandleOutbError, handle_outb}; use crate::sandbox::snapshot::{NextAction, SnapshotMemoryBacking}; @@ -108,6 +109,25 @@ pub(super) fn get_guest_log_filter(guest_max_log_level: Option) -> GuestLogFilter::from(guest_log_level_filter).into() } +impl HyperlightVm { + fn write_startup_args( + &self, + seed: u64, + mem_mgr: &mut SandboxMemoryManager, + config: &SandboxConfiguration, + ) -> Result { + let args = hyperlight_common::layout::StartupArgs { + seed, + host_page_size: self.page_size as u64, + max_log_level: get_guest_log_filter(config.get_max_guest_log_level()), + heap_executable: u64::from(config.get_executable_heap()), + }; + mem_mgr + .write_startup_args(args) + .map_err(|error| InitializeError::WriteStartupArgs(Box::new(error))) + } +} + /// DispatchGuestCall error #[derive(Debug, thiserror::Error)] pub enum DispatchGuestCallError { @@ -164,6 +184,8 @@ impl DispatchGuestCallError { pub enum InitializeError { #[error("Failed to convert pointer: {0}")] ConvertPointer(String), + #[error("Failed to write guest startup arguments: {0}")] + WriteStartupArgs(Box), #[error("Failed to run vm: {0}")] Run(#[from] RunVmError), #[error("Failed to setup registers: {0}")] diff --git a/src/hyperlight_host/src/hypervisor/hyperlight_vm/x86_64.rs b/src/hyperlight_host/src/hypervisor/hyperlight_vm/x86_64.rs index dc194b9ba..62273e87e 100644 --- a/src/hyperlight_host/src/hypervisor/hyperlight_vm/x86_64.rs +++ b/src/hyperlight_host/src/hypervisor/hyperlight_vm/x86_64.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; use tracing::{Span, instrument}; +#[cfg(test)] use tracing_core::LevelFilter; use super::*; @@ -171,11 +172,12 @@ impl HyperlightVm { seed: u64, mem_mgr: &mut SandboxMemoryManager, host_funcs: &Arc>, - guest_max_log_level: Option, + config: &SandboxConfiguration, ) -> std::result::Result<(), InitializeError> { let NextAction::Initialise(initialise) = self.next_action else { return Ok(()); }; + let startup_args = self.write_startup_args(seed, mem_mgr, config)?; let regs = CommonRegisters { rip: initialise, @@ -188,9 +190,7 @@ impl HyperlightVm { rsp: self.rsp_gva - 8, // function args - rdi: seed, - rsi: self.page_size as u64, - rdx: get_guest_log_filter(guest_max_log_level), + rdi: startup_args, rflags: 1 << 1, ..Default::default() @@ -1491,7 +1491,8 @@ mod tests { let host_funcs = Arc::new(Mutex::new(FunctionRegistry::default())); - vm.initialise(seed, &mut hshm, &host_funcs, None).unwrap(); + vm.initialise(seed, &mut hshm, &host_funcs, &config) + .unwrap(); TestVmContext { vm, diff --git a/src/hyperlight_host/src/hypervisor/mod.rs b/src/hyperlight_host/src/hypervisor/mod.rs index 7320e124a..f768d453c 100644 --- a/src/hyperlight_host/src/hypervisor/mod.rs +++ b/src/hyperlight_host/src/hypervisor/mod.rs @@ -507,7 +507,8 @@ pub(crate) mod tests { let filename = dummy_guest_as_pathbuf(); - let config: SandboxConfiguration = Default::default(); + let mut config: SandboxConfiguration = Default::default(); + config.set_max_guest_log_level(tracing_core::LevelFilter::ERROR); let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(filename.clone()), Some(config))?; let (mut mem_mgr, gshm) = sandbox.mgr.build().unwrap(); @@ -526,10 +527,9 @@ pub(crate) mod tests { // The dummy guest halts only when it receives these values. let seed = 1234567890u64; let host_funcs = Arc::new(Mutex::new(FunctionRegistry::default())); - let guest_max_log_level = Some(tracing_core::LevelFilter::ERROR); // Test the initialise method - vm.initialise(seed, &mut mem_mgr, &host_funcs, guest_max_log_level) + vm.initialise(seed, &mut mem_mgr, &host_funcs, &config) .unwrap(); Ok(()) diff --git a/src/hyperlight_host/src/mem/mgr.rs b/src/hyperlight_host/src/mem/mgr.rs index 0d28c67b9..34c2e5138 100644 --- a/src/hyperlight_host/src/mem/mgr.rs +++ b/src/hyperlight_host/src/mem/mgr.rs @@ -1082,6 +1082,17 @@ impl SandboxMemoryManager { Ok((gsnapshot, gscratch)) } + pub(crate) fn write_startup_args( + &mut self, + args: hyperlight_common::layout::StartupArgs, + ) -> Result { + use hyperlight_common::layout::{SCRATCH_TOP_GVA, SCRATCH_TOP_STARTUP_ARGS_OFFSET}; + + let offset = self.scratch_mem.mem_size() - SCRATCH_TOP_STARTUP_ARGS_OFFSET as usize; + self.scratch_mem.write(offset, args)?; + Ok(SCRATCH_TOP_GVA as u64 + 1 - SCRATCH_TOP_STARTUP_ARGS_OFFSET) + } + #[inline] fn update_scratch_bookkeeping_item(&mut self, offset: u64, value: u64) -> Result<()> { let scratch_size = self.scratch_mem.mem_size(); @@ -1160,10 +1171,6 @@ impl SandboxMemoryManager { SCRATCH_TOP_SNAPSHOT_GENERATION_OFFSET, self.snapshot_count, )?; - self.update_scratch_bookkeeping_item( - SCRATCH_TOP_HEAP_EXECUTABLE_OFFSET, - cfg!(feature = "executable_heap") as u64, - )?; // Record the G2H and H2G queue sizes, pool page counts, and buffer sizes. self.update_scratch_bookkeeping_item( SCRATCH_TOP_G2H_QUEUE_SIZE_OFFSET, diff --git a/src/hyperlight_host/src/sandbox/builder.rs b/src/hyperlight_host/src/sandbox/builder.rs index def83caa2..c3788177e 100644 --- a/src/hyperlight_host/src/sandbox/builder.rs +++ b/src/hyperlight_host/src/sandbox/builder.rs @@ -118,6 +118,7 @@ impl SandboxBuilder { /// Build a sandbox restoring the guest from `snapshot`. /// /// The snapshot's layout overrides scratch and transport settings. + /// Initialized snapshots keep their executable heap setting. pub fn from_snapshot(snapshot: Arc) -> Self { Self::with_source(Source::Snapshot(snapshot)) } @@ -283,6 +284,16 @@ impl SandboxBuilder { self.cfg.get_scratch_size() } + /// Set whether guest heap pages are mapped executable. + /// + /// Initialized snapshots keep their setting and ignore this value. + /// + /// See [`SandboxConfiguration::set_executable_heap`]. + pub fn executable_heap(mut self, executable: bool) -> Self { + self.cfg.set_executable_heap(executable); + self + } + /// Set the G2H virtqueue descriptor count. /// /// See [`SandboxConfiguration::set_g2h_queue_size`] for normalization. diff --git a/src/hyperlight_host/src/sandbox/config.rs b/src/hyperlight_host/src/sandbox/config.rs index 0f70e2f34..697711449 100644 --- a/src/hyperlight_host/src/sandbox/config.rs +++ b/src/hyperlight_host/src/sandbox/config.rs @@ -83,6 +83,7 @@ pub struct SandboxConfiguration { g2h_pool_pages: usize, /// Number of pages in the H2G buffer pool. h2g_pool_pages: usize, + executable_heap: bool, /// Declared guest MSRs, stored inline to keep this type `Copy`. #[cfg(target_arch = "x86_64")] guest_msrs: [u32; Self::MAX_GUEST_MSRS], @@ -113,6 +114,8 @@ impl SandboxConfiguration { pub const DEFAULT_G2H_POOL_PAGES: usize = 12; /// The default total number of H2G pool pages. pub const DEFAULT_H2G_POOL_PAGES: usize = 8; + /// The `executable_heap` Cargo feature sets this default. + pub const DEFAULT_EXECUTABLE_HEAP: bool = cfg!(feature = "executable_heap"); /// The minimum G2H virtqueue descriptor count. const MIN_QUEUE_SIZE: usize = 2; /// The maximum G2H virtqueue descriptor count. @@ -146,6 +149,7 @@ impl SandboxConfiguration { h2g_buffer_size: Self::DEFAULT_H2G_BUFFER_SIZE, g2h_pool_pages: Self::DEFAULT_G2H_POOL_PAGES, h2g_pool_pages: Self::DEFAULT_H2G_POOL_PAGES, + executable_heap: Self::DEFAULT_EXECUTABLE_HEAP, interrupt_retry_delay, interrupt_vcpu_sigrtmin_offset, #[cfg(gdb)] @@ -281,6 +285,19 @@ impl SandboxConfiguration { self.scratch_size = scratch_size; } + /// Set whether guest heap pages are mapped executable at initialization. + /// + /// Initialized snapshots keep their setting. The guest controls its page + /// tables, so this permission is not a host security boundary. + #[instrument(skip_all, parent = Span::current(), level= "Trace")] + pub fn set_executable_heap(&mut self, executable: bool) { + self.executable_heap = executable; + } + + pub(crate) fn get_executable_heap(&self) -> bool { + self.executable_heap + } + /// Sets the maximum log level for guest code execution. /// /// If not set, the level is determined from the `RUST_LOG` environment diff --git a/src/hyperlight_host/src/sandbox/initialized.rs b/src/hyperlight_host/src/sandbox/initialized.rs index 6fe9ca3c9..a3e8df04f 100644 --- a/src/hyperlight_host/src/sandbox/initialized.rs +++ b/src/hyperlight_host/src/sandbox/initialized.rs @@ -333,7 +333,7 @@ impl Sandbox { // For NextAction::Call, initialise is a no-op. Runtime overrides are // delivered through the scratch-memory request below. - vm.initialise(seed, &mut hshm, &host_funcs, max_guest_log_level) + vm.initialise(seed, &mut hshm, &host_funcs, &config) .map_err(crate::hypervisor::hyperlight_vm::HyperlightVmError::Initialize)?; if matches!( snapshot.next_action(), diff --git a/src/hyperlight_host/src/sandbox/snapshot/file/media_types.rs b/src/hyperlight_host/src/sandbox/snapshot/file/media_types.rs index a87eb1999..126cb0c17 100644 --- a/src/hyperlight_host/src/sandbox/snapshot/file/media_types.rs +++ b/src/hyperlight_host/src/sandbox/snapshot/file/media_types.rs @@ -27,7 +27,7 @@ pub(in crate::sandbox::snapshot) const MT_TRANSPORT_CURRENT: &str = MT_TRANSPORT /// ABI version for the snapshot memory blob. Bumped when the /// host-guest contract for the snapshot bytes changes. See /// docs/snapshot-versioning.md. -pub(in crate::sandbox::snapshot) const SNAPSHOT_ABI_VERSION: u32 = 11; +pub(in crate::sandbox::snapshot) const SNAPSHOT_ABI_VERSION: u32 = 12; /// OCI standard annotation key for a manifest's tag inside an image /// index. Set on the manifest descriptor in `index.json`, not on the diff --git a/src/hyperlight_host/src/sandbox/snapshot/file_tests.rs b/src/hyperlight_host/src/sandbox/snapshot/file_tests.rs index 970bbf3b5..0dd295409 100644 --- a/src/hyperlight_host/src/sandbox/snapshot/file_tests.rs +++ b/src/hyperlight_host/src/sandbox/snapshot/file_tests.rs @@ -336,7 +336,7 @@ fn layered_writer_preserves_blobs_and_transport_geometry() { manifest["artifactType"], "application/vnd.hyperlight.snapshot.config.v6+json" ); - assert_eq!(config["abi_version"], 11); + assert_eq!(config["abi_version"], 12); let blobs = snapshot.snapshot_memory().blobs().collect::>(); assert_eq!(layers.len(), blobs.len() + 1); for (index, blob) in blobs.iter().enumerate() { @@ -2520,7 +2520,7 @@ fn legacy_config_versions_rejected() { &path, OciTag::new("latest").unwrap(), )); - assert_err_contains(err, "incompatible with snapshot ABI 11"); + assert_err_contains(err, "incompatible with snapshot ABI 12"); } } diff --git a/src/hyperlight_host/src/sandbox/snapshot/tripwires.rs b/src/hyperlight_host/src/sandbox/snapshot/tripwires.rs index f7a56dc7a..7f0f5bbbc 100644 --- a/src/hyperlight_host/src/sandbox/snapshot/tripwires.rs +++ b/src/hyperlight_host/src/sandbox/snapshot/tripwires.rs @@ -18,7 +18,7 @@ use super::file::{ SNAPSHOT_ABI_VERSION, }; -const EXPECTED_ABI_VERSION: u32 = 11; +const EXPECTED_ABI_VERSION: u32 = 12; const EXPECTED_MT_CONFIG: &str = "application/vnd.hyperlight.snapshot.config.v6+json"; const EXPECTED_MT_SNAPSHOT: &str = "application/vnd.hyperlight.snapshot.memory.v2"; const EXPECTED_MT_TRANSPORT: &str = "application/vnd.hyperlight.snapshot.transport.v1"; diff --git a/src/hyperlight_host/src/sandbox/uninitialized_evolve.rs b/src/hyperlight_host/src/sandbox/uninitialized_evolve.rs index 19102f3c1..1ca624f96 100644 --- a/src/hyperlight_host/src/sandbox/uninitialized_evolve.rs +++ b/src/hyperlight_host/src/sandbox/uninitialized_evolve.rs @@ -70,7 +70,7 @@ pub(super) fn initialize_sandbox(u_sbox: UninitializedSandbox) -> Result("ExecuteOnHeap", ()); + with_rust_sandbox(|mut sandbox| { + assert_heap_execution(&mut sandbox, cfg!(feature = "executable_heap")); + }); +} - #[cfg(feature = "executable_heap")] - assert_eq!( - result.unwrap(), - "Executed on heap successfully", - "should execute successfully" - ); +fn assert_heap_execution(sandbox: &mut Sandbox, executable: bool) { + let result = sandbox.call::("ExecuteOnHeap", ()); + if executable { + assert_eq!(result.unwrap(), "Executed on heap successfully"); + } else { + let err = result.unwrap_err(); + let HyperlightError::GuestAborted(code, message) = err else { + panic!("expected guest exception: {err:?}"); + }; + assert_eq!(code, ErrorCode::GuestError as u8, "{message}"); + + #[cfg(target_arch = "x86_64")] + let prefix = "Error code: 0x"; + #[cfg(target_arch = "aarch64")] + let prefix = "Exception Syndrome: 0x"; + let fault_code = message + .lines() + .find_map(|line| line.strip_prefix(prefix)) + .unwrap_or_else(|| panic!("missing fault code: {message}")); + let fault_code = u64::from_str_radix(fault_code, 16) + .unwrap_or_else(|err| panic!("invalid fault code: {err}: {message}")); + + #[cfg(target_arch = "x86_64")] + { + assert!( + message.starts_with("Exception: PageFault | "), + "expected page fault: {message}" + ); + assert_eq!( + fault_code, + (1 << 4) | 1, + "expected instruction-fetch protection fault: {message}" + ); + } + #[cfg(target_arch = "aarch64")] + { + use hyperlight_common::arch::exn::{ + Exception, InsnFault, InsnFaultKind, decode_syndrome, + }; - #[cfg(not(feature = "executable_heap"))] - assert!( - result.unwrap_err().to_string().contains( - #[cfg(target_arch = "x86_64")] - "PageFault", - #[cfg(target_arch = "aarch64")] - "Exception Syndrome: 0x86", - ), - "should get page fault" + assert!( + matches!( + decode_syndrome(fault_code), + Exception::InsnFault(InsnFault { + from_lower_el: false, + is_s1ptw: false, + kind: InsnFaultKind::PermissionFault(_), + }) + ), + "expected instruction permission fault: {message}" + ); + } + } +} + +#[test] +fn executable_heap_runtime_setting() { + for executable in [true, false] { + with_rust_sandbox_from( + |builder| builder.executable_heap(executable), + |mut sandbox| assert_heap_execution(&mut sandbox, executable), ); - }); + } +} + +#[test] +fn snapshot_keeps_executable_heap_setting() { + for executable in [true, false] { + let mut source = build_rust_sandbox(|builder| builder.executable_heap(executable)); + let snapshot = source.snapshot().unwrap(); + + let mut restored = SandboxBuilder::from_snapshot(snapshot.clone()) + .executable_heap(!executable) + .build() + .unwrap(); + assert_heap_execution(&mut restored, executable); + drop(restored); + drop(source); + + let mut target = build_rust_sandbox(|builder| builder.executable_heap(!executable)); + target.restore(snapshot.clone()).unwrap(); + assert_heap_execution(&mut target, executable); + + target.recreate(snapshot).unwrap(); + assert_heap_execution(&mut target, executable); + } +} + +#[test] +fn saved_snapshot_keeps_executable_heap_setting() { + for executable in [true, false] { + let mut source = build_rust_sandbox(|builder| builder.executable_heap(executable)); + let snapshot = source.snapshot().unwrap(); + let dir = tempfile::tempdir().unwrap(); + let tag = OciTag::new("latest").unwrap(); + snapshot.save(dir.path(), &tag).unwrap(); + drop(source); + drop(snapshot); + + let loaded = Arc::new(Snapshot::checked_load(dir.path(), tag).unwrap()); + let mut restored = SandboxBuilder::from_snapshot(loaded) + .executable_heap(!executable) + .build() + .unwrap(); + assert_heap_execution(&mut restored, executable); + } } // checks that a recursive function with stack allocation eventually fails with stackoverflow diff --git a/src/hyperlight_host/tests/snapshot_goldens/goldens_version.rs b/src/hyperlight_host/tests/snapshot_goldens/goldens_version.rs index c1b3f149b..4fc6ca7f6 100644 --- a/src/hyperlight_host/tests/snapshot_goldens/goldens_version.rs +++ b/src/hyperlight_host/tests/snapshot_goldens/goldens_version.rs @@ -8,7 +8,7 @@ //! publish. See `docs/snapshot-versioning.md`. /// Goldens version, a `vMAJOR.MINOR` string. -pub(crate) const GOLDENS_VERSION: &str = "v11.0"; +pub(crate) const GOLDENS_VERSION: &str = "v12.0"; /// Old majors kept loadable through a compatibility path, verified /// alongside `GOLDENS_VERSION`. A backwards-compatible break (Option 2) diff --git a/src/tests/rust_guests/dummyguest/src/main.rs b/src/tests/rust_guests/dummyguest/src/main.rs index 4d361723c..3d55fdfc9 100644 --- a/src/tests/rust_guests/dummyguest/src/main.rs +++ b/src/tests/rust_guests/dummyguest/src/main.rs @@ -47,8 +47,13 @@ fn mmio_read() { } #[allow(non_snake_case)] +/// # Safety +/// `args` must point to an aligned, initialized `[u64; 2]` until copied. #[no_mangle] -pub extern "C" fn entrypoint(seed: u64, page_size: u64) -> i32 { +pub unsafe extern "C" fn entrypoint(args: *const [u64; 2]) -> i32 { + // Raw words keep this guest independent of alloc. + // SAFETY: the caller guarantees two aligned, initialized argument words. + let [seed, page_size] = unsafe { args.read() }; if seed != 1234567890 || page_size != 4096 { mmio_read(); } diff --git a/src/tests/rust_guests/simpleguest/src/main.rs b/src/tests/rust_guests/simpleguest/src/main.rs index 99da90ed0..ff02d8f45 100644 --- a/src/tests/rust_guests/simpleguest/src/main.rs +++ b/src/tests/rust_guests/simpleguest/src/main.rs @@ -12,7 +12,6 @@ const MAX_BUFFER_SIZE: usize = 1024; extern crate alloc; -use alloc::boxed::Box; use alloc::string::{String, ToString}; use alloc::vec::Vec; use alloc::{format, vec}; @@ -735,20 +734,34 @@ fn test_guest_panic(message: String) { #[guest_function("ExecuteOnHeap")] fn execute_on_heap() -> String { + #[cfg(target_arch = "x86_64")] + let code = [0x90u8, 0xC3]; + #[cfg(target_arch = "aarch64")] + let code = [0x1fu8, 0x20, 0x03, 0xd5, 0xc0, 0x03, 0x5f, 0xd6]; + let layout = Layout::from_size_align(16 * PAGE_SIZE, PAGE_SIZE).unwrap(); + // SAFETY: the code fits within the live allocation and is cache-synced. + // NOP/RET preserves the registers required by the declared ABI. unsafe { - // NO-OP followed by RET - let mut heap_memory = Box::new( - #[cfg(target_arch = "x86_64")] - [0x90u8, 0xC3], - #[cfg(target_arch = "aarch64")] - [0x1f, 0x20, 0x03, 0xd5, 0xc0, 0x03, 0x5f, 0xd6], - ); - dicachesync(heap_memory.as_mut_ptr(), heap_memory.len()); - let heap_fn: fn() = core::mem::transmute(Box::into_raw(heap_memory)); + let allocation = alloc::alloc::alloc(layout); + if allocation.is_null() { + alloc::alloc::handle_alloc_error(layout); + } + // An unmapped page exercises first-touch permissions after restore. + let code_page = (0..16) + .map(|page| allocation.add(page * PAGE_SIZE)) + .find(|page| { + hyperlight_guest_bin::paging::virt_to_phys(*page as u64) + .next() + .is_none() + }) + .expect("heap execution test needs an untouched page"); + code_page.copy_from_nonoverlapping(code.as_ptr(), code.len()); + dicachesync(code_page, code.len()); + let heap_fn: unsafe extern "C" fn() = core::mem::transmute(code_page); heap_fn(); - black_box(heap_fn); // avoid optimization when running in release mode + black_box(heap_fn); + alloc::alloc::dealloc(allocation, layout); } - // will only reach this point if heap is executable String::from("Executed on heap successfully") }