libs-team: Report allocation errors through the panic handler
Proposal
Problem statement
We currently use a completely separate mechanism to handle allocation errors (OOM) than panics, despite both of these being very similar. However both are very similar: they involve handling runtime failures that are generally unrecoverable. It would be better to unify them under a single mechanism which is already stable: panic handlers (no_std) and panic hooks (std).
Motivation, use-cases
This change has already been done for no_std in https://github.com/rust-lang/rust/issues/66741: it was noticed that the vast majority of users of #[alloc_error_handler] were just calling panic anyways. This avoided the need to stabilize the alloc_error_handler attribute by defaulting to a panic if one was not specified.
The OOM hook API has also remained unstable for a very long time with no clear path to stabilization. The best way forward would be to unify it with the panic hook API just like it has been done in no_std.
Some use cases (firefox) still need access to the size of the failed allocation, so this is provided as a custom panic payload.
Solution sketches
// alloc::alloc
pub struct AllocErrorPanicPayload { .. }
impl AllocErrorPanicPayload {
pub fn layout(&self) -> Layout;
}
This ACP proposes to make the following changes:
- Introduce the
AllocErrorPanicPayloadtype which holds theLayoutof a failed allocation. - Make
handle_alloc_errorcall the normal panic handler with a special payload ofAllocErrorPanicPayload.- This can be observed by panic hooks with std or by the panic handler with no_std.
- Unless
-Zoom=panicis used, this is a non-unwinding panic: if the panic hook returns then the process is aborted. This maintains the current behavior of aborting on OOM by default.
- Remove the unstable
alloc_error_hookstd API (https://github.com/rust-lang/rust/issues/51245). Its job is now handled by panic hooks. - Remove the unstable
#[alloc_error_handler]attribute (https://github.com/rust-lang/rust/issues/51540) for no_std programs. Its job is now handled by#[panic_handler].
In the standard library, no additional allocations are made as part of the OOM handling process, except in 1 specific situation:
- With
-Zoom=panic, if the panic hook returns then an allocation is necessary to box the payload and allocate an exception object. However it’s not a big deal if this, fails, the process will simply abort immediately in that case.
Links and related work
Tracking issues that will be closed by this change:
- https://github.com/rust-lang/rust/issues/51245
alloc_error_hook - https://github.com/rust-lang/rust/issues/51540
alloc_error_handler
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 2
- Comments: 17 (17 by maintainers)
Commits related to this issue
- Rollup merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which hol... — committed to matthiaskrgr/rust by matthiaskrgr 4 years ago
- Auto merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds... — committed to rust-lang-ci/rust by bors 4 years ago
- Auto merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds... — committed to RalfJung/miri by bors 4 years ago
- Auto merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds... — committed to rust-lang/rustc_codegen_cranelift by bors 4 years ago
- Auto merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds... — committed to lnicola/rust-analyzer by bors 4 years ago
- Auto merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds... — committed to rust-lang/rustc_codegen_gcc by bors 4 years ago
- Auto merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds... — committed to tcdi/postgrestd by bors 4 years ago
- Auto merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds... — committed to RalfJung/rust-analyzer by bors 4 years ago
- Auto merge of #109507 - Amanieu:panic-oom-payload, r=davidtwco Report allocation errors as panics OOM is now reported as a panic but with a custom payload type (`AllocErrorPanicPayload`) which holds... — committed to RalfJung/rust-analyzer by bors 4 years ago
But it sounds like option 2 isn’t possible then, because
set_hookis safe.Yeah, we support unsafe EIIs. We can mark the EII as unsafe with something like:
And then a user needs to use an unsafe attribute to override it:
Only in std.
std::panic::panic_anywill directly call the hook you set withstd::panic::set_hookwith any payload, but that doesn’t go through any of (core’s) panic machinery.It “resolves” all concerns because it means this acp is no longer possible.
Note that as of https://github.com/rust-lang/rust/pull/115974, core (panic handler) no longer has any concept of a ‘panic payload’. That is now an std (panic hook) only concept.
I don’t see this as a strong argument to avoid using payloads, it’s just that payloads are underused today. Treating OOM as a panic is almost always what people want, and allows the resulting error message to be properly integrated into whatever error reporting mechanism an application is using (e.g. uploading crash reports).
I don’t think this is the right thing to do.
Currently, a (no-std)
#[panic_handler]never gets apayload(), not even a&'static strorString, even for something as simple aspanic!("hello"). Payloads are currently a std-only feature. It is true that one can callinfo.payload()in a#[panic_handler], but that’s likely a mistake (see https://github.com/rust-lang/rust/pull/115974). That method has never done anything useful in a panic handler. Currently, any time anyone implements a#[panic_handler]by downcasting apayload()they’re just adding dead code: downcasting there always returnsNone.So, I disagree this proposal is a way “to unify them under a single mechanism”.
&dyn Anypanic payloads are currently a std-only feature, that just happens to also compile (but not work) in no-std.