Skip to content

Fix ICE in GVN that interns allocation containing provenance - #162463

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
cclfmht:issue-160750
Sep 15, 2026
Merged

rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
cclfmht:issue-160750

Conversation

@cclfmht

@cclfmht cclfmht commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

While trying to intern an allocation into the global memory, GVN only checked if the portion of the allocation referred by the MPlaceTy from const eval contains provenance, but then it interned the whole allocation instead of just that portion. This causes ICE if the unchecked part contains provenance, as in #160750.

This PR extends the check to the whole allocation so that we won't accidentally intern an allocation with provenance.

Also, the check for unsized local in try_as_evaluated_constant seems to be redundant with the one in op_to_prop_const, this PR removes it.

@rustbot

rustbot commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 8, 2026
@rustbot

rustbot commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
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, mir, mir-opt
  • compiler, mir, mir-opt expanded to 75 candidates
  • Random selection from 20 candidates

@rust-log-analyzer

This comment has been minimized.

@folkertdev

Copy link
Copy Markdown
Contributor

r? mir-opt

@rustbot rustbot assigned oli-obk and unassigned folkertdev Sep 8, 2026
@oli-obk

oli-obk commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

This PR extends the check to the whole allocation so that we won't accidentally intern an allocation with provenance.

hmm, that means it applies less often now, even if it could apply by just interning the part of the alloc that is needed. We could create a new alloc for just the needed part and copy over that memory region. I guess that could be problematic if someone relies on the other memory lying right next to the now-copied memory. Thoughts?

@cclfmht

cclfmht commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

We could create a new alloc for just the needed part and copy over that memory region. I guess that could be problematic if someone relies on the other memory lying right next to the now-copied memory. Thoughts?

That's actually the first option came into my head, and yeah, in the situation you described, we might have to do something like track and merge allocation blocks in the global memory, which can be expensive and unnecessarily complex for a "hack" IMO.

Maybe another option is to clear the provenance from the allocation before interning it, in that case we can just check the needed part. This should also avoid triggering ICE, but I am not sure if there is any subsequent phase relies on that provenance to work correctly.

Comment thread compiler/rustc_mir_transform/src/gvn.rs Outdated
// Due to https://github.com/rust-lang/rust/issues/128775, doing so could lead to bugs.
// FIXME: remove this hack once that issue is fixed.
let size = ecx.get_alloc_info(alloc_id).size;
let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).discard_err()??;

@oli-obk oli-obk Sep 10, 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.

doesn't this mean that if there is provenance before where the pointer points, we'd still get the ICE?.

I think you'd just want to do the GlobalAlloc::Memory check below real early and then work off the alloc directly, as that's what we're going to be interning.

View changes since the review

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.

doesn't this mean that if there is provenance before where the pointer points, we'd still get the ICE?.

You are right. I forgot that a pointer also contains an offset and treated it as a reference to an allocation 😅. I think this is likely the reason that failed the CI.

I think you'd just want to do the GlobalAlloc::Memory check below real early

Does this mean to move the GlobalAlloc::Memory check before even the call to intern_const_alloc_for_constprop? From my understanding this won't make sense if we haven't intern the allocation into the global memory yet, and it will trigger ICE in that case. Please correct me if I misunderstand something.

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.

Oh right 🤦 yea, unsure how to load the entire alloc first, but there should be sth for loading it

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.

I think we can make a copy of the pointer inside mplace and set its offset to zero, then we can access the full allocation with the pointer copy and the allocation size by calling ecx.get_ptr_alloc().

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.

Hmm, maybe just add a method to Memory that checks if an alloc has any relocations. That method can access the alloc directly without exposing such a capability to everyone

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.

That works for me. How about add it to InterpCx so that it is directly accesible under ecx? I just took a quick glance at the current APIs and felt that such method will go there.

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.

If that has enough access and there are similar existing methods, then yes

@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 Sep 10, 2026
@rustbot

rustbot commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@cclfmht

cclfmht commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

/// Intern `ret`. This function assumes that `ret` references no other allocation.
#[instrument(level = "debug", skip(ecx))]
pub fn intern_const_alloc_for_constprop<'tcx, M: CompileTimeMachine<'tcx>>(
ecx: &mut InterpCx<'tcx, M>,
alloc_id: AllocId,
) -> InterpResult<'tcx, ()> {

BTW, I am wondering if we shall also update the documentation of intern_const_alloc_for_constprop as it doesn't have a ret parameter now.

@rustbot

rustbot commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

@rustbot

This comment has been minimized.

@cclfmht

cclfmht commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@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 Sep 15, 2026
GVN only checked whether the portion of allocation referred by `MPlaceTy`
contains provenance, but we then intern the whole allocation. This causes
ICE if the unchecked part contains provenance. This commit extends the
check to the whole allocation.

Also, removes a check for unsized local in `try_as_evaluated_constant`
which seems to duplicate with the one in `op_to_prop_const`.
We only checked the provenance for a part of the allocation that will be
interned. Fixed by adding a method under `InterpCx` to check the whole
allocation.
@rustbot

rustbot commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

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.

@oli-obk oli-obk left a comment •

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.

@rust-bors

rust-bors Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 2cfd6c5 has been approved by oli-obk

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 15, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 15, 2026
…uwer

Rollup of 3 pull requests

Successful merges:

 - #162463 (Fix ICE in GVN that interns allocation containing provenance)
 - #162806 (wasm: correctly implement `union` trivial aggregates)
 - #162711 (cleanup: Move consumed predicate obligations into recursive evaluation)
@rust-bors
rust-bors Bot merged commit 38ed4e4 into rust-lang:main Sep 15, 2026
13 checks passed
rust-bors Bot pushed a commit that referenced this pull request Sep 15, 2026
Rollup merge of #162463 - cclfmht:issue-160750, r=oli-obk

Fix ICE in GVN that interns allocation containing provenance

While trying to intern an allocation into the global memory, GVN only checked if the portion of the allocation referred by the `MPlaceTy` from const eval contains provenance, but then it interned the whole allocation instead of just that portion. This causes ICE if the unchecked part contains provenance, as in #160750.

This PR extends the check to the whole allocation so that we won't accidentally intern an allocation with provenance.

Also, the check for unsized local in `try_as_evaluated_constant` seems to be redundant with the one in `op_to_prop_const`, this PR removes it.
@rustbot rustbot added this to the 1.100.0 milestone Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler 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