Skip to content

Commit 0a29546

Browse files
Remove the last vestiges of autodetecting heap/stack size from exes (#1097)
When we supported loading guest binaries from PE files, we also supported autodetecting reasonable defaults for heap and stack size for the sandbox based on the loaded executable's stack/heap size hints. ELF files did not have a similar convention for stack/heap size hints embedded in the file, so when loading an ELF file, we just used some vaguely reasonable small defaults (64k stack and 128k heap). Now that PE support is gone, these are in fact the defaults used for /all/ files, so it doesn't make much sense that we bother putting them into an executable information structure and passing it around. This commit removes that vestigial use of the ExeInfo structure, replacing it with defaults inline in `sandbox::config`. Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com> Co-authored-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent b82cf78 commit 0a29546

File tree

3 files changed

+12
-29
lines changed

3 files changed

+12
-29
lines changed

src/hyperlight_host/src/mem/exe.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ pub enum ExeInfo {
3232
Elf(ElfInfo),
3333
}
3434

35-
// There isn't a commonly-used standard convention for heap and stack
36-
// limits to be included in ELF files as they are in
37-
// PEs. Consequently, we use these static defaults as the default
38-
// limits, unless overwritten when setting up the sandbox.
39-
const DEFAULT_ELF_STACK_RESERVE: u64 = 65536;
40-
const DEFAULT_ELF_HEAP_RESERVE: u64 = 131072;
41-
4235
#[cfg(feature = "mem_profile")]
4336
pub(crate) trait UnwindInfo: Send + Sync {
4437
fn as_module(&self) -> framehop::Module<Vec<u8>>;
@@ -84,16 +77,6 @@ impl ExeInfo {
8477
pub fn from_buf(buf: &[u8]) -> Result<Self> {
8578
ElfInfo::new(buf).map(ExeInfo::Elf)
8679
}
87-
pub fn stack_reserve(&self) -> u64 {
88-
match self {
89-
ExeInfo::Elf(_) => DEFAULT_ELF_STACK_RESERVE,
90-
}
91-
}
92-
pub fn heap_reserve(&self) -> u64 {
93-
match self {
94-
ExeInfo::Elf(_) => DEFAULT_ELF_HEAP_RESERVE,
95-
}
96-
}
9780
pub fn entrypoint(&self) -> Offset {
9881
match self {
9982
ExeInfo::Elf(elf) => Offset::from(elf.entrypoint_va()),

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
327327
let layout = SandboxMemoryLayout::new(
328328
cfg,
329329
exe_info.loaded_size(),
330-
usize::try_from(cfg.get_stack_size(&exe_info))?,
331-
usize::try_from(cfg.get_heap_size(&exe_info))?,
330+
usize::try_from(cfg.get_stack_size())?,
331+
usize::try_from(cfg.get_heap_size())?,
332332
guest_blob_size,
333333
guest_blob_mem_flags,
334334
)?;

src/hyperlight_host/src/sandbox/config.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ use std::time::Duration;
2121
use libc::c_int;
2222
use tracing::{Span, instrument};
2323

24-
use crate::mem::exe::ExeInfo;
25-
2624
/// Used for passing debug configuration to a sandbox
2725
#[cfg(gdb)]
2826
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
@@ -105,6 +103,10 @@ impl SandboxConfiguration {
105103
pub const DEFAULT_INTERRUPT_RETRY_DELAY: Duration = Duration::from_micros(500);
106104
/// The default signal offset from `SIGRTMIN` used to determine the signal number for interrupting
107105
pub const INTERRUPT_VCPU_SIGRTMIN_OFFSET: u8 = 0;
106+
/// The default heap size of a hyperlight sandbox
107+
pub const DEFAULT_HEAP_SIZE: u64 = 131072;
108+
/// The default stack size of a hyperlight sandbox
109+
pub const DEFAULT_STACK_SIZE: u64 = 65536;
108110

109111
#[allow(clippy::too_many_arguments)]
110112
/// Create a new configuration for a sandbox with the given sizes.
@@ -267,17 +269,17 @@ impl SandboxConfiguration {
267269
/// If self.stack_size is non-zero, return it. Otherwise,
268270
/// return exe_info.stack_reserve()
269271
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
270-
pub(crate) fn get_stack_size(&self, exe_info: &ExeInfo) -> u64 {
272+
pub(crate) fn get_stack_size(&self) -> u64 {
271273
self.stack_size_override_opt()
272-
.unwrap_or_else(|| exe_info.stack_reserve())
274+
.unwrap_or(Self::DEFAULT_STACK_SIZE)
273275
}
274276

275277
/// If self.heap_size_override is non-zero, return it. Otherwise,
276278
/// return exe_info.heap_reserve()
277279
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
278-
pub(crate) fn get_heap_size(&self, exe_info: &ExeInfo) -> u64 {
280+
pub(crate) fn get_heap_size(&self) -> u64 {
279281
self.heap_size_override_opt()
280-
.unwrap_or_else(|| exe_info.heap_reserve())
282+
.unwrap_or(Self::DEFAULT_HEAP_SIZE)
281283
}
282284
}
283285

@@ -303,7 +305,6 @@ impl Default for SandboxConfiguration {
303305
#[cfg(test)]
304306
mod tests {
305307
use super::SandboxConfiguration;
306-
use crate::testing::simple_guest_exe_info;
307308

308309
#[test]
309310
fn overrides() {
@@ -325,10 +326,9 @@ mod tests {
325326
#[cfg(crashdump)]
326327
true,
327328
);
328-
let exe_info = simple_guest_exe_info().unwrap();
329329

330-
let stack_size = cfg.get_stack_size(&exe_info);
331-
let heap_size = cfg.get_heap_size(&exe_info);
330+
let stack_size = cfg.get_stack_size();
331+
let heap_size = cfg.get_heap_size();
332332
assert_eq!(STACK_SIZE_OVERRIDE, stack_size);
333333
assert_eq!(HEAP_SIZE_OVERRIDE, heap_size);
334334

0 commit comments

Comments
 (0)