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

Dangling raw pointers in constants will ICE in codegen #55353

Closed
oli-obk opened this issue Oct 25, 2018 · 3 comments
Closed

Dangling raw pointers in constants will ICE in codegen #55353

oli-obk opened this issue Oct 25, 2018 · 3 comments
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Oct 25, 2018

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.

@oli-obk oli-obk added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ A-const-eval Area: Constant evaluation (MIR interpretation) labels Oct 25, 2018
@oli-obk
Copy link
Contributor Author

oli-obk commented Oct 25, 2018

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.

@oli-obk oli-obk added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Oct 25, 2018
@RalfJung
Copy link
Member

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

@RalfJung
Copy link
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.

oli-obk added a commit to oli-obk/rust that referenced this issue Oct 26, 2018
@oli-obk oli-obk removed the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Nov 21, 2018
@oli-obk oli-obk closed this as completed Nov 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

2 participants