Skip to content

Commit 91ae36f

Browse files
committed
Allow configuring the signal number of the signal used to interrupt a sandbox
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 15786fe commit 91ae36f

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ impl HypervLinuxDriver {
400400
cancel_requested: AtomicBool::new(false),
401401
tid: AtomicU64::new(unsafe { libc::pthread_self() }),
402402
retry_delay: config.get_interrupt_retry_delay(),
403+
sig_rt_min_offset: config.get_interrupt_vcpu_sigrtmin_offset(),
403404
dropped: AtomicBool::new(false),
404405
}),
405406

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ impl KVMDriver {
356356
tid: AtomicU64::new(unsafe { libc::pthread_self() }),
357357
retry_delay: config.get_interrupt_retry_delay(),
358358
dropped: AtomicBool::new(false),
359+
sig_rt_min_offset: config.get_interrupt_vcpu_sigrtmin_offset(),
359360
}),
360361

361362
#[cfg(gdb)]

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use tracing::{instrument, Span};
2020
use crate::error::HyperlightError::ExecutionCanceledByHost;
2121
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
2222
use crate::metrics::METRIC_GUEST_CANCELLATION;
23-
#[cfg(any(kvm, mshv))]
24-
use crate::signal_handlers::INTERRUPT_VCPU_SIGRTMIN_OFFSET;
2523
use crate::{log_then_return, new_error, HyperlightError, Result};
2624

2725
/// Util for handling x87 fpu state
@@ -357,14 +355,16 @@ pub(super) struct LinuxInterruptHandle {
357355
dropped: AtomicBool,
358356
/// Retry delay between signals sent to the vcpu thread
359357
retry_delay: Duration,
358+
/// The offset of the SIGRTMIN signal used to interrupt the vcpu thread
359+
sig_rt_min_offset: usize,
360360
}
361361

362362
#[cfg(any(kvm, mshv))]
363363
impl InterruptHandle for LinuxInterruptHandle {
364364
fn kill(&self) -> bool {
365365
self.cancel_requested.store(true, Ordering::Relaxed);
366366

367-
let signal_number = libc::SIGRTMIN() + INTERRUPT_VCPU_SIGRTMIN_OFFSET;
367+
let signal_number = libc::SIGRTMIN() + self.sig_rt_min_offset as libc::c_int;
368368
let mut sent_signal = false;
369369

370370
while self.running.load(Ordering::Relaxed) {

src/hyperlight_host/src/sandbox/config.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ pub struct SandboxConfiguration {
6363
/// signal can be delivered to the thread, but the thread may not yet
6464
/// have entered kernel space.
6565
interrupt_retry_delay: Duration,
66+
/// Offset from `SIGRTMIN` used to determine the signal number for interrupting
67+
/// the VCPU thread. The actual signal sent is `SIGRTMIN + interrupt_vcpu_sigrtmin_offset`.
68+
///
69+
/// This signal must fall within the valid real-time signal range supported by the host.
70+
///
71+
/// Note: Since real-time signals can vary across platforms, ensure that the offset
72+
/// results in a signal number that is not already in use by other components of the system.
73+
interrupt_vcpu_sigrtmin_offset: usize,
6674
}
6775

6876
impl SandboxConfiguration {
@@ -76,6 +84,8 @@ impl SandboxConfiguration {
7684
pub const MIN_OUTPUT_SIZE: usize = 0x2000;
7785
/// The default interrupt retry delay
7886
pub const DEFAULT_INTERRUPT_RETRY_DELAY: Duration = Duration::from_micros(500);
87+
/// The default signal offset from `SIGRTMIN` used to determine the signal number for interrupting
88+
pub const INTERRUPT_VCPU_SIGRTMIN_OFFSET: usize = 0;
7989

8090
#[allow(clippy::too_many_arguments)]
8191
/// Create a new configuration for a sandbox with the given sizes.
@@ -86,6 +96,7 @@ impl SandboxConfiguration {
8696
stack_size_override: Option<u64>,
8797
heap_size_override: Option<u64>,
8898
interrupt_retry_delay: Duration,
99+
interrupt_vcpu_sigrtmin_offset: usize,
89100
#[cfg(gdb)] guest_debug_info: Option<DebugInfo>,
90101
) -> Self {
91102
Self {
@@ -94,7 +105,7 @@ impl SandboxConfiguration {
94105
stack_size_override: stack_size_override.unwrap_or(0),
95106
heap_size_override: heap_size_override.unwrap_or(0),
96107
interrupt_retry_delay,
97-
108+
interrupt_vcpu_sigrtmin_offset,
98109
#[cfg(gdb)]
99110
guest_debug_info,
100111
}
@@ -136,6 +147,20 @@ impl SandboxConfiguration {
136147
self.interrupt_retry_delay
137148
}
138149

150+
/// Get the signal offset from `SIGRTMIN` used to determine the signal number for interrupting the VCPU thread
151+
pub fn get_interrupt_vcpu_sigrtmin_offset(&self) -> usize {
152+
self.interrupt_vcpu_sigrtmin_offset
153+
}
154+
155+
/// Sets the offset from `SIGRTMIN` to determine the real-time signal used for
156+
/// interrupting the VCPU thread.
157+
///
158+
/// The final signal number is computed as `SIGRTMIN + offset`, and it must fall within
159+
/// the valid range of real-time signals supported by the host system.
160+
pub fn set_interrupt_vcpu_sigrtmin_offset(&mut self, offset: usize) {
161+
self.interrupt_vcpu_sigrtmin_offset = offset;
162+
}
163+
139164
/// Sets the configuration for the guest debug
140165
#[cfg(gdb)]
141166
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
@@ -195,6 +220,7 @@ impl Default for SandboxConfiguration {
195220
None,
196221
None,
197222
Self::DEFAULT_INTERRUPT_RETRY_DELAY,
223+
Self::INTERRUPT_VCPU_SIGRTMIN_OFFSET,
198224
#[cfg(gdb)]
199225
None,
200226
)
@@ -218,6 +244,7 @@ mod tests {
218244
Some(STACK_SIZE_OVERRIDE),
219245
Some(HEAP_SIZE_OVERRIDE),
220246
SandboxConfiguration::DEFAULT_INTERRUPT_RETRY_DELAY,
247+
SandboxConfiguration::INTERRUPT_VCPU_SIGRTMIN_OFFSET,
221248
#[cfg(gdb)]
222249
None,
223250
);
@@ -244,6 +271,7 @@ mod tests {
244271
None,
245272
None,
246273
SandboxConfiguration::DEFAULT_INTERRUPT_RETRY_DELAY,
274+
SandboxConfiguration::INTERRUPT_VCPU_SIGRTMIN_OFFSET,
247275
#[cfg(gdb)]
248276
None,
249277
);

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ where
8888
let dbg_mem_access_hdl = dbg_mem_access_handler_wrapper(hshm.clone());
8989

9090
#[cfg(target_os = "linux")]
91-
setup_signal_handlers()?;
91+
setup_signal_handlers(&u_sbox.config)?;
9292

9393
vm.initialise(
9494
peb_addr,

src/hyperlight_host/src/signal_handlers/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
use libc::c_int;
18+
19+
use crate::sandbox::SandboxConfiguration;
20+
1721
#[cfg(feature = "seccomp")]
1822
pub mod sigsys_signal_handler;
1923

20-
pub(crate) const INTERRUPT_VCPU_SIGRTMIN_OFFSET: i32 = 0;
21-
22-
pub(crate) fn setup_signal_handlers() -> crate::Result<()> {
24+
pub(crate) fn setup_signal_handlers(config: &SandboxConfiguration) -> crate::Result<()> {
2325
// This is unsafe because signal handlers only allow a very restrictive set of
2426
// functions (i.e., async-signal-safe functions) to be executed inside them.
2527
// Anything that performs memory allocations, locks, and others are non-async-signal-safe.
@@ -48,7 +50,7 @@ pub(crate) fn setup_signal_handlers() -> crate::Result<()> {
4850
}));
4951
}
5052
vmm_sys_util::signal::register_signal_handler(
51-
libc::SIGRTMIN() + INTERRUPT_VCPU_SIGRTMIN_OFFSET,
53+
libc::SIGRTMIN() + config.get_interrupt_vcpu_sigrtmin_offset() as c_int,
5254
vm_kill_signal,
5355
)?;
5456

0 commit comments

Comments
 (0)