Skip to content

ICE when using offset intrinsic with an i32 #111699

New issue

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

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

Already on GitHub? # to your account

Closed
zhassan-aws opened this issue May 17, 2023 · 8 comments · Fixed by #128173
Closed

ICE when using offset intrinsic with an i32 #111699

zhassan-aws opened this issue May 17, 2023 · 8 comments · Fixed by #128173
Labels
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

@zhassan-aws
Copy link

Code

#![feature(core_intrinsics)]
use std::intrinsics::offset;

fn main() {
    let a = [1u8, 2, 3];
    let ptr: *const u8 = a.as_ptr();

    unsafe {
        assert_eq!(*offset(ptr, 0), 1);
    }
}

Meta

rustc --version --verbose:

$ rustc --version --verbose
rustc 1.71.0-nightly (e77366b57 2023-05-16)
binary: rustc
commit-hash: e77366b57b799dfa3ce1fcb850c068723a3213ee
commit-date: 2023-05-16
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2

Error output

error: internal compiler error: no errors encountered even though `delay_span_bug` issued

error: internal compiler error: broken MIR in Item(DefId(0:4 ~ test[90e8]::main)) (after phase change to runtime-optimized) at bb1[1]:
                                Cannot offset by non-isize type i32
 --> test.rs:9:21
  |
9 |         assert_eq!(*offset(ptr, 0), 1);
  |                     ^^^^^^^^^^^^^^
  |
  = note: delayed at    0: <rustc_errors::HandlerInner>::emit_diagnostic
Backtrace

             1: <rustc_errors::Handler>::delay_span_bug::<rustc_span::span_encoding::Span, alloc::string::String>
             2: <rustc_const_eval::transform::validate::TypeChecker>::fail::<alloc::string::String>
             3: <rustc_const_eval::transform::validate::Validator as rustc_middle::mir::MirPass>::run_pass
             4: rustc_mir_transform::optimized_mir
             5: <rustc_query_impl::dynamic_query::optimized_mir::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::DefId)>>::call_once
             6: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
             7: rustc_query_impl::get_query_non_incr::optimized_mir
             8: rustc_monomorphize::collector::collect_neighbours
             9: rustc_monomorphize::collector::collect_items_rec
            10: rustc_data_structures::sync::par_for_each_in::<alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>
            11: <rustc_session::session::Session>::time::<(), rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}>
            12: rustc_monomorphize::collector::collect_crate_mono_items
            13: rustc_monomorphize::partitioning::collect_and_partition_mono_items
            14: <rustc_query_impl::dynamic_query::collect_and_partition_mono_items::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, ())>>::call_once
            15: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 24]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
            16: rustc_query_impl::get_query_non_incr::collect_and_partition_mono_items
            17: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::LlvmCodegenBackend>
            18: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
            19: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
            20: rustc_interface::passes::start_codegen
            21: <rustc_middle::ty::context::GlobalCtxt>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_span::ErrorGuaranteed>>
            22: <rustc_interface::queries::Queries>::ongoing_codegen
            23: <rustc_interface::interface::Compiler>::enter::<rustc_driver_impl::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_span::ErrorGuaranteed>>
            24: rustc_span::set_source_map::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
            25: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>
            26: std::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>
            27: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
            28: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
                       at /rustc/e77366b57b799dfa3ce1fcb850c068723a3213ee/library/alloc/src/boxed.rs:1985:9
            29: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
                       at /rustc/e77366b57b799dfa3ce1fcb850c068723a3213ee/library/alloc/src/boxed.rs:1985:9
            30: std::sys::unix::thread::Thread::new::thread_start
                       at /rustc/e77366b57b799dfa3ce1fcb850c068723a3213ee/library/std/src/sys/unix/thread.rs:108:17
            31: start_thread
                       at /build/glibc-SzIz7B/glibc-2.31/nptl/pthread_create.c:477:8
            32: clone
                       at /build/glibc-SzIz7B/glibc-2.31/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:95
          

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.71.0-nightly (e77366b57 2023-05-16) running on x86_64-unknown-linux-gnu

query stack during panic:
end of query stack

Adding an isize suffix fixes the issue:

       assert_eq!(*offset(ptr, 0isize), 1);

The error first appears with the nightly-2023-04-29 toolchain. It works fine with nightly-2023-04-28.

@zhassan-aws zhassan-aws 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 May 17, 2023
@Noratrieb
Copy link
Member

The offset intrinsic does not support doing this, it requires isize or usize. ICEs using intrinsics wrongly are expected and not a bug. Thanks for the report anyways.

@Noratrieb Noratrieb closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2023
@zhassan-aws
Copy link
Author

Shouldn't a type coercion occur for the 0 argument?

@Noratrieb
Copy link
Member

There is nothing to be coerced to, since the intrinsics accept any type on the type level.

@zhassan-aws
Copy link
Author

the intrinsics accept any type on the type level.

Is this specific to the offset intrinsic? I tried a couple of other intrinsics, and the compiler complained about mismatched types:

arith_offset:

#![feature(core_intrinsics)]
use std::intrinsics::arith_offset;

fn main() {
    let a = [1u8, 2, 3];
    let ptr: *const u8 = a.as_ptr();

    unsafe {
        assert_eq!(*arith_offset(ptr, true), 1);
    }
}
error[[E0308]](https://doc.rust-lang.org/nightly/error_codes/E0308.html): mismatched types
 --> src/main.rs:9:39
  |
9 |         assert_eq!(*arith_offset(ptr, true), 1);
  |                     ------------      ^^^^ expected `isize`, found `bool`
  |                     |
  |                     arguments to this function are incorrect
  |
note: function defined here
 --> /rustc/e9e1bbc7a820c472b39d3de54b3049bf14050655/library/core/src/intrinsics.rs:1457:12

floorf32:

#![feature(core_intrinsics)]
use std::intrinsics::floorf32;

fn main() {
    unsafe { floorf32(true); }
}
error[[E0308]](https://doc.rust-lang.org/nightly/error_codes/E0308.html): mismatched types
 --> src/main.rs:5:23
  |
5 |     unsafe { floorf32(true); }
  |              -------- ^^^^ expected `f32`, found `bool`
  |              |
  |              arguments to this function are incorrect
  |
note: function defined here
 --> /rustc/e9e1bbc7a820c472b39d3de54b3049bf14050655/library/core/src/intrinsics.rs:1749:12

Doing the same for offset results in an ICE, e.g.:

#![feature(core_intrinsics)]
use std::intrinsics::offset;

fn main() {
    let a = [1u8, 2, 3];
    let ptr: *const u8 = a.as_ptr();

    unsafe {
        assert_eq!(*offset(ptr, true), 1);
    }
}
error: internal compiler error: no errors encountered even though `delay_span_bug` issued

error: internal compiler error: broken MIR in Item(DefId(0:4 ~ playground[f128]::main)) (after phase change to runtime-optimized) at bb1[1]:
                                Cannot offset by non-isize type bool
 --> src/main.rs:9:21
  |
9 |         assert_eq!(*offset(ptr, true), 1);
  |                     ^^^^^^^^^^^^^^^^^
  |
  = note: delayed at    0: <rustc_errors::HandlerInner>::emit_diagnostic

@Noratrieb
Copy link
Member

the offset intrinsic is generic, that's why it passes type checking

@zhassan-aws
Copy link
Author

It's only generic in the first parameter. And arith_offset has the exact same signature, but the issue doesn't occur for it.

@celinval
Copy link
Contributor

Hi @Nilstrieb, what is the motivation to keep this check as an ICE instead of a user friendly error since the error arises from misusage of the API?

@Noratrieb
Copy link
Member

Giving better errors for intrinsics and other internal-only features increases maintenance burden, so MCP 620 decided to not support it. Invalid usage of intrinsics may lead to ICEs (since intrinsics should not be used - I see for your case here it makes sense to use them in the tests, but I think you can live with ICEs). I guess it's not ideal that the ICE message says to report it - but fixing that is hard.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 25, 2024
…, r=oli-obk

Remove crashes for misuses of intrinsics

All of these do not crash if the feature gate is removed. An ICE due *opting into* the intrinsics feature gate is not a bug that needs to be fixed, but instead a misuse of an internal-only API.

See rust-lang/compiler-team#620

The last two issues are already closed anyways, but:
Fixes rust-lang#97501
Fixes rust-lang#111699
Fixes rust-lang#101962
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
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