Skip to content

feat: add narrow shared memory accessor to UninitializedSandbox#1270

Open
danbugs wants to merge 2 commits intohyperlight-dev:mainfrom
nanvix:danbugs/expose-sandbox-mem-api
Open

feat: add narrow shared memory accessor to UninitializedSandbox#1270
danbugs wants to merge 2 commits intohyperlight-dev:mainfrom
nanvix:danbugs/expose-sandbox-mem-api

Conversation

@danbugs
Copy link
Contributor

@danbugs danbugs commented Mar 3, 2026

Summary

Adds a public shared_mem_mut() method to UninitializedSandbox that returns a mutable reference to the sandbox's ExclusiveSharedMemory.

This provides a narrow, type-safe accessor for downstream consumers (e.g., Nanvix) that need to write data into the sandbox's shared memory region before initialization, without exposing the internal SandboxMemoryManager or its mgr field.

Motivation

Downstream projects like Nanvix need to write configuration data (e.g., a credits counter) into the sandbox's shared memory at a known GPA offset before evolving it into an initialized sandbox. Previously this required making mgr and SandboxMemoryManager public, leaking implementation details. This PR provides a minimal accessor that exposes only what is needed.

Changes

  • src/hyperlight_host/src/sandbox/uninitialized.rs: Add pub fn shared_mem_mut(&mut self) -> &mut ExclusiveSharedMemory

No visibility changes to SandboxMemoryManager or the mgr field. Additive-only API, no behavioral changes.

Test plan

  • cargo clippy passes (linux + windows feature combinations)
  • cargo test -p hyperlight-host --no-default-features -F "kvm,init-paging" --lib passes (83 tests)
  • Existing CI should pass (additive-only, no behavioral changes)

@danbugs danbugs force-pushed the danbugs/expose-sandbox-mem-api branch from c26238f to 0aa3f86 Compare March 3, 2026 23:02
@danbugs danbugs added area/API Related to the API or public interface kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. and removed area/API Related to the API or public interface labels Mar 3, 2026
@danbugs danbugs mentioned this pull request Mar 4, 2026
@syntactically
Copy link
Member

I wouldn't like this to be part of the public API. Can we go into a little more detail about how this is being used right now, and whether there are alternative ways to get the same result?

@jsturtevant
Copy link
Contributor

This provides a narrow, type-safe accessor for downstream consumers (e.g., Nanvix) that need to write data into the sandbox's shared memory region before initialization

What region is it writing too?

I wouldn't like this to be part of the public API. Can we go into a little more detail about how this is being used right now, and whether there are alternative ways to get the same result?

If we constrained the usage to something like write_shared_mem(&mut self, src: &[u8], offset: usize) where we could do checks and things would the be helpful?

@ludfjig
Copy link
Contributor

ludfjig commented Mar 4, 2026

The main worry I have is public usage of this API which is probably somethign we don't want. I'd also want to know what it's useful more specifically, but I'd be open to add this under a new feature flag with maybe a scary name since that would discourage general use

@jsturtevant
Copy link
Contributor

The main worry I have is public usage of this API which is probably somethign we don't want. I'd also want to know what it's useful more specifically, but I'd be open to add this under a new feature flag with maybe a scary name since that would discourage general use

hmm, it seems the requirement is some system need some extra metadata during boot sequence, I wonder for those systems not using paging (or maybe even with paging), if they could use the sratch region or a subset of it?

@dblnz
Copy link
Contributor

dblnz commented Mar 5, 2026

I have the same concerns other people mentioned around here. Exposing an implementation detail binds us to keeping it that way.
Maybe we can find another way to do this that doesn't imply knowing the memory layout to be able to interact with the API.

@danbugs
Copy link
Contributor Author

danbugs commented Mar 5, 2026

Addressed the concerns about exposing shared_mem_mut() as a public API. The latest push replaces it with:

pub fn guest_memory_ptr(&mut self, gpa: u64) -> Result<*mut u8>

This does bounds-checked GPA-to-host-pointer translation — the caller provides a guest physical address and gets back a host pointer into the mapped sandbox memory. The API no longer exposes SharedMemory internals or the memory manager.

Nanvix uses this for its credits-based flow control: the host and guest agree on a fixed GPA (CREDITS_GPA, last 8 bytes of the kernel pool) where a shared credit counter lives. The host increments/decrements the counter via volatile writes through this pointer, and the guest reads it directly from memory.

This is narrower than the previous shared_mem_mut() approach: it validates that the GPA falls within the sandbox's mapped region and returns an error otherwise. It doesn't expose the base pointer or total size, so callers can't accidentally access memory outside their intended range.

@danbugs danbugs force-pushed the danbugs/expose-sandbox-mem-api branch 6 times, most recently from e4f9d2b to edf452d Compare March 5, 2026 22:24
danbugs added 2 commits March 6, 2026 05:09
Replace the public shared_mem_mut() API (which exposed the full
SharedMemory trait and raw base_ptr()) with a narrower
guest_memory_ptr(gpa) method that takes a guest physical address,
validates it falls within the sandbox's allocated memory region,
and returns the corresponding host pointer.

This addresses review feedback requesting that raw shared memory
not be part of the public API surface.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs danbugs force-pushed the danbugs/expose-sandbox-mem-api branch from edf452d to e18f1a1 Compare March 6, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants