Skip to content

Rewrite safety requirements for Allocator impls#157801

Open
theemathas wants to merge 1 commit into
rust-lang:mainfrom
theemathas:allocator-safety-rewrite
Open

Rewrite safety requirements for Allocator impls#157801
theemathas wants to merge 1 commit into
rust-lang:mainfrom
theemathas:allocator-safety-rewrite

Conversation

@theemathas

@theemathas theemathas commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

View all comments

This PR supersedes #156544.

cc #157428, #156920

cc @nia-e

r? @Amanieu (reviewer of #156544)

I define the concept of "equivalent allocators", in order to be able to talk about cloning allocators, and to give commonsense guarantees about stdlib Allocator impls.

I define the concept of "invalidating a memory block" in order to be able to talk about users not being allowed to use a block of allocated memory after its allocator is "gone".

An Allocator implementation is now allowed to invalidate its allocations when the allocator is mutated or when a lifetime in the allocator type expires.

Mutation of an Allocator should sensibly be allowed to invalidate its allocations. For example, the bumpalo crates has a Bump::reset method that takes &mut self and invalidates all past allocations. Accesses via & still must not invalidate past allocations since, for example, Box provides & access to the allocator.

I still had the "allocator destructor" clause as a separate clause from the &mut clause, to avoid questions about whether drop glue of types that don't implement Drop but have fields that implement Drop counts as creating a &mut to the whole thing.

The "lifetime expiry" clause closes a hole/ambiguity on when an allocator is considered to be "dropped" if it does not have a destructor. Additionally, this clause matches what is required for Box::into_pin and {Rc, Arc}::pin to be sound. (Those methods have an A: 'static bound to prevent allocating via a &MyAllocator and then running MyAllocator's destructor.)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 12, 2026
@theemathas theemathas added A-allocators Area: Custom and system allocators T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 12, 2026
Comment on lines +152 to +155
/// until that memory block is [*invalidated*]. The implementor must also
/// not violate this invariant of `Allocator` via allocator equivalences
/// that are in the implementor's control (e.g., via a misbehaving
/// `impl Clone for Box<MyAllocator>`).

@theemathas theemathas Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Any ideas on how to better word this?

View changes since the review

I define the concept of "equivalent allocators", in order to be able
to talk about cloning allocators, and to give commonsense guarantees
about stdlib `Allocator` impls.

I define the concept of "invalidating a memory block" in order to be
able to talk about users not being allowed to use a block of allocated
memory after its allocator is "gone".

An `Allocator` implementation is now allowed to invalidate its
allocations when the allocator is mutated or when a lifetime in the
allocator type expires.

Mutation of an `Allocator` should sensibly be allowed to invalidate its
allocations. For example, the `bumpalo` crates has a `Bump::reset`
method that takes `&mut self` and invalidates all past allocations.
Accesses via `&` still must not invalidate past allocations since,
for example, `Box` provides `&` access to the allocator.

I still had the "allocator destructor" clause as a separate clause from
the `&mut` clause, to avoid questions about whether drop glue of types
that don't implement `Drop` but have fields that implement `Drop` counts
as creating a `&mut` to the whole thing.

The "lifetime expiry" clause closes a hole/ambiguity on when an
allocator is considered to be "dropped" if it does not have a
destructor. Additionally, this clause matches what is required for
`Box::into_pin` and `{Rc, Arc}::pin` to be sound. (Those methods have an
`A: 'static` bound to prevent allocating via a `&MyAllocator` and then
running `MyAllocator`'s destructor.)
@theemathas theemathas force-pushed the allocator-safety-rewrite branch from 2861839 to d9b90b0 Compare June 12, 2026 12:23
@nia-e

nia-e commented Jun 12, 2026

Copy link
Copy Markdown
Member

The language overall looks good, but I have one design nitpick; do we want to only enforce commutative equivalence? I agree transitivity is probably necessary, but there is a world where it makes sense to say A can free from B but the reverse is not (always) true, e.g. if B allocates from a subset of the memory pool A does. The relationship I'm envisioning looks more like lifetime-y subtyping, but I'm also happy merging something like this if it would be at least possible to relax it in the future.

@theemathas

Copy link
Copy Markdown
Contributor Author

@nia-e A problem with this kind of guarantee is that we can neither relax nor strengthen guarantees in the future.

If we require commutativity now, and allow non-commutativity later, then this would break users of the trait that rely on commutativity, if there was a way to rely on it.

If we allow non-commutativity now, and then require commutativity later, then this would break implementers of the trait that somehow have non-commutative implementations, if there was a way to have such an implementation.

@theemathas

Copy link
Copy Markdown
Contributor Author

I suppose a way to do this (at the expense of more complexity in the documentation), is to require implementers to have commutativity, but don't allow users of the trait to rely on commutativity. Not sure how I would write that down in the documentation that is comprehensible though. The requirements are extremely complicated already.

Comment thread library/core/src/alloc/mod.rs
Comment thread library/core/src/alloc/mod.rs
/// ### Currently allocated memory
///
/// Some of the methods require that a memory block is *currently allocated* by an allocator.
/// Some of the methods require that a memory block is *currently allocated* by specific allocator.

@nia-e nia-e Jun 12, 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.

Suggested change
/// Some of the methods require that a memory block is *currently allocated* by specific allocator.
/// Some of the methods require that a memory block is *currently allocated* by some specific allocator.

View changes since the review

/// returned by [`allocate`], [`grow`], or [`shrink`], and
/// * the memory block has not subsequently been deallocated.
/// * the starting address for that memory block was previously
/// returned by the [`allocate`], [`allocate_zeroed`], [`grow`], or [`shrink`] methods,

@nia-e nia-e Jun 12, 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.

or grow_zeroed, if we're being pedantic. May be worth defining somewhere the concept of "allocating methods", "deallocating methods", and "reallocating methods"

View changes since the review

Comment thread library/core/src/alloc/mod.rs
Comment thread library/core/src/alloc/mod.rs
/// any specific currently allocated memory block won't be invalidated, by:
/// * not deallocating that memory block,
/// * owning an allocator that memory block is allocated with, and
/// * not publicly exposing `&mut` access to that allocator.

@nia-e nia-e Jun 12, 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.

may be worth clarifying that even if some allocator is never equivalent with any other, exposing a &mut to said allocator is still potentially unsound since it lets that allocator be mem::swapped with another (though that wouldn't invalidate its allocated memory, just means we have a different deallocator now)

View changes since the review

/// A memory block which is [*currently allocated*] may be passed to
/// any method of the allocator that accepts such an argument.
/// Implementors of `Allocator` must ensure that a memory block that
/// are [*currently allocated*] by the allocator points to valid memory,

@nia-e nia-e Jun 12, 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.

Suggested change
/// are [*currently allocated*] by the allocator points to valid memory,
/// is [*currently allocated*] by the allocator points to valid memory,

View changes since the review

/// allocation was grown in-place. The newly returned pointer is the only valid pointer
/// for accessing this memory now.
/// If this returns `Ok`, then the memory block referenced by `ptr` has been [*invalidated*].
/// Any access to the old `ptr` is Undefined Behavior, even if the allocation was grown in-place.

@nia-e nia-e Jun 12, 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.

I think we usually avoid phrasing it as "x is UB" and instead say e.g. "Accesses may not be performed through the old pointer"

View changes since the review

@nia-e

nia-e commented Jun 12, 2026

Copy link
Copy Markdown
Member

@nia-e A problem with this kind of guarantee is that we can neither relax nor strengthen guarantees in the future.

If we require commutativity now, and allow non-commutativity later, then this would break users of the trait that rely on commutativity, if there was a way to rely on it.

If we allow non-commutativity now, and then require commutativity later, then this would break implementers of the trait that somehow have non-commutative implementations, if there was a way to have such an implementation.

I'm thinking of this in terms of Eq and PartialOrd. We can for now require commutative equivalence (i.e. a PartialEq-like trait that also requires Eq semantics that queries equivalence), and then later introduce semantics for something weaker (a PartialOrd-like trait that queries a subset relationship). Though yes the complexity is getting a bit high so maybe that's best left to an ecosystem crate

@theemathas

Copy link
Copy Markdown
Contributor Author

Talked in DMs with @nia-e, and we agreed that we should use a symmetric relationship, since that is easier to explain to users. If a library needs an asymmetric relationship, they can define their own concept with their own name, and that library can add that extra promise for their allocators on top of the requirements of Allocator.

@theemathas theemathas added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. and removed needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Jun 12, 2026
Comment on lines +90 to +92
/// Note: Currently, the interaction between cloning and unsize-coercing allocators
/// is unsound, and there is ongoing discussion on how to revise the `Allocator` trait
/// to fix this. See [#156920].

@clarfonthey clarfonthey Jun 12, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Personally, I would be in favour of just incorporating AllocatorEq and AllocatorClone into whichever PR does these wording changes, since that fixes the problem and feels like a more reasonable way to do it.

That way, we require implementing those two traits to prove sound equivalence.

View changes since the review

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-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API 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