Skip to content

Commit a5ba136

Browse files
committed
Apply cargo fmt
Signed-off-by: adamperlin <[email protected]>
1 parent abe4d0f commit a5ba136

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

src/hyperlight_common/src/fixed_buf.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
use core::fmt;
1818
use core::result::Result;
1919

20-
2120
/// FixedStringBuf is a buffer that can hold a fixed-size string of capacity N.
2221
/// It is meant to be used with a slice that the user has pre-allocated
2322
/// to avoid extra allocations during string formatting.
@@ -28,11 +27,11 @@ pub struct FixedStringBuf<const N: usize> {
2827

2928
impl<'a, const N: usize> fmt::Write for FixedStringBuf<N> {
3029
fn write_str(&mut self, s: &str) -> fmt::Result {
31-
// we always reserve 1 byte for the null terminator,
30+
// we always reserve 1 byte for the null terminator,
3231
// as the buffer must be convertible to CStr.
3332
let buf_end = self.buf.len() - 1;
3433
if self.pos + s.len() > buf_end {
35-
return Err(fmt::Error)
34+
return Err(fmt::Error);
3635
}
3736

3837
self.buf[self.pos..self.pos + s.len()].copy_from_slice(s.as_bytes());
@@ -41,22 +40,22 @@ impl<'a, const N: usize> fmt::Write for FixedStringBuf<N> {
4140
}
4241
}
4342

44-
impl <const N: usize> FixedStringBuf<N> {
43+
impl<const N: usize> FixedStringBuf<N> {
4544
pub fn as_str(&self) -> Result<&str, core::str::Utf8Error> {
4645
core::str::from_utf8(&self.buf[..self.pos])
4746
}
4847

4948
pub const fn new() -> Self {
50-
return FixedStringBuf{
49+
return FixedStringBuf {
5150
buf: [0u8; N],
5251
pos: 0,
53-
}
52+
};
5453
}
5554

5655
/// Null terminates the underlying buffer,
5756
/// and converts to a CStr which borrows the underlying buffer's slice.
5857
pub fn as_c_str(&mut self) -> Result<&core::ffi::CStr, core::ffi::FromBytesUntilNulError> {
59-
// null terminate the buffer.
58+
// null terminate the buffer.
6059
// we are guaranteed to have enough space since we always reserve one extra
6160
// byte for null in write_str, and assert buf.len() > 0 in the constructor.
6261
assert!(self.buf.len() > 0 && self.pos < self.buf.len());
@@ -65,12 +64,12 @@ impl <const N: usize> FixedStringBuf<N> {
6564
}
6665
}
6766

68-
6967
mod test {
7068
// disable unused import warnings
7169
#![allow(unused_imports)]
72-
use core::fmt::Write;
7370
use core::fmt;
71+
use core::fmt::Write;
72+
7473
use super::FixedStringBuf;
7574

7675
#[test]
@@ -90,4 +89,4 @@ mod test {
9089
let c_str = buf.as_c_str().unwrap();
9190
assert_eq!(c_str.to_bytes(), b"01234567890123456789");
9291
}
93-
}
92+
}

src/hyperlight_guest_bin/src/lib.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ limitations under the License.
1818
// === Dependencies ===
1919
extern crate alloc;
2020

21-
use core::fmt::{Write};
22-
2321
use alloc::string::ToString;
22+
use core::fmt::Write;
23+
2424
use buddy_system_allocator::LockedHeap;
2525
#[cfg(target_arch = "x86_64")]
2626
use exceptions::{gdt::load_gdt, idtr::load_idt};
2727
use guest_function::call::dispatch_function;
2828
use guest_function::register::GuestFunctionRegister;
2929
use guest_logger::init_logger;
30+
use hyperlight_common::fixed_buf::FixedStringBuf;
3031
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
3132
use hyperlight_common::mem::HyperlightPEB;
3233
#[cfg(feature = "mem_profile")]
3334
use hyperlight_common::outb::OutBAction;
34-
use hyperlight_common::fixed_buf::{FixedStringBuf};
3535
use hyperlight_guest::exit::{abort_with_code_and_message, halt};
3636
use hyperlight_guest::guest_handle::handle::GuestHandle;
3737
use hyperlight_guest_tracing::{trace, trace_function};
3838
use log::LevelFilter;
39-
use spin::{Once, Mutex, MutexGuard};
39+
use spin::{Mutex, MutexGuard, Once};
4040

4141
// === Modules ===
4242
#[cfg(target_arch = "x86_64")]
@@ -151,18 +151,33 @@ fn _panic_handler(info: &core::panic::PanicInfo) -> ! {
151151
let mut panic_buf_guard = PANIC_BUF.lock();
152152
let write_res = write!(panic_buf_guard, "{}", info);
153153
if let Err(_) = write_res {
154-
unsafe { abort_with_code_and_message(&[ErrorCode::UnknownError as u8], b"panic: message format failed\0".as_ptr() as *const i8)}
154+
unsafe {
155+
abort_with_code_and_message(
156+
&[ErrorCode::UnknownError as u8],
157+
b"panic: message format failed\0".as_ptr() as *const i8,
158+
)
159+
}
155160
}
156161

157162
// create a CStr from the underlying array in PANIC_BUF using the as_cstr method.
158163
// this wraps CStr::from_bytes_until_nul which takes a borrowed byte slice
159-
// and does not allocate.
164+
// and does not allocate.
160165
let c_string_res = panic_buf_guard.as_c_str();
161166
if let Err(_) = c_string_res {
162-
unsafe { abort_with_code_and_message(&[ErrorCode::UnknownError as u8], b"panic: failed to convert to CStr\0".as_ptr() as *const i8)}
167+
unsafe {
168+
abort_with_code_and_message(
169+
&[ErrorCode::UnknownError as u8],
170+
b"panic: failed to convert to CStr\0".as_ptr() as *const i8,
171+
)
172+
}
173+
}
174+
175+
unsafe {
176+
abort_with_code_and_message(
177+
&[ErrorCode::UnknownError as u8],
178+
c_string_res.unwrap().as_ptr(),
179+
)
163180
}
164-
165-
unsafe { abort_with_code_and_message(&[ErrorCode::UnknownError as u8], c_string_res.unwrap().as_ptr()) }
166181
}
167182

168183
// === Entrypoint ===
@@ -232,4 +247,4 @@ pub extern "C" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_leve
232247
});
233248

234249
halt();
235-
}
250+
}

src/hyperlight_host/tests/integration_test.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,12 @@ fn guest_panic_no_alloc() {
546546
.unwrap();
547547
let mut sbox: MultiUseSandbox = uninit.evolve().unwrap();
548548

549-
let res = sbox.call::<i32>(
550-
"ExhaustHeap", // uses the rust allocator to allocate small blocks on the heap until OOM
551-
()
552-
).unwrap_err();
549+
let res = sbox
550+
.call::<i32>(
551+
"ExhaustHeap", // uses the rust allocator to allocate small blocks on the heap until OOM
552+
(),
553+
)
554+
.unwrap_err();
553555
println!("{:?}", res);
554556

555557
if let HyperlightError::StackOverflow() = res {
@@ -560,7 +562,6 @@ fn guest_panic_no_alloc() {
560562
res,
561563
HyperlightError::GuestAborted(code, msg) if code == ErrorCode::UnknownError as u8 && msg.contains("memory allocation of ") && msg.contains("bytes failed")
562564
));
563-
564565
}
565566

566567
// Tests libc alloca

0 commit comments

Comments
 (0)