Skip to content
Draft
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
8 changes: 0 additions & 8 deletions compiler/rustc_codegen_cranelift/src/inline_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
options: InlineAsmOptions,
destination: Option<mir::BasicBlock>,
) {
// Used by panic_abort on Windows, but uses a syntax which only happens to work with
// asm!() by accident and breaks with the GNU assembler as well as global_asm!() for
// the LLVM backend.
if template.len() == 1 && template[0] == InlineAsmTemplatePiece::String("int $$0x29".into()) {
fx.bcx.ins().trap(TrapCode::user(2).unwrap());
return;
}

if fx.tcx.sess.target.arch == Arch::S390x
&& template.len() == 3
&& template[0] == InlineAsmTemplatePiece::String("stfle 0(".into())
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
self.unreachable();
}
} else {
// FIXME use `__fastfail(FAST_FAIL_GUARD_ICALL_CHECK_FAILURE)` on Windows
self.abort_immediate();
self.unreachable();
}
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_codegen_ssa/src/traits/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ pub trait IntrinsicCallBuilderMethods<'tcx>: BackendTypes {
is_cleanup: bool,
) -> Self::Value;

/// Ungracefully stop execution in the simplest way possible.
/// Ungracefully stop execution in the simplest way possible
/// that uses the fewest instructions.
/// Use this when [`super::BuilderMethods::unreachable`] would also work
/// but you want to be nice.
/// Prefer e.g. [`core::process::crash`] when the abort is meant to be
/// a user-visible guarantee.
fn abort_immediate(&mut self);
fn assume(&mut self, val: Self::Value);
fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value;
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/collections/btree/mem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{intrinsics, mem, ptr};
use core::{mem, process, ptr};

/// This replaces the value behind the `v` unique reference by calling the
/// relevant function.
Expand All @@ -19,7 +19,7 @@ pub(super) fn replace<T, R>(v: &mut T, change: impl FnOnce(T) -> (T, R)) -> R {
struct PanicGuard;
impl Drop for PanicGuard {
fn drop(&mut self) {
intrinsics::abort_immediate()
process::crash()
}
}
let guard = PanicGuard;
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/collections/btree/split.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::alloc::AllocatorClone;
use core::borrow::Borrow;
use core::{intrinsics, mem};
use core::{mem, process};

use super::node::ForceResult::*;
use super::node::Root;
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<K, V> Root<K, V> {
// A panic from a later `search_node` comparison would unwind out of
// that state and double-free the shared values (#158165), so abort
// instead of exposing it.
let guard = mem::DropGuard::new((), |()| intrinsics::abort_immediate());
let guard = mem::DropGuard::new((), |()| process::crash());

loop {
split_edge.move_suffix(&mut right_node);
Expand Down
4 changes: 4 additions & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
//
// Library features:
// tidy-alphabetical-start
#![cfg_attr(windows, feature(core_os))]
#![cfg_attr(windows, feature(core_os_windows))]
#![cfg_attr(windows, feature(windows_fastfail))]
#![feature(abort_immediate)]
#![feature(allocator_ext)]
#![feature(array_into_iter_constructors)]
#![feature(ascii_char)]
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/raw_vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod tests;
// only one location which panics rather than a bunch throughout the module.
#[cfg(not(no_global_oom_handling))]
#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
#[cfg_attr(panic = "immediate-abort", inline)]
const fn capacity_overflow() -> ! {
panic!("capacity overflow");
}
Expand Down
10 changes: 5 additions & 5 deletions library/alloc/src/rcs/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::ptr::{self, NonNull};
use core::slice::from_raw_parts_mut;
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use core::sync::atomic::{self, Atomic};
use core::{borrow, fmt, hint, intrinsics};
use core::{borrow, fmt, hint};

#[cfg(not(no_global_oom_handling))]
use crate::alloc::handle_alloc_error;
Expand All @@ -29,7 +29,7 @@ use crate::alloc::{
};
use crate::borrow::{Cow, ToOwned};
use crate::boxed::Box;
use crate::rc::is_dangling;
use crate::rc::{crash_for_invalid_refcount, is_dangling};
#[cfg(not(no_global_oom_handling))]
use crate::string::String;
#[cfg(not(no_global_oom_handling))]
Expand Down Expand Up @@ -2541,7 +2541,7 @@ impl<T: ?Sized, A: AllocatorClone> Clone for Arc<T, A> {
// Otherwise, the counter could be brought to an almost-overflow using a compare-exchange loop,
// and then overflow using a few `fetch_add`s.
if old_size > MAX_REFCOUNT {
intrinsics::abort_immediate();
crash_for_invalid_refcount()
}

// SAFETY: Pointer is valid & allocator corresponds to the one used to allocate it.
Expand Down Expand Up @@ -3595,7 +3595,7 @@ impl<T: ?Sized, A: AllocatorClone> Clone for Weak<T, A> {

// See comments in Arc::clone() for why we do this (for mem::forget).
if old_size > MAX_REFCOUNT {
intrinsics::abort_immediate();
crash_for_invalid_refcount()
}
}

Expand Down Expand Up @@ -5073,7 +5073,7 @@ impl<T: ?Sized, A: AllocatorClone> UniqueArc<T, A> {

// See comments in Arc::clone() for why we do this (for mem::forget).
if old_size > MAX_REFCOUNT {
intrinsics::abort_immediate();
crash_for_invalid_refcount()
}

Weak { ptr: this.ptr, alloc: this.alloc.clone() }
Expand Down
21 changes: 19 additions & 2 deletions library/alloc/src/rcs/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3436,6 +3436,23 @@ pub(crate) fn is_dangling<T: ?Sized>(ptr: *const T) -> bool {
(ptr.cast::<()>()).addr() == usize::MAX
}

#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[cold]
#[inline]
pub(super) fn crash_for_invalid_refcount() -> ! {
cfg_select! {
windows => {
// `core::process::crash()` crashes with `FAST_FAIL_FATAL_APP_EXIT`,
// but in this case we can use a more specific error code

/// From `winnt.h`
const FAST_FAIL_INVALID_REFERENCE_COUNT: core::ffi::c_uint = 14;
core::os::windows::__fastfail(FAST_FAIL_INVALID_REFERENCE_COUNT)
}
_ => core::process::crash(),
}
}

/// Helper type to allow accessing the reference counts without
/// making any assertions about the data field.
struct WeakInner<'a> {
Expand Down Expand Up @@ -3951,7 +3968,7 @@ trait RcInnerPtr {
// Checking for overflow after the store instead of before
// allows for slightly better code generation.
if intrinsics::unlikely(strong == 0) {
intrinsics::abort_immediate();
crash_for_invalid_refcount()
}
}

Expand Down Expand Up @@ -3984,7 +4001,7 @@ trait RcInnerPtr {
// Checking for overflow after the store instead of before
// allows for slightly better code generation.
if intrinsics::unlikely(weak == 0) {
intrinsics::abort_immediate();
crash_for_invalid_refcount()
}
}

Expand Down
4 changes: 4 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,7 @@ impl<T, A: Allocator> Vec<T, A> {
pub fn swap_remove(&mut self, index: usize) -> T {
#[cold]
#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
#[cfg_attr(panic = "immediate-abort", inline)]
#[optimize(size)]
fn assert_failed(index: usize, len: usize) -> ! {
panic!("swap_remove index (is {index}) should be < len (is {len})");
Expand Down Expand Up @@ -2262,6 +2263,7 @@ impl<T, A: Allocator> Vec<T, A> {
pub fn insert_mut(&mut self, index: usize, element: T) -> &mut T {
#[cold]
#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
#[cfg_attr(panic = "immediate-abort", inline)]
#[track_caller]
#[optimize(size)]
fn assert_failed(index: usize, len: usize) -> ! {
Expand Down Expand Up @@ -2327,6 +2329,7 @@ impl<T, A: Allocator> Vec<T, A> {
pub fn remove(&mut self, index: usize) -> T {
#[cold]
#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
#[cfg_attr(panic = "immediate-abort", inline)]
#[track_caller]
#[optimize(size)]
fn assert_failed(index: usize, len: usize) -> ! {
Expand Down Expand Up @@ -3226,6 +3229,7 @@ impl<T, A: Allocator> Vec<T, A> {
{
#[cold]
#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
#[cfg_attr(panic = "immediate-abort", inline)]
#[track_caller]
#[optimize(size)]
fn assert_failed(at: usize, len: usize) -> ! {
Expand Down
2 changes: 1 addition & 1 deletion library/alloctests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//
// Library features:
// tidy-alphabetical-start
#![feature(abort_immediate)]
#![feature(allocator_ext)]
#![feature(array_into_iter_constructors)]
#![feature(char_internals)]
Expand All @@ -26,7 +27,6 @@
#![feature(const_result_trait_fn)]
#![feature(const_try)]
#![feature(copied_into_inner)]
#![feature(core_intrinsics)]
#![feature(exact_size_is_empty)]
#![feature(extend_one)]
#![feature(extend_one_unchecked)]
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ impl Display for BorrowMutError {

// This ensures the panicking code is outlined from `borrow_mut` for `RefCell`.
#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
#[cfg_attr(panic = "immediate-abort", inline)]
#[track_caller]
#[cold]
const fn panic_already_borrowed(err: BorrowMutError) -> ! {
Expand All @@ -924,6 +925,7 @@ const fn panic_already_borrowed(err: BorrowMutError) -> ! {

// This ensures the panicking code is outlined from `borrow` for `RefCell`.
#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
#[cfg_attr(panic = "immediate-abort", inline)]
#[track_caller]
#[cold]
const fn panic_already_mutably_borrowed(err: BorrowError) -> ! {
Expand Down
8 changes: 5 additions & 3 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,7 @@ pub fn rustc_peek<T>(_: T) -> T;

/// Ungracefully aborts the execution of the process.
///
/// This is the intrinsic for directly implementing [`core::process::crash`] which, on most
/// platforms, will invoke an invalid instruction. On Unix, the process will probably terminate with
/// On most platforms, this will invoke an invalid instruction. On Unix, the process will probably terminate with
/// a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or `SIGBUS`. The precise behavior is not
/// guaranteed and not stable.
///
Expand All @@ -628,7 +627,10 @@ pub fn rustc_peek<T>(_: T) -> T;
/// Note that, unlike most intrinsics, this is safe to call; it does not require an `unsafe` block.
/// Therefore, implementations must not require the user to uphold any safety invariants.
///
/// The stabilized version of this intrinsic is [`core::process::crash`].
/// Used to implement [`core::process::crash`] on most platforms, but not Windows.
/// Prefer that function over this one for "user-visible well-defined immediate abort";
/// prefer this intrinsic for for contexts where [`unreachable()`]
/// would also have been acceptable.
#[rustc_nounwind]
#[rustc_intrinsic]
pub const fn abort_immediate() -> !;
Expand Down
35 changes: 33 additions & 2 deletions library/core/src/os/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Platform-specific extensions to `core` for Windows platforms.

#![unstable(feature = "codeview_annotation", issue = "163964")]
#![unstable(feature = "core_os_windows", issue = "none")]
#![doc(cfg(windows))]

#[doc(inline)]
Expand All @@ -21,7 +21,7 @@ pub use crate::intrinsics::CodeViewAnnotationArgs;
///
#[cfg_attr(windows, doc = "```")]
#[cfg_attr(not(windows), doc = "```ignore (needs windows)")]
/// #![feature(codeview_annotation, core_os)]
/// #![feature(codeview_annotation, core_os, core_os_windows)]
/// use core::os::windows::{codeview_annotation, CodeViewAnnotationArgs};
///
/// struct Args;
Expand All @@ -37,3 +37,34 @@ pub use crate::intrinsics::CodeViewAnnotationArgs;
pub fn codeview_annotation<T: CodeViewAnnotationArgs>() {
crate::intrinsics::codeview_annotation::<T>();
}

/// Use `__fastfail` to abort the process
///
/// In Windows 8 and later, this will terminate the process immediately without
/// running any in-process exception handlers. In earlier versions of Windows,
/// this sequence of instructions will be treated as an access violation, which
/// will still terminate the process but might run some exception handlers.
///
/// <https://docs.microsoft.com/en-us/cpp/intrinsics/fastfail>
#[inline(always)]
#[unstable(feature = "windows_fastfail", issue = "none")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub fn __fastfail(code: crate::ffi::c_uint) -> ! {
// SAFETY: These assembly sequences are documented at
// <https://docs.microsoft.com/en-us/cpp/intrinsics/fastfail>
unsafe {
cfg_select! {
// Miri does not support inline assembly
miri => crate::intrinsics::abort_immediate(),
any(target_arch = "x86", target_arch = "x86_64") => {
crate::arch::asm!("int 0x29", in("ecx") code, options(noreturn, nostack))
}
all(target_arch = "arm", target_feature = "thumb-mode") => {
crate::arch::asm!(".inst 0xDEFB", in("r0") code, options(noreturn, nostack))
}
any(target_arch = "aarch64", target_arch = "arm64ec") => {
crate::arch::asm!("brk 0xF003", in("x0") code, options(noreturn, nostack))
}
}
}
}
Loading
Loading