Skip to content

Commit 2119779

Browse files
committed
Move file mapping directory behind nanvix-unstable feature
This can't work in normal configurations, because it relies on mutating snapshot memories. It's also definitely unstable/may be removed shortly. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent 7104583 commit 2119779

6 files changed

Lines changed: 98 additions & 62 deletions

File tree

src/hyperlight_common/src/mem.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ pub struct HyperlightPEB {
7777
/// [`FileMappingInfo`] entries), NOT a byte size. `ptr` holds the
7878
/// guest address of the preallocated array (immediately after the
7979
/// PEB struct).
80+
#[cfg(feature = "nanvix-unstable")]
8081
pub file_mappings: GuestMemoryRegion,
8182
}

src/hyperlight_host/src/mem/layout.rs

Lines changed: 86 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub(crate) struct SandboxMemoryLayout {
9292
peb_output_data_offset: usize,
9393
peb_init_data_offset: usize,
9494
peb_heap_data_offset: usize,
95+
#[cfg(feature = "nanvix-unstable")]
9596
peb_file_mappings_offset: usize,
9697

9798
guest_heap_buffer_offset: usize,
@@ -113,57 +114,58 @@ pub(crate) struct SandboxMemoryLayout {
113114

114115
impl Debug for SandboxMemoryLayout {
115116
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116-
f.debug_struct("SandboxMemoryLayout")
117-
.field(
118-
"Total Memory Size",
119-
&format_args!("{:#x}", self.get_memory_size().unwrap_or(0)),
120-
)
121-
.field("Heap Size", &format_args!("{:#x}", self.heap_size))
122-
.field(
123-
"Init Data Size",
124-
&format_args!("{:#x}", self.init_data_size),
125-
)
126-
.field("PEB Address", &format_args!("{:#x}", self.peb_address))
127-
.field("PEB Offset", &format_args!("{:#x}", self.peb_offset))
128-
.field("Code Size", &format_args!("{:#x}", self.code_size))
129-
.field(
130-
"Input Data Offset",
131-
&format_args!("{:#x}", self.peb_input_data_offset),
132-
)
133-
.field(
134-
"Output Data Offset",
135-
&format_args!("{:#x}", self.peb_output_data_offset),
136-
)
137-
.field(
138-
"Init Data Offset",
139-
&format_args!("{:#x}", self.peb_init_data_offset),
140-
)
141-
.field(
142-
"Guest Heap Offset",
143-
&format_args!("{:#x}", self.peb_heap_data_offset),
144-
)
145-
.field(
146-
"File Mappings Offset",
147-
&format_args!("{:#x}", self.peb_file_mappings_offset),
148-
)
149-
.field(
150-
"Guest Heap Buffer Offset",
151-
&format_args!("{:#x}", self.guest_heap_buffer_offset),
152-
)
153-
.field(
154-
"Init Data Offset",
155-
&format_args!("{:#x}", self.init_data_offset),
156-
)
157-
.field("PT Size", &format_args!("{:#x}", self.pt_size.unwrap_or(0)))
158-
.field(
159-
"Guest Code Offset",
160-
&format_args!("{:#x}", self.guest_code_offset),
161-
)
162-
.field(
163-
"Scratch region size",
164-
&format_args!("{:#x}", self.scratch_size),
165-
)
166-
.finish()
117+
let mut ff = f.debug_struct("SandboxMemoryLayout");
118+
ff.field(
119+
"Total Memory Size",
120+
&format_args!("{:#x}", self.get_memory_size().unwrap_or(0)),
121+
)
122+
.field("Heap Size", &format_args!("{:#x}", self.heap_size))
123+
.field(
124+
"Init Data Size",
125+
&format_args!("{:#x}", self.init_data_size),
126+
)
127+
.field("PEB Address", &format_args!("{:#x}", self.peb_address))
128+
.field("PEB Offset", &format_args!("{:#x}", self.peb_offset))
129+
.field("Code Size", &format_args!("{:#x}", self.code_size))
130+
.field(
131+
"Input Data Offset",
132+
&format_args!("{:#x}", self.peb_input_data_offset),
133+
)
134+
.field(
135+
"Output Data Offset",
136+
&format_args!("{:#x}", self.peb_output_data_offset),
137+
)
138+
.field(
139+
"Init Data Offset",
140+
&format_args!("{:#x}", self.peb_init_data_offset),
141+
)
142+
.field(
143+
"Guest Heap Offset",
144+
&format_args!("{:#x}", self.peb_heap_data_offset),
145+
);
146+
#[cfg(feature = "nanvix-unstable")]
147+
ff.field(
148+
"File Mappings Offset",
149+
&format_args!("{:#x}", self.peb_file_mappings_offset),
150+
);
151+
ff.field(
152+
"Guest Heap Buffer Offset",
153+
&format_args!("{:#x}", self.guest_heap_buffer_offset),
154+
)
155+
.field(
156+
"Init Data Offset",
157+
&format_args!("{:#x}", self.init_data_offset),
158+
)
159+
.field("PT Size", &format_args!("{:#x}", self.pt_size.unwrap_or(0)))
160+
.field(
161+
"Guest Code Offset",
162+
&format_args!("{:#x}", self.guest_code_offset),
163+
)
164+
.field(
165+
"Scratch region size",
166+
&format_args!("{:#x}", self.scratch_size),
167+
)
168+
.finish()
167169
}
168170
}
169171

@@ -213,6 +215,7 @@ impl SandboxMemoryLayout {
213215
let peb_output_data_offset = peb_offset + offset_of!(HyperlightPEB, output_stack);
214216
let peb_init_data_offset = peb_offset + offset_of!(HyperlightPEB, init_data);
215217
let peb_heap_data_offset = peb_offset + offset_of!(HyperlightPEB, guest_heap);
218+
#[cfg(feature = "nanvix-unstable")]
216219
let peb_file_mappings_offset = peb_offset + offset_of!(HyperlightPEB, file_mappings);
217220

218221
// The following offsets are the actual values that relate to memory layout,
@@ -227,11 +230,16 @@ impl SandboxMemoryLayout {
227230
// many file mappings the host will register, so we reserve space for
228231
// the maximum number.
229232
// The heap starts at the next page boundary after this reserved area.
233+
#[cfg(feature = "nanvix-unstable")]
230234
let file_mappings_array_end = peb_offset
231235
+ size_of::<HyperlightPEB>()
232236
+ hyperlight_common::mem::MAX_FILE_MAPPINGS
233237
* size_of::<hyperlight_common::mem::FileMappingInfo>();
238+
#[cfg(feature = "nanvix-unstable")]
234239
let guest_heap_buffer_offset = file_mappings_array_end.next_multiple_of(PAGE_SIZE_USIZE);
240+
#[cfg(not(feature = "nanvix-unstable"))]
241+
let guest_heap_buffer_offset =
242+
(peb_offset + size_of::<HyperlightPEB>()).next_multiple_of(PAGE_SIZE_USIZE);
235243

236244
// make sure init data starts at 4K boundary
237245
let init_data_offset =
@@ -244,6 +252,7 @@ impl SandboxMemoryLayout {
244252
peb_output_data_offset,
245253
peb_init_data_offset,
246254
peb_heap_data_offset,
255+
#[cfg(feature = "nanvix-unstable")]
247256
peb_file_mappings_offset,
248257
sandbox_memory_config: cfg,
249258
code_size,
@@ -367,22 +376,26 @@ impl SandboxMemoryLayout {
367376

368377
/// Get the offset in guest memory to the file_mappings count field
369378
/// (the `size` field of the `GuestMemoryRegion` in the PEB).
379+
#[cfg(feature = "nanvix-unstable")]
370380
pub(crate) fn get_file_mappings_size_offset(&self) -> usize {
371381
self.peb_file_mappings_offset
372382
}
373383

374384
/// Get the offset in guest memory to the file_mappings pointer field.
385+
#[cfg(feature = "nanvix-unstable")]
375386
fn get_file_mappings_pointer_offset(&self) -> usize {
376387
self.get_file_mappings_size_offset() + size_of::<u64>()
377388
}
378389

379390
/// Get the offset in snapshot memory where the FileMappingInfo array starts
380391
/// (immediately after the PEB struct, within the same page).
392+
#[cfg(feature = "nanvix-unstable")]
381393
pub(crate) fn get_file_mappings_array_offset(&self) -> usize {
382394
self.peb_offset + size_of::<HyperlightPEB>()
383395
}
384396

385397
/// Get the guest address of the FileMappingInfo array.
398+
#[cfg(feature = "nanvix-unstable")]
386399
fn get_file_mappings_array_gva(&self) -> u64 {
387400
(Self::BASE_ADDRESS + self.get_file_mappings_array_offset()) as u64
388401
}
@@ -484,14 +497,20 @@ impl SandboxMemoryLayout {
484497
}
485498

486499
// PEB + preallocated FileMappingInfo array
487-
let peb_and_array_size = size_of::<HyperlightPEB>()
488-
+ hyperlight_common::mem::MAX_FILE_MAPPINGS
489-
* size_of::<hyperlight_common::mem::FileMappingInfo>();
490-
let heap_offset = builder.push_page_aligned(
491-
peb_and_array_size,
492-
MemoryRegionFlags::READ | MemoryRegionFlags::WRITE,
493-
Peb,
494-
);
500+
#[cfg(feature = "nanvix-unstable")]
501+
let heap_offset = {
502+
let peb_and_array_size = size_of::<HyperlightPEB>()
503+
+ hyperlight_common::mem::MAX_FILE_MAPPINGS
504+
* size_of::<hyperlight_common::mem::FileMappingInfo>();
505+
builder.push_page_aligned(
506+
peb_and_array_size,
507+
MemoryRegionFlags::READ | MemoryRegionFlags::WRITE,
508+
Peb,
509+
)
510+
};
511+
#[cfg(not(feature = "nanvix-unstable"))]
512+
let heap_offset =
513+
builder.push_page_aligned(size_of::<HyperlightPEB>(), MemoryRegionFlags::READ, Peb);
495514

496515
let expected_heap_offset = TryInto::<usize>::try_into(self.guest_heap_buffer_offset)?;
497516

@@ -634,8 +653,11 @@ impl SandboxMemoryLayout {
634653
// later by map_file_cow / evolve).
635654
// - The `ptr` field holds the guest address of the preallocated
636655
// FileMappingInfo array
637-
shared_mem.write_u64(self.get_file_mappings_size_offset(), 0)?;
638-
shared_mem.write_u64(
656+
#[cfg(feature = "nanvix-unstable")]
657+
write_u64(mem, self.get_file_mappings_size_offset(), 0)?;
658+
#[cfg(feature = "nanvix-unstable")]
659+
write_u64(
660+
mem,
639661
self.get_file_mappings_pointer_offset(),
640662
self.get_file_mappings_array_gva(),
641663
)?;
@@ -664,9 +686,12 @@ mod tests {
664686
expected_size += layout.code_size;
665687

666688
// PEB + preallocated FileMappingInfo array
689+
#[cfg(feature = "nanvix-unstable")]
667690
let peb_and_array = size_of::<HyperlightPEB>()
668691
+ hyperlight_common::mem::MAX_FILE_MAPPINGS
669692
* size_of::<hyperlight_common::mem::FileMappingInfo>();
693+
#[cfg(not(feature = "nanvix-unstable"))]
694+
let peb_and_array = size_of::<HyperlightPEB>();
670695
expected_size += peb_and_array.next_multiple_of(PAGE_SIZE_USIZE);
671696

672697
expected_size += layout.heap_size.next_multiple_of(PAGE_SIZE_USIZE);

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ distributed under the License is distributed on an "AS IS" BASIS,
1212
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
15-
*/
15+
*/
16+
#[cfg(feature = "nanvix-unstable")]
1617
use std::mem::offset_of;
1718

1819
use flatbuffers::FlatBufferBuilder;
@@ -369,6 +370,7 @@ impl SandboxMemoryManager<HostSharedMemory> {
369370
///
370371
/// [`FileMappingInfo`]: hyperlight_common::mem::FileMappingInfo
371372
/// [`MAX_FILE_MAPPINGS`]: hyperlight_common::mem::MAX_FILE_MAPPINGS
373+
#[cfg(feature = "nanvix-unstable")]
372374
pub(crate) fn write_file_mapping_entry(
373375
&mut self,
374376
guest_addr: u64,

src/hyperlight_host/src/sandbox/file_mapping.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub(crate) struct PreparedFileMapping {
5858
/// The page-aligned size of the mapping in bytes.
5959
pub(crate) size: usize,
6060
/// Null-terminated C-style label for this mapping (max 63 chars + null).
61+
#[cfg_attr(not(feature = "nanvix-unstable"), allow(unused))]
6162
pub(crate) label: [u8; hyperlight_common::mem::FILE_MAPPING_LABEL_MAX_LEN + 1],
6263
/// Host-side OS resources. `None` after successful consumption
6364
/// by the apply step (ownership transferred to the VM layer).

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,13 @@ impl MultiUseSandbox {
562562

563563
// Pre-check the file mapping limit before doing any expensive
564564
// OS or VM work. The PEB count is the source of truth.
565+
#[cfg(feature = "nanvix-unstable")]
565566
let current_count = self
566567
.mem_mgr
567568
.shared_mem
568569
.read::<u64>(self.mem_mgr.layout.get_file_mappings_size_offset())?
569570
as usize;
571+
#[cfg(feature = "nanvix-unstable")]
570572
if current_count >= hyperlight_common::mem::MAX_FILE_MAPPINGS {
571573
return Err(crate::HyperlightError::Error(format!(
572574
"map_file_cow: file mapping limit reached ({} of {})",
@@ -638,6 +640,7 @@ impl MultiUseSandbox {
638640
// still holds a valid mapping but the PEB won't list it — the
639641
// limit was already pre-checked above so this should not fail
640642
// in practice.
643+
#[cfg(feature = "nanvix-unstable")]
641644
self.mem_mgr
642645
.write_file_mapping_entry(prepared.guest_base, size, &prepared.label)?;
643646

@@ -2135,6 +2138,7 @@ mod tests {
21352138
/// the FileMappingInfo entry (count, guest_addr, size, label) into
21362139
/// the PEB.
21372140
#[test]
2141+
#[cfg(feature = "nanvix-unstable")]
21382142
fn test_map_file_cow_peb_entry_multiuse() {
21392143
use std::mem::offset_of;
21402144

@@ -2210,6 +2214,7 @@ mod tests {
22102214
/// Tests that deferred `map_file_cow` (before evolve) correctly
22112215
/// writes FileMappingInfo entries into the PEB during evolve.
22122216
#[test]
2217+
#[cfg(feature = "nanvix-unstable")]
22132218
fn test_map_file_cow_peb_entry_deferred() {
22142219
use std::mem::offset_of;
22152220

@@ -2283,6 +2288,7 @@ mod tests {
22832288
/// populates all PEB FileMappingInfo slots with the right guest_addr,
22842289
/// size, and label for each entry.
22852290
#[test]
2291+
#[cfg(feature = "nanvix-unstable")]
22862292
fn test_map_file_cow_peb_multiple_entries() {
22872293
use std::mem::{offset_of, size_of};
22882294

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub(super) fn evolve_impl_multi_use(u_sbox: UninitializedSandbox) -> Result<Mult
114114
// it — acceptable since we're about to return Err and the
115115
// VM will be dropped. The limit was already validated in
116116
// UninitializedSandbox::map_file_cow.
117+
#[cfg(feature = "nanvix-unstable")]
117118
hshm.write_file_mapping_entry(prepared.guest_base, prepared.size as u64, &prepared.label)?;
118119
hshm.mapped_rgns += 1;
119120
}

0 commit comments

Comments
 (0)