Skip to content

Commit 23ef234

Browse files
authored
Rollup merge of rust-lang#95221 - RalfJung:check_and_deref_ptr, r=oli-obk
interpret/memory: simplify check_and_deref_ptr *Finally* I saw a way to make this code simpler. The odd preprocessing in `let ptr_or_addr =` has bothered me since forever, but it actually became unnecessary in the last provenance refactoring. :) This also leads to slightly more explicit error messages as a nice side-effect. 🎉 r? `@oli-obk`
2 parents 0c79c86 + a76e5b1 commit 23ef234

File tree

8 files changed

+14
-24
lines changed

8 files changed

+14
-24
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -427,22 +427,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
427427
}
428428
}
429429

430-
// Extract from the pointer an `Option<AllocId>` and an offset, which is relative to the
431-
// allocation or (if that is `None`) an absolute address.
432-
let ptr_or_addr = if size.bytes() == 0 {
433-
// Let's see what we can do, but don't throw errors if there's nothing there.
434-
self.ptr_try_get_alloc(ptr)
435-
} else {
436-
// A "real" access, we insist on getting an `AllocId`.
437-
Ok(self.ptr_get_alloc(ptr)?)
438-
};
439-
Ok(match ptr_or_addr {
430+
Ok(match self.ptr_try_get_alloc(ptr) {
440431
Err(addr) => {
441-
// No memory is actually being accessed.
442-
debug_assert!(size.bytes() == 0);
443-
// Must be non-null.
444-
if addr == 0 {
445-
throw_ub!(DanglingIntPointer(0, msg))
432+
// We couldn't get a proper allocation. This is only okay if the access size is 0,
433+
// and the address is not null.
434+
if size.bytes() > 0 || addr == 0 {
435+
throw_ub!(DanglingIntPointer(addr, msg));
446436
}
447437
// Must be aligned.
448438
if let Some(align) = align {

src/test/ui/consts/const-deref-ptr.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: could not evaluate static initializer
22
--> $DIR/const-deref-ptr.rs:4:29
33
|
44
LL | static C: u64 = unsafe {*(0xdeadbeef as *const u64)};
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 0xdeadbeef is not a valid pointer
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: 0xdeadbeef is not a valid pointer
66

77
error: aborting due to previous error
88

src/test/ui/consts/const-eval/const_raw_ptr_ops2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error[E0080]: evaluation of constant value failed
22
--> $DIR/const_raw_ptr_ops2.rs:7:26
33
|
44
LL | const Z2: i32 = unsafe { *(42 as *const i32) };
5-
| ^^^^^^^^^^^^^^^^^^^ 0x2a is not a valid pointer
5+
| ^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: 0x2a is not a valid pointer
66

77
error[E0080]: evaluation of constant value failed
88
--> $DIR/const_raw_ptr_ops2.rs:9:26
99
|
1010
LL | const Z3: i32 = unsafe { *(44 as *const i32) };
11-
| ^^^^^^^^^^^^^^^^^^^ 0x2c is not a valid pointer
11+
| ^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: 0x2c is not a valid pointer
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ error[E0080]: could not evaluate static initializer
296296
--> $DIR/ub-wide-ptr.rs:135:5
297297
|
298298
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
299-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: null pointer is not a valid pointer
300300

301301
error[E0080]: could not evaluate static initializer
302302
--> $DIR/ub-wide-ptr.rs:139:5

src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ error[E0080]: could not evaluate static initializer
296296
--> $DIR/ub-wide-ptr.rs:135:5
297297
|
298298
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
299-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: null pointer is not a valid pointer
300300

301301
error[E0080]: could not evaluate static initializer
302302
--> $DIR/ub-wide-ptr.rs:139:5

src/test/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0080]: evaluation of constant value failed
44
LL | Some(&mut *(42 as *mut i32))
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
| |
7-
| 0x2a is not a valid pointer
7+
| dereferencing pointer failed: 0x2a is not a valid pointer
88
| inside `helper` at $DIR/mut_ref_in_final_dynamic_check.rs:13:10
99
...
1010
LL | const A: Option<&mut i32> = helper();

src/test/ui/consts/offset_ub.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ error[E0080]: evaluation of constant value failed
130130
LL | unsafe { intrinsics::offset(self, count) as *mut T }
131131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
132132
| |
133-
| 0x1 is not a valid pointer
133+
| pointer arithmetic failed: 0x1 is not a valid pointer
134134
| inside `ptr::mut_ptr::<impl *mut u8>::offset` at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
135135
|
136136
::: $DIR/offset_ub.rs:19:42
@@ -158,7 +158,7 @@ error[E0080]: evaluation of constant value failed
158158
LL | unsafe { intrinsics::offset(self, count) }
159159
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
160160
| |
161-
| 0x7f..f is not a valid pointer
161+
| pointer arithmetic failed: 0x7f..f is not a valid pointer
162162
| inside `ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
163163
|
164164
::: $DIR/offset_ub.rs:25:47

src/test/ui/error-codes/E0396-fixed.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
22
--> $DIR/E0396-fixed.rs:5:28
33
|
44
LL | const VALUE: u8 = unsafe { *REG_ADDR };
5-
| ^^^^^^^^^ 0x5f3759df is not a valid pointer
5+
| ^^^^^^^^^ dereferencing pointer failed: 0x5f3759df is not a valid pointer
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)