Skip to content

Building a no_std program without explicitly using eyra now fails with "no global memory allocator found but one is required" #60

Description

@polarathene

This might not be Eyra specifically, and technically it's a non-issue, I only observed this when building a basic program that only needs the rustix dep, but having eyra in the Cargo.toml deps introduced a build failure with nightly in the past month. My actual project had two separate sources in src/bin/*.rs, one was for Eyra, another for Rustix.

Since it's highly likely one of the crates that @sunfishcode maintains, I thought I'd open an issue to raise awareness of the observation just in case there was a mistaken assumption made 🤔 I assume because of the eyra dep during compilation, even though it's not used/required, it now messes with the global allocator in some manner?

Reproduction

Cargo.toml:

[package]
name = "example"
version = "0.1.0"
edition = "2024"

[dependencies]
eyra = { version = "0.21.0", default-features = false }
rustix = { version = "1.0.0", default-features = false, features = ["runtime", "stdio"] }

# Requirements to build (unrelated to reported build failure with eyra)
# `no_std` usage in `src/main.rs` requires `panic="abort"`
# `lto = true` is a workaround required in nightly, alternatively `-Z build-std`
# More details covered in collapsed segment of:
# https://github.com/sunfishcode/eyra/issues/60#issuecomment-2892735792
[profile.release]
lto = true
panic = "abort"

src/main.rs:

#![no_std]
#![no_main]

#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
  hello_world();
  exit();
}

fn exit() -> ! { unsafe { rustix::runtime::exit_thread(42) } }

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} }

#[inline(always)]
fn hello_world() {
  rustix::io::write(
    unsafe { rustix::stdio::stdout() },
    "Hello, world!\n".as_bytes()
  ).unwrap();
}

Nightly May

rust-toolchain.toml:

[toolchain]
profile = "minimal"
channel = "nightly-2025-05-18"
components = ["rust-src"]
targets = ["x86_64-unknown-linux-gnu"]
$ RUSTFLAGS="-C target-feature=+crt-static -C relocation-model=static -C link-arg=-nostartfiles" cargo build --release --target x86_64-unknown-linux-gnu

# ...

error: no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait

error: could not compile `example` (bin "example") due to 1 previous error

Nightly April

An earlier nightly is required with older crate versions to compile approx a month ago:

[toolchain]
profile = "minimal"
channel = "nightly-2025-04-14"
components = ["rust-src"]
targets = ["x86_64-unknown-linux-gnu"]

This requires a few changes to be compatible with the earlier nightly, but now it builds without the global allocator requirement:

# Rollback eyra one release:
$ cargo add eyra@0.20.0

# Pin `unwinding` for nightly compatibility:
# (related to newer nightly change making the naked attribute safe)
$ cargo update unwinding --precise 0.2.5

$ RUSTFLAGS="-C target-feature=+crt-static -C relocation-model=static -C link-arg=-nostartfiles" cargo +nightly-2025-04-14 build --release --target x86_64-unknown-linux-gnu

# ...

   Compiling example v0.1.0 (/example)
    Finished `release` profile [optimized] target(s) in 54.42s

For context, the eyra rollback made the following lock file adjustments:

Downgrading c-gull v0.22.1 -> v0.21.0
Downgrading c-scape v0.22.1 -> v0.21.0
Downgrading eyra v0.21.0 -> v0.20.0 (available: v0.22.0)
Downgrading getrandom v0.3.3 -> v0.2.16
    Adding itoa v1.0.15
    Adding linux-raw-sys v0.4.15
Downgrading origin v0.26.2 -> v0.24.0
Downgrading rand v0.9.1 -> v0.8.5
Downgrading rand_core v0.9.3 -> v0.6.4
Downgrading rand_pcg v0.9.0 -> v0.3.1
    Adding rustix v0.38.44
Downgrading rustix-dlmalloc v0.2.2 -> v0.1.7
Downgrading rustix-futex-sync v0.4.0 -> v0.2.3
Downgrading rustix-openpty v0.2.0 -> v0.1.1
Downgrading wasi v0.14.2+wasi-0.2.4 -> v0.11.0+wasi-snapshot-preview1

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions