Skip to content

Commit c5aeada

Browse files
committed
add guest debug port to sandbox configuration
Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 1913aa5 commit c5aeada

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

src/hyperlight_host/src/hypervisor/hypervisor_handler.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ use crate::mem::shared_mem::{GuestSharedMemory, HostSharedMemory, SharedMemory};
4949
use crate::sandbox::hypervisor::{get_available_hypervisor, HypervisorType};
5050
#[cfg(feature = "function_call_metrics")]
5151
use crate::sandbox::metrics::SandboxMetric::GuestFunctionCallDurationMicroseconds;
52+
#[cfg(gdb)]
53+
use crate::sandbox::uninitialized::DebugInfo;
5254
#[cfg(target_os = "linux")]
5355
use crate::signal_handlers::setup_signal_handlers;
5456
use crate::HyperlightError::{
@@ -232,6 +234,7 @@ impl HypervisorHandler {
232234
pub(crate) fn start_hypervisor_handler(
233235
&mut self,
234236
sandbox_memory_manager: SandboxMemoryManager<GuestSharedMemory>,
237+
#[cfg(gdb)] debug_info: Option<DebugInfo>,
235238
) -> Result<()> {
236239
let configuration = self.configuration.clone();
237240
#[cfg(target_os = "windows")]
@@ -292,6 +295,8 @@ impl HypervisorHandler {
292295
hv = Some(set_up_hypervisor_partition(
293296
execution_variables.shm.try_lock().unwrap().deref_mut().as_mut().unwrap(),
294297
configuration.outb_handler.clone(),
298+
#[cfg(gdb)]
299+
&debug_info,
295300
)?);
296301
}
297302
let hv = hv.as_mut().unwrap();
@@ -827,6 +832,7 @@ fn set_up_hypervisor_partition(
827832
mgr: &mut SandboxMemoryManager<GuestSharedMemory>,
828833
#[allow(unused_variables)] // parameter only used for in-process mode
829834
outb_handler: OutBHandlerWrapper,
835+
#[cfg(gdb)] _debug_info: &Option<DebugInfo>,
830836
) -> Result<Box<dyn Hypervisor>> {
831837
let mem_size = u64::try_from(mgr.shared_mem.mem_size())?;
832838
let mut regions = mgr.layout.get_memory_regions(&mgr.shared_mem)?;

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,11 @@ pub(crate) mod tests {
336336
// whether we can configure the shared memory region, load a binary
337337
// into it, and run the CPU to completion (e.g., a HLT interrupt)
338338

339-
hv_handler.start_hypervisor_handler(gshm)?;
339+
hv_handler.start_hypervisor_handler(
340+
gshm,
341+
#[cfg(gdb)]
342+
None,
343+
)?;
340344

341345
hv_handler.execute_hypervisor_handler_action(HypervisorHandlerAction::Initialise)
342346
}

src/hyperlight_host/src/sandbox/config.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ use crate::mem::exe::ExeInfo;
2525
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2626
#[repr(C)]
2727
pub struct SandboxConfiguration {
28+
#[cfg(gdb)]
29+
/// Guest gdb debug port
30+
guest_debug_port: Option<u16>,
2831
/// The maximum size of the guest error buffer.
2932
guest_error_buffer_size: usize,
3033
/// The size of the memory buffer that is made available for Guest Function
@@ -136,6 +139,9 @@ impl SandboxConfiguration {
136139
pub const MIN_KERNEL_STACK_SIZE: usize = 0x1000;
137140
/// The default value for kernel stack size
138141
pub const DEFAULT_KERNEL_STACK_SIZE: usize = Self::MIN_KERNEL_STACK_SIZE;
142+
#[cfg(gdb)]
143+
/// The minimum value for debug port
144+
pub const MIN_GUEST_DEBUG_PORT: u16 = 9000;
139145

140146
#[allow(clippy::too_many_arguments)]
141147
/// Create a new configuration for a sandbox with the given sizes.
@@ -153,6 +159,7 @@ impl SandboxConfiguration {
153159
max_initialization_time: Option<Duration>,
154160
max_wait_for_cancellation: Option<Duration>,
155161
guest_panic_context_buffer_size: usize,
162+
#[cfg(gdb)] guest_debug_port: Option<u16>,
156163
) -> Self {
157164
Self {
158165
input_data_size: max(input_data_size, Self::MIN_INPUT_SIZE),
@@ -220,6 +227,8 @@ impl SandboxConfiguration {
220227
guest_panic_context_buffer_size,
221228
Self::MIN_GUEST_PANIC_CONTEXT_BUFFER_SIZE,
222229
),
230+
#[cfg(gdb)]
231+
guest_debug_port,
223232
}
224233
}
225234

@@ -346,6 +355,13 @@ impl SandboxConfiguration {
346355
);
347356
}
348357

358+
#[cfg(gdb)]
359+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
360+
/// Sets the configuration for the guest debug
361+
pub fn set_guest_debug_port(&mut self, port: u16) {
362+
self.guest_debug_port = Some(max(port, Self::MIN_GUEST_DEBUG_PORT));
363+
}
364+
349365
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
350366
pub(crate) fn get_guest_error_buffer_size(&self) -> usize {
351367
self.guest_error_buffer_size
@@ -390,6 +406,12 @@ impl SandboxConfiguration {
390406
self.max_initialization_time
391407
}
392408

409+
#[cfg(gdb)]
410+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
411+
pub(crate) fn get_guest_debug_port(&self) -> Option<u16> {
412+
self.guest_debug_port
413+
}
414+
393415
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
394416
fn stack_size_override_opt(&self) -> Option<u64> {
395417
(self.stack_size_override > 0).then_some(self.stack_size_override)
@@ -438,6 +460,8 @@ impl Default for SandboxConfiguration {
438460
None,
439461
None,
440462
Self::DEFAULT_GUEST_PANIC_CONTEXT_BUFFER_SIZE,
463+
#[cfg(gdb)]
464+
None,
441465
)
442466
}
443467
}
@@ -480,6 +504,8 @@ mod tests {
480504
MAX_WAIT_FOR_CANCELLATION_OVERRIDE as u64,
481505
)),
482506
GUEST_PANIC_CONTEXT_BUFFER_SIZE_OVERRIDE,
507+
#[cfg(gdb)]
508+
None,
483509
);
484510
let exe_infos = vec![
485511
simple_guest_exe_info().unwrap(),
@@ -543,6 +569,8 @@ mod tests {
543569
SandboxConfiguration::MIN_MAX_WAIT_FOR_CANCELLATION as u64 - 1,
544570
)),
545571
SandboxConfiguration::MIN_GUEST_PANIC_CONTEXT_BUFFER_SIZE - 1,
572+
#[cfg(gdb)]
573+
None,
546574
);
547575
assert_eq!(SandboxConfiguration::MIN_INPUT_SIZE, cfg.input_data_size);
548576
assert_eq!(SandboxConfiguration::MIN_OUTPUT_SIZE, cfg.output_data_size);
@@ -711,6 +739,14 @@ mod tests {
711739
cfg.set_heap_size(size);
712740
prop_assert_eq!(size, cfg.heap_size_override);
713741
}
742+
743+
#[test]
744+
#[cfg(gdb)]
745+
fn guest_debug_port(port in 9000..=u16::MAX) {
746+
let mut cfg = SandboxConfiguration::default();
747+
cfg.set_guest_debug_port(port);
748+
prop_assert_eq!(port, *cfg.get_guest_debug_port().as_ref().unwrap());
749+
}
714750
}
715751
}
716752
}

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ use crate::sandbox_state::sandbox::EvolvableSandbox;
3636
use crate::sandbox_state::transition::Noop;
3737
use crate::{log_build_details, log_then_return, new_error, MultiUseSandbox, Result};
3838

39+
#[allow(dead_code)]
40+
#[cfg(gdb)]
41+
/// Used for passing debug configuration to a sandbox
42+
pub struct DebugInfo {
43+
/// Guest debug port
44+
pub(crate) port: u16,
45+
}
46+
3947
/// A preliminary `Sandbox`, not yet ready to execute guest code.
4048
///
4149
/// Prior to initializing a full-fledged `Sandbox`, you must create one of
@@ -52,6 +60,8 @@ pub struct UninitializedSandbox {
5260
pub(crate) max_initialization_time: Duration,
5361
pub(crate) max_execution_time: Duration,
5462
pub(crate) max_wait_for_cancellation: Duration,
63+
#[cfg(gdb)]
64+
pub(crate) debug_info: Option<DebugInfo>,
5565
}
5666

5767
impl crate::sandbox_state::sandbox::UninitializedSandbox for UninitializedSandbox {
@@ -161,6 +171,10 @@ impl UninitializedSandbox {
161171
}
162172

163173
let sandbox_cfg = cfg.unwrap_or_default();
174+
#[cfg(gdb)]
175+
let debug_info = sandbox_cfg
176+
.get_guest_debug_port()
177+
.map(|port| DebugInfo { port });
164178
let mut mem_mgr_wrapper = {
165179
let mut mgr = UninitializedSandbox::load_guest_binary(
166180
sandbox_cfg,
@@ -188,6 +202,8 @@ impl UninitializedSandbox {
188202
max_wait_for_cancellation: Duration::from_millis(
189203
sandbox_cfg.get_max_wait_for_cancellation() as u64,
190204
),
205+
#[cfg(gdb)]
206+
debug_info,
191207
};
192208

193209
// TODO: These only here to accommodate some writer functions.

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use std::sync::{Arc, Mutex};
2020
use rand::Rng;
2121
use tracing::{instrument, Span};
2222

23+
#[cfg(gdb)]
24+
use super::uninitialized::DebugInfo;
2325
use crate::hypervisor::hypervisor_handler::{
2426
HvHandlerConfig, HypervisorHandler, HypervisorHandlerAction,
2527
};
@@ -66,6 +68,8 @@ where
6668
u_sbox.max_initialization_time,
6769
u_sbox.max_execution_time,
6870
u_sbox.max_wait_for_cancellation,
71+
#[cfg(gdb)]
72+
u_sbox.debug_info,
6973
)?;
7074

7175
{
@@ -98,6 +102,7 @@ fn hv_init(
98102
max_init_time: Duration,
99103
max_exec_time: Duration,
100104
max_wait_for_cancellation: Duration,
105+
#[cfg(gdb)] debug_info: Option<DebugInfo>,
101106
) -> Result<HypervisorHandler> {
102107
let outb_hdl = outb_handler_wrapper(hshm.clone(), host_funcs);
103108
let mem_access_hdl = mem_access_handler_wrapper(hshm.clone());
@@ -126,7 +131,11 @@ fn hv_init(
126131

127132
let mut hv_handler = HypervisorHandler::new(hv_handler_config);
128133

129-
hv_handler.start_hypervisor_handler(gshm)?;
134+
hv_handler.start_hypervisor_handler(
135+
gshm,
136+
#[cfg(gdb)]
137+
debug_info,
138+
)?;
130139

131140
hv_handler
132141
.execute_hypervisor_handler_action(HypervisorHandlerAction::Initialise)

0 commit comments

Comments
 (0)