Skip to content

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

Closed
rust-lang/cargo
#6176
@joshtriplett

Description

@joshtriplett

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-codegenArea: Code generationA-diagnosticsArea: Messages for errors, warnings, and lintsI-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions