rust/compiler/rustc_mir_transform/src
bors 0e7f91b75e 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 https://github.com/rust-lang/rust/pull/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 https://github.com/rust-lang/rust/pull/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.
2023-12-07 18:11:01 +00:00
..
coverage coverage: Be more strict about what counts as a "visible macro" 2023-12-05 21:21:05 +11:00
inline subst -> instantiate 2023-09-26 09:37:55 +02:00
abort_unwinding_calls.rs s/Generator/Coroutine/ 2023-10-20 21:10:38 +00:00
add_call_guards.rs when terminating during unwinding, show the reason why 2023-08-24 13:28:26 +02:00
add_moves_for_packed_drops.rs Remove DesugaringKind::Replace. 2023-05-25 17:40:46 +00:00
add_retag.rs cleanup misinformation regarding has_deref 2023-08-06 17:29:09 +03:00
add_subtyping_projections.rs move subtyper change reveal_all 2023-10-05 18:56:30 +03:00
check_alignment.rs Check alignment of pointers only when read/written through 2023-11-04 13:01:32 -04:00
check_const_item_mutation.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
check_packed_ref.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
check_unsafety.rs Rename HandlerInner::delay_span_bug as HandlerInner::span_delayed_bug. 2023-12-02 09:01:19 +11:00
cleanup_post_borrowck.rs rename BorrowKind::Shallow to Fake 2023-11-08 22:55:28 +01:00
const_debuginfo.rs Format all the let chains in compiler 2023-10-13 08:59:36 +00:00
const_goto.rs rename mir::Constant -> mir::ConstOperand, mir::ConstKind -> mir::Const 2023-09-21 08:12:30 +02:00
const_prop_lint.rs merge DefKind::Coroutine into DefKind::Closure 2023-11-26 21:05:08 +08:00
const_prop.rs compile-time evaluation: emit a lint when a write through an immutable pointer occurs 2023-12-07 17:46:36 +01:00
copy_prop.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
coroutine.rs Auto merge of #118230 - nnethercote:streamline-dataflow-cursors, r=cjgillot 2023-12-05 02:36:50 +00:00
cost_checker.rs Re-format code with new rustfmt 2023-11-15 21:45:48 -05:00
cross_crate_inline.rs Add -Zcross-crate-inline-threshold=yes 2023-11-07 18:45:11 -05:00
ctfe_limit.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
dataflow_const_prop.rs compile-time evaluation: emit a lint when a write through an immutable pointer occurs 2023-12-07 17:46:36 +01:00
dead_store_elimination.rs Preserve DebugInfo in DeadStoreElimination. 2023-10-06 15:46:11 +00:00
deduce_param_attrs.rs deduce_param_attrs: explain a read-only case 2023-10-30 10:29:08 +01:00
deduplicate_blocks.rs rename mir::Constant -> mir::ConstOperand, mir::ConstKind -> mir::Const 2023-09-21 08:12:30 +02:00
deref_separator.rs Remove mir::LocalDecl::internal. 2023-10-04 17:55:15 +00:00
dest_prop.rs s/generator/coroutine/ 2023-10-20 21:14:01 +00:00
dump_mir.rs Write to stdout if - is given as output file 2023-06-06 17:53:29 -04:00
early_otherwise_branch.rs add some comments explaining why MIR opts are marked as unsound 2023-10-10 11:17:27 +02:00
elaborate_box_derefs.rs Remove mir::LocalDecl::internal. 2023-10-04 17:55:15 +00:00
elaborate_drops.rs Rename HandlerInner::delay_span_bug as HandlerInner::span_delayed_bug. 2023-12-02 09:01:19 +11:00
errors.rs De-genericize some IntoDiagnostic impls. 2023-12-04 18:57:42 +11:00
ffi_unwind_calls.rs s/Generator/Coroutine/ 2023-10-20 21:10:38 +00:00
function_item_references.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
gvn.rs ctfe interpreter: extend provenance so that it can track whether a pointer is immutable 2023-12-07 17:46:36 +01:00
inline.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
instsimplify.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
jump_threading.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
large_enums.rs add some comments explaining why MIR opts are marked as unsound 2023-10-10 11:17:27 +02:00
lib.rs Rename HandlerInner::delay_span_bug as HandlerInner::span_delayed_bug. 2023-12-02 09:01:19 +11:00
lower_intrinsics.rs Remove option_payload_ptr; redundant to offset_of 2023-11-16 22:56:25 +00:00
lower_slice_len.rs Format all the let chains in compiler 2023-10-13 08:59:36 +00:00
match_branches.rs rename mir::Constant -> mir::ConstOperand, mir::ConstKind -> mir::Const 2023-09-21 08:12:30 +02:00
multiple_return_terminators.rs Remove unused TyCtxt from remove_dead_blocks 2023-10-18 23:42:45 +11:00
normalize_array_len.rs Format all the let chains in compiler 2023-10-13 08:59:36 +00:00
nrvo.rs add some comments explaining why MIR opts are marked as unsound 2023-10-10 11:17:27 +02:00
pass_manager.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
prettify.rs make reorder BB pass use cached RPO 2023-06-14 20:08:04 +00:00
ref_prop.rs Format all the let chains in compiler 2023-10-13 08:59:36 +00:00
remove_noop_landing_pads.rs s/Generator/Coroutine/ 2023-10-20 21:10:38 +00:00
remove_place_mention.rs Actually keep PlaceMention if requested. 2023-04-21 21:34:59 +00:00
remove_storage_markers.rs Make MIR basic blocks field public 2022-07-07 08:11:49 +02:00
remove_uninit_drops.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
remove_unneeded_drops.rs inline format!() args from rustc_codegen_llvm to the end (4) 2023-07-25 23:20:28 +02:00
remove_zsts.rs Remove incorrect transformation from RemoveZsts 2023-11-11 01:51:09 +01:00
required_consts.rs rename mir::Constant -> mir::ConstOperand, mir::ConstKind -> mir::Const 2023-09-21 08:12:30 +02:00
reveal_all.rs move subtyper change reveal_all 2023-10-05 18:56:30 +03:00
separate_const_switch.rs s/Generator/Coroutine/ 2023-10-20 21:10:38 +00:00
shim.rs Fix coroutine validation for mixed panic strategy 2023-11-28 21:11:24 +01:00
simplify_branches.rs Simplify assume of a constant. 2023-10-31 11:44:23 +00:00
simplify_comparison_integral.rs rename mir::Constant -> mir::ConstOperand, mir::ConstKind -> mir::Const 2023-09-21 08:12:30 +02:00
simplify.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
sroa.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
ssa.rs Fix def-use check for call terminators 2023-11-14 17:07:34 +01:00
uninhabited_enum_branching.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
unreachable_prop.rs Only emit != assumptions if the otherwise target is reachable. 2023-10-31 11:44:23 +00:00