Skip to content

Commit e05aae8

Browse files
committed
Also check typed raw pointers in validation for dangliness
1 parent 2e446cc commit e05aae8

File tree

6 files changed

+59
-20
lines changed

6 files changed

+59
-20
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,18 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
606606
if place.layout.is_unsized() {
607607
self.check_wide_ptr_meta(place.meta(), place.layout)?;
608608
}
609+
if let Some(prov) = place.ptr().provenance {
610+
if let Some(alloc_id) = prov.get_alloc_id() {
611+
if let AllocKind::Dead = self.ecx.get_alloc_info(alloc_id).2 {
612+
throw_validation_failure!(
613+
self.path,
614+
DanglingPtrUseAfterFree {
615+
ptr_kind: PointerKind::Ref(Mutability::Not)
616+
}
617+
)
618+
}
619+
}
620+
}
609621
Ok(true)
610622
}
611623
ty::Ref(_, _ty, mutbl) => {

tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ error[E0080]: it is undefined behavior to use this value
3535
--> $DIR/mut_ref_in_final_dynamic_check.rs:36:1
3636
|
3737
LL | const DANGLING: Option<&mut i32> = helper_dangling();
38-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered a dangling reference (use-after-free)
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-tag>: encountered a dangling reference (use-after-free)
3939
|
4040
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
4141
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
@@ -46,7 +46,7 @@ error[E0080]: it is undefined behavior to use this value
4646
--> $DIR/mut_ref_in_final_dynamic_check.rs:37:1
4747
|
4848
LL | static DANGLING_STATIC: Option<&mut i32> = helper_dangling();
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered a dangling reference (use-after-free)
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-tag>: encountered a dangling reference (use-after-free)
5050
|
5151
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
5252
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {

tests/ui/consts/dangling_raw_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const FOO: *const u32 = { //~ ERROR encountered dangling pointer in final value of constant
1+
const FOO: *const u32 = { //~ ERROR it is undefined behavior
22
let x = 42;
33
&x
44
};
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
error: encountered dangling pointer in final value of constant
1+
error[E0080]: it is undefined behavior to use this value
22
--> $DIR/dangling_raw_ptr.rs:1:1
33
|
44
LL | const FOO: *const u32 = {
5-
| ^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (use-after-free)
6+
|
7+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
8+
= note: the raw bytes of the constant (size: 8, align: 8) {
9+
╾ALLOC0<imm>╼ │ ╾──────╼
10+
}
611

712
error: aborting due to 1 previous error
813

14+
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/std/cell.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::cell::*;
44

55
// not ok, because this creates a dangling pointer, just like `let x = Cell::new(42).as_ptr()` would
66
static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
7-
//~^ ERROR encountered dangling pointer
7+
//~^ ERROR it is undefined behavior
88
const FOO_CONST: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
9-
//~^ ERROR encountered dangling pointer
9+
//~^ ERROR it is undefined behavior
1010

1111
// Ok, these are just base values and it is the `Wrap` author's job to uphold `Send` and `Sync`
1212
// invariants, since they used `unsafe impl`.
@@ -20,27 +20,27 @@ static FOO4: Wrap<*mut u32> = Wrap(FOO3.0.as_ptr());
2020
// its memory will get freed before the constant is finished evaluating, thus creating a dangling
2121
// pointer. This would happen exactly the same at runtime.
2222
const FOO4_CONST: Wrap<*mut u32> = Wrap(FOO3_CONST.0.as_ptr());
23-
//~^ ERROR encountered dangling pointer
23+
//~^ ERROR it is undefined behavior
2424

2525
// not ok, because the `as_ptr` call takes a reference to a temporary that will get freed
2626
// before the constant is finished evaluating.
2727
const FOO2: *mut u32 = Cell::new(42).as_ptr();
28-
//~^ ERROR encountered dangling pointer
28+
//~^ ERROR it is undefined behavior
2929

3030
struct IMSafeTrustMe(UnsafeCell<u32>);
3131
unsafe impl Send for IMSafeTrustMe {}
3232
unsafe impl Sync for IMSafeTrustMe {}
3333

3434
static BAR: IMSafeTrustMe = IMSafeTrustMe(UnsafeCell::new(5));
3535

36-
37-
3836
struct Wrap<T>(T);
3937
unsafe impl<T> Send for Wrap<T> {}
4038
unsafe impl<T> Sync for Wrap<T> {}
4139

4240
static BAR_PTR: Wrap<*mut u32> = Wrap(BAR.0.get());
4341

44-
const fn fst_ref<T, U>(x: &(T, U)) -> &T { &x.0 }
42+
const fn fst_ref<T, U>(x: &(T, U)) -> &T {
43+
&x.0
44+
}
4545

4646
fn main() {}

tests/ui/consts/std/cell.stderr

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
1-
error: encountered dangling pointer in final value of static
1+
error[E0080]: it is undefined behavior to use this value
22
--> $DIR/cell.rs:6:1
33
|
44
LL | static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered a dangling reference (use-after-free)
6+
|
7+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
8+
= note: the raw bytes of the constant (size: 8, align: 8) {
9+
╾ALLOC0╼ │ ╾──────╼
10+
}
611

7-
error: encountered dangling pointer in final value of constant
12+
error[E0080]: it is undefined behavior to use this value
813
--> $DIR/cell.rs:8:1
914
|
1015
LL | const FOO_CONST: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered a dangling reference (use-after-free)
17+
|
18+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
19+
= note: the raw bytes of the constant (size: 8, align: 8) {
20+
╾ALLOC1╼ │ ╾──────╼
21+
}
1222

13-
error: encountered dangling pointer in final value of constant
23+
error[E0080]: it is undefined behavior to use this value
1424
--> $DIR/cell.rs:22:1
1525
|
1626
LL | const FOO4_CONST: Wrap<*mut u32> = Wrap(FOO3_CONST.0.as_ptr());
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered a dangling reference (use-after-free)
28+
|
29+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
30+
= note: the raw bytes of the constant (size: 8, align: 8) {
31+
╾ALLOC2╼ │ ╾──────╼
32+
}
1833

19-
error: encountered dangling pointer in final value of constant
34+
error[E0080]: it is undefined behavior to use this value
2035
--> $DIR/cell.rs:27:1
2136
|
2237
LL | const FOO2: *mut u32 = Cell::new(42).as_ptr();
23-
| ^^^^^^^^^^^^^^^^^^^^
38+
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (use-after-free)
39+
|
40+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
41+
= note: the raw bytes of the constant (size: 8, align: 8) {
42+
╾ALLOC3╼ │ ╾──────╼
43+
}
2444

2545
error: aborting due to 4 previous errors
2646

47+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)