rust-lang / rust-lang/rust

`i2p-p2i-opt` miscompiles rust code

Open
#147,538 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-LLVM C-bug I-miscompile T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Making a separate tracking issue for the core problem @nikic identified in https://github.com/rust-lang/rust/issues/147265#issuecomment-3360729310 since it's been in stable since at least May 2024.

The following rust code: https://rust.godbolt.org/z/eT9fbKvbq

#[no_mangle]
unsafe fn f(a: usize) -> u8 {
    let b = g(a) as usize;
    unsafe { *(b as *const u8) }
}

#[cold]
fn g(a: usize) -> *const u8 {
    unsafe { std::mem::transmute(a) }
}

in 1.77 compiled to

f:
        movzx   eax, byte ptr [rdi]
        ret

but starting in 1.78 becomes

f:
        ud2

The change on the rust side is https://github.com/rust-lang/rust/pull/121282 (cc @saethlin), but that's just exposing the problem in this example -- the root problem could probably be repro'd on older stable too, just in less-obvious ways.

For now we mitigated this with https://github.com/rust-lang/rust/pull/147541, but that's likely just a partial fix. That PR should be reverted when the issue is properly fixed.

It happens because LLVM removes the ptrtoint+inttoptr that's needed to recover exposed provenance from an otherwise-without_provenance pointer.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Rust code and assembly difference in the linked Godbolt example, then read Rust issues 147265 and 147538 plus pull requests 121282 and 147541. Trace the LLVM handling of the ptrtoint and inttoptr sequence for exposed provenance; done means the miscompilation is fixed and the mitigation in pull request 147541 can be reverted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.