Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal compiler error: unexpected panic: thread 'rustc' panicked at 'called Option::unwrap() on a None value', compiler/rustc_target/src/abi/call/x86_64.rs:158:47 #94223

Closed
horazont opened this issue Feb 21, 2022 · 1 comment · Fixed by #108611
Labels
A-ABI Area: Concerning the application binary interface (ABI) A-FFI Area: Foreign function interface (FFI) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@horazont
Copy link

Code

use std::os::raw::*;
use std::ffi::CStr;

pub trait Driver {
	fn resolution(&self) -> u32;
}

#[repr(C)]
pub struct CDriver {
	open: unsafe extern "C" fn(path: CStr, rd_obj: *mut c_void, wr_obj: *mut c_void) -> c_int,
	get_meta: unsafe extern "C" fn(name: *mut *const c_char, version: *mut *const c_char),
	get_resolution: unsafe extern "C" fn() -> u32,
}

#[repr(transparent)]
struct CDriverWrap {
	inner: CDriver,
}

impl Driver for CDriverWrap {
	fn resolution(&self) -> u32 {
		todo!();
	}
}

Notes:

  • Removing inner: CDriver removes the ICE
  • Removing any of the other fields from CDriver removes the ICE
  • Removing the trait + impl removes the ICE

Meta

NB: reproduces with beta + nightly via play.rust-lang.org

rustc --version --verbose:

rustc 1.57.0 (f1edd0429 2021-11-29)
binary: rustc
commit-hash: f1edd0429582dd29cccacaf50fd134b05593bd9c
commit-date: 2021-11-29
host: x86_64-unknown-linux-gnu
release: 1.57.0
LLVM version: 13.0.0

Error output

thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_target/src/abi/call/x86_64.rs:158:47
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Full output + Backtrace

warning: field is never read: `inner`
  --> main.rs:17:2
   |
17 |     inner: CDriver,
   |     ^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_target/src/abi/call/x86_64.rs:158:47
stack backtrace:
   0: rust_begin_unwind
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:517:5
   1: core::panicking::panic_fmt
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:100:14
   2: core::panicking::panic
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:50:5
   3: rustc_target::abi::call::x86_64::cast_target
   4: rustc_target::abi::call::x86_64::compute_abi_info::{{closure}}
   5: rustc_target::abi::call::x86_64::compute_abi_info
   6: rustc_target::abi::call::FnAbi<Ty>::adjust_for_foreign_abi
   7: rustc_middle::ty::layout::LayoutCx<rustc_middle::ty::context::TyCtxt>::fn_abi_new_uncached
   8: rustc_middle::ty::layout::fn_abi_of_fn_ptr
   9: rustc_query_system::query::plumbing::get_query
  10: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::fn_abi_of_fn_ptr
  11: <rustc_target::abi::TyAndLayout<&rustc_middle::ty::TyS> as rustc_codegen_llvm::type_of::LayoutLlvmExt>::llvm_type
  12: rustc_codegen_llvm::type_of::struct_llfields
  13: <rustc_target::abi::TyAndLayout<&rustc_middle::ty::TyS> as rustc_codegen_llvm::type_of::LayoutLlvmExt>::llvm_type
  14: rustc_codegen_llvm::type_of::struct_llfields
  15: <rustc_target::abi::TyAndLayout<&rustc_middle::ty::TyS> as rustc_codegen_llvm::type_of::LayoutLlvmExt>::llvm_type
  16: <rustc_target::abi::TyAndLayout<&rustc_middle::ty::TyS> as rustc_codegen_llvm::type_of::LayoutLlvmExt>::llvm_type
  17: <rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS> as rustc_codegen_llvm::abi::FnAbiLlvmExt>::llvm_type
  18: rustc_codegen_llvm::mono_item::<impl rustc_codegen_ssa::traits::declare::PreDefineMethods for rustc_codegen_llvm::context::CodegenCx>::predefine_fn
  19: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
  20: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  21: rustc_codegen_llvm::base::compile_codegen_unit
  22: rustc_codegen_ssa::base::codegen_crate
  23: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  24: rustc_interface::queries::Queries::ongoing_codegen
  25: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  26: rustc_span::with_source_map
  27: scoped_tls::ScopedKey<T>::set
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.57.0 (f1edd0429 2021-11-29) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib

query stack during panic:
#0 [fn_abi_of_fn_ptr] computing call ABI of `unsafe extern "C" fn(std::ffi::c_str::CStr, *mut core::ffi::c_void, *mut core::ffi::c_void) -> i32` function pointers
end of query stack
warning: 1 warning emitted

@horazont horazont added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 21, 2022
@nipzu
Copy link
Contributor

nipzu commented Feb 21, 2022

Minimized to:

pub fn bad(f: extern "C" fn([u8])) {}

Changing the argument of f from [u8] to another FFI-unsafe type like dyn std::any::Any causes the same ICE. Maybe f being a function argument skips some FFI-related checks.

@workingjubilee workingjubilee added A-FFI Area: Foreign function interface (FFI) A-ABI Area: Concerning the application binary interface (ABI) labels Feb 26, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 27, 2023
…-fn-ptr-in-internal-abi-fn, r=jackh726

lint/ctypes: ext. abi fn-ptr in internal abi fn

Fixes rust-lang#94223.

- In the improper ctypes lint, instead of skipping functions with internal ABIs, check that the signature doesn't contain any fn-ptr types with external ABIs that aren't FFI-safe.
- When computing the ABI for fn-ptr types, remove an `unwrap` that assumed FFI-safe types in foreign fn-ptr types.
  - I'm not certain that this is the correct approach.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Apr 28, 2023
…-fn-ptr-in-internal-abi-fn, r=jackh726

lint/ctypes: ext. abi fn-ptr in internal abi fn

Fixes rust-lang#94223.

- In the improper ctypes lint, instead of skipping functions with internal ABIs, check that the signature doesn't contain any fn-ptr types with external ABIs that aren't FFI-safe.
- When computing the ABI for fn-ptr types, remove an `unwrap` that assumed FFI-safe types in foreign fn-ptr types.
  - I'm not certain that this is the correct approach.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 1, 2023
…-fn-ptr-in-internal-abi-fn, r=jackh726

lint/ctypes: ext. abi fn-ptr in internal abi fn

Fixes rust-lang#94223.

- In the improper ctypes lint, instead of skipping functions with internal ABIs, check that the signature doesn't contain any fn-ptr types with external ABIs that aren't FFI-safe.
- When computing the ABI for fn-ptr types, remove an `unwrap` that assumed FFI-safe types in foreign fn-ptr types.
  - I'm not certain that this is the correct approach.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue May 2, 2023
…-fn-ptr-in-internal-abi-fn, r=jackh726

lint/ctypes: ext. abi fn-ptr in internal abi fn

Fixes rust-lang#94223.

- In the improper ctypes lint, instead of skipping functions with internal ABIs, check that the signature doesn't contain any fn-ptr types with external ABIs that aren't FFI-safe.
- When computing the ABI for fn-ptr types, remove an `unwrap` that assumed FFI-safe types in foreign fn-ptr types.
  - I'm not certain that this is the correct approach.
@bors bors closed this as completed in 0ab38e9 Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ABI Area: Concerning the application binary interface (ABI) A-FFI Area: Foreign function interface (FFI) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants