Skip to content

Commit 43e5480

Browse files
committed
Stack allocate panic message buffer to avoid use of mutex on static
Signed-off-by: adamperlin <[email protected]>
1 parent 1df1be6 commit 43e5480

File tree

1 file changed

+5
-6
lines changed
  • src/hyperlight_guest_bin/src

1 file changed

+5
-6
lines changed

src/hyperlight_guest_bin/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ 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::{Mutex, Once};
39+
use spin::Once;
4040

4141
// === Modules ===
4242
#[cfg(target_arch = "x86_64")]
@@ -144,12 +144,11 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
144144
_panic_handler(info)
145145
}
146146

147-
static PANIC_BUF: Mutex<String<512>> = Mutex::new(String::new());
148-
149147
#[inline(always)]
150148
fn _panic_handler(info: &core::panic::PanicInfo) -> ! {
151-
let mut panic_buf_guard = PANIC_BUF.lock();
152-
let write_res = write!(panic_buf_guard, "{}\0", info);
149+
// stack allocate a 256-byte message buffer.
150+
let mut panic_buf = String::<256>::new();
151+
let write_res = write!(panic_buf, "{}\0", info);
153152
if write_res.is_err() {
154153
unsafe {
155154
abort_with_code_and_message(
@@ -159,7 +158,7 @@ fn _panic_handler(info: &core::panic::PanicInfo) -> ! {
159158
}
160159
}
161160

162-
let c_str_res = CStr::from_bytes_with_nul(panic_buf_guard.as_bytes());
161+
let c_str_res = CStr::from_bytes_with_nul(panic_buf.as_bytes());
163162
if c_str_res.is_err() {
164163
unsafe {
165164
abort_with_code_and_message(

0 commit comments

Comments
 (0)