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

str as extern "C" arg causes compiler panic #80125

Closed
IoTPanic opened this issue Dec 17, 2020 · 7 comments · Fixed by #122895
Closed

str as extern "C" arg causes compiler panic #80125

IoTPanic opened this issue Dec 17, 2020 · 7 comments · Fixed by #122895
Labels
A-FFI Area: Foreign function interface (FFI) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. glacier ICE tracked in rust-lang/glacier. 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

@IoTPanic
Copy link

Code

I am still getting used to Rust, and making plenty of mistakes, and I did the silly move of putting a str type as an argument for an external function. I understand this isn't correct rust and rewriting for correctness fixes the compiler panic.

// Silly me made this external callback signature, this is incorrect and how not
// to make a callback signature. Removing the str type fixes the issue.
type ExternCallback = extern "C" fn(*const u8, u32, str);

// Above type is used in a struct-
pub struct Client<'a> {
    // ...
    conn_tx_bind: ExternCallback,
    // ...
}

Meta

rustc --version --verbose:

rustc 1.49.0-nightly (ffa2e7ae8 2020-10-24)
binary: rustc
commit-hash: ffa2e7ae8fbf9badc035740db949b9dae271c29f
commit-date: 2020-10-24
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
LLVM version: 11.0

Error output

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.49.0-nightly (ffa2e7ae8 2020-10-24) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type staticlib --crate-type cdylib --crate-type lib

note: some of the compiler flags provided by cargo are hidden
Backtrace

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:     0x7ff122cfea20 - std::backtrace_rs::backtrace::libunwind::trace::h303a626d53553a64
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:100:5
   1:     0x7ff122cfea20 - std::backtrace_rs::backtrace::trace_unsynchronized::h6abb882733c3f18c
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7ff122cfea20 - std::sys_common::backtrace::_print_fmt::h57829321f1d9217a
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7ff122cfea20 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h400b66a054640aac
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x7ff122d6de9c - core::fmt::write::h1857a60b204f1b6a
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/core/src/fmt/mod.rs:1076:17
   5:     0x7ff122cf0642 - std::io::Write::write_fmt::h6453b28c8b27c6ea
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/io/mod.rs:1516:15
   6:     0x7ff122d039d5 - std::sys_common::backtrace::_print::h15cc4b3923cc9bc0
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x7ff122d039d5 - std::sys_common::backtrace::print::h953c7be178c0674d
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x7ff122d039d5 - std::panicking::default_hook::{{closure}}::h635d667f571198f0
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/panicking.rs:208:50
   9:     0x7ff122d03678 - std::panicking::default_hook::h3bc2efb48060abaa
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/panicking.rs:227:9
  10:     0x7ff1235cc618 - rustc_driver::report_ice::hbf1325c7565687ba
  11:     0x7ff122d04216 - std::panicking::rust_panic_with_hook::h887eb0c89f46b8b2
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/panicking.rs:581:17
  12:     0x7ff122d03d99 - std::panicking::begin_panic_handler::{{closure}}::hf0d3f46d37dd3bc1
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/panicking.rs:484:9
  13:     0x7ff122cfeebc - std::sys_common::backtrace::__rust_end_short_backtrace::h4bce8b52717576e3
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/sys_common/backtrace.rs:141:18
  14:     0x7ff122d03d59 - rust_begin_unwind
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/panicking.rs:483:5
  15:     0x7ff122d6a1c1 - core::panicking::panic_fmt::hb15d6f55e8472f62
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/core/src/panicking.rs:85:14
  16:     0x7ff122d6a10d - core::panicking::panic::h5d1c61fed2502a5f
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/core/src/panicking.rs:50:5
  17:     0x7ff12665eaf2 - rustc_target::abi::call::x86_64::cast_target::h8f310b8a881734f3
  18:     0x7ff1239260ff - rustc_target::abi::call::x86_64::compute_abi_info::{{closure}}::hc9f3a1278d83e5c4
  19:     0x7ff1239f7448 - <rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS> as rustc_middle::ty::layout::FnAbiExt<C>>::adjust_for_abi::h60db6c4404fb6b68
  20:     0x7ff1239f8870 - <rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS> as rustc_middle::ty::layout::FnAbiExt<C>>::of_fn_ptr::hde6ff39c43d480d4
  21:     0x7ff1239c23d4 - <rustc_target::abi::TyAndLayout<&rustc_middle::ty::TyS> as rustc_codegen_llvm::type_of::LayoutLlvmExt>::llvm_type::h4992f20070fda05e
  22:     0x7ff1239c10ed - rustc_codegen_llvm::type_of::struct_llfields::h61b2b2bab04bcb79
  23:     0x7ff1239c1cc9 - <rustc_target::abi::TyAndLayout<&rustc_middle::ty::TyS> as rustc_codegen_llvm::type_of::LayoutLlvmExt>::llvm_type::h4992f20070fda05e
  24:     0x7ff123a15e8f - <rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS> as rustc_codegen_llvm::abi::FnAbiLlvmExt>::llvm_type::h2eba71bc4a6fef8d
  25:     0x7ff12394280e - rustc_codegen_llvm::mono_item::<impl rustc_codegen_ssa::traits::declare::PreDefineMethods for rustc_codegen_llvm::context::CodegenCx>::predefine_fn::ha43f5adbb9cfa37b
  26:     0x7ff1238b8efc - rustc_codegen_llvm::base::compile_codegen_unit::module_codegen::h4c8c9c4c2a43bc33
  27:     0x7ff12391ec69 - rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps::h858e942ea11c2f75
  28:     0x7ff12395878b - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task::h84330fcec5992c22
  29:     0x7ff1238b8b04 - rustc_codegen_llvm::base::compile_codegen_unit::h6e0644e09523002f
  30:     0x7ff1239fc354 - rustc_codegen_ssa::base::codegen_crate::h940381ed200dc2d9
  31:     0x7ff123a4dcd5 - <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate::h3b5822f1feaab3d9
  32:     0x7ff123810855 - rustc_interface::passes::QueryContext::enter::h066717d47aa6a888
  33:     0x7ff12383d7a3 - rustc_interface::queries::Queries::ongoing_codegen::hb9f9c60cdf1bfb85
  34:     0x7ff1235e74ea - rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter::h6eb07f11eba80bd6
  35:     0x7ff1235ae2a2 - rustc_span::with_source_map::h3820d41b3a036910
  36:     0x7ff1235e89c2 - rustc_interface::interface::create_compiler_and_run::h08ad396f4e7721e0
  37:     0x7ff1235d0d5a - scoped_tls::ScopedKey<T>::set::hd425a36fbe06985e
  38:     0x7ff1235ed9b5 - std::sys_common::backtrace::__rust_begin_short_backtrace::h23925571219dbc63
  39:     0x7ff12356b3ce - core::ops::function::FnOnce::call_once{{vtable.shim}}::h43b61b9af30b3669
  40:     0x7ff122d1321a - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h88864ee659ddb161
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/alloc/src/boxed.rs:1042:9
  41:     0x7ff122d1321a - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h9f03cbdc7ab33431
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/alloc/src/boxed.rs:1042:9
  42:     0x7ff122d1321a - std::sys::unix::thread::Thread::new::thread_start::h5979dd5560a11418
                               at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/sys/unix/thread.rs:89:17
  43:     0x7ff122c0b3e9 - start_thread
  44:     0x7ff122b28293 - __GI___clone
  45:                0x0 - <unknown>

@IoTPanic IoTPanic 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 Dec 17, 2020
@jonas-schievink jonas-schievink added the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Dec 17, 2020
@IoTPanic
Copy link
Author

Here is a working example-

// lib.rs
type ExternCallback = extern "C" fn(*const u8, u32, str);

pub struct Client {
    // ...
    conn_tx_bind: ExternCallback,
    // ...
}

#[no_mangle]
pub extern fn register_something(bind: ExternCallback) -> Client {
    Client{
        conn_tx_bind: bind    
    }
}

Note I made a new library project, and didnt modify Cargo.toml, just paste and run.

@fanninpm
Copy link

fanninpm added a commit to fanninpm/glacier that referenced this issue Dec 17, 2020
@camelid camelid removed the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Dec 17, 2020
@camelid
Copy link
Member

camelid commented Dec 17, 2020

A bit more minimized:

type ExternCallback = extern "C" fn(*const u8, u32, str);

pub struct Struct(ExternCallback);

#[no_mangle]
pub extern "C" fn register_something(bind: ExternCallback) -> Struct {
    Struct(bind)
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
warning: `extern` fn uses type `str`, which is not FFI-safe
 --> src/lib.rs:6:44
  |
6 | pub extern "C" fn register_something(bind: ExternCallback) -> Struct {
  |                                            ^^^^^^^^^^^^^^ not FFI-safe
  |
  = note: `#[warn(improper_ctypes_definitions)]` on by default
  = help: consider using `*const u8` and a length instead
  = note: string slices have no C equivalent

warning: `extern` fn uses type `Struct`, which is not FFI-safe
 --> src/lib.rs:6:63
  |
6 | pub extern "C" fn register_something(bind: ExternCallback) -> Struct {
  |                                                               ^^^^^^ not FFI-safe
  |
  = help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
  = note: this struct has unspecified layout
note: the type is defined here
 --> src/lib.rs:3:1
  |
3 | pub struct Struct(ExternCallback);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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.48.0 (7eac88abb 2020-11-16) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type lib

note: some of the compiler flags provided by cargo are hidden

warning: 2 warnings emitted

error: could not compile `playground`

To learn more, run the command again with --verbose.

fanninpm added a commit to fanninpm/glacier that referenced this issue Dec 19, 2020
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Dec 20, 2020
@fanninpm
Copy link

This does not cause an ICE on an M1 Mac:

ices/80125.rs: fixed with no errors
=== stdout ===
=== stderr ===
warning: `extern` fn uses type `str`, which is not FFI-safe
 --> ~/Documents/GitHub/glacier/ices/80125.rs:6:44
  |
6 | pub extern "C" fn register_something(bind: ExternCallback) -> Struct {
  |                                            ^^^^^^^^^^^^^^ not FFI-safe
  |
  = note: `#[warn(improper_ctypes_definitions)]` on by default
  = help: consider using `*const u8` and a length instead
  = note: string slices have no C equivalent

warning: `extern` fn uses type `Struct`, which is not FFI-safe
 --> ~/Documents/GitHub/glacier/ices/80125.rs:6:63
  |
6 | pub extern "C" fn register_something(bind: ExternCallback) -> Struct {
  |                                                               ^^^^^^ not FFI-safe
  |
  = help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
  = note: this struct has unspecified layout
note: the type is defined here
 --> ~/Documents/GitHub/glacier/ices/80125.rs:3:1
  |
3 | pub struct Struct(ExternCallback);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: 2 warnings emitted

@camelid
Copy link
Member

camelid commented Jan 28, 2021

The ICE shows this as the ICEing file: thread 'rustc' panicked at 'called Option::unwrap()on aNone value', compiler/rustc_target/src/abi/call/x86_64.rs:158:47

Note that it's in the ABI file for x86-64, so it's not triggered on ARM.

@fanninpm
Copy link

@camelid I think it's very similar to #52334 (in fact, I believe it appears in the same file, on the same line).

@jonas-schievink jonas-schievink added A-FFI Area: Foreign function interface (FFI) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Mar 19, 2021
@JohnTitor
Copy link
Member

Triage: ICE has been fixed, marking as E-needs-test
@rustbot labels: +E-needs-test

@rustbot rustbot added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jul 8, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 23, 2024
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Mar 24, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 24, 2024
@bors bors closed this as completed in 19310ce Mar 24, 2024
RenjiSann pushed a commit to RenjiSann/rust that referenced this issue Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-FFI Area: Foreign function interface (FFI) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. glacier ICE tracked in rust-lang/glacier. 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.

7 participants