Skip to content

Commit ea263cf

Browse files
committed
Review updates
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent a1565c2 commit ea263cf

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

src/hyperlight_host/src/mem/layout.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ impl SandboxMemoryLayout {
225225
const MAX_MEMORY_SIZE: usize = 0x40000000 - Self::BASE_ADDRESS;
226226

227227
/// The base address of the sandbox's memory.
228+
#[cfg(feature = "init-paging")]
228229
pub(crate) const BASE_ADDRESS: usize = 0x1000;
230+
#[cfg(not(feature = "init-paging"))]
231+
pub(crate) const BASE_ADDRESS: usize = 0x0;
229232

230233
// the offset into a sandbox's input/output buffer where the stack starts
231234
const STACK_POINTER_SIZE_BYTES: u64 = 8;
@@ -478,7 +481,7 @@ impl SandboxMemoryLayout {
478481
self.pt_offset
479482
}
480483

481-
/// Get the offset into the snapshot region of the page tables
484+
/// Sets the size of the memory region used for page tables
482485
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
483486
#[cfg(feature = "init-paging")]
484487
pub(crate) fn set_pt_size(&mut self, size: usize) {
@@ -668,8 +671,8 @@ impl SandboxMemoryLayout {
668671
if after_init_offset != expected_pt_offset {
669672
return Err(new_error!(
670673
"Page table offset does not match expected: {}, actual: {}",
671-
expected_init_data_offset,
672-
init_data_offset
674+
expected_pt_offset,
675+
after_init_offset
673676
));
674677
}
675678

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ 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.
1515
*/
16+
#[cfg(feature = "init-paging")]
17+
use std::cmp::Ordering;
1618

1719
use flatbuffers::FlatBufferBuilder;
1820
use hyperlight_common::flatbuffer_wrappers::function_call::{
@@ -127,7 +129,7 @@ impl GuestPageTableBuffer {
127129
}
128130

129131
pub(crate) fn size(&self) -> usize {
130-
self.buffer.borrow().len() * PAGE_TABLE_SIZE
132+
self.buffer.borrow().len()
131133
}
132134

133135
pub(crate) fn into_bytes(self) -> Box<[u8]> {
@@ -308,8 +310,18 @@ impl SandboxMemoryManager<HostSharedMemory> {
308310
/// documentation at the bottom `set_stack_guard` for description
309311
/// of why it isn't.
310312
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
313+
#[cfg(feature ="init-paging")]
311314
pub(crate) fn check_stack_guard(&self) -> Result<bool> {
312-
Ok(true)
315+
let expected = self.stack_cookie;
316+
let offset = self.layout.get_top_of_user_stack_offset();
317+
let actual: [u8; STACK_COOKIE_LEN] = self.shared_mem.read(offset)?;
318+
let cmp_res = expected.iter().cmp(actual.iter());
319+
Ok(cmp_res == Ordering::Equal)
320+
}
321+
322+
#[cfg(not(feature ="init-paging"))]
323+
pub(crate) fn check_stack_guard(&self) -> Result<bool> {
324+
Ok(true)
313325
}
314326

315327
/// Get the address of the dispatch function in memory
@@ -409,8 +421,6 @@ impl SandboxMemoryManager<HostSharedMemory> {
409421
}
410422
}
411423

412-
// ...existing code...
413-
414424
#[cfg(test)]
415425
#[cfg(all(feature = "init-paging", target_arch = "x86_64"))]
416426
mod tests {

src/hyperlight_host/src/sandbox/snapshot.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,19 @@ pub struct Snapshot {
6262
/// require constant-time equality checking
6363
hash: [u8; 32],
6464

65-
/// TODO: this should not necessarily be around in the long term...
65+
/// Preinitialisation entry point for snapshots created directly from a
66+
/// guest binary.
6667
///
67-
/// When creating a snapshot directly from a guest binary, this
68-
/// tracks the address that we need to call into before actually
69-
/// using a sandbox from this snapshot in order to do
70-
/// preinitialisation. Ideally we would either not need to do this
71-
/// at all, or do it as part of the snapshot creation process and
72-
/// never need this.
68+
/// When creating a snapshot directly from a guest binary, this tracks
69+
/// the address that we need to call into before actually using a
70+
/// sandbox from this snapshot in order to perform guest-side
71+
/// preinitialisation.
72+
///
73+
/// Long-term, the intention is to run this preinitialisation eagerly as
74+
/// part of the snapshot creation process so that restored sandboxes can
75+
/// begin executing from their normal entry point without requiring this
76+
/// field. Until that refactoring happens, this remains part of the
77+
/// snapshot format and must be preserved.
7378
preinitialise: Option<u64>,
7479
}
7580

@@ -105,7 +110,8 @@ fn hash(memory: &[u8], regions: &[MemoryRegion]) -> Result<[u8; 32]> {
105110
}
106111

107112
impl Snapshot {
108-
/// Create a new snapshot that runs the guest binary identified by env
113+
/// Create a new snapshot from the guest binary identified by `env`. With the configuration
114+
/// specified in `cfg`.
109115
pub(crate) fn from_env<'a, 'b>(
110116
env: impl Into<GuestEnvironment<'a, 'b>>,
111117
cfg: SandboxConfiguration,

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ pub enum GuestBinary<'a> {
8888
impl<'a> GuestBinary<'a> {
8989
/// If the guest binary is identified by a file, canonicalise the path
9090
///
91+
/// For [`GuestBinary::FilePath`], this resolves the path to its canonical
92+
/// form. For [`GuestBinary::Buffer`], this method is a no-op.
9193
/// TODO: Maybe we should make the GuestEnvironment or
9294
/// GuestBinary constructors crate-private and turn this
9395
/// into an invariant on one of those types.
@@ -161,7 +163,7 @@ impl UninitializedSandbox {
161163
// that can be changed (from the original snapshot) is the configuration defines the behaviour of
162164
// `InterruptHandler` on Linux.
163165
//
164-
// This is ok for now as this is not a public
166+
// This is ok for now as this is not a public function
165167
fn from_snapshot(
166168
snapshot: Arc<Snapshot>,
167169
cfg: Option<SandboxConfiguration>,

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ pub(crate) fn set_up_hypervisor_partition(
120120
};
121121
let entrypoint_ptr = mgr
122122
.entrypoint_offset
123-
.map(|x| {
123+
.ok_or_else(|| new_error!("Entrypoint offset is None"))
124+
.and_then(|x| {
124125
let entrypoint_total_offset = mgr.load_addr.clone() + x;
125126
GuestPtr::try_from(entrypoint_total_offset)
126-
})
127-
.ok_or(new_error!("Failed to create entrypoint pointer"))??;
127+
})?;
128128

129129
// Create gdb thread if gdb is enabled and the configuration is provided
130130
#[cfg(gdb)]

0 commit comments

Comments
 (0)