Skip to content

Commit 46c86bc

Browse files
cshungCopilot
andcommitted
refactor: concretise get_memory_regions to GuestMemoryRegion
Rename get_memory_regions_ to get_memory_regions and remove the generic type parameter. All callers use GuestMemoryRegion, so the generic is unnecessary. The host_base argument is now always BASE_ADDRESS, supplied internally. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6f20a05d-6bee-4e2e-b320-12f8d9759bbc Signed-off-by: cshung <3410332+cshung@users.noreply.github.com>
1 parent 4a30d2c commit 46c86bc

5 files changed

Lines changed: 9 additions & 19 deletions

File tree

.github/workflows/dep_build_guests.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
run: |
5555
sudo chown -R $(id -u):$(id -g) /opt/cargo || true
5656
57-
# cargo-hyperlight builds a custom sysroot for x86_64-hyperlight-none target.
57+
# cargo-hyperlight builds a custom sysroot for the Hyperlight guest target.
5858
# rust-cache cleans "anything not a dependency" from target dirs, removing the sysroot.
5959
# We cache sysroot separately to avoid rebuilding it (~10s) on every run.
6060
- name: Sysroot cache
@@ -89,7 +89,6 @@ jobs:
8989
just move-rust-guests ${{ inputs.config }}
9090
9191
- name: Build non-PIE Rust guests
92-
if: inputs.arch == 'X64'
9392
run: |
9493
just build-rust-guests-non-pie ${{ inputs.config }}
9594
just move-rust-guests-non-pie ${{ inputs.config }}
@@ -115,4 +114,3 @@ jobs:
115114
path: src/tests/c_guests/bin/${{ inputs.config }}/
116115
retention-days: 1
117116
if-no-files-found: error
118-

Justfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ build-and-move-c-guests: (build-c-guests "debug") (move-c-guests "debug") (build
8181
# Phase 2 uses plain cargo with --sysroot and non-PIE link flags.
8282
build-rust-guests-non-pie target=default-target: (ensure-cargo-hyperlight)
8383
cd src/tests/rust_guests/simpleguest && cargo hyperlight build --target-dir ../target-non-pie --profile={{ if target == "debug" { "dev" } else { target } }}
84-
{{ if os() == "windows" { "$env:RUSTC_BOOTSTRAP=1; $env:RUSTFLAGS='--sysroot=' + (Resolve-Path src/tests/rust_guests/target-non-pie/sysroot).Path + ' -C relocation-model=static -C link-args=--no-pie -C link-args=--image-base=0x1000000 --cfg=hyperlight --check-cfg=cfg(hyperlight) -Clink-args=-eentrypoint';" } else { "" } }} cd src/tests/rust_guests/simpleguest && {{ if os() == "windows" { "" } else { "RUSTC_BOOTSTRAP=1 RUSTFLAGS=\"--sysroot=$(cd .. && pwd)/target-non-pie/sysroot -C relocation-model=static -C link-args=--no-pie -C link-args=--image-base=0x1000000 --cfg=hyperlight --check-cfg=cfg(hyperlight) -Clink-args=-eentrypoint\"" } }} cargo build --target x86_64-hyperlight-none --target-dir ../target-non-pie/build --profile={{ if target == "debug" { "dev" } else { target } }}
84+
{{ if os() == "windows" { "$env:RUSTC_BOOTSTRAP=1; $env:RUSTFLAGS='--sysroot=' + (Resolve-Path src/tests/rust_guests/target-non-pie/sysroot).Path + ' -C relocation-model=static -C link-args=--no-pie -C link-args=--image-base=0x1000000 --cfg=hyperlight --check-cfg=cfg(hyperlight) -Clink-args=-eentrypoint';" } else { "" } }} cd src/tests/rust_guests/simpleguest && {{ if os() == "windows" { "" } else { "RUSTC_BOOTSTRAP=1 RUSTFLAGS=\"--sysroot=$(cd .. && pwd)/target-non-pie/sysroot -C relocation-model=static -C link-args=--no-pie -C link-args=--image-base=0x1000000 --cfg=hyperlight --check-cfg=cfg(hyperlight) -Clink-args=-eentrypoint\"" } }} cargo build --target {{ hyperlight-target }} --target-dir ../target-non-pie/build --profile={{ if target == "debug" { "dev" } else { target } }}
8585

86-
non_pie_guests_target := "src/tests/rust_guests/target-non-pie/build/x86_64-hyperlight-none"
86+
non_pie_guests_target := "src/tests/rust_guests/target-non-pie/build/" + hyperlight-target
8787

8888
@move-rust-guests-non-pie target=default-target:
8989
{{ if os() == "windows" { "New-Item -ItemType Directory -Path " + rust_guests_bin_dir + "/" + target + "/non_pie -Force | Out-Null" } else { "mkdir -p " + rust_guests_bin_dir + "/" + target + "/non_pie" } }}

src/hyperlight_host/src/hypervisor/hyperlight_vm/x86_64.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ mod tests {
930930
use crate::hypervisor::regs::{CommonSegmentRegister, CommonTableRegister, MXCSR_DEFAULT};
931931
use crate::hypervisor::virtual_machine::VirtualMachine;
932932
use crate::mem::layout::SandboxMemoryLayout;
933-
use crate::mem::memory_region::{GuestMemoryRegion, MemoryRegionFlags};
933+
use crate::mem::memory_region::MemoryRegionFlags;
934934
use crate::mem::mgr::{GuestPageTableBuffer, SandboxMemoryManager};
935935
use crate::mem::ptr::RawPtr;
936936
use crate::mem::shared_mem::{ExclusiveSharedMemory, ReadonlySharedMemory};
@@ -1494,11 +1494,7 @@ mod tests {
14941494
let pt_base_gpa = layout.get_pt_base_gpa();
14951495
let pt_buf = GuestPageTableBuffer::new(pt_base_gpa as usize);
14961496

1497-
for rgn in layout
1498-
.get_memory_regions_::<GuestMemoryRegion>(SandboxMemoryLayout::BASE_ADDRESS)
1499-
.unwrap()
1500-
.iter()
1501-
{
1497+
for rgn in layout.get_memory_regions().unwrap().iter() {
15021498
let readable = rgn.flags.contains(MemoryRegionFlags::READ);
15031499
let writable = rgn.flags.contains(MemoryRegionFlags::WRITE);
15041500
let executable = rgn.flags.contains(MemoryRegionFlags::EXECUTE);

src/hyperlight_host/src/mem/layout.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use tracing::{Span, instrument};
6969
use super::memory_region::MemoryRegionType::{self, Code, Heap, InitData, Peb};
7070
use super::memory_region::{
7171
DEFAULT_GUEST_BLOB_MEM_FLAGS, GuestMemoryRegion, MemoryRegion, MemoryRegion_,
72-
MemoryRegionFlags, MemoryRegionKind, MemoryRegionVecBuilder,
72+
MemoryRegionFlags, MemoryRegionVecBuilder,
7373
};
7474
#[cfg(readable_shared_mem)]
7575
use super::shared_mem::HostSharedMemory;
@@ -469,11 +469,8 @@ impl SandboxMemoryLayout {
469469

470470
/// Returns the memory regions associated with this memory layout,
471471
/// suitable for passing to a hypervisor for mapping into memory
472-
pub(crate) fn get_memory_regions_<K: MemoryRegionKind>(
473-
&self,
474-
host_base: K::HostBaseType,
475-
) -> Result<Vec<MemoryRegion_<K>>> {
476-
let mut builder = MemoryRegionVecBuilder::new(Self::BASE_ADDRESS, host_base);
472+
pub(crate) fn get_memory_regions(&self) -> Result<Vec<MemoryRegion_<GuestMemoryRegion>>> {
473+
let mut builder = MemoryRegionVecBuilder::new(Self::BASE_ADDRESS, Self::BASE_ADDRESS);
477474

478475
// code
479476
let peb_offset = builder.push_page_aligned(
@@ -580,7 +577,7 @@ impl SandboxMemoryLayout {
580577
let load_addr = self.get_guest_code_address() as u64;
581578
let code_virt_base = if is_pie { load_addr } else { elf_base_va };
582579

583-
let mut regions = self.get_memory_regions_::<GuestMemoryRegion>(Self::BASE_ADDRESS)?;
580+
let mut regions = self.get_memory_regions()?;
584581

585582
if !is_pie {
586583
let code_virt_end = code_virt_base.checked_add(loaded_size).ok_or_else(|| {

src/hyperlight_host/tests/integration_test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,6 @@ fn hw_timer_interrupts() {
18921892
}
18931893

18941894
#[test]
1895-
#[cfg(target_arch = "x86_64")]
18961895
fn non_pie_guest_hello_world() {
18971896
let path =
18981897
hyperlight_testing::simple_guest_non_pie_as_string().expect("non-PIE guest not found");

0 commit comments

Comments
 (0)