std: Unsafe-wrap backtrace code held in-common

This commit is contained in:
Jubilee Young 2024-07-23 01:16:46 -07:00
parent ed809e9b79
commit e4d89bc802

View File

@ -1,4 +1,5 @@
//! Common code for printing backtraces. //! Common code for printing backtraces.
#![forbid(unsafe_op_in_unsafe_fn)]
use crate::backtrace_rs::{self, BacktraceFmt, BytesOrWideString, PrintFmt}; use crate::backtrace_rs::{self, BacktraceFmt, BytesOrWideString, PrintFmt};
use crate::borrow::Cow; use crate::borrow::Cow;
@ -62,6 +63,8 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
// Start immediately if we're not using a short backtrace. // Start immediately if we're not using a short backtrace.
let mut start = print_fmt != PrintFmt::Short; let mut start = print_fmt != PrintFmt::Short;
set_image_base(); set_image_base();
// SAFETY: we roll our own locking in this town
unsafe {
backtrace_rs::trace_unsynchronized(|frame| { backtrace_rs::trace_unsynchronized(|frame| {
if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES { if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
return false; return false;
@ -128,7 +131,8 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
idx += 1; idx += 1;
res.is_ok() res.is_ok()
}); })
};
res?; res?;
bt_fmt.finish()?; bt_fmt.finish()?;
if print_fmt == PrintFmt::Short { if print_fmt == PrintFmt::Short {