Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert #109075 to partially fix #114375 #114988

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions library/std/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ mod tests;
// a backtrace or actually symbolizing it.

use crate::backtrace_rs::{self, BytesOrWideString};
use crate::cell::UnsafeCell;
use crate::env;
use crate::ffi::c_void;
use crate::fmt;
use crate::panic::UnwindSafe;
use crate::panic::{RefUnwindSafe, UnwindSafe};
use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed};
use crate::sync::LazyLock;
use crate::sync::Once;
use crate::sys_common::backtrace::{lock, output_filename};
use crate::vec::Vec;

Expand Down Expand Up @@ -133,11 +134,12 @@ pub enum BacktraceStatus {
enum Inner {
Unsupported,
Disabled,
Captured(LazyLock<Capture, LazyResolve>),
Captured(LazilyResolvedCapture),
}

struct Capture {
actual_start: usize,
resolved: bool,
frames: Vec<BacktraceFrame>,
}

Expand Down Expand Up @@ -178,7 +180,7 @@ impl fmt::Debug for Backtrace {
let capture = match &self.inner {
Inner::Unsupported => return fmt.write_str("<unsupported>"),
Inner::Disabled => return fmt.write_str("<disabled>"),
Inner::Captured(c) => &**c,
Inner::Captured(c) => c.force(),
};

let frames = &capture.frames[capture.actual_start..];
Expand Down Expand Up @@ -346,10 +348,11 @@ impl Backtrace {
let inner = if frames.is_empty() {
Inner::Unsupported
} else {
Inner::Captured(LazyLock::new(lazy_resolve(Capture {
Inner::Captured(LazilyResolvedCapture::new(Capture {
actual_start: actual_start.unwrap_or(0),
frames,
})))
resolved: false,
}))
};

Backtrace { inner }
Expand All @@ -374,7 +377,7 @@ impl<'a> Backtrace {
#[must_use]
#[unstable(feature = "backtrace_frames", issue = "79676")]
pub fn frames(&'a self) -> &'a [BacktraceFrame] {
if let Inner::Captured(c) = &self.inner { &c.frames } else { &[] }
if let Inner::Captured(c) = &self.inner { &c.force().frames } else { &[] }
}
}

Expand All @@ -384,7 +387,7 @@ impl fmt::Display for Backtrace {
let capture = match &self.inner {
Inner::Unsupported => return fmt.write_str("unsupported backtrace"),
Inner::Disabled => return fmt.write_str("disabled backtrace"),
Inner::Captured(c) => &**c,
Inner::Captured(c) => c.force(),
};

let full = fmt.alternate();
Expand Down Expand Up @@ -428,15 +431,49 @@ impl fmt::Display for Backtrace {
}
}

type LazyResolve = impl (FnOnce() -> Capture) + Send + Sync + UnwindSafe;
struct LazilyResolvedCapture {
sync: Once,
capture: UnsafeCell<Capture>,
}

impl LazilyResolvedCapture {
fn new(capture: Capture) -> Self {
LazilyResolvedCapture { sync: Once::new(), capture: UnsafeCell::new(capture) }
}

fn force(&self) -> &Capture {
self.sync.call_once(|| {
// SAFETY: This exclusive reference can't overlap with any others
// `Once` guarantees callers will block until this closure returns
// `Once` also guarantees only a single caller will enter this closure
unsafe { &mut *self.capture.get() }.resolve();
});

// SAFETY: This shared reference can't overlap with the exclusive reference above
unsafe { &*self.capture.get() }
}
}

// SAFETY: Access to the inner value is synchronized using a thread-safe `Once`
// So long as `Capture` is `Sync`, `LazilyResolvedCapture` is too
unsafe impl Sync for LazilyResolvedCapture where Capture: Sync {}

impl UnwindSafe for LazilyResolvedCapture {}
impl RefUnwindSafe for LazilyResolvedCapture {}

impl Capture {
fn resolve(&mut self) {
// If we're already resolved, nothing to do!
if self.resolved {
return;
}
self.resolved = true;

fn lazy_resolve(mut capture: Capture) -> LazyResolve {
move || {
// Use the global backtrace lock to synchronize this as it's a
// requirement of the `backtrace` crate, and then actually resolve
// everything.
let _lock = lock();
for frame in capture.frames.iter_mut() {
for frame in self.frames.iter_mut() {
let symbols = &mut frame.symbols;
let frame = match &frame.frame {
RawFrame::Actual(frame) => frame,
Expand All @@ -457,8 +494,6 @@ fn lazy_resolve(mut capture: Capture) -> LazyResolve {
});
}
}

capture
}
}

Expand Down
6 changes: 4 additions & 2 deletions library/std/src/backtrace/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ fn generate_fake_frames() -> Vec<BacktraceFrame> {
#[test]
fn test_debug() {
let backtrace = Backtrace {
inner: Inner::Captured(LazyLock::preinit(Capture {
inner: Inner::Captured(LazilyResolvedCapture::new(Capture {
actual_start: 1,
resolved: true,
frames: generate_fake_frames(),
})),
};
Expand All @@ -66,8 +67,9 @@ fn test_debug() {
#[test]
fn test_frames() {
let backtrace = Backtrace {
inner: Inner::Captured(LazyLock::preinit(Capture {
inner: Inner::Captured(LazilyResolvedCapture::new(Capture {
actual_start: 1,
resolved: true,
frames: generate_fake_frames(),
})),
};
Expand Down
1 change: 0 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@
#![feature(staged_api)]
#![feature(thread_local)]
#![feature(try_blocks)]
#![feature(type_alias_impl_trait)]
#![feature(utf8_chunks)]
// tidy-alphabetical-end
//
Expand Down
9 changes: 0 additions & 9 deletions library/std/src/sync/lazy_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,6 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
LazyLock { once: Once::new(), data: UnsafeCell::new(Data { f: ManuallyDrop::new(f) }) }
}

/// Creates a new lazy value that is already initialized.
#[inline]
#[cfg(test)]
pub(crate) fn preinit(value: T) -> LazyLock<T, F> {
let once = Once::new();
once.call_once(|| {});
LazyLock { once, data: UnsafeCell::new(Data { value: ManuallyDrop::new(value) }) }
}

/// Consumes this `LazyLock` returning the stored value.
///
/// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise.
Expand Down
Loading