Commit Graph

54 Commits

Author SHA1 Message Date
Pavel Grigorenko e212560317 Stabilize alloc_layout_extra 2026-01-11 16:39:18 +03:00
Matthias Krüger 6078dd3bdf Rollup merge of #147725 - bjorn3:remove_oom_panic, r=Amanieu
Remove -Zoom=panic

There are major questions remaining about the reentrancy that this allows. It doesn't have any users on github outside of a single project that uses it in a panic=abort project to show backtraces. It can still be emulated through `#[alloc_error_handler]` or `set_alloc_error_hook` depending on if you use the standard library or not. And finally it makes it harder to do various improvements to the allocator shim.

With this PR the sole remaining symbol in the allocator shim that is not effectively emulating weak symbols is the symbol that prevents skipping the allocator shim on stable even when it would otherwise be empty because libstd + `#[global_allocator]` is used.

Closes https://github.com/rust-lang/rust/issues/43596
Fixes https://github.com/rust-lang/rust/issues/126683
2025-12-10 07:54:17 +01:00
bors 467250ddb2 Auto merge of #144465 - orlp:system-alloc-tls, r=Mark-Simulacrum
Allow the global allocator to use thread-local storage and std::thread::current()

Fixes https://github.com/rust-lang/rust/issues/115209.

Currently the thread-local storage implementation uses the `Global` allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd *really* want to use thread-locals. We also see that this lead to hacks such as https://github.com/rust-lang/rust/pull/116402, where we detect re-entrance and abort.

So I've made the places where I could find allocation happening in the TLS implementation use the `System` allocator instead. I also applied this change to the storage allocated for a `Thread` handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.

r? `@joboet`
2025-11-29 02:08:53 +00:00
bjorn3 8f55c15bfe Remove -Zoom=panic
There are major questions remaining about the reentrancy that this
allows. It doesn't have any users on github outside of a single project
that uses it in a panic=abort project to show backtraces. It
can still be emulated through #[alloc_error_handler] or
set_alloc_error_hook depending on if you use the standard library or
not. And finally it makes it harder to do various improvements to the
allocator shim.
2025-11-28 19:30:39 +00:00
Orson Peters f9b0811d01 Add documentation guaranteeing global allocator use of TLS
Remove outdated part of comment claiming thread_local re-enters global allocator

Fix typo in doc comment

Add comments for guarantees given and footnote that System may still be called

Revise mention of using the global allocator

Allow for the possibility that the global allocator is the system allocator.

Co-authored-by: Mark Rousskov <mark.simulacrum@gmail.com>
2025-11-28 00:09:12 +01:00
bjorn3 4627bff6e6 Show backtrace on allocation failures when possible
And if an allocation while printing the backtrace fails, don't try to
print another backtrace as that will never succeed.
2025-11-26 14:02:37 +00:00
bjorn3 116f4ae171 Support #[alloc_error_handler] without the allocator shim
Currently it is possible to avoid linking the allocator shim when
__rust_no_alloc_shim_is_unstable_v2 is defined when linking rlibs
directly as some build systems need. However this requires liballoc to
be compiled with --cfg no_global_oom_handling, which places huge
restrictions on what functions you can call and makes it impossible to
use libstd. Or alternatively you have to define
__rust_alloc_error_handler and (when using libstd)
__rust_alloc_error_handler_should_panic
using #[rustc_std_internal_symbol]. With this commit you can either use
libstd and define __rust_alloc_error_handler_should_panic or not use
libstd and use #[alloc_error_handler] instead. Both options are still
unstable though.

Eventually the alloc_error_handler may either be removed entirely
(though the PR for that has been stale for years now) or we may start
using weak symbols for it instead. For the latter case this commit is a
prerequisite anyway.
2025-10-10 13:04:53 +00:00
Daniel Paoliello 2b22d0f0d2 Make __rust_alloc_error_handler_should_panic a function 2025-07-03 10:52:21 -07:00
Christopher Durham 4d93f60568 use generic Atomic type where possible
in core/alloc/std only for now, and ignoring test files

Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2025-04-27 02:18:08 +03:00
Boxy c93005ee65 update cfgs 2025-04-09 12:29:59 +01:00
bjorn3 42de015549 Mark imports of #[rustc_std_internal_symbol] items with this attribute
This ensures that they will be correctly mangled in a future commit.
2025-03-17 14:06:56 +00:00
Eric Huss 4f4ea35a69 std: Apply unsafe_op_in_unsafe_fn 2025-02-14 07:36:17 -08:00
Michael Goulet a4e7f8f9bf Mark extern blocks as unsafe 2025-02-09 17:11:13 +00:00
Nicholas Nethercote 84ac80f192 Reformat use declarations.
The previous commit updated `rustfmt.toml` appropriately. This commit is
the outcome of running `x fmt --all` with the new formatting options.
2024-07-29 08:26:52 +10:00
Tobias Bucher bb8eb44511 Unify guarantees about the default allocator
`std::alloc` said that the default allocator is unspecified for all
crrate types except `cdylib` and `staticlib`. Adjust
`std::alloc::System` documentation to say the same.

Fixes #125870.
2024-06-11 17:13:26 +02:00
Ralf Jung 3f6703bbd8 default_alloc_error_hook: explain difference to default __rdl_oom in alloc 2024-05-03 19:12:33 +02:00
Mara Bos 904fef0e24 SeqCst->{Release,Acquire} for alloc error hook.
SeqCst is unnecessary.
2024-03-19 15:27:11 +01:00
joboet fa9a911a57 libs: use assert_unchecked instead of intrinsic 2024-01-13 20:10:00 +01:00
Kevin Reid 3dde25edc4 Correct and expand documentation of handle_alloc_error and set_alloc_error_hook.
Add the following facts:

* `handle_alloc_error` may panic instead of aborting.
* What happens if a hook returns rather than diverging.
* A hook may panic. (This was already demonstrated in an example,
  but not stated in prose.)
* A hook must be sound to call — it cannot assume that it is only
  called by the runtime, since its function pointer can be retrieved by
  safe code.
2023-08-19 13:27:03 -07:00
James Dietz db4a153440 remove additional [allow(unused_unsafe)] 2023-07-24 17:56:38 -04:00
Mikail Bagishov 7b9f64475e Change memory ordering in System wrapper example
Currently, the `SeqCst` ordering is used, which seems unnecessary:
+ Even `Relaxed` ordering guarantees that all updates are atomic and are executed in total order
+ User code only reads atomic for monitoring purposes, no "happens-before" relationships with actual allocations and deallocations are needed for this

If argumentation above is correct, I propose changing ordering to `Relaxed` to clarify that no synchronization is required here, and improve performance (if somebody copy-pastes this example into their code).
2023-01-08 22:53:51 +03:00
Ralf Jung 5974f6f0a5 default OOM handler: use non-unwinding panic (unless -Zoom=panic is set), to match std handler 2023-01-02 16:35:14 +01:00
Christopher Durham 69721defc2 Forbid mixing System with sytem allocator calls 2022-09-03 16:47:12 -05:00
Lukas Wirth 756118e2b9 Update std::alloc::System docs 2022-06-26 16:31:29 +02:00
Ralf Jung b05d71f880 make std not use &A: Allocator instance 2022-06-18 07:38:28 -07:00
Lucas Dumont 5adef6c795 Add std::alloc::set_alloc_error_hook example 2022-06-07 15:06:18 +02:00
kraktus 519aa6e4d7 update jemallocator example to use 2018 edition import syntax 2022-05-04 13:43:33 +02:00
Pietro Albini 181d28bb61 trivial cfg(bootstrap) changes 2022-04-05 23:18:40 +02:00
bors d6f3a4ecb4 Auto merge of #88098 - Amanieu:oom_panic, r=nagisa
Implement -Z oom=panic

This PR removes the `#[rustc_allocator_nounwind]` attribute on `alloc_error_handler` which allows it to unwind with a panic instead of always aborting. This is then used to implement `-Z oom=panic` as per RFC 2116 (tracking issue #43596).

Perf and binary size tests show negligible impact.
2022-03-18 03:01:46 +00:00
T-O-R-U-S 72a25d05bf Use implicit capture syntax in format_args
This updates the standard library's documentation to use the new syntax. The
documentation is worthwhile to update as it should be more idiomatic
(particularly for features like this, which are nice for users to get acquainted
with). The general codebase is likely more hassle than benefit to update: it'll
hurt git blame, and generally updates can be done by folks updating the code if
(and when) that makes things more readable with the new format.

A few places in the compiler and library code are updated (mostly just due to
already having been done when this commit was first authored).
2022-03-10 10:23:40 -05:00
Amanieu d'Antras aa36237e16 Add -Z oom={panic,abort} command-line option 2022-03-03 12:58:38 +00:00
Christiaan Dirkx 4ff5ab5296 Rename rterr to rtprintpanic 2021-05-19 15:52:09 +02:00
Christiaan Dirkx 6145051eee Replace sys_common::util::dumb_print with rterr! 2021-05-19 15:05:35 +02:00
LingMan 769fb8a8b7 Fix safety comment
The size assertion in the comment was inverted compared to the code. After fixing that the implication that `(new_size >= old_size) => new_size != 0` still doesn't hold so explain why `old_size != 0` at this point.
2021-01-07 09:13:21 +01:00
pierwill 9cb43bd994 Add missing punctuation to std::alloc docs
Add a period to first line of module docs to match other modules in std.
2020-12-17 21:49:32 -08:00
Tim Diekmann 9274b37d99 Rename AllocRef to Allocator and (de)alloc to (de)allocate 2020-12-04 14:47:15 +01:00
hyd-dev 70e175b551 Add missing newline to error message of the default OOM hook 2020-11-10 00:15:07 +08:00
Jacob Hughes 5829560a68 Rename AllocErr to AllocError 2020-09-28 14:51:03 -04:00
blitzerr 2b19b14cec a few more &mut self -> self changes 2020-09-22 21:04:31 -07:00
blitzerr 3ffd403c6b removing &mut self for other methods of AllocRef 2020-09-22 06:22:02 -07:00
blitzerr d9d02fa168 Changing the alloc() to accept &self instead of &mut self 2020-09-21 16:43:36 -07:00
Flying-Toast c66789d572 Capitalize safety comments 2020-09-08 22:26:44 -04:00
Tim Diekmann 438c40efa1 Allow reallocation to different alignment 2020-08-19 06:46:47 +02:00
Tim Diekmann 63d241a7b7 Make grow_impl unsafe 2020-08-18 15:22:10 +02:00
Tim Diekmann 66a651244e Add comment, which was removed by accident (again) 2020-08-18 10:00:31 +02:00
Tim Diekmann a9fe0ca47a Clean up AllocRef implementation and documentation 2020-08-18 09:53:22 +02:00
bors 515c9fa505 Auto merge of #75621 - TimDiekmann:no-fast-realloc, r=Amanieu
Remove fast path in reallocation for same layout sizes

r? @Amanieu

Before merging a perf-run should be done.

Closes https://github.com/rust-lang/wg-allocators/issues/70
2020-08-18 05:42:05 +00:00
Ellen a2dfc3ec78 Switch to intra-doc links for std/src/alloc.rs 2020-08-17 14:42:40 +01:00
Tim Diekmann c48f784418 Fix typo in comment 2020-08-17 15:05:19 +02:00
Tim Diekmann c619b36975 Remove fast path in reallocation for same layout sizes 2020-08-17 13:23:38 +02:00