Skip to content

Conversation

Mark-Simulacrum
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum commented Oct 11, 2025

I'm not sure this is the right fix, but it's simple enough and does roughly what I'd expect. Like with the previous optimization to codegen usize rather than a zero-sized static, there's no guarantee that we continue returning a particular value from the offsetting.

A grep for const_usize.*align found the same code copied to rustc_codegen_gcc and cranelift but a quick skim didn't find other cases of similar 'optimization'. That said, I'm not convinced I caught everything, it's not trivial to search for this.

Closes #147516

@rustbot
Copy link
Collaborator

rustbot commented Oct 11, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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 Oct 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 11, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
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

@rust-log-analyzer

This comment has been minimized.

@Mark-Simulacrum Mark-Simulacrum force-pushed the fix-offset-zst branch 4 times, most recently from 060e453 to 7a513dd Compare October 12, 2025 10:55
@Mark-Simulacrum
Copy link
Member Author

@bors r=nnethercote

@bors
Copy link
Collaborator

bors commented Oct 12, 2025

📌 Commit 7a513dd has been approved by nnethercote

It is now in the queue for this repository.

@bors bors 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 Oct 12, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 12, 2025
…nnethercote

Fix ICE on offsetted ZST pointer

I'm not sure this is the *right* fix, but it's simple enough and does roughly what I'd expect. Like with the previous optimization to codegen usize rather than a zero-sized static, there's no guarantee that we continue returning a particular value from the offsetting.

A grep for `const_usize.*align` found the same code copied to rustc_codegen_gcc and cranelift but a quick skim didn't find other cases of similar 'optimization'. That said, I'm not convinced I caught everything, it's not trivial to search for this.

Closes rust-lang#147516
bors added a commit that referenced this pull request Oct 12, 2025
Rollup of 7 pull requests

Successful merges:

 - #147168 (Don't unconditionally build alloc for `no-std` targets)
 - #147178 ([DebugInfo] Improve formatting of MSVC enum struct variants)
 - #147495 (Update wasm-component-ld to 0.5.18)
 - #147576 (Fix ICE on offsetted ZST pointer)
 - #147592 (Add tidy to the target of ./x check)
 - #147597 (Add a regression test for #72207)
 - #147604 (Some clippy cleanups in compiler)

r? `@ghost`
`@rustbot` modify labels: rollup
@matthiaskrgr
Copy link
Member

@bors r-
#147609 (comment)

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 12, 2025

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

@rustbot
Copy link
Collaborator

rustbot commented Oct 12, 2025

This PR was rebased onto a different master 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.

@Mark-Simulacrum
Copy link
Member Author

@bors try jobs=dist-i586-gnu-i586-i686-musl

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Oct 12, 2025
Fix ICE on offsetted ZST pointer

try-job: dist-i586-gnu-i586-i686-musl
/// Assuming that the pointer is to a ZST, this computes the appropriate value to use for the
/// result of properly aligning (essentially `ptr::dangling`) and offsetting the resulting
/// pointer.
pub fn zst_addr(self, align: Align, cx: &impl HasDataLayout) -> u64 {
Copy link
Member

Choose a reason for hiding this comment

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

I can't make any sense of this comment, unfortunately. If this pointer points to an actual allocation, it makes no logical sense to just add its offset to anything else except for the address of that allocation.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, this is about pointers to zero-sized allocations -- not ZST pointers in general. &() can point inside a larger allocation and then this function must not be used.

And even then, the only time this function is correct to use if if the caller can guarantee that all pointers to this allocation are treated like this. I am not sure that should be such an easy-to-accidentally-misuse helper like this.

@rust-log-analyzer

This comment has been minimized.

A grep for `const_usize.*align` found the same code copied to
rustc_codegen_gcc but I don't see other cases where we get this wrong.
@rust-bors
Copy link

rust-bors bot commented Oct 12, 2025

☀️ Try build successful (CI)
Build commit: 33c7e18 (33c7e186ed3534529f54d75674a864e6e1617999, parent: ff6dc928c5e33ce8e65c6911a790b9efcb5ef53a)


// This tests for regression of https://github.com/rust-lang/rust/issues/147516
//
// THe compiler will codegen `&Zst` without creating a real allocation, just a properly aligned
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
// THe compiler will codegen `&Zst` without creating a real allocation, just a properly aligned
// The compiler will codegen `&Zst` without creating a real allocation, just a properly aligned

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. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE compiler/rustc_codegen_llvm/src/common.rs 2 == 0

7 participants