Skip to content

Internal compiler error when implementing local allocator and box. #90911

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
adsnaider opened this issue Nov 14, 2021 · 4 comments
Closed

Internal compiler error when implementing local allocator and box. #90911

adsnaider opened this issue Nov 14, 2021 · 4 comments
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@adsnaider
Copy link

Code

#![no_std]
#![feature(allocator_api)]
#![feature(new_uninit)]

extern crate alloc;

use alloc::boxed::Box;
use core::{
    alloc::{AllocError, Allocator, Layout},
    ptr::NonNull,
};

pub struct Foo {}

unsafe impl Allocator for Foo {
    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
        panic!();
    }

    unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
        panic!();
    }
}

impl Foo {
    pub fn foo(&self) {
        let mut bar = unsafe { Box::new_uninit_slice_in(20, self).assume_init() };
        let foobar: &mut [u8] = &mut bar;
    }
}

Meta

rustc --version --verbose:

rustc 1.58.0-nightly (8b09ba6a5 2021-11-09)
binary: rustc
commit-hash: 8b09ba6a5d5c644fe0f1c27c7f9c80b334241707
commit-date: 2021-11-09
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

Error output

Box<[u8], &Foo>", align 8), None, Align { pow2: 3 }) @ TyAndLayout { ty: alloc::boxed::Box<[u8], &Foo>, layout: Layout { fields: Arbitrary { offsets: [Size { raw: 0 }, Size { raw: 16 }], memory_index: [0, 1] }, variants: Single { index: 0 }, abi: Aggregate { sized: true }, largest_niche: Some(Niche { offset: Size { raw: 0 }, scalar: Scalar { value: Pointer, valid_range: 1..=18446744073709551615 } }), align: AbiAndPrefAlign { abi: Align { pow2: 3 }, pref: Align { pow2: 3 } }, size: Size { raw: 24 } } })

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1169:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: the compiler unexpectedly panicked. this is a bug.

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.58.0-nightly (8b09ba6a5 2021-11-09) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type lib

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: could not compile `compiler_bug`
Backtrace

stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: std::panic::panic_any::<rustc_errors::ExplicitBug>
   2: <rustc_errors::HandlerInner>::bug
   3: <rustc_errors::Handler>::bug
   4: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, ()>
   5: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>
   6: rustc_middle::util::bug::bug_fmt
   7: <rustc_codegen_ssa::mir::FunctionCx<rustc_codegen_llvm::builder::Builder>>::codegen_place
   8: <rustc_codegen_ssa::mir::FunctionCx<rustc_codegen_llvm::builder::Builder>>::codegen_rvalue_operand
   9: rustc_codegen_ssa::mir::codegen_mir::<rustc_codegen_llvm::builder::Builder>
  10: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
  11: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
  12: rustc_codegen_llvm::base::compile_codegen_unit
  13: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  14: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
  15: <rustc_interface::passes::QueryContext>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorReported>>
  16: <rustc_interface::queries::Queries>::ongoing_codegen
  17: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorReported>>
  18: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}>
  19: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


@adsnaider adsnaider 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 Nov 14, 2021
@adsnaider
Copy link
Author

I realized that the compiler still panics when the Box isn't dereferenced and even when creating a value with Box::new_in:

#![no_std]
#![feature(allocator_api)]
#![feature(new_uninit)]

extern crate alloc;

use alloc::boxed::Box;
use core::{
    alloc::{AllocError, Allocator, Layout},
    ptr::NonNull,
};

pub struct Foo {}

unsafe impl Allocator for Foo {
    fn allocate(&self, _layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
        panic!();
    }

    unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {
        panic!();
    }
}

impl Foo {
    pub fn foo(&self) {
        let _bar = Box::new_in(20, self);
    }
}

@hellow554
Copy link
Contributor

I get a different error, probably due to a newer compiler version:

thread 'rustc' panicked at 'assertion failed: i < this.fields.count()', /rustc/ad442399756573dccacb314b6bf8079964bcc72a/compiler/rustc_middle/src/ty/layout.rs:2290:21
note: run with `RUST_BACKTRACE=1` environment variable to display 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/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.58.0-nightly (ad4423997 2021-11-14) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type lib

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
Backtrace
thread 'rustc' panicked at 'assertion failed: i < this.fields.count()', /rustc/ad442399756573dccacb314b6bf8079964bcc72a/compiler/rustc_middle/src/ty/layout.rs:2290:21
stack backtrace:
   0:     0x7f699ab3b13c - std::backtrace_rs::backtrace::libunwind::trace::h793e05efd273d0f4
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7f699ab3b13c - std::backtrace_rs::backtrace::trace_unsynchronized::h640b7b86ff610c77
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f699ab3b13c - std::sys_common::backtrace::_print_fmt::h362fa2a4f354f877
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7f699ab3b13c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf439e5ed84c74abd
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x7f699ab9837c - core::fmt::write::h72801a82c94e6ff1
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/core/src/fmt/mod.rs:1149:17
   5:     0x7f699ab2b8d5 - std::io::Write::write_fmt::h5562a8b6da0f0339
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/io/mod.rs:1697:15
   6:     0x7f699ab3e390 - std::sys_common::backtrace::_print::hb29ddd998d02631c
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x7f699ab3e390 - std::sys_common::backtrace::print::h81965e3d7c90fbb6
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x7f699ab3e390 - std::panicking::default_hook::{{closure}}::h84db205ab6674b38
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/panicking.rs:211:50
   9:     0x7f699ab3df3b - std::panicking::default_hook::h1bf8bb4159936bca
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/panicking.rs:228:9
  10:     0x7f699b2d4781 - rustc_driver[e620c7401644acc4]::DEFAULT_HOOK::{closure#0}::{closure#0}
  11:     0x7f699ab3eba9 - std::panicking::rust_panic_with_hook::hf8e86850fbbd03b1
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/panicking.rs:610:17
  12:     0x7f699ab3e632 - std::panicking::begin_panic_handler::{{closure}}::h590a0d6060ff866e
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/panicking.rs:500:13
  13:     0x7f699ab3b5f4 - std::sys_common::backtrace::__rust_end_short_backtrace::h260b8bd1c848a03c
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/sys_common/backtrace.rs:139:18
  14:     0x7f699ab3e5c9 - rust_begin_unwind
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/panicking.rs:498:5
  15:     0x7f699ab03631 - core::panicking::panic_fmt::h7b8580d81fcbbacd
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/core/src/panicking.rs:106:14
  16:     0x7f699ab0357d - core::panicking::panic::h50b51d19800453c0
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/core/src/panicking.rs:47:5
  17:     0x7f699c52bbf7 - <&rustc_middle[93690e4789d7fe1d]::ty::TyS as rustc_target[819931b88a693851]::abi::TyAbiInterface<_>>::ty_and_layout_field::field_ty_or_layout::<rustc_codegen_llvm[3ea7e54ed77cfea0]::context::CodegenCx>
  18:     0x7f699c55479c - <rustc_target[819931b88a693851]::abi::TyAndLayout<&rustc_middle[93690e4789d7fe1d]::ty::TyS> as rustc_codegen_llvm[3ea7e54ed77cfea0]::type_of::LayoutLlvmExt>::scalar_pair_element_llvm_type
  19:     0x7f699c55509e - <rustc_target[819931b88a693851]::abi::TyAndLayout<&rustc_middle[93690e4789d7fe1d]::ty::TyS> as rustc_codegen_llvm[3ea7e54ed77cfea0]::type_of::LayoutLlvmExt>::scalar_pair_element_llvm_type
  20:     0x7f699c53e4f4 - <rustc_codegen_llvm[3ea7e54ed77cfea0]::context::CodegenCx as rustc_codegen_ssa[f6374deef2cbb8fd]::traits::declare::PreDefineMethods>::predefine_fn
  21:     0x7f699c54ce3f - rustc_codegen_llvm[3ea7e54ed77cfea0]::base::compile_codegen_unit::module_codegen
  22:     0x7f699d1d2797 - <rustc_query_system[efb192c4209e4e11]::dep_graph::graph::DepGraph<rustc_middle[93690e4789d7fe1d]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[93690e4789d7fe1d]::ty::context::TyCtxt, rustc_span[2d5555579096f1fe]::symbol::Symbol, rustc_codegen_ssa[f6374deef2cbb8fd]::ModuleCodegen<rustc_codegen_llvm[3ea7e54ed77cfea0]::ModuleLlvm>>
  23:     0x7f699d208dd6 - rustc_codegen_llvm[3ea7e54ed77cfea0]::base::compile_codegen_unit
  24:     0x7f699d1e8e58 - <rustc_codegen_llvm[3ea7e54ed77cfea0]::LlvmCodegenBackend as rustc_codegen_ssa[f6374deef2cbb8fd]::traits::backend::CodegenBackend>::codegen_crate
  25:     0x7f699d1a4dc7 - <rustc_session[cec017cef00f19c9]::session::Session>::time::<alloc[9a4bc13598ff604f]::boxed::Box<dyn core[cc79c391059f8e46]::any::Any>, rustc_interface[f89f8228a4e35bc7]::passes::start_codegen::{closure#0}>
  26:     0x7f699d1848d3 - <rustc_interface[f89f8228a4e35bc7]::passes::QueryContext>::enter::<<rustc_interface[f89f8228a4e35bc7]::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core[cc79c391059f8e46]::result::Result<alloc[9a4bc13598ff604f]::boxed::Box<dyn core[cc79c391059f8e46]::any::Any>, rustc_errors[c8a333c965fedc03]::ErrorReported>>
  27:     0x7f699d17f34f - <rustc_interface[f89f8228a4e35bc7]::queries::Queries>::ongoing_codegen
  28:     0x7f699d163667 - <rustc_interface[f89f8228a4e35bc7]::interface::Compiler>::enter::<rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}::{closure#2}, core[cc79c391059f8e46]::result::Result<core[cc79c391059f8e46]::option::Option<rustc_interface[f89f8228a4e35bc7]::queries::Linker>, rustc_errors[c8a333c965fedc03]::ErrorReported>>
  29:     0x7f699d1545cd - rustc_span[2d5555579096f1fe]::with_source_map::<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_interface[f89f8228a4e35bc7]::interface::create_compiler_and_run<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>::{closure#1}>
  30:     0x7f699d174b7a - rustc_interface[f89f8228a4e35bc7]::interface::create_compiler_and_run::<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>
  31:     0x7f699d157f7b - <scoped_tls[3fea4c3dcac147b1]::ScopedKey<rustc_span[2d5555579096f1fe]::SessionGlobals>>::set::<rustc_interface[f89f8228a4e35bc7]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[f89f8228a4e35bc7]::interface::run_compiler<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#0}::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>
  32:     0x7f699d157095 - std[a5529df289459975]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[f89f8228a4e35bc7]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[f89f8228a4e35bc7]::interface::run_compiler<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>
  33:     0x7f699d153162 - <<std[a5529df289459975]::thread::Builder>::spawn_unchecked<rustc_interface[f89f8228a4e35bc7]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[f89f8228a4e35bc7]::interface::run_compiler<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#1} as core[cc79c391059f8e46]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  34:     0x7f699ab49e93 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h771719d52c343434
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/alloc/src/boxed.rs:1694:9
  35:     0x7f699ab49e93 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hf441746dfa4b0f57
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/alloc/src/boxed.rs:1694:9
  36:     0x7f699ab49e93 - std::sys::unix::thread::Thread::new::thread_start::hfd168f9d312b29ca
                               at /rustc/ad442399756573dccacb314b6bf8079964bcc72a/library/std/src/sys/unix/thread.rs:106:17
  37:     0x7f699a8ec927 - start_thread
                               at ./nptl/./nptl/pthread_create.c:435:8
  38:     0x7f699a97c9e4 - __clone
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:100
  39:                0x0 - <unknown>

@asquared31415
Copy link
Contributor

this is because the value you pass as the allocator is a &Foo (due to the &self receiver) and the compiler does not like allocators that have a non-zero size

duplicate of #78459

@JohnTitor
Copy link
Member

Indeed this does look like a duplicate of #78459, that issue has been fixed by #94043 and the above code compiles fine too. I'm going to close as that PR should have a sufficient testcase. Thanks for reporting!

# 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) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants