Skip to content

Commit 576df92

Browse files
committed
Use heapless crate instead of custom fixed string buffer
for panic string formatting Signed-off-by: adamperlin <[email protected]>
1 parent ea3bcac commit 576df92

File tree

10 files changed

+168
-111
lines changed

10 files changed

+168
-111
lines changed

Cargo.lock

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

src/hyperlight_common/src/fixed_buf.rs

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/hyperlight_common/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,4 @@ pub mod mem;
4141
pub mod outb;
4242

4343
/// cbindgen:ignore
44-
pub mod resource;
45-
46-
pub mod fixed_buf;
44+
pub mod resource;

src/hyperlight_guest_bin/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ hyperlight-guest-tracing = { workspace = true, default-features = false }
2727
buddy_system_allocator = "0.11.0"
2828
log = { version = "0.4", default-features = false }
2929
spin = "0.10.0"
30+
heapless = "0.9.1"
3031

3132
[lints]
3233
workspace = true

src/hyperlight_guest_bin/src/lib.rs

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

21+
use core::ffi::CStr;
2122
use core::fmt::Write;
2223

2324
use buddy_system_allocator::LockedHeap;
@@ -26,7 +27,7 @@ use exceptions::{gdt::load_gdt, idtr::load_idt};
2627
use guest_function::call::dispatch_function;
2728
use guest_function::register::GuestFunctionRegister;
2829
use guest_logger::init_logger;
29-
use hyperlight_common::fixed_buf::FixedStringBuf;
30+
use heapless::String;
3031
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
3132
use hyperlight_common::mem::HyperlightPEB;
3233
#[cfg(feature = "mem_profile")]
@@ -143,7 +144,7 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
143144
_panic_handler(info)
144145
}
145146

146-
static PANIC_BUF: Mutex<FixedStringBuf<512>> = Mutex::new(FixedStringBuf::new());
147+
static PANIC_BUF: Mutex<String<512>> = Mutex::new(String::new());
147148

148149
#[inline(always)]
149150
fn _panic_handler(info: &core::panic::PanicInfo) -> ! {
@@ -158,23 +159,31 @@ fn _panic_handler(info: &core::panic::PanicInfo) -> ! {
158159
}
159160
}
160161

161-
// create a CStr from the underlying array in PANIC_BUF using the as_cstr method.
162-
// this wraps CStr::from_bytes_until_nul which takes a borrowed byte slice
163-
// and does not allocate.
164-
let c_string_res = panic_buf_guard.as_c_str();
165-
if c_string_res.is_err() {
162+
let append_res = panic_buf_guard.push('\0');
163+
if append_res.is_err() {
166164
unsafe {
167165
abort_with_code_and_message(
168166
&[ErrorCode::UnknownError as u8],
169-
c"panic: failed to convert to CStr".as_ptr(),
167+
c"panic: message too long".as_ptr(),
168+
)
169+
}
170+
}
171+
172+
let c_str_res = CStr::from_bytes_with_nul(panic_buf_guard.as_bytes());
173+
174+
if c_str_res.is_err() {
175+
unsafe {
176+
abort_with_code_and_message(
177+
&[ErrorCode::UnknownError as u8],
178+
c"panic: failed to convert to CString".as_ptr(),
170179
)
171180
}
172181
}
173182

174183
unsafe {
175184
abort_with_code_and_message(
176185
&[ErrorCode::UnknownError as u8],
177-
c_string_res.unwrap().as_ptr(),
186+
c_str_res.unwrap().as_ptr(),
178187
)
179188
}
180189
}

src/hyperlight_host/tests/integration_test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,6 @@ fn guest_panic_no_alloc() {
552552
(),
553553
)
554554
.unwrap_err();
555-
println!("{:?}", res);
556555

557556
if let HyperlightError::StackOverflow() = res {
558557
panic!("panic on OOM caused stack overflow, this implies allocation in panic handler");

src/tests/rust_guests/callbackguest/Cargo.lock

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

src/tests/rust_guests/dummyguest/Cargo.lock

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

0 commit comments

Comments
 (0)