Skip to content

[EXPERIMENT] Move handle_alloc_error to core#158198

Draft
clarfonthey wants to merge 1 commit into
rust-lang:mainfrom
clarfonthey:handle-alloc-error-core
Draft

[EXPERIMENT] Move handle_alloc_error to core#158198
clarfonthey wants to merge 1 commit into
rust-lang:mainfrom
clarfonthey:handle-alloc-error-core

Conversation

@clarfonthey

Copy link
Copy Markdown
Contributor

Not meant to be merged as-is. This exists as a test that would be needed to make hashbrown no longer depend on alloc.

Currently, this method does not actually need to be in alloc, minus the cfg(not(no_global_oom_handling)), which could potentially be removed. It ultimately gets replaced with something reasonable in environments with std, but just aborts in no_std environments.

The unfortunate bit is that we can't change the stability of re-exported functions, and so, this makes the version in alloc a wrapper function.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 21, 2026
@clarfonthey clarfonthey added the A-allocators Area: Custom and system allocators label Jun 21, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job x86_64-gnu-miri failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
tests/fail/tree_borrows/wildcard/subtree_internal_relatedness.rs ... ok
tests/fail/tree_borrows/wildcard/subtree_internal_relatedness_wildcard.rs ... ok

FAILED TEST: tests/fail/alloc/alloc_error_handler.rs
command: MIRI_ENV_VAR_TEST="0" MIRI_TEMP="/tmp/miri-uitest-kLghlF" RUST_BACKTRACE="1" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/miri" "--error-format=json" "-Dwarnings" "-Dunused" "-Ainternal_features" "-Zui-testing" "--sysroot=/checkout/obj/build/x86_64-unknown-linux-gnu/miri-sysroot" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/tests/fail/alloc" "tests/fail/alloc/alloc_error_handler.rs" "--edition" "2021"

error: actual output differed from expected
Execute `./miri test --bless` to update `tests/fail/alloc/alloc_error_handler.stderr` to the actual output
--- tests/fail/alloc/alloc_error_handler.stderr
+++ <stderr output>
---

Full unnormalized output:
memory allocation of 4 bytes failed
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
error: abnormal termination: the program aborted execution
##[error]  --> /checkout/library/std/src/alloc.rs:429:9
   |
LL |         crate::process::abort()
   |         ^^^^^^^^^^^^^^^^^^^^^^^ abnormal termination occurred here
   |
   = note: stack backtrace:
           0: std::alloc::rust_oom::{closure#0}
               at /checkout/library/std/src/alloc.rs:429:9: 429:32
           1: std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::alloc::rust_oom::{closure#0}}, !>
               at /checkout/library/std/src/sys/backtrace.rs:182:18: 182:21
           2: std::alloc::rust_oom
               at /checkout/library/std/src/alloc.rs:424:5: 430:7
           3: std::alloc::_::__rust_alloc_error_handler
               at /checkout/library/std/src/alloc.rs:423:1: 423:37
           4: core::alloc::handle_alloc_error::rt_error
               at /checkout/library/core/src/alloc/mod.rs:516:13: 516:70
---

full stderr:
memory allocation of 4 bytes failed
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect
error: abnormal termination: the program aborted execution
##[error]  --> /checkout/library/std/src/alloc.rs:429:9
   |
LL |         crate::process::abort()
   |         ^^^^^^^^^^^^^^^^^^^^^^^ abnormal termination occurred here
   |
   = note: stack backtrace:
           0: std::alloc::rust_oom::{closure#0}
               at /checkout/library/std/src/alloc.rs:429:9: 429:32
           1: std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::alloc::rust_oom::{closure#0}}, !>
               at /checkout/library/std/src/sys/backtrace.rs:182:18: 182:21
           2: std::alloc::rust_oom
               at /checkout/library/std/src/alloc.rs:424:5: 430:7
           3: std::alloc::_::__rust_alloc_error_handler
               at /checkout/library/std/src/alloc.rs:423:1: 423:37
           4: core::alloc::handle_alloc_error::rt_error
               at /checkout/library/core/src/alloc/mod.rs:516:13: 516:70
---



FAILED TEST: tests/fail/alloc/alloc_error_handler_custom.rs
command: MIRI_ENV_VAR_TEST="0" MIRI_TEMP="/tmp/miri-uitest-kLghlF" RUST_BACKTRACE="1" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/miri" "--error-format=json" "-Dwarnings" "-Dunused" "-Ainternal_features" "-Zui-testing" "--sysroot=/checkout/obj/build/x86_64-unknown-linux-gnu/miri-sysroot" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/tests/fail/alloc" "tests/fail/alloc/alloc_error_handler_custom.rs" "-Cpanic=abort" "--edition" "2021"

error: actual output differed from expected
Execute `./miri test --bless` to update `tests/fail/alloc/alloc_error_handler_custom.stderr` to the actual output
--- tests/fail/alloc/alloc_error_handler_custom.stderr
+++ <stderr output>
 custom alloc error handler: Layout { size: 4, align: 4 (1 << 2) }
 error: abnormal termination: the program aborted execution
... 8 lines skipped ...
            1: _::__rust_alloc_error_handler
                at tests/fail/alloc/alloc_error_handler_custom.rs:LL:CC
-           2: alloc::alloc::handle_alloc_error::rt_error
---
 
 error: aborting due to 1 previous error
 

Full unnormalized output:
custom alloc error handler: Layout { size: 4, align: 4 (1 << 2) }
error: abnormal termination: the program aborted execution
##[error]  --> tests/fail/alloc/alloc_error_handler_custom.rs:19:5
   |
LL |     core::intrinsics::abort();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ abnormal termination occurred here
---



full stderr:
custom alloc error handler: Layout { size: 4, align: 4 (1 << 2) }
error: abnormal termination: the program aborted execution
##[error]  --> tests/fail/alloc/alloc_error_handler_custom.rs:19:5
   |
LL |     core::intrinsics::abort();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ abnormal termination occurred here
---



FAILED TEST: tests/fail/alloc/alloc_error_handler_no_std.rs
command: MIRI_ENV_VAR_TEST="0" MIRI_TEMP="/tmp/miri-uitest-kLghlF" RUST_BACKTRACE="1" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/miri" "--error-format=json" "-Dwarnings" "-Dunused" "-Ainternal_features" "-Zui-testing" "--sysroot=/checkout/obj/build/x86_64-unknown-linux-gnu/miri-sysroot" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/tests/fail/alloc" "tests/fail/alloc/alloc_error_handler_no_std.rs" "-Cpanic=abort" "--edition" "2021"

error: actual output differed from expected
Execute `./miri test --bless` to update `tests/fail/alloc/alloc_error_handler_no_std.stderr` to the actual output
--- tests/fail/alloc/alloc_error_handler_no_std.stderr
+++ <stderr output>
---
 error: abnormal termination: the program aborted execution
... 6 lines skipped ...
            0: panic_handler
                at tests/fail/alloc/alloc_error_handler_no_std.rs:LL:CC
-           1: alloc::alloc::__alloc_error_handler::__rdl_alloc_error_handler
+           1: core::alloc::__alloc_error_handler::__rdl_alloc_error_handler
+               at RUSTLIB/core/src/alloc/mod.rs:LL:CC
+           2: core::alloc::handle_alloc_error::rt_error
+               at RUSTLIB/core/src/alloc/mod.rs:LL:CC
+           3: core::alloc::handle_alloc_error
+               at RUSTLIB/core/src/alloc/mod.rs:LL:CC
---
   |
   = note: stack backtrace:
           0: panic_handler
               at tests/fail/alloc/alloc_error_handler_no_std.rs:22:5: 22:30
           1: core::alloc::__alloc_error_handler::__rdl_alloc_error_handler
               at /checkout/library/core/src/alloc/mod.rs:538:9: 541:10
           2: core::alloc::handle_alloc_error::rt_error
               at /checkout/library/core/src/alloc/mod.rs:516:13: 516:70
           3: core::alloc::handle_alloc_error
               at /checkout/library/core/src/alloc/mod.rs:522:9: 522:75
---
   |
   = note: stack backtrace:
           0: panic_handler
               at tests/fail/alloc/alloc_error_handler_no_std.rs:22:5: 22:30
           1: core::alloc::__alloc_error_handler::__rdl_alloc_error_handler
               at /checkout/library/core/src/alloc/mod.rs:538:9: 541:10
           2: core::alloc::handle_alloc_error::rt_error
               at /checkout/library/core/src/alloc/mod.rs:516:13: 516:70
           3: core::alloc::handle_alloc_error
               at /checkout/library/core/src/alloc/mod.rs:522:9: 522:75
---
Location:
   /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ui_test-0.30.7/src/lib.rs:365

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   1: <color_eyre[a452ab70a1ebc890]::config::EyreHook>::into_eyre_hook::{closure#0}<unknown>
      at <unknown source file>:<unknown line>
   2: eyre[341337e102e3a7bb]::capture_handler<unknown>
      at <unknown source file>:<unknown line>
   3: eyre[341337e102e3a7bb]::private::format_err<unknown>
      at <unknown source file>:<unknown line>
   4: ui_test[f4a47f4df6b636fa]::run_tests_generic::<ui_test[f4a47f4df6b636fa]::default_file_filter, ui[912b4033f7b05550]::run_tests::{closure#1}, alloc[e0f6f22e2c747733]::boxed::Box<dyn ui_test[f4a47f4df6b636fa]::status_emitter::StatusEmitter>><unknown>
      at <unknown source file>:<unknown line>
   5: ui[912b4033f7b05550]::ui<unknown>
      at <unknown source file>:<unknown line>
   6: ui[912b4033f7b05550]::main<unknown>
      at <unknown source file>:<unknown line>
   7: std[7ef69a6b900fffa6]::sys::backtrace::__rust_begin_short_backtrace::<fn() -> core[f5b52ff5da77da03]::result::Result<(), eyre[341337e102e3a7bb]::Report>, core[f5b52ff5da77da03]::result::Result<(), eyre[341337e102e3a7bb]::Report>><unknown>
      at <unknown source file>:<unknown line>
   8: std[7ef69a6b900fffa6]::rt::lang_start::<core[f5b52ff5da77da03]::result::Result<(), eyre[341337e102e3a7bb]::Report>>::{closure#0}<unknown>
      at <unknown source file>:<unknown line>
   9: std[7ef69a6b900fffa6]::rt::lang_start_internal<unknown>
      at <unknown source file>:<unknown line>
  10: main<unknown>
      at <unknown source file>:<unknown line>
  11: __libc_start_main<unknown>
      at <unknown source file>:<unknown line>
  12: _start<unknown>
      at <unknown source file>:<unknown line>

Run with COLORBT_SHOW_HIDDEN=1 environment variable to disable frame filtering.
Run with RUST_BACKTRACE=full to include source snippets.
error: test failed, to rerun pass `--test ui`

Caused by:
  process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/ui-2814ac7bbd734e86` (exit status: 1)
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo test --target x86_64-unknown-linux-gnu -Zbinary-dep-depinfo -j 4 -Zroot-dir=/checkout --locked --color=always --profile=release --manifest-path /checkout/src/tools/miri/Cargo.toml -- [workdir=/checkout]` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:191:21
Executed at: src/bootstrap/src/core/build_steps/test.rs:769:19

--- BACKTRACE vvv
   0: <bootstrap::utils::exec::DeferredCommand>::finish_process
             at /checkout/src/bootstrap/src/utils/exec.rs:939:17
   1: <bootstrap::utils::exec::DeferredCommand>::wait_for_output::<&bootstrap::utils::exec::ExecutionContext>
             at /checkout/src/bootstrap/src/utils/exec.rs:831:21
   2: <bootstrap::utils::exec::ExecutionContext>::run
             at /checkout/src/bootstrap/src/utils/exec.rs:741:45
   3: <bootstrap::utils::exec::BootstrapCommand>::run::<&bootstrap::core::builder::Builder>
             at /checkout/src/bootstrap/src/utils/exec.rs:339:27
   4: <bootstrap::core::build_steps::test::Miri as bootstrap::core::builder::Step>::run
             at /checkout/src/bootstrap/src/core/build_steps/test.rs:769:19
   5: <bootstrap::core::builder::Builder>::ensure::<bootstrap::core::build_steps::test::Miri>
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1596:36
   6: <bootstrap::core::build_steps::test::Miri as bootstrap::core::builder::Step>::make_run
             at /checkout/src/bootstrap/src/core/build_steps/test.rs:695:21
   7: <bootstrap::core::builder::StepDescription>::maybe_run
             at /checkout/src/bootstrap/src/core/builder/mod.rs:476:13
   8: bootstrap::core::builder::cli_paths::match_paths_to_steps_and_run
             at /checkout/src/bootstrap/src/core/builder/cli_paths.rs:232:18
   9: <bootstrap::core::builder::Builder>::run_step_descriptions
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1139:9
  10: <bootstrap::core::builder::Builder>::execute_cli
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1118:14
  11: <bootstrap::Build>::build
             at /checkout/src/bootstrap/src/lib.rs:803:25
  12: bootstrap::main
             at /checkout/src/bootstrap/src/bin/main.rs:130:11
  13: <fn() as core::ops::function::FnOnce<()>>::call_once
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/core/src/ops/function.rs:250:5
  14: std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/sys/backtrace.rs:166:18
  15: std::rt::lang_start::<()>::{closure#0}
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/rt.rs:206:18
  16: <&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe as core::ops::function::FnOnce<()>>::call_once
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/core/src/ops/function.rs:287:21
  17: std::panicking::catch_unwind::do_call::<&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe, i32>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/panicking.rs:581:40
  18: std::panicking::catch_unwind::<i32, &dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/panicking.rs:544:19
  19: std::panic::catch_unwind::<&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe, i32>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/panic.rs:359:14
  20: std::rt::lang_start_internal::{closure#0}
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/rt.rs:175:24
  21: std::panicking::catch_unwind::do_call::<std::rt::lang_start_internal::{closure#0}, isize>
             at /rustc/0417c25868d6dfbd1c291dfeae950504faa6f790/library/std/src/panicking.rs:581:40
---
  28: __libc_start_main
  29: _start


Command has failed. Rerun with -v to see more details.
Bootstrap failed while executing `test --stage 2 src/tools/miri src/tools/miri/cargo-miri`
Build completed unsuccessfully in 0:47:33
  local time: Sun Jun 21 02:18:30 UTC 2026
  network time: Sun, 21 Jun 2026 02:18:30 GMT
##[error]Process completed with exit code 1.

/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
/// [The panic handler]: https://doc.rust-lang.org/reference/runtime.html#the-panic_handler-attribute
/// [no_std]: https://doc.rust-lang.org/reference/names/preludes.html#the-no_std-attribute
#[stable(feature = "alloc_module", since = "1.28.0")]

@programmerjake programmerjake Jun 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be feature = "global_alloc" to match the original code?

View changes since the review

@bjorn3

bjorn3 commented Jun 21, 2026

Copy link
Copy Markdown
Member

Hang on won't this break rust-for-linux and others that only use libcore and don't let rustc do the linking to generate the default implementation of alloc_error_handler? This might have to wait until EII can be used for alloc_error_handler.

@clarfonthey

Copy link
Copy Markdown
Contributor Author

Yeah, to be clear, this would not be mergeable as-is and you're right that this would probably break things. I mostly wanted to see how many things would break.

You're right EII is probably the right solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-allocators Area: Custom and system allocators S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants