rust-lang / rust-lang/rust

Panic while formatting panic message + always_abort leads to stack overflow

Open
#122,940 24 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-panic C-bug T-compiler T-libs
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Example and initial analysis by @correabuscar:

#![feature(panic_always_abort)]

use std::fmt::{Display, self};

struct MyStruct;

impl Display for MyStruct {
    fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
        panic!("this is bad: {}", MyStruct);
    }
}

fn main() {
    std::panic::always_abort();
    println!("{}", MyStruct);
}

The reason for this is that if both "panic while processing panic" and "panic after always_abort" apply, then we use the AlwaysAbort code path which does format the panic message.

This could be fixed by moving this check further down below the in_panic_hook check:

https://github.com/rust-lang/rust/blob/9f25a04498fbe30dcf6a9c764953704c333a0137/library/std/src/panicking.rs#L393-L395

But I don't know if this has other adverse side-effects. Can we even access thread-local variables after fork (i.e., when always-abort is set)?
Cc @Amanieu

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust example in the issue and inspect library/std/src/panicking.rs around lines 393-395, especially the AlwaysAbort and in_panic_hook checks. Reproduce the stack overflow, then determine whether moving the check avoids formatting during panic processing without adverse effects, including after fork and when accessing thread-local state.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.