Skip to content

Tracking issue for the #[alloc_error_handler] attribute (for no_std + liballoc) #51540

Description

@SimonSapin

View all comments

This attribute is mandatory when using the alloc crate without the std crate. It is used like this:

#[alloc_error_handler]
fn foo(_: core::alloc::Layout) -> ! {
    // …
}

Implementation PR: #52191

Blocking issues

Original issue:


In a no_std program or staticlib, linking to the alloc crate may cause this error:

error: language item required, but not found: `oom`

This is fixed by providing the oom lang item, which is is normally provided by the std crate (where it calls a dynamically-settable hook #51245, then aborts). This is called by alloc::alloc::handle_alloc_error (which is called by Vec and others on memory allocation failure).

#![feature(lang_items)]

#[lang = "oom"]
extern fn foo(_: core::alloc::Layout) -> ! {
    // example implementation based on libc
    extern "C" { fn abort() -> !; }
    unsafe { abort() }
}

However, defining a lang item is an unstable feature.

Possible solutions include:

  1. Add and stabilize a dedicated attribute similar to the #[panic_implementation] attribute:
#[alloc_error_handler]
fn foo(_: core::alloc::Layout) -> ! {
    // …
}

The downside is that this is one more mandatory hoop to jump through for no_std program that would have been fine with a default hook that aborts.

  1. Move std’s dynamically-settable hook into alloc: Move OOM handling to liballoc and remove the oom lang item #51607. The downside is some mandatory space overhead.

Activity

  1. added
    A-allocatorsArea: Custom and system allocators
    T-libs-api[DEPRECATED; DO NOT USE]
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Jun 13, 2018
  2. SimonSapin commented on Jun 13, 2018

    @SimonSapin
    ContributorAuthor

    Better idea: remove the lang item and move all of OOM handling to liballoc, except for the default hook that prints to stderr. Have compiler magic similar to that of #[global_allocator] that uses the printing default hook when std is linked, or a no-op default hook otherwise.

  3. glandium commented on Jun 13, 2018

    @glandium
    Contributor

    Bikeshedding on oom renaming: alloc_error would be shorter.

  4. SimonSapin commented on Jun 14, 2018

    @SimonSapin
    ContributorAuthor

    I forgot to link #51543, which does indeed use alloc_error.

  5. SimonSapin commented on Jun 29, 2018

    @SimonSapin
    ContributorAuthor

    @japaric, @glandium, @Amanieu I’ve updated the the issue description here with arguments from #51607 and rust-lang/rfcs#2480 and to list two alternatives.

  6. 157 remaining items

  7. added a commit that references this issue on Apr 26, 2023
  8. added a commit that references this issue on Apr 26, 2023
  9. added a commit that references this issue on Apr 29, 2023
    c7155f2
  10. added a commit that references this issue on Jun 5, 2023
    6d14583
  11. added a commit that references this issue on Jun 19, 2023
    321337b
  12. added a commit that references this issue on Sep 5, 2023
  13. RalfJung commented on Dec 9, 2023

    @RalfJung
    Member

    Given that this issue is closed, why is the unstable book still listing a feature that points here?
    https://doc.rust-lang.org/nightly/unstable-book/language-features/alloc-error-handler.html

  14. SimonSapin commented on Dec 9, 2023

    @SimonSapin
    ContributorAuthor

    This tracking issue was closed when PR #109507 merged to remove the feature, but that was reverted in PR #110782. Reopening to reflect that.

    PR #112331 is open to remove the feature again, and is already marked as closing this.

  15. RalfJung commented on Jul 22, 2026

    @RalfJung
    Member

    PR #112331 is open to remove the feature again, and is already marked as closing this.

    That PR got closed in the mean time.
    What is preventing this feature from being removed?

  16. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
  17. StackOverflowExcept1on commented on Sep 25, 2026

    @StackOverflowExcept1on
    Contributor

    Is there any news after #156882 stabilization?

  18. theemathas commented on Sep 25, 2026

    @theemathas
    Contributor

    @StackOverflowExcept1on I'm told that it's blocked on figuring out how to handle unwinding. Don't get your hopes up.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.S-tracking-design-concernsStatus: There are blocking design concerns.S-tracking-needs-summaryStatus: It's hard to tell what's been done and what hasn't! Someone should do some investigation.T-langRelevant to the language teamT-libsRelevant to the library team, which will review and decide on the PR/issue.WG-embeddedWorking group: Embedded systemsdisposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions