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

std: Partial fix for #33368 #33408

Closed
wants to merge 1 commit into from
Closed
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
67 changes: 42 additions & 25 deletions src/libstd/sys/common/unwind/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,48 @@ pub unsafe fn try<F: FnOnce()>(f: F) -> Result<(), Box<Any + Send>> {
}
}

unsafe fn inner_try(f: fn(*mut u8), data: *mut u8)
-> Result<(), Box<Any + Send>> {
PANIC_COUNT.with(|s| {
let prev = s.get();
s.set(0);

// The "payload" here is a platform-specific region of memory which is
// used to transmit information about the exception being thrown from
// the point-of-throw back to this location.
//
// A pointer to this data is passed to the `try` intrinsic itself,
// allowing this function, the `try` intrinsic, imp::payload(), and
// imp::cleanup() to all work in concert to transmit this information.
//
// More information about what this pointer actually is can be found in
// each implementation as well as browsing the compiler source itself.
let mut payload = imp::payload();
let r = intrinsics::try(f, data, &mut payload as *mut _ as *mut _);
s.set(prev);
if r == 0 {
Ok(())
} else {
Err(imp::cleanup(payload))
}
})
unsafe fn inner_try(f: fn(*mut u8), data: *mut u8) -> Result<(), Box<Any + Send>> {
// Coroutines make it a possibility that the execution inside `f`
// is parked and transferred to another thread.
// Thus it's important to keep in mind that we do not hold
// any references to the current thread while calling `f`.
// This requires an additional `inline(never)` attribute to
// prevent the compiler from caching the TLS access.
#[inline(never)]
fn panic_count_swap(val: usize) -> usize {
PANIC_COUNT.with(|s| {
let prev = s.get();
s.set(val);
prev
})
}

// It is possible to safely use this method with coroutines under the condition
// that `panicking()` is false when this method is called and false whenever
// the current stack is transferred to another thread.
// This ensures that `PANIC_COUNT` will consistently be overwritten with 0.
let prev = panic_count_swap(0);

// The "payload" here is a platform-specific region of memory which is
// used to transmit information about the exception being thrown from
// the point-of-throw back to this location.
//
// A pointer to this data is passed to the `try` intrinsic itself,
// allowing this function, the `try` intrinsic, imp::payload(), and
// imp::cleanup() to all work in concert to transmit this information.
//
// More information about what this pointer actually is can be found in
// each implementation as well as browsing the compiler source itself.
let mut payload = imp::payload();
let r = intrinsics::try(f, data, &mut payload as *mut _ as *mut _);

panic_count_swap(prev);

if r == 0 {
Ok(())
} else {
Err(imp::cleanup(payload))
}
}

/// Determines whether the current thread is unwinding because of panic.
Expand Down