Skip to content

expr.rs index out of bounds: the len is 0 but the index is 0 #123442

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

Closed
Naserume opened this issue Apr 4, 2024 · 10 comments · Fixed by #123526
Closed

expr.rs index out of bounds: the len is 0 but the index is 0 #123442

Naserume opened this issue Apr 4, 2024 · 10 comments · Fixed by #123526
Assignees
Labels
A-intrinsics Area: Intrinsics C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Naserume
Copy link

Naserume commented Apr 4, 2024

Code

(reduced)

extern crate issue_15562;

fn main() {
    {
        transmute();
    }
}
extern "rust-intrinsic" fn transmute() {}

(original)

//@ run-pass
//@ aux-build:issue-15562.rs

//@ pretty-expanded FIXME #23616

extern crate issue_15562 as i;

pub fn main() {
    unsafe {
        transmute();
        i::transmute();
    }
}

// We declare this so we don't run into unresolved symbol errors
// The above extern is NOT `extern "rust-intrinsic"` and thus
// means it'll try to find a corresponding symbol to link to.
#[no_mangle]
pub unsafe extern "rust-intrinsic" fn transmute() {}

Meta

rustc --version --verbose:

rustc 1.79.0-nightly (4fd4797c2 2024-04-03)
binary: rustc
commit-hash: 4fd4797c2654977f545c9a91e2aa4e6cdbb38919
commit-date: 2024-04-03
host: x86_64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.2

Error output

error[E0463]: can't find crate for `issue_15562`
 --> ./CF20DE9D60924DCBAD5950D10D572176B7F7ED570B099E173A86F64C0AAB2C6A.rs:1:1
  |
1 | extern crate issue_15562;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error[E0658]: intrinsics are subject to change
 --> ./CF20DE9D60924DCBAD5950D10D572176B7F7ED570B099E173A86F64C0AAB2C6A.rs:8:8
  |
8 | extern "rust-intrinsic" fn transmute() {}
  |        ^^^^^^^^^^^^^^^^
  |
  = help: add `#![feature(intrinsics)]` to the crate attributes to enable
  = note: this compiler was built on 2024-04-03; consider upgrading it if it is out of date

error[E0094]: intrinsic has wrong number of type parameters: found 0, expected 2
 --> ./CF20DE9D60924DCBAD5950D10D572176B7F7ED570B099E173A86F64C0AAB2C6A.rs:8:37
  |
8 | extern "rust-intrinsic" fn transmute() {}
  |                                     ^ expected 2 type parameters
Backtrace

thread 'rustc' panicked at compiler/rustc_hir_typeck/src/expr.rs:550:28:
index out of bounds: the len is 0 but the index is 0
stack backtrace:
   0:        0x10e092797 - std::backtrace::Backtrace::create::hde801799bfcf1a18
   1:        0x10e0926e5 - std::backtrace::Backtrace::force_capture::h19ae693dac8964df
   2:        0x116c3dcfb - std[38e870d6de0dab70]::panicking::update_hook::<alloc[43ccfcebb6346d8b]::boxed::Box<rustc_driver_impl[b21b79e6d003739c]::install_ice_hook::{closure#0}>>::{closure#0}
   3:        0x10e0ac564 - std::panicking::rust_panic_with_hook::h980ef0202c45a742
   4:        0x10e0abea4 - std::panicking::begin_panic_handler::{{closure}}::h6974c7b825f3e848
   5:        0x10e0a9379 - std::sys_common::backtrace::__rust_end_short_backtrace::h8e1c545181895940
   6:        0x10e0abbd6 - _rust_begin_unwind
   7:        0x10e111355 - core::panicking::panic_fmt::h47e8c306b2b8d99c
   8:        0x10e111556 - core::panicking::panic_bounds_check::h7de25c62010f13eb
   9:        0x1170bb209 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_path
  10:        0x1170b951f - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  11:        0x117098eb6 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_call
  12:        0x117196e29 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_kind
  13:        0x1170b95db - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  14:        0x1170f05c6 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_stmt
  15:        0x1170f0dac - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_block_with_expected
  16:        0x1170b95db - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  17:        0x1170f0dea - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_block_with_expected
  18:        0x1170b95db - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  19:        0x1170bb595 - <rustc_hir_typeck[b79567a4313f5def]::fn_ctxt::FnCtxt>::check_return_expr
  20:        0x11718103e - rustc_hir_typeck[b79567a4313f5def]::check::check_fn
  21:        0x1171777e9 - rustc_hir_typeck[b79567a4313f5def]::typeck
  22:        0x11806c11a - rustc_query_impl[3605ca4bc1475aca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3605ca4bc1475aca]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[292fdd7a6f274851]::query::erase::Erased<[u8; 8usize]>>
  23:        0x117eef00c - rustc_query_system[854611ef8e5fc0a7]::query::plumbing::try_execute_query::<rustc_query_impl[3605ca4bc1475aca]::DynamicConfig<rustc_query_system[854611ef8e5fc0a7]::query::caches::VecCache<rustc_hir[9efa7d06bb4f22]::hir_id::OwnerId, rustc_middle[292fdd7a6f274851]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[3605ca4bc1475aca]::plumbing::QueryCtxt, false>
  24:        0x11808ef1b - rustc_query_impl[3605ca4bc1475aca]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  25:        0x116e7a3e7 - <rustc_middle[292fdd7a6f274851]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[76877c2d5917afe]::check_crate::{closure#4}>::{closure#0}
  26:        0x116f8200c - rustc_hir_analysis[76877c2d5917afe]::check_crate
  27:        0x1173e8470 - rustc_interface[9070b66c4f6dc5ef]::passes::analysis
  28:        0x11806c16a - rustc_query_impl[3605ca4bc1475aca]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3605ca4bc1475aca]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[292fdd7a6f274851]::query::erase::Erased<[u8; 1usize]>>
  29:        0x117e56b6e - rustc_query_system[854611ef8e5fc0a7]::query::plumbing::try_execute_query::<rustc_query_impl[3605ca4bc1475aca]::DynamicConfig<rustc_query_system[854611ef8e5fc0a7]::query::caches::SingleCache<rustc_middle[292fdd7a6f274851]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[3605ca4bc1475aca]::plumbing::QueryCtxt, false>
  30:        0x118076537 - rustc_query_impl[3605ca4bc1475aca]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  31:        0x116bee547 - <rustc_interface[9070b66c4f6dc5ef]::queries::QueryResult<&rustc_middle[292fdd7a6f274851]::ty::context::GlobalCtxt>>::enter::<core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>, rustc_driver_impl[b21b79e6d003739c]::run_compiler::{closure#0}::{closure#1}::{closure#3}>
  32:        0x116c44d1f - rustc_interface[9070b66c4f6dc5ef]::interface::run_compiler::<core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>, rustc_driver_impl[b21b79e6d003739c]::run_compiler::{closure#0}>::{closure#0}
  33:        0x116c37cad - std[38e870d6de0dab70]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[9070b66c4f6dc5ef]::util::run_in_thread_with_globals<rustc_interface[9070b66c4f6dc5ef]::util::run_in_thread_pool_with_globals<rustc_interface[9070b66c4f6dc5ef]::interface::run_compiler<core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>, rustc_driver_impl[b21b79e6d003739c]::run_compiler::{closure#0}>::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>
  34:        0x116c4ab7f - <<std[38e870d6de0dab70]::thread::Builder>::spawn_unchecked_<rustc_interface[9070b66c4f6dc5ef]::util::run_in_thread_with_globals<rustc_interface[9070b66c4f6dc5ef]::util::run_in_thread_pool_with_globals<rustc_interface[9070b66c4f6dc5ef]::interface::run_compiler<core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>, rustc_driver_impl[b21b79e6d003739c]::run_compiler::{closure#0}>::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[14fbfc68cf988ae3]::result::Result<(), rustc_span[c77fbea70134c538]::ErrorGuaranteed>>::{closure#1} as core[14fbfc68cf988ae3]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  35:        0x10e0b574b - std::sys::pal::unix::thread::Thread::new::thread_start::hb7c71ee179e441c0
  36:     0x7ff818270202 - __pthread_start


rustc version: 1.79.0-nightly (4fd4797c2 2024-04-03)
platform: x86_64-apple-darwin

query stack during panic:
#0 [typeck] type-checking `main`
#1 [analysis] running analysis passes on this crate
end of query stack

@Naserume Naserume 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 Apr 4, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 4, 2024
@compiler-errors
Copy link
Member

compiler-errors commented Apr 4, 2024

Please try to minimize the example as much as possible:

fn main() {
    transmute();
}

extern "rust-intrinsic" fn transmute() {}

@jieyouxu jieyouxu added A-intrinsics Area: Intrinsics S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 4, 2024
@gurry
Copy link
Contributor

gurry commented Apr 5, 2024

@rustbot claim

@compiler-errors
Copy link
Member

@gurry: heads up that @estebank already put up a fix

@gurry
Copy link
Contributor

gurry commented Apr 6, 2024

Thanks @compiler-errors. Just want to add that some other intrinsics also ICE.

For example:

fn main() {
    read_via_copy();
}

extern "rust-intrinsic" fn read_via_copy() {}

ICEs as well.

@compiler-errors
Copy link
Member

Interesting @gurry -- I wonder if we should perhaps fix this in a less specific way than #123526. I wonder if we should instead detect that the extern "rust-intrinsic" declaration is not valid b/c feature(intrinsics) has not been enabled in the crate...

If not, then I'd be glad to review a PR that fixes whatever other intrinsics you find to make too many assumptions in the compiler.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 6, 2024
…errors

Do not ICE when calling incorrectly defined `transmute` intrinsic

Fix rust-lang#123442
@bors bors closed this as completed in aa53bc0 Apr 6, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 6, 2024
Rollup merge of rust-lang#123526 - estebank:issue-123442, r=compiler-errors

Do not ICE when calling incorrectly defined `transmute` intrinsic

Fix rust-lang#123442
@gurry
Copy link
Contributor

gurry commented Apr 7, 2024

I wonder if we should instead detect that the extern "rust-intrinsic" declaration is not valid b/c feature(intrinsics) has not been enabled in the crate...

Even adding feature(intrinsics) does not prevent the ICE unfortunately.

It happens in lower_intrinsics.rs due to the wrong number of arguments e.g. here for read_via_copy:

let [arg] = args.as_slice() else {
span_bug!(terminator.source_info.span, "Wrong number of arguments");
};

span_bug!(..., "Wrong number of arguments...") occurs for a lot of intrinsics in this file. I'm guessing all of them will ICE under the conditions in this issue.

To fix it, we could extend what's done for transmute in #123526 to all intrinsics, maybe leveraging this code to know how many args are expected for any given intrinsic:

let (n_tps, n_cts, inputs, output) = match intrinsic_name {

@gurry
Copy link
Contributor

gurry commented Apr 7, 2024

I have opened #123587

@gurry
Copy link
Contributor

gurry commented Apr 8, 2024

Looks like intrinsics are expected to ICE if used wrongly (see comment: #123587 (comment)) so #123587 I opened has been rightly closed.

But that mean that transmute should also ICE in these conditions. However, in the present issue it was ICEing for the wrong reason i.e. out of bound access . So #123526 does the right thing by stopping that. However, IMHO it should not taint the context and let it go to MIR lowering and then let it ICE there. That keeps transmute consistent with other intrinsics' behaviour as noted above.

Thoughts @estebank & @compiler-errors? If you guys agree I can open a PR to remove the tainting.

@compiler-errors
Copy link
Member

See #123603 which takes a different approach

@gurry
Copy link
Contributor

gurry commented Apr 8, 2024

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-intrinsics Area: Intrinsics C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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.

5 participants