Skip to content

Commit 2bbfbbb

Browse files
committed
refactor: gate x86_64-specific code in hyperlight-guest-bin
- Gate paging and exception modules on target_arch = x86_64 - Gate ProfiledLockedHeap (uses x86 out asm) on x86_64 - Skip musl/printf C compilation in build.rs on non-x86_64 Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent dfab682 commit 2bbfbbb

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

src/hyperlight_guest_bin/build.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ fn copy_includes<P: AsRef<Path>, Q: AsRef<Path> + std::fmt::Debug>(include_dir:
4242
fn cargo_main() {
4343
println!("cargo:rerun-if-changed=third_party");
4444

45-
let mut cfg = cc::Build::new();
45+
let target = env::var("TARGET").unwrap_or_default();
46+
let is_x86_64 = target.starts_with("x86_64");
4647

47-
if cfg!(feature = "printf") {
48-
cfg.include("third_party/printf")
49-
.file("third_party/printf/printf.c");
50-
}
48+
// Skip C/musl compilation on aarch64 — no musl support yet
49+
if is_x86_64 {
50+
let mut cfg = cc::Build::new();
51+
52+
if cfg!(feature = "printf") {
53+
cfg.include("third_party/printf")
54+
.file("third_party/printf/printf.c");
55+
}
5156

5257
if cfg!(feature = "libc") {
5358
let entries = glob::glob("third_party/musl/**/*.[cs]") // .c and .s files
@@ -108,19 +113,22 @@ fn cargo_main() {
108113
}
109114
cfg.compile("hyperlight_guest_bin");
110115
}
116+
} // end if is_x86_64
111117

112118
let out_dir = env::var("OUT_DIR").expect("cargo OUT_DIR not set");
113119
let include_dir = PathBuf::from(&out_dir).join("include");
114120
fs::create_dir_all(&include_dir)
115121
.unwrap_or_else(|e| panic!("Could not create include dir {:?}: {}", &include_dir, e));
116-
if cfg!(feature = "printf") {
117-
copy_includes(&include_dir, "third_party/printf/");
118-
}
119-
if cfg!(feature = "libc") {
120-
copy_includes(&include_dir, "third_party/musl/include");
121-
copy_includes(&include_dir, "third_party/musl/arch/generic");
122-
copy_includes(&include_dir, "third_party/musl/arch/x86_64");
123-
copy_includes(&include_dir, "third_party/musl/src/internal");
122+
if is_x86_64 {
123+
if cfg!(feature = "printf") {
124+
copy_includes(&include_dir, "third_party/printf/");
125+
}
126+
if cfg!(feature = "libc") {
127+
copy_includes(&include_dir, "third_party/musl/include");
128+
copy_includes(&include_dir, "third_party/musl/arch/generic");
129+
copy_includes(&include_dir, "third_party/musl/arch/x86_64");
130+
copy_includes(&include_dir, "third_party/musl/src/internal");
131+
}
124132
}
125133
/* do not canonicalize: clang has trouble with UNC paths */
126134
let include_str = include_dir

src/hyperlight_guest_bin/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use hyperlight_guest::guest_handle::handle::GuestHandle;
3434

3535
// === Modules ===
3636
#[cfg_attr(target_arch = "x86_64", path = "arch/amd64/mod.rs")]
37+
#[cfg_attr(target_arch = "aarch64", path = "arch/aarch64/mod.rs")]
3738
mod arch;
3839
// temporarily expose the architecture-specific exception interface;
3940
// this should be replaced with something a bit more abstract in the
@@ -49,12 +50,13 @@ pub mod guest_function {
4950
pub mod guest_logger;
5051
pub mod host_comm;
5152
pub mod memory;
53+
#[cfg(target_arch = "x86_64")]
5254
pub mod paging;
5355

5456
// Globals
55-
#[cfg(feature = "mem_profile")]
57+
#[cfg(all(feature = "mem_profile", target_arch = "x86_64"))]
5658
struct ProfiledLockedHeap<const ORDER: usize>(LockedHeap<ORDER>);
57-
#[cfg(feature = "mem_profile")]
59+
#[cfg(all(feature = "mem_profile", target_arch = "x86_64"))]
5860
unsafe impl<const ORDER: usize> alloc::alloc::GlobalAlloc for ProfiledLockedHeap<ORDER> {
5961
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
6062
let addr = unsafe { self.0.alloc(layout) };
@@ -107,10 +109,10 @@ unsafe impl<const ORDER: usize> alloc::alloc::GlobalAlloc for ProfiledLockedHeap
107109
}
108110

109111
// === Globals ===
110-
#[cfg(not(feature = "mem_profile"))]
112+
#[cfg(not(all(feature = "mem_profile", target_arch = "x86_64")))]
111113
#[global_allocator]
112114
pub(crate) static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::empty();
113-
#[cfg(feature = "mem_profile")]
115+
#[cfg(all(feature = "mem_profile", target_arch = "x86_64"))]
114116
#[global_allocator]
115117
pub(crate) static HEAP_ALLOCATOR: ProfiledLockedHeap<32> =
116118
ProfiledLockedHeap(LockedHeap::<32>::empty());
@@ -191,9 +193,9 @@ pub(crate) extern "C" fn generic_init(
191193

192194
let heap_start = (*peb_ptr).guest_heap.ptr as usize;
193195
let heap_size = (*peb_ptr).guest_heap.size as usize;
194-
#[cfg(not(feature = "mem_profile"))]
196+
#[cfg(not(all(feature = "mem_profile", target_arch = "x86_64")))]
195197
let heap_allocator = &HEAP_ALLOCATOR;
196-
#[cfg(feature = "mem_profile")]
198+
#[cfg(all(feature = "mem_profile", target_arch = "x86_64"))]
197199
let heap_allocator = &HEAP_ALLOCATOR.0;
198200
heap_allocator
199201
.try_lock()

0 commit comments

Comments
 (0)