Skip to content

Implement MaybeDangling compiler support#150447

Open
WaffleLapkin wants to merge 6 commits intorust-lang:mainfrom
WaffleLapkin:maybe-dangling-semantics
Open

Implement MaybeDangling compiler support#150447
WaffleLapkin wants to merge 6 commits intorust-lang:mainfrom
WaffleLapkin:maybe-dangling-semantics

Conversation

@WaffleLapkin
Copy link
Member

@WaffleLapkin WaffleLapkin commented Dec 27, 2025

Tracking issue: #118166

cc @RalfJung

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 27, 2025
@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch 2 times, most recently from aebfd56 to 63e07cf Compare December 27, 2025 21:28
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Feb 19, 2026
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch from 6d96bdb to d71787d Compare February 19, 2026 18:35
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch 2 times, most recently from f6fafff to baa9118 Compare February 19, 2026 23:25
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch from baa9118 to c135ac1 Compare February 23, 2026 16:07
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch from c135ac1 to f399322 Compare February 25, 2026 23:07
@rustbot rustbot added the PG-exploit-mitigations Project group: Exploit mitigations label Feb 25, 2026
@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch 3 times, most recently from 8bbf80d to a72a3f3 Compare February 25, 2026 23:34
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch from a72a3f3 to 42ce338 Compare February 26, 2026 12:26
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch 2 times, most recently from 51a5c77 to efc351d Compare February 26, 2026 13:52
@WaffleLapkin WaffleLapkin marked this pull request as ready for review February 26, 2026 15:58
@rustbot
Copy link
Collaborator

rustbot commented Feb 26, 2026

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred in tests/codegen-llvm/sanitizer

cc @rcvalle

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 26, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 26, 2026

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 68 candidates
  • Random selection from 14 candidates

@WaffleLapkin
Copy link
Member Author

@RalfJung this is finally ready for review :)

Let me know if you'd prefer the PointeeInfo changes in a separate PR.

@rust-bors

This comment has been minimized.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

This looks generally great, thanks! Nice cleanup for getting rid of those align 1 everywhere.

I left some comments. I don't understand why the may_dangle field exists. Please resolve the conflicts so we can do a perf run.

View changes since this review

if offset.bytes() == 0
&& let Some(pointee) = this.ty.boxed_ty() =>
{
let optimize = tcx.sess.opts.optimize != OptLevel::No;
Copy link
Member

Choose a reason for hiding this comment

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

let optimize looks like it could be moved out of the match.

Copy link
Contributor

Choose a reason for hiding this comment

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

The let optimize = ... line itself is still there, now shadowing the one you put outside the match.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 1, 2026
@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch from efc351d to 8102f65 Compare March 2, 2026 13:43
@rustbot
Copy link
Collaborator

rustbot commented Mar 2, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@theemathas
Copy link
Contributor

This PR can result in stable code using ManuallyDrop getting less optimization, right?

@WaffleLapkin
Copy link
Member Author

@theemathas yes. Importantly also less UB ^^'

@RalfJung
Copy link
Member

RalfJung commented Mar 2, 2026

Indeed, that is the intent of this PR. ;)

Note that this PR does not guarantee the absence of those optimizations on stable. It only guarantees it for code that uses MaybeDangling directly, which remains unstable.

Make `size`/`align` always correct rather than conditionally on the
`safe` field. This makes it less error prone and easier to work with for
`MaybeDangling` / potential future pointer kinds like `Aligned<_>`.
Instead of defaulting to `None` it now defaults to `Align::ONE` i.e.
no alignment restriction. Codegen test changes are due to us now skipping
`align 1` annotations (they are useless; not skipping them makes all the
raw pointers gain an `align 1` annotation which doesn't seem any good)
@WaffleLapkin WaffleLapkin force-pushed the maybe-dangling-semantics branch from 395a073 to 62cbdee Compare March 2, 2026 15:48
// Handle safe Rust thin and wide pointers.
/// Returns argument attributes for a scalar argument.
///
/// `drop_target_pointee` is used to special-case the argument of `ptr::drop_in_place`,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// `drop_target_pointee` is used to special-case the argument of `ptr::drop_in_place`,
/// `drop_target_pointee`, if set, causes the scalar, if it is a pointer, to be treated as a mutable
/// reference to the given type. This is used to special-case the argument of `ptr::drop_in_place`,

This is unlike before where that treatment only happened if the scalar was by itself not already a safe pointer -- right?

Copy link
Member Author

Choose a reason for hiding this comment

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

if it is a pointer

that is not quite correct, we have an assertion that it's a pointer, not a check.

This is unlike before where that treatment only happened if the scalar was by itself not already a safe pointer -- right?

Yes, before it happened if the scalar was a pointer, and specifically a raw one.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, please make the docs whatever the correct thing is then. :)

Copy link
Member

@RalfJung RalfJung Mar 2, 2026

Choose a reason for hiding this comment

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

Please add a test (or extend an existing one) ensuring that MaybeDangling<&T> is not marked with captures or readonly, with a comment pointing this out as a relevant thing to test.

I wonder if it would make sense to make the tests like tests/codegen-llvm/function-arguments.rs where we actually list all the attributes so that we can easily notice when they change? The fact that I didn't realize that we are setting readdonly captures on these references concerns me.

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason I made the test like I did is that the differences between local and ci llvm were driving me insane trying to write a test that works on both of them ^^'

I'll look at function-argument.rs

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure that MaybeDangling<&T> actually need to strip captures and readonly? Those do not imply any validity invariants, and effectively only cause restrictions on any &Ts that get extracted out of the MaybeDangling and how they can be used.

That being said, I do not know what the miri paragraph in the RFC is saying because I don't know off the top of my head what retagging is, so... sorry if that's related to the answer to my question (though, even then I feel like this should be spelled out more explicitly).

Copy link
Member

@RalfJung RalfJung Mar 2, 2026

Choose a reason for hiding this comment

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

Are you sure that MaybeDangling<&T> actually need to strip captures and readonly?

For the model implemented in #150446, yes. So I'd say let's implement that model for now because it's the one we have and know how to implement; I have added this as an open question in the tracking issue.

Those do not imply any validity invariants

They do. They have to come from somewhere, after all.

safe: None,
// Make sure we don't assert dereferenceability of the pointer.
size: Size::ZERO,
..info
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
..info
// Preserve the alignment assertion! That is required even inside `MaybeDangling`.
..info

Copy link
Member

Choose a reason for hiding this comment

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

Also, why does this test ManuallyDrop? The actually relevant type is MaybeDangling.

Copy link
Member Author

Choose a reason for hiding this comment

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

(it's because that's the currently stable type that users care about)

Will change it though, no real reason to test ManuallyDrop

/// Indicates whether the type is `FieldRepresentingType`.
const IS_FIELD_REPRESENTING_TYPE = 1 << 13;
/// Indicates whether the type is `MaybeDangling<_>`.
const IS_MAYBE_DANGLING = 1 << 14;
Copy link
Member

Choose a reason for hiding this comment

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

Most of these are mutually exclusive so it is a huge waste of bits to have a separate bit for each of them... 🤷 that's pre-existing though, not for this PR to fix.

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

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants