Skip to content

Commit 132f37c

Browse files
committed
1. Add vec allocation after alloc::alloc failure to ensure panic in
integration test 2. Resolve clippy errors Signed-off-by: adamperlin <[email protected]>
1 parent f2c5a30 commit 132f37c

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

src/hyperlight_common/src/fixed_buf.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct FixedStringBuf<const N: usize> {
2525
pub pos: usize,
2626
}
2727

28-
impl<'a, const N: usize> fmt::Write for FixedStringBuf<N> {
28+
impl<const N: usize> fmt::Write for FixedStringBuf<N> {
2929
fn write_str(&mut self, s: &str) -> fmt::Result {
3030
// we always reserve 1 byte for the null terminator,
3131
// as the buffer must be convertible to CStr.
@@ -40,16 +40,22 @@ impl<'a, const N: usize> fmt::Write for FixedStringBuf<N> {
4040
}
4141
}
4242

43+
impl<const N: usize> Default for FixedStringBuf<N> {
44+
fn default() -> Self {
45+
FixedStringBuf::<N>::new()
46+
}
47+
}
48+
4349
impl<const N: usize> FixedStringBuf<N> {
4450
pub fn as_str(&self) -> Result<&str, core::str::Utf8Error> {
4551
core::str::from_utf8(&self.buf[..self.pos])
4652
}
4753

4854
pub const fn new() -> Self {
49-
return FixedStringBuf {
55+
FixedStringBuf {
5056
buf: [0u8; N],
5157
pos: 0,
52-
};
58+
}
5359
}
5460

5561
/// Null terminates the underlying buffer,
@@ -58,7 +64,7 @@ impl<const N: usize> FixedStringBuf<N> {
5864
// null terminate the buffer.
5965
// we are guaranteed to have enough space since we always reserve one extra
6066
// byte for null in write_str, and assert buf.len() > 0 in the constructor.
61-
assert!(self.buf.len() > 0 && self.pos < self.buf.len());
67+
assert!(!self.buf.is_empty() && self.pos < self.buf.len());
6268
self.buf[self.pos] = 0;
6369
core::ffi::CStr::from_bytes_until_nul(&self.buf[..self.pos + 1])
6470
}
@@ -78,8 +84,8 @@ mod test {
7884

7985
assert_eq!(buf.as_str().unwrap(), "");
8086

81-
write!(&mut buf, "{}", "0123456789").expect("Failed to write to FixedBuf");
82-
write!(&mut buf, "{}", "0123456789").expect("Failed to write to FixedBuf");
87+
write!(&mut buf, "0123456789").expect("Failed to write to FixedBuf");
88+
write!(&mut buf, "0123456789").expect("Failed to write to FixedBuf");
8389
assert_eq!(buf.as_str().unwrap(), "01234567890123456789");
8490
assert_eq!(buf.pos, 20);
8591

src/hyperlight_guest_bin/src/lib.rs

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

21-
use alloc::string::ToString;
2221
use core::fmt::Write;
2322

2423
use buddy_system_allocator::LockedHeap;
@@ -150,11 +149,11 @@ static PANIC_BUF: Mutex<FixedStringBuf<512>> = Mutex::new(FixedStringBuf::new())
150149
fn _panic_handler(info: &core::panic::PanicInfo) -> ! {
151150
let mut panic_buf_guard = PANIC_BUF.lock();
152151
let write_res = write!(panic_buf_guard, "{}", info);
153-
if let Err(_) = write_res {
152+
if write_res.is_err() {
154153
unsafe {
155154
abort_with_code_and_message(
156155
&[ErrorCode::UnknownError as u8],
157-
b"panic: message format failed\0".as_ptr() as *const i8,
156+
c"panic: message format failed".as_ptr(),
158157
)
159158
}
160159
}
@@ -163,11 +162,11 @@ fn _panic_handler(info: &core::panic::PanicInfo) -> ! {
163162
// this wraps CStr::from_bytes_until_nul which takes a borrowed byte slice
164163
// and does not allocate.
165164
let c_string_res = panic_buf_guard.as_c_str();
166-
if let Err(_) = c_string_res {
165+
if c_string_res.is_err() {
167166
unsafe {
168167
abort_with_code_and_message(
169168
&[ErrorCode::UnknownError as u8],
170-
b"panic: failed to convert to CStr\0".as_ptr() as *const i8,
169+
c"panic: failed to convert to CStr".as_ptr(),
171170
)
172171
}
173172
}

src/tests/rust_guests/simpleguest/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,11 @@ unsafe fn exhaust_heap(_: &FunctionCall) -> ! {
517517
ptr = alloc::alloc::alloc_zeroed(layout);
518518
}
519519

520+
// after alloc::alloc_zeroed failure (null return when called in loop above)
521+
// allocate a Vec to ensure OOM panic
522+
let vec = Vec::<i32>::with_capacity(1);
523+
black_box(vec);
524+
520525
panic!("function should have panicked before due to OOM")
521526
}
522527

src/tests/rust_guests/witguest/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)