rust-lang / rust-lang/rust

Miscompilation of a program projecting field of an extern type

Open
#127,336 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-mir-opt C-bug F-extern_types I-miscompile I-unsound requires-nightly T-compiler T-opsem
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Since #118534 projecting a field of an extern type at non-zero offset panics. MIR transformations do not understand this:

$ cat a.rs
#![feature(extern_types)]
extern "C" {
    type Opaque;
}
fn main() {
    let a = [0u8; 1];
    let b: &(u8, Opaque) = unsafe { &*(&a as *const _ as *const (u8, Opaque)) };
    let _ = &b.1;
}
$ rustc a.rs -Zmir-opt-level=1 && ./a
$ rustc a.rs -Zmir-opt-level=0 && ./a
thread 'main' panicked at library/core/src/panicking.rs:221:5:
attempted to compute the size or alignment of extern type `Opaque`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting.
Aborted

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

Reproduce the issue with the provided a.rs program using rustc at -Zmir-opt-level=1 and -Zmir-opt-level=0, then investigate the MIR transformations implicated in the report. Done means the two optimization levels no longer differ in this case and the program does not panic while projecting the extern type field.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.