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

ICE (stable segfault) in LLVM with creative FFI types #54410

Closed
joshtriplett opened this issue Sep 21, 2018 · 4 comments · Fixed by #55004 or rust-lang/cargo#6176
Closed

ICE (stable segfault) in LLVM with creative FFI types #54410

joshtriplett opened this issue Sep 21, 2018 · 4 comments · Fixed by #55004 or rust-lang/cargo#6176
Labels
A-codegen Area: Code generation A-diagnostics Area: Messages for errors, warnings, and lints A-FFI Area: Foreign function interface (FFI) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

joshtriplett commented Sep 21, 2018

Found this while experimenting with the right FFI type to declare a char[] (not char *) symbol in an extern "C" block. Credit to @acfoltzer for the original code sample that I tweaked to end up with this error. This produces an ICE on nightly, or a segfault on stable.

rustc 1.30.0-nightly (20dc0c507 2018-09-19)

Code:

$ head Cargo.toml build.rs src/*
==> Cargo.toml <==
[package]
name = "testcrate"
version = "0.0.1"

[build-dependencies]
cc = "1.0"

==> build.rs <==
extern crate cc;

fn main() {
    cc::Build::new()
        .file("src/foo.c")
        .compile("foo");
}

==> src/foo.c <==
char symbol[1024] = "hello world";

==> src/main.rs <==
use std::os::raw::c_char;
extern "C" {
    pub static mut symbol: [c_char];
}

fn main() {
    println!("{:p}", unsafe { &symbol });
}

Result:

$ cargo +nightly build --verbose
       Fresh cc v1.0.25
   Compiling testcrate v0.0.1 (/tmp/testcrate)                                                                                                                                                                                                                 
     Running `rustc --crate-name testcrate src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=eb3a438da9110935 -C extra-filename=-eb3a438da9110935 --out-dir /tmp/testcrate/target/debug/deps -C incremental=/tmp/testcrate/target/debug/incremental -L dependency=/tmp/testcrate/target/debug/deps -L native=/tmp/testcrate/target/debug/build/testcrate-b3e5d3e42b558a4c/out -l static=foo`
warning: `extern` block uses type `[i8]` which is not FFI-safe: slices have no C equivalent                                                                                                                                                                    
 --> src/main.rs:3:28                                                                                                                                                                                                                                          
  |                                                                                                                                                                                                                                                            
3 |     pub static mut symbol: [c_char];                                                                                                                                                                                                                       
  |                            ^^^^^^^^                                                                                                                                                                                                                        
  |                                                                                                                                                                                                                                                            
  = note: #[warn(improper_ctypes)] on by default                                                                                                                                                                                                               
  = help: consider using a raw pointer instead                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                               
thread 'main' panicked at 'assertion failed: !layout.is_unsized()', librustc_codegen_llvm/mir/place.rs:50:9                                                                                                                                                    
note: Run with `RUST_BACKTRACE=1` for 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/blob/master/CONTRIBUTING.md#bug-reports                                                                                                                                              
                                                                                                                                                                                                                                                               
note: rustc 1.30.0-nightly (20dc0c507 2018-09-19) running on x86_64-unknown-linux-gnu                                                                                                                                                                          
                                                                                                                                                                                                                                                               
note: compiler flags: -C debuginfo=2 -C incremental --crate-type bin                                                                                                                                                                                           
                                                                                                                                                                                                                                                               
note: some of the compiler flags provided by cargo are hidden                                                                                                                                                                                                  
                                                                                                                                                                                                                                                               
error: Could not compile `testcrate`.                                                                                                                                                                                                                          

Caused by:
  process didn't exit successfully: `rustc --crate-name testcrate src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=eb3a438da9110935 -C extra-filename=-eb3a438da9110935 --out-dir /tmp/testcrate/target/debug/deps -C incremental=/tmp/testcrate/target/debug/incremental -L dependency=/tmp/testcrate/target/debug/deps -L native=/tmp/testcrate/target/debug/build/testcrate-b3e5d3e42b558a4c/out -l static=foo` (exit code: 101)
@joshtriplett joshtriplett added 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 Sep 21, 2018
@estebank estebank added regression-from-stable-to-beta Performance or correctness regression from stable to beta. A-codegen Area: Code generation A-FFI Area: Foreign function interface (FFI) labels Oct 5, 2018
@estebank
Copy link
Contributor

estebank commented Oct 5, 2018

https://play.rust-lang.org/?gist=cc96867783c61f65db9011b2edfc02f7&version=beta&mode=debug&edition=2015

warning: `extern` block uses type `[i8]` which is not FFI-safe: slices have no C equivalent
 --> src/main.rs:3:28
  |
3 |     pub static mut symbol: [c_char];
  |                            ^^^^^^^^
  |
  = note: #[warn(improper_ctypes)] on by default
  = help: consider using a raw pointer instead

thread 'main' panicked at 'assertion failed: !layout.is_unsized()', librustc_codegen_llvm/mir/place.rs:50:9
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::print
             at libstd/sys_common/backtrace.rs:71
             at libstd/sys_common/backtrace.rs:59
   2: std::panicking::default_hook::{{closure}}
             at libstd/panicking.rs:211
   3: std::panicking::default_hook
             at libstd/panicking.rs:227
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:481
   6: std::panicking::begin_panic
   7: rustc_codegen_llvm::mir::place::<impl rustc_codegen_llvm::mir::FunctionCx<'a, 'll, 'tcx>>::codegen_place
   8: rustc_codegen_llvm::mir::rvalue::<impl rustc_codegen_llvm::mir::FunctionCx<'a, 'll, 'tcx>>::codegen_rvalue_operand
   9: rustc_codegen_llvm::mir::rvalue::<impl rustc_codegen_llvm::mir::FunctionCx<'a, 'll, 'tcx>>::codegen_rvalue
  10: rustc_codegen_llvm::mir::codegen_mir
  11: rustc_codegen_llvm::base::codegen_instance
  12: rustc_codegen_llvm::mono_item::MonoItemExt::define
  13: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
  14: rustc::dep_graph::graph::DepGraph::with_task
  15: rustc_codegen_llvm::base::codegen_crate
  16: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate
  17: rustc::util::common::time
  18: rustc_driver::driver::phase_4_codegen
  19: rustc_driver::driver::compile_input::{{closure}}
  20: rustc::ty::context::tls::enter_context
  21: <std::thread::local::LocalKey<T>>::with
  22: rustc::ty::context::TyCtxt::create_and_enter
  23: rustc_driver::driver::compile_input
  24: rustc_driver::run_compiler_with_pool
  25: rustc_driver::driver::spawn_thread_pool
  26: rustc_driver::run_compiler
  27: <scoped_tls::ScopedKey<T>>::set
  28: syntax::with_globals
  29: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
  30: rustc_driver::run
  31: rustc_driver::main
  32: std::rt::lang_start::{{closure}}
  33: std::panicking::try::do_call
             at libstd/rt.rs:59
             at libstd/panicking.rs:310
  34: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
  35: std::rt::lang_start_internal
             at libstd/panicking.rs:289
             at libstd/panic.rs:392
             at libstd/rt.rs:58
  36: main
  37: __libc_start_main
  38: <unknown>
query stack during panic:
end of query stack

@pietroalbini pietroalbini added this to the 1.30 milestone Oct 8, 2018
@oli-obk oli-obk added A-diagnostics Area: Messages for errors, warnings, and lints and removed I-nominated regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Oct 11, 2018
@pnkfelix
Copy link
Member

visited for triage. General consensus was that going from a segfault to an ICE (an the ICE is preceded by a warning pointing you to what is wrong in your code), does not exactly seem like a regression.

@pnkfelix
Copy link
Member

@oli-obk is going to mentor how to make this even nicer.

@pnkfelix pnkfelix added the P-low Low priority label Oct 11, 2018
@oli-obk oli-obk removed this from the 1.30 milestone Oct 11, 2018
@joshtriplett
Copy link
Member Author

joshtriplett commented Oct 11, 2018 via email

oli-obk added a commit to oli-obk/cargo that referenced this issue Oct 15, 2018
statics (even extern ones) will get their type checked for a `Sized` bound in order to fix rust-lang/rust#54410
bors added a commit to rust-lang/cargo that referenced this issue Oct 16, 2018
Future proof an unstable test

statics (even extern ones) will get their type checked for a `Sized` bound in order to fix rust-lang/rust#54410
@oli-obk oli-obk reopened this Oct 16, 2018
kennytm added a commit to kennytm/rust that referenced this issue Oct 18, 2018
Check the type of statics and constants for `Sized`ness

fixes rust-lang#54410
bors added a commit that referenced this issue Oct 19, 2018
Check the type of statics and constants for `Sized`ness

fixes #54410
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-diagnostics Area: Messages for errors, warnings, and lints A-FFI Area: Foreign function interface (FFI) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants