Skip to content

Commit 17f0dd5

Browse files
committed
Auto merge of #118324 - RalfJung:ctfe-read-only-pointers, r=saethlin
compile-time evaluation: detect writes through immutable pointers This has two motivations: - it unblocks rust-lang/rust#116745 (and therefore takes a big step towards `const_mut_refs` stabilization), because we can now detect if the memory that we find in `const` can be interned as "immutable" - it would detect the UB that was uncovered in rust-lang/rust#117905, which was caused by accidental stabilization of `copy` functions in `const` that can only be called with UB When UB is detected, we emit a future-compat warn-by-default lint. This is not a breaking change, so completely in line with [the const-UB RFC](https://rust-lang.github.io/rfcs/3016-const-ub.html), meaning we don't need t-lang FCP here. I made the lint immediately show up for dependencies since it is nearly impossible to even trigger this lint without `const_mut_refs` -- the accidentally stabilized `copy` functions are the only way this can happen, so the crates that popped up in #117905 are the only causes of such UB (in the code that crater covers), and the three cases of UB that we know about have all been fixed in their respective crates already. The way this is implemented is by making use of the fact that our interpreter is already generic over the notion of provenance. For CTFE we now use the new `CtfeProvenance` type which is conceptually an `AllocId` plus a boolean `immutable` flag (but packed for a more efficient representation). This means we can mark a pointer as immutable when it is created as a shared reference. The flag will be propagated to all pointers derived from this one. We can then check the immutable flag on each write to reject writes through immutable pointers. I just hope perf works out.
2 parents 3cfff05 + cb32ffd commit 17f0dd5

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
199199
}
200200
}
201201
Scalar::Ptr(ptr, _size) => {
202-
let (alloc_id, offset) = ptr.into_parts();
202+
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
203+
let alloc_id = prov.alloc_id();
203204
let base_addr =
204205
match self.tcx.global_alloc(alloc_id) {
205206
GlobalAlloc::Memory(alloc) => {

src/consts.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAl
285285
let pointer_size = dl.pointer_size.bytes() as usize;
286286

287287
let mut next_offset = 0;
288-
for &(offset, alloc_id) in alloc.provenance().ptrs().iter() {
288+
for &(offset, prov) in alloc.provenance().ptrs().iter() {
289+
let alloc_id = prov.alloc_id();
289290
let offset = offset.bytes();
290291
assert_eq!(offset as usize as u64, offset);
291292
let offset = offset as usize;
@@ -313,7 +314,7 @@ pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAl
313314

314315
llvals.push(cx.scalar_to_backend(
315316
InterpScalar::from_pointer(
316-
interpret::Pointer::new(alloc_id, Size::from_bytes(ptr_offset)),
317+
interpret::Pointer::new(prov, Size::from_bytes(ptr_offset)),
317318
&cx.tcx,
318319
),
319320
abi::Scalar::Initialized { value: Primitive::Pointer(address_space), valid_range: WrappingRange::full(dl.pointer_size) },

0 commit comments

Comments
 (0)