Skip to content
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

ICE with #![feature(allocator_api)] #81270

Closed
jfrimmel opened this issue Jan 22, 2021 · 8 comments · Fixed by #94414
Closed

ICE with #![feature(allocator_api)] #81270

jfrimmel opened this issue Jan 22, 2021 · 8 comments · Fixed by #94414
Assignees
Labels
A-allocators Area: Custom and system allocators C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. 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

@jfrimmel
Copy link
Contributor

jfrimmel commented Jan 22, 2021

Hello,
I've tested the allocator_api feature and encountered an ICE:

Code

If tried this code (playground):

#![feature(allocator_api)]
use std::alloc::{self, Allocator, Global, AllocError};
use std::ptr;

struct InlineAlloc<A: Allocator = Global> {
    upstream: A,
    buffer: [u8; 32],
}
impl InlineAlloc<Global> {
    pub fn new() -> Self {
        Self {
            upstream: Global,
            buffer: [0; 32],
        }
    }
}
unsafe impl<A: Allocator> Allocator for InlineAlloc<A> {
    fn allocate(&self, _: alloc::Layout) -> Result<ptr::NonNull<[u8]>, AllocError> {
        todo!();
    }
    
    unsafe fn deallocate(&self, _: ptr::NonNull<u8>, _: alloc::Layout) {
        todo!();
    }
}

fn main() {
    let x = Box::new_in(0, InlineAlloc::new());
}

Meta

I've used the "current" playground nighty compiler, but I can reproduce locally.

rustc --version --verbose:

rustc 1.51.0-nightly (a4cbb44ae 2021-01-20)
binary: rustc
commit-hash: a4cbb44ae2c80545db957763b502dc7f6ea22085
commit-date: 2021-01-20
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1

and also on

rustc 1.51.0-nightly (202720bf4 2021-01-21)
binary: rustc
commit-hash: 202720bf483088dbdb343f78c0aa77067fdd8156
commit-date: 2021-01-21
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1

Error output

error: internal compiler error: /rustc/202720bf483088dbdb343f78c0aa77067fdd8156/compiler/rustc_codegen_ssa/src/mir/operand.rs:133:38: Deref of by-Ref operand OperandRef(Ref((%"std::boxed::Box<std::mem::MaybeUninit<i32>, InlineAlloc>"*:  %10 = alloca %"std::boxed::Box<std::mem::MaybeUninit<i32>, InlineAlloc>", align 8), None, Align { pow2: 3 }) @ TyAndLayout { ty: std::boxed::Box<std::mem::MaybeUninit<i32>, InlineAlloc>, layout: Layout { fields: Arbitrary { offsets: [Size { raw: 0 }, Size { raw: 8 }], 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: 40 } } })

thread 'rustc' panicked at 'Box<Any>', compiler/rustc_errors/src/lib.rs:958: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.51.0-nightly (202720bf4 2021-01-21) running on x86_64-unknown-linux-gnu

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

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

query stack during panic:
end of query stack
error: aborting due to previous error; 3 warnings emitted

error: could not compile `test2`

To learn more, run the command again with --verbose.
Backtrace

Standard Error

   Compiling playground v0.0.1 (/playground)
warning: unused variable: `x`
  --> src/main.rs:28:9
   |
28 |     let x = Box::new_in(0, InlineAlloc::new());
   |         ^ help: if this is intentional, prefix it with an underscore: `_x`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: field is never read: `upstream`
 --> src/main.rs:6:5
  |
6 |     upstream: A,
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: field is never read: `buffer`
 --> src/main.rs:7:5
  |
7 |     buffer: [u8; 32],
  |     ^^^^^^^^^^^^^^^^

error: internal compiler error: /rustc/202720bf483088dbdb343f78c0aa77067fdd8156/compiler/rustc_codegen_ssa/src/mir/operand.rs:133:38: Deref of by-Ref operand OperandRef(Ref((%"std::boxed::Box<std::mem::MaybeUninit<i32>, InlineAlloc>"*:  %10 = alloca %"std::boxed::Box<std::mem::MaybeUninit<i32>, InlineAlloc>", align 8), None, Align { pow2: 3 }) @ TyAndLayout { ty: std::boxed::Box<std::mem::MaybeUninit<i32>, InlineAlloc>, layout: Layout { fields: Arbitrary { offsets: [Size { raw: 0 }, Size { raw: 8 }], 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: 40 } } })

thread 'rustc' panicked at 'Box<Any>', compiler/rustc_errors/src/lib.rs:958:9
stack backtrace:
   0: std::panicking::begin_panic
   1: rustc_errors::HandlerInner::bug
   2: rustc_errors::Handler::bug
   3: rustc_middle::ty::context::tls::with_opt
   4: rustc_middle::util::bug::opt_span_bug_fmt
   5: rustc_middle::util::bug::bug_fmt
   6: rustc_codegen_ssa::mir::operand::OperandRef<V>::deref
   7: rustc_codegen_ssa::mir::place::<impl rustc_codegen_ssa::mir::FunctionCx<Bx>>::codegen_place
   8: rustc_codegen_ssa::mir::rvalue::<impl rustc_codegen_ssa::mir::FunctionCx<Bx>>::codegen_rvalue_operand
   9: rustc_codegen_ssa::mir::codegen_mir
  10: rustc_codegen_ssa::base::codegen_instance
  11: <rustc_middle::mir::mono::MonoItem as rustc_codegen_ssa::mono_item::MonoItemExt>::define
  12: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
  13: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  14: rustc_codegen_llvm::base::compile_codegen_unit
  15: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  16: rustc_session::utils::<impl rustc_session::session::Session>::time
  17: rustc_interface::passes::QueryContext::enter
  18: rustc_interface::queries::Queries::ongoing_codegen
  19: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  20: rustc_span::with_source_map
  21: rustc_interface::interface::create_compiler_and_run
  22: rustc_span::with_session_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose 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.51.0-nightly (202720bf4 2021-01-21) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

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

query stack during panic:
end of query stack
error: aborting due to previous error; 3 warnings emitted

error: could not compile `playground`

To learn more, run the command again with --verbose.

@jfrimmel jfrimmel 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 Jan 22, 2021
@jfrimmel

This comment has been minimized.

@rustbot rustbot added A-allocators Area: Custom and system allocators requires-nightly This issue requires a nightly compiler in some way. labels Jan 22, 2021
@jfrimmel
Copy link
Contributor Author

jfrimmel commented Jan 22, 2021

Slightly minimized:

#![feature(allocator_api)]
use std::alloc::{Layout, Allocator, Global, AllocError};
use std::ptr::NonNull;
use std::marker::PhantomData;

struct S<A>{ a: PhantomData<A>, b: [u8; 1] }
unsafe impl<A> Allocator for S<A> {
    fn allocate(&self, _: Layout) -> Result<NonNull<[u8]>, AllocError> {
        todo!();
    }
    unsafe fn deallocate(&self, _: NonNull<u8>, _: Layout) {
        todo!();
    }
}

fn main() {
    let x: Box<u8, S<u8>> = Box::new_in(0, S { a: PhantomData, b: [0; 1] });
}

Doesn't ICE anymore if you either remove the field b or if it is not an array.

@mbartlett21
Copy link
Contributor

Duplicate of #78459?

@jfrimmel
Copy link
Contributor Author

Duplicate of #78459?

No, not really a duplicate (but possibly related). This issue and the linked one have completely different backtraces.

@mbartlett21
Copy link
Contributor

Just by the way, using inline storage in an allocator is actually unsafe unless the impl of the Allocator trait is using a reference.

See the Allocator safety notes:

cloning or moving the allocator must not invalidate memory blocks returned from this allocator. A cloned allocator must behave like the same allocator.

(Emphasis mine)

(link)

fanninpm added a commit to fanninpm/glacier that referenced this issue Jan 30, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jan 30, 2021
@petertodd
Copy link
Contributor

petertodd commented Feb 19, 2021

FWIW this is also triggered with uninhabited types. Though looks like only when drop glue is involved. Eg Box<u8, NeverAlloc> works but Box<Box<u8>, NeverAlloc> doesn't.

@hellow554
Copy link
Contributor

hellow554 commented Jun 3, 2021

Another MCVE that leads to this ICE:

#![feature(allocator_api)]

use std::alloc::Global;
use std::mem::MaybeUninit;

fn crash(_: &[MaybeUninit<()>]) {}

fn main() {
    crash(&Box::new_uninit_slice_in(0, &Global));
}

found in #85916

@beepster4096
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-allocators Area: Custom and system allocators C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. 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

Successfully merging a pull request may close this issue.

7 participants