hyperlight_host/sandbox/run_options.rs
1/*
2Copyright 2024 The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use tracing::{instrument, Span};
18
19/// Configuration options for setting up a new `UninitializedSandbox` and
20/// subsequent initialized sandboxes, including `MultiUseSandbox`.
21///
22/// A `SandboxRunOptions` instance must be created with either in-process
23/// or in-hypervisor execution mode, and then can optionally be augmented
24/// with run-from-guest-binary mode if created with in-hypervisor mode.
25#[derive(Debug, Clone, Default, Eq, PartialEq)]
26pub enum SandboxRunOptions {
27 /// Run directly in a platform-appropriate hypervisor
28 #[default]
29 RunInHypervisor,
30 /// Run in-process, without a hypervisor, optionally using the
31 /// Windows LoadLibrary API to load the binary if the `bool` field is
32 /// set to `true`. This should only be used for testing and debugging
33 /// as it does not offer any security guarantees.
34 RunInProcess(bool),
35}
36
37impl SandboxRunOptions {
38 /// Returns true if the sandbox should be run in-process using the LoadLibrary API.
39 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
40 pub(super) fn use_loadlib(&self) -> bool {
41 matches!(self, Self::RunInProcess(true))
42 }
43
44 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
45 /// Returns true if the sandbox should be run in-process
46 pub(super) fn in_process(&self) -> bool {
47 matches!(self, SandboxRunOptions::RunInProcess(_))
48 }
49}