Skip to content

Commit 6353599

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 0657c1b + 994d36b commit 6353599

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/constant.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ pub(crate) fn codegen_const_value<'tcx>(
126126
}
127127
}
128128
Scalar::Ptr(ptr, _size) => {
129-
let (alloc_id, offset) = ptr.into_parts(); // we know the `offset` is relative
129+
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
130+
let alloc_id = prov.alloc_id();
130131
let base_addr = match fx.tcx.global_alloc(alloc_id) {
131132
GlobalAlloc::Memory(alloc) => {
132133
let data_id = data_id_for_alloc_id(
@@ -374,7 +375,8 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
374375
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len()).to_vec();
375376
data.define(bytes.into_boxed_slice());
376377

377-
for &(offset, alloc_id) in alloc.provenance().ptrs().iter() {
378+
for &(offset, prov) in alloc.provenance().ptrs().iter() {
379+
let alloc_id = prov.alloc_id();
378380
let addend = {
379381
let endianness = tcx.data_layout.endian;
380382
let offset = offset.bytes() as usize;

0 commit comments

Comments
 (0)