Skip to content

Commit b419bde

Browse files
authored
Remove old code used for removed in-process feature (#879)
* Remove old code Signed-off-by: Ludvig Liljenberg <[email protected]> * Remove unused snapshot method + fix tests Signed-off-by: Ludvig Liljenberg <[email protected]> * Clippy Signed-off-by: Ludvig Liljenberg <[email protected]> * fixup! Remove old code Signed-off-by: Ludvig Liljenberg <[email protected]> --------- Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 08d8d8c commit b419bde

File tree

5 files changed

+54
-141
lines changed

5 files changed

+54
-141
lines changed

src/hyperlight_host/src/mem/layout.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -385,24 +385,6 @@ impl SandboxMemoryLayout {
385385
self.stack_size
386386
}
387387

388-
/// Get the offset in guest memory to the OutB pointer.
389-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
390-
#[allow(dead_code)]
391-
pub(super) fn get_outb_pointer_offset(&self) -> usize {
392-
// The outb pointer is immediately after the code pointer
393-
// in the `CodeAndOutBPointers` struct which is a u64
394-
self.peb_code_pointer_offset + size_of::<u64>()
395-
}
396-
397-
/// Get the offset in guest memory to the OutB context.
398-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
399-
#[allow(dead_code)]
400-
pub(super) fn get_outb_context_offset(&self) -> usize {
401-
// The outb context is immediately after the outb pointer
402-
// in the `CodeAndOutBPointers` struct which is a u64
403-
self.get_outb_pointer_offset() + size_of::<u64>()
404-
}
405-
406388
/// Get the offset in guest memory to the output data pointer.
407389
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
408390
fn get_output_data_pointer_offset(&self) -> usize {
@@ -420,11 +402,8 @@ impl SandboxMemoryLayout {
420402
}
421403

422404
/// Get the offset in guest memory to the start of output data.
423-
///
424-
/// This function exists to accommodate the macro that generates C API
425-
/// compatible functions.
426405
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
427-
#[allow(dead_code)]
406+
#[cfg(test)]
428407
pub(crate) fn get_output_data_offset(&self) -> usize {
429408
self.output_data_buffer_offset
430409
}
@@ -459,13 +438,6 @@ impl SandboxMemoryLayout {
459438
self.peb_guest_dispatch_function_ptr_offset
460439
}
461440

462-
/// Get the offset in guest memory to the PEB address
463-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
464-
#[allow(dead_code)]
465-
pub(super) fn get_in_process_peb_offset(&self) -> usize {
466-
self.peb_offset
467-
}
468-
469441
/// Get the offset in guest memory to the heap size
470442
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
471443
fn get_heap_size_offset(&self) -> usize {
@@ -494,13 +466,6 @@ impl SandboxMemoryLayout {
494466
self.get_min_guest_stack_address_offset() + size_of::<u64>()
495467
}
496468

497-
/// Get the offset to the guest guard page
498-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
499-
#[allow(dead_code)]
500-
pub fn get_guard_page_offset(&self) -> usize {
501-
self.guard_page_offset
502-
}
503-
504469
/// Get the total size of guest memory in `self`'s memory
505470
/// layout.
506471
#[instrument(skip_all, parent = Span::current(), level= "Trace")]

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,6 @@ where
281281
snapshot.restore_from_snapshot(&mut self.shared_mem)?;
282282
Ok(())
283283
}
284-
285-
/// Sets `addr` to the correct offset in the memory referenced by
286-
/// `shared_mem` to indicate the address of the outb pointer and context
287-
/// for calling outb function
288-
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
289-
#[allow(dead_code)]
290-
pub(crate) fn set_outb_address_and_context(&mut self, addr: u64, context: u64) -> Result<()> {
291-
let pointer_offset = self.layout.get_outb_pointer_offset();
292-
let context_offset = self.layout.get_outb_context_offset();
293-
self.shared_mem.with_exclusivity(|excl| -> Result<()> {
294-
excl.write_u64(pointer_offset, addr)?;
295-
excl.write_u64(context_offset, context)?;
296-
Ok(())
297-
})?
298-
}
299284
}
300285

301286
impl SandboxMemoryManager<ExclusiveSharedMemory> {

src/hyperlight_host/src/mem/shared_mem.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use windows::Win32::System::Memory::PAGE_READWRITE;
3030
#[cfg(target_os = "windows")]
3131
use windows::Win32::System::Memory::{
3232
CreateFileMappingA, FILE_MAP_ALL_ACCESS, MEMORY_MAPPED_VIEW_ADDRESS, MapViewOfFile,
33-
PAGE_EXECUTE_READWRITE, PAGE_NOACCESS, PAGE_PROTECTION_FLAGS, UnmapViewOfFile, VirtualProtect,
33+
PAGE_NOACCESS, PAGE_PROTECTION_FLAGS, UnmapViewOfFile, VirtualProtect,
3434
};
3535
#[cfg(target_os = "windows")]
3636
use windows::core::PCSTR;
@@ -502,46 +502,6 @@ impl ExclusiveSharedMemory {
502502
})
503503
}
504504

505-
#[allow(dead_code)]
506-
pub(super) fn make_memory_executable(&self) -> Result<()> {
507-
#[cfg(target_os = "windows")]
508-
{
509-
let mut _old_flags = PAGE_PROTECTION_FLAGS::default();
510-
if let Err(e) = unsafe {
511-
VirtualProtect(
512-
self.region.ptr as *const c_void,
513-
self.region.size,
514-
PAGE_EXECUTE_READWRITE,
515-
&mut _old_flags as *mut PAGE_PROTECTION_FLAGS,
516-
)
517-
} {
518-
log_then_return!(WindowsAPIError(e.clone()));
519-
}
520-
}
521-
522-
// make the memory executable on Linux
523-
#[cfg(target_os = "linux")]
524-
{
525-
use libc::{PROT_EXEC, PROT_READ, PROT_WRITE, mprotect};
526-
527-
let res = unsafe {
528-
mprotect(
529-
self.region.ptr as *mut c_void,
530-
self.region.size,
531-
PROT_READ | PROT_WRITE | PROT_EXEC,
532-
)
533-
};
534-
535-
if res != 0 {
536-
return Err(new_error!(
537-
"Failed to make memory executable: {:#?}",
538-
Error::last_os_error().raw_os_error()
539-
));
540-
}
541-
}
542-
Ok(())
543-
}
544-
545505
/// Internal helper method to get the backing memory as a mutable slice.
546506
///
547507
/// # Safety
@@ -614,15 +574,6 @@ impl ExclusiveSharedMemory {
614574
Ok(())
615575
}
616576

617-
/// Return the address of memory at an offset to this `SharedMemory` checking
618-
/// that the memory is within the bounds of the `SharedMemory`.
619-
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
620-
#[allow(dead_code)]
621-
pub(crate) fn calculate_address(&self, offset: usize) -> Result<usize> {
622-
bounds_check!(offset, 0, self.mem_size());
623-
Ok(self.base_addr() + offset)
624-
}
625-
626577
generate_reader!(read_u8, u8);
627578
generate_reader!(read_i8, i8);
628579
generate_reader!(read_u16, u16);

src/hyperlight_host/src/mem/shared_mem_snapshot.rs

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ impl SharedMemorySnapshot {
5050
})
5151
}
5252

53-
/// Take another snapshot of the internally-stored `SharedMemory`,
54-
/// then store it internally.
55-
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
56-
#[allow(dead_code)]
57-
pub(super) fn replace_snapshot<S: SharedMemory>(&mut self, shared_mem: &mut S) -> Result<()> {
58-
self.snapshot = shared_mem.with_exclusivity(|e| e.copy_all_to_vec())??;
59-
Ok(())
60-
}
61-
6253
/// Copy the memory from the internally-stored memory snapshot
6354
/// into the internally-stored `SharedMemory`.
6455
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
@@ -91,36 +82,58 @@ mod tests {
9182
use crate::mem::shared_mem::ExclusiveSharedMemory;
9283

9384
#[test]
94-
fn restore_replace() {
95-
let mut data1 = vec![b'a', b'b', b'c'];
96-
data1.resize_with(PAGE_SIZE_USIZE, || 0);
97-
let data2 = data1.iter().map(|b| b + 1).collect::<Vec<u8>>();
85+
fn restore() {
86+
// Simplified version of the original test
87+
let data1 = vec![b'a'; PAGE_SIZE_USIZE];
88+
let data2 = vec![b'b'; PAGE_SIZE_USIZE];
89+
9890
let mut gm = ExclusiveSharedMemory::new(PAGE_SIZE_USIZE).unwrap();
99-
gm.copy_from_slice(data1.as_slice(), 0).unwrap();
100-
let mut snap = super::SharedMemorySnapshot::new(&mut gm, 0, Vec::new()).unwrap();
101-
{
102-
// after the first snapshot is taken, make sure gm has the equivalent
103-
// of data1
104-
assert_eq!(data1, gm.copy_all_to_vec().unwrap());
105-
}
106-
107-
{
108-
// modify gm with data2 rather than data1 and restore from
109-
// snapshot. we should have the equivalent of data1 again
110-
gm.copy_from_slice(data2.as_slice(), 0).unwrap();
111-
assert_eq!(data2, gm.copy_all_to_vec().unwrap());
112-
snap.restore_from_snapshot(&mut gm).unwrap();
113-
assert_eq!(data1, gm.copy_all_to_vec().unwrap());
114-
}
115-
{
116-
// modify gm with data2, then retake the snapshot and restore
117-
// from the new snapshot. we should have the equivalent of data2
118-
gm.copy_from_slice(data2.as_slice(), 0).unwrap();
119-
assert_eq!(data2, gm.copy_all_to_vec().unwrap());
120-
snap.replace_snapshot(&mut gm).unwrap();
121-
assert_eq!(data2, gm.copy_all_to_vec().unwrap());
122-
snap.restore_from_snapshot(&mut gm).unwrap();
123-
assert_eq!(data2, gm.copy_all_to_vec().unwrap());
124-
}
91+
gm.copy_from_slice(&data1, 0).unwrap();
92+
93+
// Take snapshot of data1
94+
let snapshot = super::SharedMemorySnapshot::new(&mut gm, 0, Vec::new()).unwrap();
95+
96+
// Modify memory to data2
97+
gm.copy_from_slice(&data2, 0).unwrap();
98+
assert_eq!(gm.as_slice(), &data2[..]);
99+
100+
// Restore should bring back data1
101+
snapshot.restore_from_snapshot(&mut gm).unwrap();
102+
assert_eq!(gm.as_slice(), &data1[..]);
103+
}
104+
105+
#[test]
106+
fn snapshot_mem_size() {
107+
let size = PAGE_SIZE_USIZE * 2;
108+
let mut gm = ExclusiveSharedMemory::new(size).unwrap();
109+
110+
let snapshot = super::SharedMemorySnapshot::new(&mut gm, 0, Vec::new()).unwrap();
111+
assert_eq!(snapshot.mem_size(), size);
112+
}
113+
114+
#[test]
115+
fn multiple_snapshots_independent() {
116+
let mut gm = ExclusiveSharedMemory::new(PAGE_SIZE_USIZE).unwrap();
117+
118+
// Create first snapshot with pattern A
119+
let pattern_a = vec![0xAA; PAGE_SIZE_USIZE];
120+
gm.copy_from_slice(&pattern_a, 0).unwrap();
121+
let snapshot_a = super::SharedMemorySnapshot::new(&mut gm, 1, Vec::new()).unwrap();
122+
123+
// Create second snapshot with pattern B
124+
let pattern_b = vec![0xBB; PAGE_SIZE_USIZE];
125+
gm.copy_from_slice(&pattern_b, 0).unwrap();
126+
let snapshot_b = super::SharedMemorySnapshot::new(&mut gm, 2, Vec::new()).unwrap();
127+
128+
// Clear memory
129+
gm.copy_from_slice(&[0; PAGE_SIZE_USIZE], 0).unwrap();
130+
131+
// Restore snapshot A
132+
snapshot_a.restore_from_snapshot(&mut gm).unwrap();
133+
assert_eq!(gm.as_slice(), &pattern_a[..]);
134+
135+
// Restore snapshot B
136+
snapshot_b.restore_from_snapshot(&mut gm).unwrap();
137+
assert_eq!(gm.as_slice(), &pattern_b[..]);
125138
}
126139
}

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ impl MultiUseSandbox {
333333
/// Map the contents of a file into the guest at a particular address
334334
///
335335
/// Returns the length of the mapping in bytes.
336-
#[allow(dead_code)]
337336
#[instrument(err(Debug), skip(self, _fp, _guest_base), parent = Span::current())]
338337
pub fn map_file_cow(&mut self, _fp: &Path, _guest_base: u64) -> Result<u64> {
339338
#[cfg(windows)]

0 commit comments

Comments
 (0)