Skip to content

Dangling raw pointers in constants will ICE in codegen #55353

Description

@oli-obk

cc @RalfJung

#![feature(const_let)]

const FOO: *const u32 = {
    let x = 42;
    &x
};

fn main() {
    let x = FOO;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
warning: unused variable: `x`
  --> src/main.rs:10:9
   |
10 |     let x = FOO;
   |         ^ help: consider using `_x` instead
   |
   = note: #[warn(unused_variables)] on by default

error: internal compiler error: librustc_mir/monomorphize/collector.rs:1174: alloc id without corresponding allocation: 3

thread 'main' panicked at 'Box<Any>', librustc_errors/lib.rs:600:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: aborting due to previous error


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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.31.0-nightly (f99911a4a 2018-10-23) running on x86_64-unknown-linux-gnu

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

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

error: Could not compile `playground`.

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

Activity

added
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
on Oct 25, 2018

oli-obk commented on Oct 25, 2018

@oli-obk
ContributorAuthor

Fix instructions:

The if in

if !ty.is_unsafe_ptr() {
doesn't handle raw pointers at all, because the assumption is that we can't know anything about raw pointers.

In fact we can know one thing, and that is whether the raw pointer is dangling.

So, expand the condition to handle the ty.is_unsafe_ptr() case and call self.memory.get(place.ptr). If that results in an Ok, then we're good, but if you get an Err, it can essentially only mean that we got a dangling pointer. So you can call validation_failure! with an appropriate error.

Remember to also add a test in src/test/ui/consts to make sure that this doesn't ICE the compiler anymore.

added
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
on Oct 25, 2018

RalfJung commented on Oct 26, 2018

@RalfJung
Member

Why do you want to validate raw pointers not to dangle? That seems wrong, they are allowed to.

RalfJung commented on Oct 26, 2018

@RalfJung
Member

I thought you had #55262 which would bail out during interning when it sees a dangling relocation. That didn't land yet, so I am not surprised we have this ICE.

added a commit that references this issue on Oct 26, 2018
5c45fef
removed
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
on Nov 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions