From 9f0a352999bfe4f265d6930a49efee7b80fe6ef5 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 22 Oct 2018 15:45:03 +0200 Subject: [PATCH 1/3] Turn ICE for dangling pointers into error --- src/librustc_mir/interpret/memory.rs | 5 +++++ src/test/ui/consts/dangling-alloc-id-ice.rs | 15 +++++++++++++++ src/test/ui/consts/dangling-alloc-id-ice.stderr | 13 +++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 src/test/ui/consts/dangling-alloc-id-ice.rs create mode 100644 src/test/ui/consts/dangling-alloc-id-ice.stderr diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 9adca6c429798..954616fe9fcd7 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -732,6 +732,11 @@ where if self.alloc_map.contains_key(&alloc) { // Not yet interned, so proceed recursively self.intern_static(alloc, mutability)?; + } else if self.dead_alloc_map.contains_key(&alloc) { + // danging pointer + return err!(ValidationFailure( + "encountered dangling pointer in final constant".into(), + )) } } Ok(()) diff --git a/src/test/ui/consts/dangling-alloc-id-ice.rs b/src/test/ui/consts/dangling-alloc-id-ice.rs new file mode 100644 index 0000000000000..31fa23ae23221 --- /dev/null +++ b/src/test/ui/consts/dangling-alloc-id-ice.rs @@ -0,0 +1,15 @@ +// https://github.com/rust-lang/rust/issues/55223 + +#![feature(const_let)] + +union Foo<'a> { + y: &'a (), + long_live_the_unit: &'static (), +} + +const FOO: &() = { //~ ERROR this constant cannot be used + let y = (); + unsafe { Foo { y: &y }.long_live_the_unit } +}; + +fn main() {} diff --git a/src/test/ui/consts/dangling-alloc-id-ice.stderr b/src/test/ui/consts/dangling-alloc-id-ice.stderr new file mode 100644 index 0000000000000..df623f943addc --- /dev/null +++ b/src/test/ui/consts/dangling-alloc-id-ice.stderr @@ -0,0 +1,13 @@ +error: this constant cannot be used + --> $DIR/dangling-alloc-id-ice.rs:10:1 + | +LL | / const FOO: &() = { //~ ERROR this constant cannot be used +LL | | let y = (); +LL | | unsafe { Foo { y: &y }.long_live_the_unit } +LL | | }; + | |__^ type validation failed: encountered dangling pointer in final constant + | + = note: #[deny(const_err)] on by default + +error: aborting due to previous error + From 0ba126217d4d349e5d9681bbb838c036adb55d18 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 22 Oct 2018 18:05:27 +0200 Subject: [PATCH 2/3] Reproduce the underlying issue nll (and thus the algorithm for actual promotion) don't know about some casts anymore --- .../consts/dangling-alloc-id-ice-2.nll.stderr | 30 +++++++++++++++++++ src/test/ui/consts/dangling-alloc-id-ice-2.rs | 10 +++++++ .../ui/consts/dangling-alloc-id-ice-2.stderr | 11 +++++++ 3 files changed, 51 insertions(+) create mode 100644 src/test/ui/consts/dangling-alloc-id-ice-2.nll.stderr create mode 100644 src/test/ui/consts/dangling-alloc-id-ice-2.rs create mode 100644 src/test/ui/consts/dangling-alloc-id-ice-2.stderr diff --git a/src/test/ui/consts/dangling-alloc-id-ice-2.nll.stderr b/src/test/ui/consts/dangling-alloc-id-ice-2.nll.stderr new file mode 100644 index 0000000000000..e6ae57796055f --- /dev/null +++ b/src/test/ui/consts/dangling-alloc-id-ice-2.nll.stderr @@ -0,0 +1,30 @@ +warning[E0716]: temporary value dropped while borrowed + --> $DIR/dangling-alloc-id-ice-2.rs:5:28 + | +LL | static MAP: Slice = Slice(&[ + | ___________________________-^ + | |___________________________| + | || +LL | || b"CloseEvent" as &'static [u8], +LL | || ]); + | || -- temporary value is freed at the end of this statement + | ||_| + | |__creates a temporary which is freed while still in use + | cast requires that borrow lasts for `'static` + | + = warning: This error has been downgraded to a warning for backwards compatibility with previous releases. + It represents potential unsoundness in your code. + This warning will become a hard error in the future. + +error[E0080]: could not evaluate static initializer + --> $DIR/dangling-alloc-id-ice-2.rs:5:1 + | +LL | / static MAP: Slice = Slice(&[ +LL | | b"CloseEvent" as &'static [u8], +LL | | ]); + | |___^ type validation failed: encountered dangling pointer in final constant + +error: aborting due to previous error + +Some errors occurred: E0080, E0716. +For more information about an error, try `rustc --explain E0080`. diff --git a/src/test/ui/consts/dangling-alloc-id-ice-2.rs b/src/test/ui/consts/dangling-alloc-id-ice-2.rs new file mode 100644 index 0000000000000..9af72c8dc0b83 --- /dev/null +++ b/src/test/ui/consts/dangling-alloc-id-ice-2.rs @@ -0,0 +1,10 @@ +// https://github.com/rust-lang/rust/issues/55223 + +struct Slice(&'static [&'static [u8]]); + +static MAP: Slice = Slice(&[ + b"CloseEvent" as &'static [u8], +]); + + +fn main() {} diff --git a/src/test/ui/consts/dangling-alloc-id-ice-2.stderr b/src/test/ui/consts/dangling-alloc-id-ice-2.stderr new file mode 100644 index 0000000000000..42df542f55cf5 --- /dev/null +++ b/src/test/ui/consts/dangling-alloc-id-ice-2.stderr @@ -0,0 +1,11 @@ +error[E0080]: could not evaluate static initializer + --> $DIR/dangling-alloc-id-ice-2.rs:5:1 + | +LL | / static MAP: Slice = Slice(&[ +LL | | b"CloseEvent" as &'static [u8], +LL | | ]); + | |___^ type validation failed: encountered dangling pointer in final constant + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0080`. From 2d960a561061d78d44c3c56d3aa0fa6975ca0ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Tue, 23 Oct 2018 13:39:30 +0200 Subject: [PATCH 3/3] Update dangling-alloc-id-ice-2.rs --- src/test/ui/consts/dangling-alloc-id-ice-2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/consts/dangling-alloc-id-ice-2.rs b/src/test/ui/consts/dangling-alloc-id-ice-2.rs index 9af72c8dc0b83..b4691641fc98f 100644 --- a/src/test/ui/consts/dangling-alloc-id-ice-2.rs +++ b/src/test/ui/consts/dangling-alloc-id-ice-2.rs @@ -1,4 +1,4 @@ -// https://github.com/rust-lang/rust/issues/55223 +// FIXME(#55223) this is just a reproduction test showing the wrong behavior struct Slice(&'static [&'static [u8]]);