Skip to content

Commit 110ec8e

Browse files
committed
Update hyperlight-guest to 2024 edition
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent ad74c31 commit 110ec8e

16 files changed

+99
-95
lines changed

src/hyperlight_guest/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ fn cargo_main() {
105105
}
106106

107107
if cfg!(windows) {
108-
env::set_var("AR_x86_64_unknown_none", "llvm-ar");
108+
unsafe { env::set_var("AR_x86_64_unknown_none", "llvm-ar") };
109109
} else {
110-
env::set_var("AR_x86_64_pc_windows_msvc", "llvm-lib");
110+
unsafe { env::set_var("AR_x86_64_pc_windows_msvc", "llvm-lib") };
111111
}
112112

113113
cfg.compile("hyperlight_guest");

src/hyperlight_guest/src/chkstk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use hyperlight_common::mem::RunMode;
2222
use crate::guest_error::{set_invalid_runmode_error, set_stack_allocate_error};
2323
use crate::{MIN_STACK_ADDRESS, RUNNING_MODE};
2424

25-
extern "win64" {
25+
unsafe extern "win64" {
2626
fn __chkstk();
2727
fn __chkstk_in_proc();
2828
}

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn halt() {
4242
}
4343
}
4444

45-
#[no_mangle]
45+
#[unsafe(no_mangle)]
4646
pub extern "C" fn abort() -> ! {
4747
abort_with_code(0)
4848
}
@@ -57,17 +57,19 @@ pub fn abort_with_code(code: i32) -> ! {
5757
/// # Safety
5858
/// This function is unsafe because it dereferences a raw pointer.
5959
pub unsafe fn abort_with_code_and_message(code: i32, message_ptr: *const c_char) -> ! {
60-
let peb_ptr = P_PEB.unwrap();
61-
copy_nonoverlapping(
62-
message_ptr,
63-
(*peb_ptr).guestPanicContextData.guestPanicContextDataBuffer as *mut c_char,
64-
CStr::from_ptr(message_ptr).count_bytes() + 1, // +1 for null terminator
65-
);
66-
outb(OutBAction::Abort as u16, code as u8);
67-
unreachable!()
60+
unsafe {
61+
let peb_ptr = P_PEB.unwrap();
62+
copy_nonoverlapping(
63+
message_ptr,
64+
(*peb_ptr).guestPanicContextData.guestPanicContextDataBuffer as *mut c_char,
65+
CStr::from_ptr(message_ptr).count_bytes() + 1, // +1 for null terminator
66+
);
67+
outb(OutBAction::Abort as u16, code as u8);
68+
unreachable!()
69+
}
6870
}
6971

70-
extern "C" {
72+
unsafe extern "C" {
7173
fn hyperlight_main();
7274
fn srand(seed: u32);
7375
}
@@ -76,7 +78,7 @@ static INIT: Once = Once::new();
7678

7779
// Note: entrypoint cannot currently have a stackframe >4KB, as that will invoke __chkstk on msvc
7880
// target without first having setup global `RUNNING_MODE` variable, which __chkstk relies on.
79-
#[no_mangle]
81+
#[unsafe(no_mangle)]
8082
pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_level: u64) {
8183
if peb_address == 0 {
8284
panic!("PEB address is null");
@@ -88,7 +90,7 @@ pub extern "win64" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_
8890
let peb_ptr = P_PEB.unwrap();
8991
__security_cookie = peb_address ^ seed;
9092

91-
let srand_seed = ((peb_address << 8 ^ seed >> 4) >> 32) as u32;
93+
let srand_seed = (((peb_address << 8) ^ (seed >> 4)) >> 32) as u32;
9294

9395
// Set the seed for the random number generator for C code using rand;
9496
srand(srand_seed);

src/hyperlight_guest/src/gdt.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,28 @@ struct GdtPointer {
7272
}
7373

7474
/// Load the GDT
75-
pub unsafe fn load_gdt() {
75+
pub(crate) unsafe fn load_gdt() {
7676
let gdt_ptr = GdtPointer {
7777
size: (core::mem::size_of::<[GdtEntry; 3]>() - 1) as u16,
7878
base: addr_of!(GDT) as *const _ as u64,
7979
};
8080

81-
asm!(
82-
"lgdt [{0}]",
83-
"mov ax, 0x10", // Load data segment registers
84-
"mov ds, ax",
85-
"mov es, ax",
86-
"mov fs, ax",
87-
"mov gs, ax",
88-
"mov ss, ax",
89-
"push 0x08", // Push CS (kernel code segment)
90-
"lea rax, [2f + rip]", // Load the next instruction's address
91-
"push rax", // Push address onto stack
92-
"retfq", // Far return to update CS
93-
"2:", // Label for continued execution
94-
in(reg) &gdt_ptr,
95-
options(nostack, preserves_flags)
96-
);
81+
unsafe {
82+
asm!(
83+
"lgdt [{0}]",
84+
"mov ax, 0x10", // Load data segment registers
85+
"mov ds, ax",
86+
"mov es, ax",
87+
"mov fs, ax",
88+
"mov gs, ax",
89+
"mov ss, ax",
90+
"push 0x08", // Push CS (kernel code segment)
91+
"lea rax, [2f + rip]", // Load the next instruction's address
92+
"push rax", // Push address onto stack
93+
"retfq", // Far return to update CS
94+
"2:", // Label for continued execution
95+
in(reg) &gdt_ptr,
96+
options(nostack, preserves_flags)
97+
);
98+
}
9799
}

src/hyperlight_guest/src/guest_error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ pub(crate) fn set_error_and_halt(error_code: ErrorCode, message: &str) {
9292
halt();
9393
}
9494

95-
#[no_mangle]
95+
#[unsafe(no_mangle)]
9696
pub(crate) extern "win64" fn set_stack_allocate_error() {
9797
outb(OutBAction::Abort as u16, ErrorCode::StackOverflow as u8);
9898
}
9999

100-
#[no_mangle]
100+
#[unsafe(no_mangle)]
101101
pub(crate) extern "win64" fn set_invalid_runmode_error() {
102102
panic!("Invalid run mode in __chkstk");
103103
}
@@ -107,7 +107,7 @@ pub(crate) extern "win64" fn set_invalid_runmode_error() {
107107
/// # Safety
108108
/// TODO
109109
/// cbindgen:ignore
110-
#[no_mangle]
110+
#[unsafe(no_mangle)]
111111
#[allow(non_camel_case_types)]
112112
pub unsafe extern "C" fn setError(code: u64, message: *const c_char) {
113113
let error_code = ErrorCode::from(code);

src/hyperlight_guest/src/guest_function_call.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result<Vec<u8>
4343
}
4444

4545
// Find the function definition for the function call.
46-
if let Some(registered_function_definition) =
47-
unsafe { REGISTERED_GUEST_FUNCTIONS.get(&function_call.function_name) }
46+
if let Some(registered_function_definition) = REGISTERED_GUEST_FUNCTIONS
47+
.lock()
48+
.get(&function_call.function_name)
4849
{
4950
let function_call_parameter_types: Vec<ParameterType> = function_call
5051
.parameters
@@ -68,7 +69,7 @@ pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result<Vec<u8>
6869
// TODO: ideally we would define a default implementation of this with weak linkage so the guest is not required
6970
// to implement the function but its seems that weak linkage is an unstable feature so for now its probably better
7071
// to not do that.
71-
extern "Rust" {
72+
unsafe extern "Rust" {
7273
fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>>;
7374
}
7475

@@ -78,7 +79,7 @@ pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result<Vec<u8>
7879

7980
// This function is marked as no_mangle/inline to prevent the compiler from inlining it , if its inlined the epilogue will not be called
8081
// and we will leak memory as the epilogue will not be called as halt() is not going to return.
81-
#[no_mangle]
82+
#[unsafe(no_mangle)]
8283
#[inline(never)]
8384
fn internal_dispatch_function() -> Result<()> {
8485
reset_error();

src/hyperlight_guest/src/guest_function_register.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ impl GuestFunctionRegister {
5454
}
5555

5656
pub fn register_function(function_definition: GuestFunctionDefinition) {
57-
unsafe {
58-
// This is currently safe, because we are single threaded, but we
59-
// should find a better way to do this, see issue #808
60-
#[allow(static_mut_refs)]
61-
let gfd = &mut REGISTERED_GUEST_FUNCTIONS;
62-
gfd.register(function_definition);
63-
}
57+
let mut gfd = REGISTERED_GUEST_FUNCTIONS.lock();
58+
gfd.register(function_definition);
6459
}

src/hyperlight_guest/src/host_function_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn outb(port: u16, value: u8) {
110110
}
111111
}
112112

113-
extern "win64" {
113+
unsafe extern "win64" {
114114
fn hloutb(port: u16, value: u8);
115115
}
116116

src/hyperlight_guest/src/idtr.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ pub struct Idtr {
1111
static mut IDTR: Idtr = Idtr { limit: 0, base: 0 };
1212

1313
impl Idtr {
14-
pub unsafe fn init(&mut self, base: u64, size: u16) {
14+
unsafe fn init(&mut self, base: u64, size: u16) {
1515
self.limit = size - 1;
1616
self.base = base;
1717
}
1818

19-
pub unsafe fn load(&self) {
20-
core::arch::asm!("lidt [{}]", in(reg) self, options(readonly, nostack, preserves_flags));
19+
unsafe fn load(&self) {
20+
unsafe {
21+
core::arch::asm!("lidt [{}]", in(reg) self, options(readonly, nostack, preserves_flags));
22+
}
2123
}
2224
}
2325

@@ -27,6 +29,9 @@ pub(crate) unsafe fn load_idt() {
2729
let idt_size = 256 * size_of::<IdtEntry>();
2830
let expected_base = addr_of!(IDT) as *const _ as u64;
2931

30-
IDTR.init(expected_base, idt_size as u16);
31-
IDTR.load();
32+
#[allow(static_mut_refs)]
33+
unsafe {
34+
IDTR.init(expected_base, idt_size as u16);
35+
IDTR.load();
36+
}
3237
}

src/hyperlight_guest/src/interrupt_entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use core::arch::global_asm;
2121

2222
use crate::interrupt_handlers::hl_exception_handler;
2323

24-
extern "sysv64" {
24+
unsafe extern "sysv64" {
2525
// Exception handlers
2626
pub(crate) fn _do_excp0();
2727
pub(crate) fn _do_excp1();

0 commit comments

Comments
 (0)