Skip to content

Panic in item.kind() when the underlying item kind is GlobalAsm #95

Open
rust-lang/rust
#138025
@remi-delmas-3000

Description

@remi-delmas-3000

Minimum reproducer:

//@ run-pass
//! Test that item kind works as expected.

//@ ignore-stage1
//@ ignore-cross-compile
//@ ignore-remote
//@ edition: 2021

#![feature(rustc_private)]
#![feature(assert_matches)]

extern crate rustc_middle;
#[macro_use]
extern crate rustc_smir;
extern crate rustc_driver;
extern crate rustc_interface;
extern crate stable_mir;

use rustc_smir::rustc_internal;
use std::io::Write;
use std::ops::ControlFlow;

const CRATE_NAME: &str = "input";

/// This function uses the Stable MIR APIs to get information about the test crate.
fn test_item_kind() -> ControlFlow<()> {
    let items = stable_mir::all_local_items();
    // Constructor item.
    for item in items {
        let _ =  item.kind();
    }
    ControlFlow::Continue(())
}

/// This test will generate and analyze a dummy crate using the stable mir.
/// For that, it will first write the dummy crate into a file.
/// Then it will create a `StableMir` using custom arguments and then
/// it will run the compiler.
fn main() {
    let path = "item_kind_input.rs";
    generate_input(&path).unwrap();
    let args = vec![
        "rustc".to_string(),
        "-Cpanic=abort".to_string(),
        "--crate-type=lib".to_string(),
        "--crate-name".to_string(),
        CRATE_NAME.to_string(),
        path.to_string(),
    ];
    run!(args, test_item_kind).unwrap();
}

fn generate_input(path: &str) -> std::io::Result<()> {
    let mut file = std::fs::File::create(path)?;

    write!(
        file,
        r##"
        std::arch::global_asm!(r#"
            .global my_noop
            .text
        my_noop:
            ret
        "#);
        "##
        )?;
    Ok(())
}
[toolchain]
channel = "nightly-2025-03-02"
components = ["llvm-tools", "rustc-dev", "rust-src", "rustfmt"]
❯ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/stable-mir-bug`

thread 'rustc' panicked at compiler/rustc_smir/src/rustc_smir/mod.rs:145:13:
internal error: entered unreachable code: Not a valid item kind: GlobalAsm
stack backtrace:
   0:        0x11c029b50 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h01f5e23dcfa2a90e
   1:        0x118fcec23 - core::fmt::write::he4853cc08b80db6c
   2:        0x11c01ddb2 - std::io::Write::write_fmt::h2d4ff1e93b93926d
   3:        0x11c0299a2 - std::sys::backtrace::BacktraceLock::print::hc1c953a08614aca4
   4:        0x11c02c520 - std::panicking::default_hook::{{closure}}::h29c83fe2f5c6f731
   5:        0x11c02c124 - std::panicking::default_hook::h0666ac6f30006983
   6:        0x11c02ce3e - std::panicking::rust_panic_with_hook::hc99d3b08a18f95f9
   7:        0x11c02cb28 - std::panicking::begin_panic_handler::{{closure}}::h5b3ef507ee63af0e
   8:        0x11c029ff9 - std::sys::backtrace::__rust_end_short_backtrace::h7b36728f4c4db291
   9:        0x11c02c764 - _rust_begin_unwind
  10:        0x11f353eaf - core::panicking::panic_fmt::he11055326097ecd8
  11:        0x11b935943 - <rustc_smir[a1f7f53badb6847c]::rustc_smir::context::TablesWrapper as stable_mir[3c1241d0939fa9]::compiler_interface::Context>::item_kind
  12:        0x10d6c9022 - stable_mir_bug::test_item_kind::hdc645e6427a486e8
                               at /Users/delmasrd/projects/stable-mir-bug/src/main.rs:30:18
  13:        0x10d6c9afd - stable_mir_bug::main::{{closure}}::he4a8877abdbd151b
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_smir/src/rustc_internal/mod.rs:279:31
  14:        0x10d6c9ab1 - <stable_mir_bug::main::StableMir<B,C,F> as rustc_driver_impl::Callbacks>::after_analysis::{{closure}}::h866eb4518471c3f9
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_smir/src/rustc_internal/mod.rs:386:44
  15:        0x10d6c8351 - rustc_smir::rustc_internal::init::{{closure}}::h4473f79754c35572
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_smir/src/rustc_internal/mod.rs:196:33
  16:        0x10d6c6dcc - scoped_tls::ScopedKey<T>::set::h8a831d5ecd414d27
                               at /rust/deps/scoped-tls-1.0.1/src/lib.rs:137:9
  17:        0x10d6c8303 - rustc_smir::rustc_internal::init::h14cea51accd9d4d8
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_smir/src/rustc_internal/mod.rs:196:5
  18:        0x10d6c8285 - rustc_smir::rustc_internal::run::{{closure}}::h50942426ffddc9c8
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_smir/src/rustc_internal/mod.rs:227:53
  19:        0x10d6c9d73 - stable_mir::compiler_interface::run::{{closure}}::h58b5e7ef6698d7d2
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/stable_mir/src/compiler_interface.rs:268:40
  20:        0x10d6c6e97 - scoped_tls::ScopedKey<T>::set::hc6f7b556f6350e22
                               at /rust/deps/scoped-tls-1.0.1/src/lib.rs:137:9
  21:        0x10d6c9ceb - stable_mir::compiler_interface::run::h7c47e5f0420eaf0d
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/stable_mir/src/compiler_interface.rs:268:9
  22:        0x10d6c8212 - rustc_smir::rustc_internal::run::h3267ecc81af27e0b
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_smir/src/rustc_internal/mod.rs:227:5
  23:        0x10d6c99e4 - <stable_mir_bug::main::StableMir<B,C,F> as rustc_driver_impl::Callbacks>::after_analysis::h715cb9875dad65ee
                               at /Users/delmasrd/.rustup/toolchains/nightly-2025-03-02-x86_64-apple-darwin/lib/rustlib/src/rust/compiler/rustc_smir/src/rustc_internal/mod.rs:385:21
  24:        0x119c3d3a7 - rustc_interface[12e2c5bb59b8f261]::passes::create_and_enter_global_ctxt::<core[e62a462baba2588e]::option::Option<rustc_interface[12e2c5bb59b8f261]::queries::Linker>, rustc_driver_impl[fa162f55ecd1d14e]::run_compiler::{closure#0}::{closure#2}>
  25:        0x119c8ee38 - rustc_interface[12e2c5bb59b8f261]::interface::run_compiler::<(), rustc_driver_impl[fa162f55ecd1d14e]::run_compiler::{closure#0}>::{closure#1}
  26:        0x119c7e80d - std[f99b8324fcf2a019]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[12e2c5bb59b8f261]::util::run_in_thread_with_globals<rustc_interface[12e2c5bb59b8f261]::util::run_in_thread_pool_with_globals<rustc_interface[12e2c5bb59b8f261]::interface::run_compiler<(), rustc_driver_impl[fa162f55ecd1d14e]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  27:        0x119c9826d - <<std[f99b8324fcf2a019]::thread::Builder>::spawn_unchecked_<rustc_interface[12e2c5bb59b8f261]::util::run_in_thread_with_globals<rustc_interface[12e2c5bb59b8f261]::util::run_in_thread_pool_with_globals<rustc_interface[12e2c5bb59b8f261]::interface::run_compiler<(), rustc_driver_impl[fa162f55ecd1d14e]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[e62a462baba2588e]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  28:        0x11c0387fb - std::sys::pal::unix::thread::Thread::new::thread_start::h703431e570bc9684
  29:     0x7ff802bb3253 - __pthread_start

This is triggered from Kani, which uses stable MIR to load up a crate and find all MonoItems it contains to use them as entry points for analysis/GOTO codegen.

/// Collect all (top-level) items in the crate that matches the given predicate.
/// An item can only be a root if they are a non-generic function.
pub fn filter_crate_items<F>(tcx: TyCtxt, predicate: F) -> Vec<Instance>
where
    F: Fn(TyCtxt, Instance) -> bool,
{
    let crate_items = stable_mir::all_local_items();
    // Filter regular items.
    crate_items
        .iter()
        .filter_map(|item| {
            // Only collect monomorphic items.
            matches!(item.kind(), ItemKind::Fn) <<<<<<< Problem happens here
                .then(|| {
                    Instance::try_from(*item)
                        .ok()
                        .and_then(|instance| predicate(tcx, instance).then_some(instance))
                })
                .flatten()
        })
        .collect::<Vec<_>>()
}

At the Kani level we don't need to surface a representation of the ASM code. Adding an ItemKind::GlobalAsm would be sufficient for Kani to introspect/filter the Items found in a crate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions