1

So, laced throughout alloc and std methods are marked with #[cfg(not(no_global_oom_handling))], primarily methods that perform allocations where the case of running out of memory is handled via panicking. I've been researching all morning on how to use alloc with that flag enabled. I've gone down a few rabbit holes, such as build-std, but I've come up blank.

Note: I'm aware of the implications; this isn't a question of if I should or shouldn't, only a question of how to enable no_global_oom_handling?

Does anyone know how actually to enable this feature? I'm surprised it's not documented everywhere, even its tracking issue.

1 Answers1

2

You would need to pass a --cfg option. Either as arguments if using rustc directly, or in a RUSTFLAGS environment variable or build.rustflags configuration option in .cargo/config.toml when using Cargo.

An example (including build-std):

RUSTFLAGS="--cfg no_global_oom_handling" cargo +nightly run -Z build-std

See also: How to set cfg options to compile conditionally?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
  • I think they'll still need `-Zbuild-std` for this to work – PitaJ Dec 07 '22 at 20:06
  • @PitaJ you're almost definitely right, I've added that to the example command – kmdreko Dec 07 '22 at 20:11
  • So far, I've adopted the `.cargo/config.toml` route. I've then tried `cargo +nightly build -Z build-std=core,alloc --target x86_64-unknown-linux-gnu`. It now builds, but when testing, I can still use items from `alloc` marked with `#[cfg(not(no_global_oom_handling))]`. Perhaps Rust is still seeing the `extern crate alloc` to resolve to the prebuilt one? It's causing me a lot of trouble. – Sean Colin Roach Dec 08 '22 at 02:42
  • It seems that my use case is still not supported; while the answer doesn't work as-is, it's good enough for anyone who stumbles across this in the future. – Sean Colin Roach Dec 08 '22 at 19:54