Skip to content

Commit 77c82bd

Browse files
committed
fmt changes
Signed-off-by: Simon Davies <[email protected]>
1 parent c6aee96 commit 77c82bd

File tree

10 files changed

+133
-122
lines changed

10 files changed

+133
-122
lines changed

src/hyperlight_guest/build.rs

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

106106
if cfg!(windows) {
107-
unsafe {env::set_var("AR_x86_64_unknown_none", "llvm-ar")};
107+
unsafe { env::set_var("AR_x86_64_unknown_none", "llvm-ar") };
108108
} else {
109-
unsafe {env::set_var("AR_x86_64_pc_windows_msvc", "llvm-lib")};
109+
unsafe { env::set_var("AR_x86_64_pc_windows_msvc", "llvm-lib") };
110110
}
111111

112112
cfg.compile("hyperlight_guest");

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,24 @@ pub fn abort_with_code(code: &[u8]) -> ! {
4949
///
5050
/// # Safety
5151
/// This function is unsafe because it dereferences a raw pointer.
52-
pub unsafe fn abort_with_code_and_message(code: &[u8], message_ptr: *const c_char) -> ! { unsafe {
53-
// Step 1: Send abort code (typically 1 byte, but `code` allows flexibility)
54-
outb(OutBAction::Abort as u16, code);
52+
pub unsafe fn abort_with_code_and_message(code: &[u8], message_ptr: *const c_char) -> ! {
53+
unsafe {
54+
// Step 1: Send abort code (typically 1 byte, but `code` allows flexibility)
55+
outb(OutBAction::Abort as u16, code);
5556

56-
// Step 2: Convert the C string to bytes
57-
let message_bytes = CStr::from_ptr(message_ptr).to_bytes(); // excludes null terminator
57+
// Step 2: Convert the C string to bytes
58+
let message_bytes = CStr::from_ptr(message_ptr).to_bytes(); // excludes null terminator
5859

59-
// Step 3: Send the message itself in chunks
60-
outb(OutBAction::Abort as u16, message_bytes);
60+
// Step 3: Send the message itself in chunks
61+
outb(OutBAction::Abort as u16, message_bytes);
6162

62-
// Step 4: Send abort terminator to signal completion (e.g., 0xFF)
63-
outb(OutBAction::Abort as u16, &[0xFF]);
63+
// Step 4: Send abort terminator to signal completion (e.g., 0xFF)
64+
outb(OutBAction::Abort as u16, &[0xFF]);
6465

65-
// This function never returns
66-
unreachable!()
67-
}}
66+
// This function never returns
67+
unreachable!()
68+
}
69+
}
6870

6971
unsafe extern "C" {
7072
fn hyperlight_main();

src/hyperlight_guest/src/exceptions/gdt.rs

Lines changed: 24 additions & 22 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() { unsafe {
76-
let gdt_ptr = GdtPointer {
77-
size: (core::mem::size_of::<[GdtEntry; 3]>() - 1) as u16,
78-
base: addr_of!(GDT) as *const _ as u64,
79-
};
75+
pub unsafe fn load_gdt() {
76+
unsafe {
77+
let gdt_ptr = GdtPointer {
78+
size: (core::mem::size_of::<[GdtEntry; 3]>() - 1) as u16,
79+
base: addr_of!(GDT) as *const _ as u64,
80+
};
8081

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-
);
97-
}}
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+
}
99+
}

src/hyperlight_guest/src/exceptions/idtr.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@ impl Idtr {
1616
self.base = base;
1717
}
1818

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

24-
pub(crate) unsafe fn load_idt() { unsafe {
25-
init_idt();
26-
27-
let idt_size = 256 * size_of::<IdtEntry>();
28-
let expected_base = addr_of!(IDT) as *const _ as u64;
29-
30-
// Use &raw mut to get a mutable raw pointer, then dereference it
31-
// this is to avoid the clippy warning "shared reference to mutable static"
32-
let idtr = &mut *(&raw mut IDTR);
33-
idtr.init(expected_base, idt_size as u16);
34-
idtr.load();
35-
}}
26+
pub(crate) unsafe fn load_idt() {
27+
unsafe {
28+
init_idt();
29+
30+
let idt_size = 256 * size_of::<IdtEntry>();
31+
let expected_base = addr_of!(IDT) as *const _ as u64;
32+
33+
// Use &raw mut to get a mutable raw pointer, then dereference it
34+
// this is to avoid the clippy warning "shared reference to mutable static"
35+
let idtr = &mut *(&raw mut IDTR);
36+
idtr.init(expected_base, idt_size as u16);
37+
idtr.load();
38+
}
39+
}

src/hyperlight_guest/src/guest_function_call.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result<Vec<u8>
4545
// Find the function definition for the function call.
4646
// Use &raw const to get an immutable reference to the static HashMap
4747
// this is to avoid the clippy warning "shared reference to mutable static"
48-
if let Some(registered_function_definition) = unsafe { (*(&raw const REGISTERED_GUEST_FUNCTIONS)).get(&function_call.function_name) }
48+
if let Some(registered_function_definition) =
49+
unsafe { (*(&raw const REGISTERED_GUEST_FUNCTIONS)).get(&function_call.function_name) }
4950
{
5051
let function_call_parameter_types: Vec<ParameterType> = function_call
5152
.parameters

src/hyperlight_guest/src/host_function_call.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ pub fn outb(port: u16, data: &[u8]) {
107107
}
108108
}
109109

110-
pub(crate) unsafe fn out32(port: u16, val: u32) { unsafe {
111-
arch::asm!("out dx, eax", in("dx") port, in("eax") val, options(preserves_flags, nomem, nostack));
112-
}}
110+
pub(crate) unsafe fn out32(port: u16, val: u32) {
111+
unsafe {
112+
arch::asm!("out dx, eax", in("dx") port, in("eax") val, options(preserves_flags, nomem, nostack));
113+
}
114+
}
113115

114116
/// Prints a message using `OutBAction::DebugPrint`. It transmits bytes of a message
115117
/// through several VMExists and, with such, it is slower than

src/hyperlight_guest/src/memory.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,25 @@ unsafe fn alloc_helper(size: usize, zero: bool) -> *mut c_void {
8282
/// # Safety
8383
/// The returned pointer must be freed with `memory::free` when it is no longer needed, otherwise memory will leak.
8484
#[unsafe(no_mangle)]
85-
pub unsafe extern "C" fn malloc(size: usize) -> *mut c_void { unsafe {
86-
alloc_helper(size, false)
87-
}}
85+
pub unsafe extern "C" fn malloc(size: usize) -> *mut c_void {
86+
unsafe { alloc_helper(size, false) }
87+
}
8888

8989
/// Allocates a block of memory for an array of `nmemb` elements, each of `size` bytes.
9090
/// The memory is initialized to 0s.
9191
///
9292
/// # Safety
9393
/// The returned pointer must be freed with `memory::free` when it is no longer needed, otherwise memory will leak.
9494
#[unsafe(no_mangle)]
95-
pub unsafe extern "C" fn calloc(nmemb: usize, size: usize) -> *mut c_void { unsafe {
96-
let total_size = nmemb
97-
.checked_mul(size)
98-
.expect("nmemb * size should not overflow in calloc");
95+
pub unsafe extern "C" fn calloc(nmemb: usize, size: usize) -> *mut c_void {
96+
unsafe {
97+
let total_size = nmemb
98+
.checked_mul(size)
99+
.expect("nmemb * size should not overflow in calloc");
99100

100-
alloc_helper(total_size, true)
101-
}}
101+
alloc_helper(total_size, true)
102+
}
103+
}
102104

103105
/// Frees the memory block pointed to by `ptr`.
104106
///
@@ -124,39 +126,37 @@ pub unsafe extern "C" fn free(ptr: *mut c_void) {
124126
pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
125127
if ptr.is_null() {
126128
// If the pointer is null, treat as a malloc
127-
return unsafe {malloc(size)};
129+
return unsafe { malloc(size) };
128130
}
129131

130132
if size == 0 {
131133
// If the size is 0, treat as a free and return null
132-
unsafe {
133-
free(ptr);
134+
unsafe {
135+
free(ptr);
134136
}
135137
return ptr::null_mut();
136138
}
137139

138-
139-
let total_new_size = size
140-
.checked_add(size_of::<Layout>())
141-
.expect("data and layout size should not overflow in realloc");
140+
let total_new_size = size
141+
.checked_add(size_of::<Layout>())
142+
.expect("data and layout size should not overflow in realloc");
142143

143-
let block_start = unsafe { (ptr as *const Layout).sub(1) };
144-
let old_layout = unsafe { block_start.read() };
145-
let new_layout = Layout::from_size_align(total_new_size, MAX_ALIGN).unwrap();
144+
let block_start = unsafe { (ptr as *const Layout).sub(1) };
145+
let old_layout = unsafe { block_start.read() };
146+
let new_layout = Layout::from_size_align(total_new_size, MAX_ALIGN).unwrap();
146147

147-
let new_block_start =
148-
unsafe {alloc::alloc::realloc(block_start as *mut u8, old_layout, total_new_size)}
149-
as *mut Layout;
148+
let new_block_start =
149+
unsafe { alloc::alloc::realloc(block_start as *mut u8, old_layout, total_new_size) }
150+
as *mut Layout;
150151

151-
if new_block_start.is_null() {
152-
// Realloc failed
153-
abort_with_code(&[ErrorCode::MallocFailed as u8]);
154-
} else {
155-
// Update the stored Layout, then return ptr to memory right after the Layout.
156-
unsafe {
157-
new_block_start.write(new_layout);
158-
new_block_start.add(1) as *mut c_void
159-
}
152+
if new_block_start.is_null() {
153+
// Realloc failed
154+
abort_with_code(&[ErrorCode::MallocFailed as u8]);
155+
} else {
156+
// Update the stored Layout, then return ptr to memory right after the Layout.
157+
unsafe {
158+
new_block_start.write(new_layout);
159+
new_block_start.add(1) as *mut c_void
160160
}
161-
161+
}
162162
}

src/hyperlight_guest/src/print.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,39 @@ static mut MESSAGE_BUFFER: Vec<u8> = Vec::new();
3333
/// This function is not thread safe
3434
#[unsafe(no_mangle)]
3535
#[allow(static_mut_refs)]
36-
pub unsafe extern "C" fn _putchar(c: c_char) { unsafe {
37-
let char = c as u8;
36+
pub unsafe extern "C" fn _putchar(c: c_char) {
37+
unsafe {
38+
let char = c as u8;
3839

39-
// Extend buffer capacity if it's empty (like `with_capacity` in lazy_static).
40-
// TODO: replace above Vec::new() with Vec::with_capacity once it's stable in const contexts.
41-
if MESSAGE_BUFFER.capacity() == 0 {
42-
MESSAGE_BUFFER.reserve(BUFFER_SIZE);
43-
}
40+
// Extend buffer capacity if it's empty (like `with_capacity` in lazy_static).
41+
// TODO: replace above Vec::new() with Vec::with_capacity once it's stable in const contexts.
42+
if MESSAGE_BUFFER.capacity() == 0 {
43+
MESSAGE_BUFFER.reserve(BUFFER_SIZE);
44+
}
4445

45-
MESSAGE_BUFFER.push(char);
46+
MESSAGE_BUFFER.push(char);
4647

47-
if MESSAGE_BUFFER.len() == BUFFER_SIZE || char == b'\0' {
48-
let str = if char == b'\0' {
49-
CStr::from_bytes_until_nul(&MESSAGE_BUFFER)
50-
.expect("No null byte in buffer")
51-
.to_string_lossy()
52-
.into_owned()
53-
} else {
54-
String::from_utf8(mem::take(&mut MESSAGE_BUFFER))
55-
.expect("Failed to convert buffer to string")
56-
};
48+
if MESSAGE_BUFFER.len() == BUFFER_SIZE || char == b'\0' {
49+
let str = if char == b'\0' {
50+
CStr::from_bytes_until_nul(&MESSAGE_BUFFER)
51+
.expect("No null byte in buffer")
52+
.to_string_lossy()
53+
.into_owned()
54+
} else {
55+
String::from_utf8(mem::take(&mut MESSAGE_BUFFER))
56+
.expect("Failed to convert buffer to string")
57+
};
5758

58-
// HostPrint returns an i32, but we don't care about the return value
59-
let _ = call_host_function::<i32>(
60-
"HostPrint",
61-
Some(Vec::from(&[ParameterValue::String(str)])),
62-
ReturnType::Int,
63-
)
64-
.expect("Failed to call HostPrint");
59+
// HostPrint returns an i32, but we don't care about the return value
60+
let _ = call_host_function::<i32>(
61+
"HostPrint",
62+
Some(Vec::from(&[ParameterValue::String(str)])),
63+
ReturnType::Int,
64+
)
65+
.expect("Failed to call HostPrint");
6566

66-
// Clear the buffer after sending
67-
MESSAGE_BUFFER.clear();
67+
// Clear the buffer after sending
68+
MESSAGE_BUFFER.clear();
69+
}
6870
}
69-
}}
71+
}

src/hyperlight_host/src/mem/layout.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,7 @@ impl SandboxMemoryLayout {
462462

463463
// Get the number of pages needed for the PTs
464464

465-
let num_pages: usize = total_mapped_memory_size.div_ceil(AMOUNT_OF_MEMORY_PER_PT)
466-
+ 3; // PML4, PDPT, PD
465+
let num_pages: usize = total_mapped_memory_size.div_ceil(AMOUNT_OF_MEMORY_PER_PT) + 3; // PML4, PDPT, PD
467466

468467
num_pages * PAGE_SIZE_USIZE
469468
}

src/hyperlight_host/src/mem/ptr_offset.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,10 @@ impl Sub<Offset> for u64 {
201201
impl PartialEq<usize> for Offset {
202202
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
203203
fn eq(&self, other: &usize) -> bool {
204-
match usize::try_from(self) { Ok(offset_usize) => {
205-
offset_usize == *other
206-
} _ => {
207-
false
208-
}}
204+
match usize::try_from(self) {
205+
Ok(offset_usize) => offset_usize == *other,
206+
_ => false,
207+
}
209208
}
210209
}
211210

0 commit comments

Comments
 (0)