Skip to content

Add SandboxBuilder - #1725

Open
jprendes wants to merge 2 commits into
hyperlight-dev:mainfrom
jprendes:builder
Open

Add SandboxBuilder#1725
jprendes wants to merge 2 commits into
hyperlight-dev:mainfrom
jprendes:builder

Conversation

@jprendes

Copy link
Copy Markdown
Contributor

Description

SandboxBuilder is the entry point for creating a sandbox. It gathers machine configuration, host functions, init data and memory mappings, then builds a MultiUseSandbox from a guest binary on disk, a guest binary in memory, or a snapshot.

It merges the roles of SandboxConfiguration, GuestEnvironment and UninitializedSandbox into a single type, so all three can become implementation details.

Every setting has an in place accessor taking &mut self and a with_* counterpart consuming self for chaining. Machine configuration values are readable through get_*.

map_memory_region is unsafe, matching MultiUseSandbox::map_region. The mapped region must stay valid for the lifetime of the built sandbox.

build_from_snapshot errors when init_data or max_guest_log_level are set, as a snapshot already carries both.

Scope

This PR only adds the new API, it does not deprecate any old API, and does not replace the use of the old API throughout the repo.

Copilot AI lite review requested due to automatic review settings August 12, 2026 11:30
@jprendes jprendes added the area/API Related to the API or public interface label Aug 12, 2026
`SandboxBuilder` is the entry point for creating a sandbox. It gathers machine
configuration, host functions, init data and memory mappings, then builds a
`MultiUseSandbox` from a guest binary on disk, a guest binary in memory, or a
snapshot.

It merges the roles of `SandboxConfiguration`, `GuestEnvironment` and
`UninitializedSandbox` into a single type, so all three can become
implementation details.

Every setting has an in place accessor taking `&mut self` and a `with_*`
counterpart consuming `self` for chaining. Machine configuration values are
readable through `get_*`.

`map_memory_region` is `unsafe`, matching `MultiUseSandbox::map_region`. The
mapped region must stay valid for the lifetime of the built sandbox.

`build_from_snapshot` errors when `init_data` or `max_guest_log_level` are set,
as a snapshot already carries both.

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
@jprendes jprendes added kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. kind/refactor For PRs that restructure or remove code without adding new functionality. and removed kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. labels Aug 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new SandboxBuilder API in hyperlight_host as the primary entry point for configuring and constructing MultiUseSandbox instances from a guest binary (file or bytes) or from a snapshot, consolidating configuration, host-function registration, init data, and memory mappings into one builder type.

Changes:

  • Added sandbox::builder::SandboxBuilder with build_from_file, build_from_bytes, and build_from_snapshot constructors plus with_*/in-place setters.
  • Exposed MultiUseSandbox::builder() and re-exported SandboxBuilder from hyperlight_host.
  • Adjusted host function registry internals to support builder-driven registration.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/hyperlight_host/src/sandbox/mod.rs Exposes the new builder module under sandbox.
src/hyperlight_host/src/sandbox/initialized_multi_use.rs Adds MultiUseSandbox::builder() entry point returning a default SandboxBuilder.
src/hyperlight_host/src/sandbox/host_funcs.rs Loosens visibility of FunctionRegistry::functions_map to support builder registration.
src/hyperlight_host/src/sandbox/builder.rs New builder implementation + unit tests for building from file/bytes/snapshot.
src/hyperlight_host/src/lib.rs Re-exports SandboxBuilder from the crate root.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/hyperlight_host/src/sandbox/builder.rs Outdated
Comment thread src/hyperlight_host/src/sandbox/builder.rs Outdated
Comment thread src/hyperlight_host/src/sandbox/builder.rs Outdated
Comment thread src/hyperlight_host/src/sandbox/builder.rs
Comment thread src/hyperlight_host/src/sandbox/host_funcs.rs Outdated
@jsturtevant

Copy link
Copy Markdown
Contributor

This PR only adds the new API, it does not deprecate any old API, and does not replace the use of the old API throughout the repo.

I like that this just simplifies the API, and would be an almost drop in for https://github.com/hyperlight-dev/hyperlight-sandbox which I would be able to reduce the code there.

What do propose for plans to depreciate the older apis? I think this would provide a bit more flexibility to do things internally

andreiltd
andreiltd previously approved these changes Aug 14, 2026

@andreiltd andreiltd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A big fan of this PR 🚀

@jprendes

Copy link
Copy Markdown
Contributor Author

I should have dropped this link in the PR description.
Here's the document describing the API https://hackmd.io/SdWE6fKSShifX-2_DlY4Fw

At the end of the document I added how we can deprecate the old API

We have 3 deprecation levels, in increasing severity:

  • #[doc(hidden)]: the types disappear from rustdoc and from code completion,
    so new code stops reaching for them. Nothing downstream breaks.
  • #[deprecated]: existing users get a compiler warning pointing at the
    builder, or an error where warnings are denied.
  • pub(crate): the types leave the public API. A breaking change, and the only
    one of the three that guarantees no downstream use remains.

Applying #[doc(hidden)] and #[deprecated] together in one release, then
pub(crate) in the next, is the recommended path. The first release hides the
types from new code and warns existing users, the second removes them from the
public API.

I'd be happy to hear different opinions.

@ludfjig ludfjig left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this! Only one concern:

  • Looks like pr is missing newly added guest_msrs config.

Some minor things:

  • Would it be possible to somehow allow multiple sandboxes to be created from a builder? Or have builder be clonable? Just thinking about ergonomics if multiple identical sandboxes are to be created (I understand this might not be easy with host functions...)
  • Also bikeshedding if any apis should be renamed (my candiadates are max_guest_log_level, map_file_cow, map_memory_region) but not blocking for this pr...
  • Could you add this builder addition to CHANGELOG.md?
  • Do we want to consider adding the possiblity of having sandboxes without any host functions (including no hostprint?)

Comment thread src/hyperlight_host/src/sandbox/builder.rs
}

/// Like [`Self::max_guest_log_level`], but consumes and returns `self` for chaining.
pub fn with_max_guest_log_level(mut self, level: LevelFilter) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking about if we can name this something better...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any suggestions?

Comment thread src/hyperlight_host/src/sandbox/builder.rs Outdated
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
@yoshuawuyts

Copy link
Copy Markdown

I love the work you're doing on making sandboxes easier to construct! I think that will go a long way in how people experience using Hyperlight. However in Rust, the builder suffix typically does refer to a chaining constructor. These typically take self by-value or by-(mutable)-reference. I would prefer it if we just kept the with_ variants of the APIs here, but without the with_ prefix (so e.g. with_mapped_file_cow -> mapped_file_cow).

As provided I think the mutable accessors on a type named *Builder will probably confuse first-time users of the crate.

@jprendes

Copy link
Copy Markdown
Contributor Author

I was mostly following the std::process::Command, which is the builder I most frequently use. But I agree it's not called *Builder.
As for things *Builder in the rust std, there's:

outside rust's std:

So yeah, I think the most common pattern is taking self.

How would you feel about having method xxx(self, ...) and in a trait SandboxBuilderMut (name TBD) that exposes the with_xxx(&mut self, ...) (with_ prefix TBD) methods?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/API Related to the API or public interface kind/refactor For PRs that restructure or remove code without adding new functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants