rust-lang / rust-lang/rust

trying to get pointer to extern typed field causes runtime panic

Open
#126,800 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion F-extern_types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![feature(extern_types)]
extern {
    type Opaque;
}

struct Thing {
    _pad: u8,
    o: Opaque,
}

fn main() {
    let v = b"a";
    let x: &Thing = unsafe { std::mem::transmute(&v) };
    let field_ptr = &x.o as *const Opaque;
    println!("{:X}", field_ptr as usize)
}

I expected to see this happen: printing a stack address

Instead, this happened: panic: attempted to compute the size or alignment of extern type

Context

one of the main usecases for extern types is representing trailing fields of variable size for C ffi. this bug completely makes this impossible. it also panics during a piece of safe code that should be infallible. this panic does not occur if the extern type is the only field.

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (debd22da6 2024-05-29)
binary: rustc
commit-hash: debd22da66cfa97c74040ebf68e420672ac8560e
commit-date: 2024-05-29
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6
Backtrace

Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.48s
     Running `target/debug/playground`
thread 'main' panicked at library/core/src/panicking.rs:221:5:
attempted to compute the size or alignment of extern type `Opaque`
stack backtrace:
   0: rust_begin_unwind
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/std/src/panicking.rs:661:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/core/src/panicking.rs:112:18
   2: core::panicking::panic_nounwind_fmt
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/core/src/panicking.rs:122:5
   3: core::panicking::panic_nounwind
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/core/src/panicking.rs:221:5
   4: playground::main
             at ./[src/main.rs:14](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#):21
   5: core::ops::function::FnOnce::call_once
             at /rustc/684b3553f70148ded97a80371c2405984d4f6aa7/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.

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 compiling and running the minimal extern-types example from the issue, confirming the panic and its backtrace in library/core/src/panicking.rs. Trace how taking a reference to the extern-typed field computes size or alignment. Done means the example no longer panics and prints the field pointer address as expected, with regression coverage for this case.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.