Skip to content

Commit

Permalink
Rollup merge of rust-lang#128089 - workingjubilee:commonly-wrapped-to…
Browse files Browse the repository at this point in the history
…-make-safe, r=ChrisDenton

std: Unsafe-wrap actually-universal platform code

Every platform compiles the unsafe parts of this code, so just clean this up. Almost entirely a whitespace diff.
  • Loading branch information
matthiaskrgr authored Jul 23, 2024
2 parents ad9a52d + de2a037 commit ee86041
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 65 deletions.
118 changes: 61 additions & 57 deletions std/src/sys/backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Common code for printing backtraces.
#![forbid(unsafe_op_in_unsafe_fn)]

use crate::backtrace_rs::{self, BacktraceFmt, BytesOrWideString, PrintFmt};
use crate::borrow::Cow;
Expand Down Expand Up @@ -62,73 +63,76 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
// Start immediately if we're not using a short backtrace.
let mut start = print_fmt != PrintFmt::Short;
set_image_base();
backtrace_rs::trace_unsynchronized(|frame| {
if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
return false;
}
// SAFETY: we roll our own locking in this town
unsafe {
backtrace_rs::trace_unsynchronized(|frame| {
if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
return false;
}

let mut hit = false;
backtrace_rs::resolve_frame_unsynchronized(frame, |symbol| {
hit = true;

// Any frames between `__rust_begin_short_backtrace` and `__rust_end_short_backtrace`
// are omitted from the backtrace in short mode, `__rust_end_short_backtrace` will be
// called before the panic hook, so we won't ignore any frames if there is no
// invoke of `__rust_begin_short_backtrace`.
if print_fmt == PrintFmt::Short {
if let Some(sym) = symbol.name().and_then(|s| s.as_str()) {
if start && sym.contains("__rust_begin_short_backtrace") {
start = false;
return;
}
if sym.contains("__rust_end_short_backtrace") {
start = true;
return;
}
if !start {
omitted_count += 1;
let mut hit = false;
backtrace_rs::resolve_frame_unsynchronized(frame, |symbol| {
hit = true;

// Any frames between `__rust_begin_short_backtrace` and `__rust_end_short_backtrace`
// are omitted from the backtrace in short mode, `__rust_end_short_backtrace` will be
// called before the panic hook, so we won't ignore any frames if there is no
// invoke of `__rust_begin_short_backtrace`.
if print_fmt == PrintFmt::Short {
if let Some(sym) = symbol.name().and_then(|s| s.as_str()) {
if start && sym.contains("__rust_begin_short_backtrace") {
start = false;
return;
}
if sym.contains("__rust_end_short_backtrace") {
start = true;
return;
}
if !start {
omitted_count += 1;
}
}
}
}

if start {
if omitted_count > 0 {
debug_assert!(print_fmt == PrintFmt::Short);
// only print the message between the middle of frames
if !first_omit {
let _ = writeln!(
bt_fmt.formatter(),
" [... omitted {} frame{} ...]",
omitted_count,
if omitted_count > 1 { "s" } else { "" }
);
if start {
if omitted_count > 0 {
debug_assert!(print_fmt == PrintFmt::Short);
// only print the message between the middle of frames
if !first_omit {
let _ = writeln!(
bt_fmt.formatter(),
" [... omitted {} frame{} ...]",
omitted_count,
if omitted_count > 1 { "s" } else { "" }
);
}
first_omit = false;
omitted_count = 0;
}
first_omit = false;
omitted_count = 0;
res = bt_fmt.frame().symbol(frame, symbol);
}
res = bt_fmt.frame().symbol(frame, symbol);
});
#[cfg(target_os = "nto")]
if libc::__my_thread_exit as *mut libc::c_void == frame.ip() {
if !hit && start {
use crate::backtrace_rs::SymbolName;
res = bt_fmt.frame().print_raw(
frame.ip(),
Some(SymbolName::new("__my_thread_exit".as_bytes())),
None,
None,
);
}
return false;
}
});
#[cfg(target_os = "nto")]
if libc::__my_thread_exit as *mut libc::c_void == frame.ip() {
if !hit && start {
use crate::backtrace_rs::SymbolName;
res = bt_fmt.frame().print_raw(
frame.ip(),
Some(SymbolName::new("__my_thread_exit".as_bytes())),
None,
None,
);
res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
}
return false;
}
if !hit && start {
res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
}

idx += 1;
res.is_ok()
});
idx += 1;
res.is_ok()
})
};
res?;
bt_fmt.finish()?;
if print_fmt == PrintFmt::Short {
Expand Down
19 changes: 11 additions & 8 deletions std/src/sys/pal/common/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![forbid(unsafe_op_in_unsafe_fn)]
use crate::alloc::{GlobalAlloc, Layout, System};
use crate::cmp;
use crate::ptr;
Expand Down Expand Up @@ -46,14 +47,16 @@ pub unsafe fn realloc_fallback(
old_layout: Layout,
new_size: usize,
) -> *mut u8 {
// Docs for GlobalAlloc::realloc require this to be valid:
let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
// SAFETY: Docs for GlobalAlloc::realloc require this to be valid
unsafe {
let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());

let new_ptr = GlobalAlloc::alloc(alloc, new_layout);
if !new_ptr.is_null() {
let size = cmp::min(old_layout.size(), new_size);
ptr::copy_nonoverlapping(ptr, new_ptr, size);
GlobalAlloc::dealloc(alloc, ptr, old_layout);
let new_ptr = GlobalAlloc::alloc(alloc, new_layout);
if !new_ptr.is_null() {
let size = cmp::min(old_layout.size(), new_size);
ptr::copy_nonoverlapping(ptr, new_ptr, size);
GlobalAlloc::dealloc(alloc, ptr, old_layout);
}
new_ptr
}
new_ptr
}

0 comments on commit ee86041

Please sign in to comment.