rust/compiler
Matthias Krüger 6055793062
Rollup merge of #135296 - lukas-code:dyn-leak-check, r=compiler-errors
interpret: adjust vtable validity check for higher-ranked types

## What

Transmuting between trait objects where a generic argument or associated type only differs in bound regions (not bound at or above the trait object's binder) is now UB. For example

* transmuting between `&dyn Trait<for<'a> fn(&'a u8)>` and `&dyn Trait<fn(&'static u8)>` is UB.
* transmuting between `&dyn Trait<Assoc = for<'a> fn(&'a u8)>` and `&dyn Trait<Assoc = fn(&'static u8)>` is UB.
* transmuting between `&dyn Trait<for<'a> fn(&'a u8) -> (&'a u8, &'static u8)>` and `&dyn Trait<for<'a> fn(&'a u8) -> (&'static u8, &'a u8)>` is UB.

Transmuting between subtypes (in either direction) is still allowed, which means that bound regions that are bound at or above the trait object's binder can still be changed:

* transmuting between `&dyn for<'a> Trait<fn(&'a u8)>` and `&dyn for Trait<fn(&'static u8)>` is fine.
* transmuting between `&dyn for<'a> Trait<dyn Trait<fn(&'a u8)>>` and `&dyn for Trait<dyn Trait<fn(&'static u8)>>` is fine.

## Why

Very similar to https://github.com/rust-lang/rust/issues/120217 and https://github.com/rust-lang/rust/issues/120222, changing a trait object's generic argument to a type that only differs in bound regions can still affect the vtable layout and lead to segfaults at runtime (for an example see `src/tools/miri/tests/fail/validity/dyn-transmute-inner-binder.rs`).

Since we already already require that the trait object predicates must be equal modulo bound regions, it is only natural to extend this check to also require type equality considering bound regions.

However, it also makes sense to allow transmutes between a type and a subtype thereof. For example `&dyn for<'a> Trait<&'a u8>` is a subtype of `&dyn Trait<&'static ()>` and they are guaranteed to have the same vtable, so it makes sense to allow this transmute. So that's why bound lifetimes that are bound to the trait object itself are treated as free lifetime for the purpose of this check.

Note that codegen already relies on the property that subtyping cannot change the the vtable and this is asserted here (note the leak check): 251206c27b/compiler/rustc_codegen_ssa/src/base.rs (L106-L153)

Furthermore, we allow some pointer-to-pointer casts like `*const dyn for<'a> Trait<&'a u8>` to `*const Wrapper<dyn Trait<&'static u8>>` that instantiate the trait object binder and are currently lowered to a single pointer-to-pointer cast in MIR (`CastKind::PtrToPtr`) and *not* an unsizing coercion (`CastKind::PointerCoercion(Unsize)`), so the current MIR lowering of these would be UB if we didn't allow subtyping transmutes.

---

fixes https://github.com/rust-lang/rust/issues/135230
cc `@rust-lang/opsem`
r? `@compiler-errors` for the implementation
2025-02-20 00:55:11 +01:00
..
rustc switch jemalloc-sys back to tikv-jemalloc-sys, and update to 0.6.0 2024-12-03 08:56:33 +00:00
rustc_abi Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_arena Add inherent versions of MaybeUninit methods for slices 2025-01-11 23:57:00 -05:00
rustc_ast Add a TyPat in the AST to reuse the generic arg lowering logic 2025-02-11 08:51:05 +00:00
rustc_ast_ir Add sugar for &pin (const|mut) types 2024-10-07 11:15:04 -07:00
rustc_ast_lowering Move some Map methods onto TyCtxt. 2025-02-17 13:21:02 +11:00
rustc_ast_passes compiler: remove rustc_target reexport of rustc_abi::HashStableContext 2025-02-11 18:55:48 -08:00
rustc_ast_pretty Add a TyPat in the AST to reuse the generic arg lowering logic 2025-02-11 08:51:05 +00:00
rustc_attr_data_structures rustc_allowed_through_unstable_modules: require deprecation message 2025-02-02 12:36:12 +01:00
rustc_attr_parsing Rustfmt 2025-02-08 22:12:13 +00:00
rustc_baked_icu_data Delete the cfg(not(parallel)) serial compiler 2024-11-12 13:38:58 +00:00
rustc_borrowck Auto merge of #136539 - matthewjasper:late-normalize-errors, r=compiler-errors 2025-02-19 07:49:08 +00:00
rustc_builtin_macros Rollup merge of #136646 - oli-obk:pattern-types-ast, r=BoxyUwU 2025-02-12 06:07:37 +01:00
rustc_codegen_cranelift cg_clif: Tweak formatting of global comments 2025-02-18 01:29:23 -08:00
rustc_codegen_gcc Auto merge of #136575 - scottmcm:nsuw-math, r=nikic 2025-02-14 14:21:29 +00:00
rustc_codegen_llvm Rollup merge of #137210 - workingjubilee:fixup-passmode-import, r=RalfJung 2025-02-19 01:30:12 +01:00
rustc_codegen_ssa Rollup merge of #137213 - nnethercote:rm-rustc_middle-mir-tcx, r=compiler-errors 2025-02-19 01:30:13 +01:00
rustc_const_eval Rollup merge of #135296 - lukas-code:dyn-leak-check, r=compiler-errors 2025-02-20 00:55:11 +01:00
rustc_data_structures Enforce T: Hash for Interned<...> 2025-02-17 21:35:52 -05:00
rustc_driver
rustc_driver_impl Rollup merge of #137151 - Urgau:register-more-signals, r=workingjubilee 2025-02-18 18:34:14 +01:00
rustc_error_codes fix: Alloc new errorcode E0803 for E0495 2025-02-15 12:18:30 +08:00
rustc_error_messages tree-wide: parallel: Fully removed all Lrc, replaced with Arc 2025-02-03 13:25:57 +03:00
rustc_errors Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_expand Reject macro calls inside of #![crate_name] 2025-02-15 16:47:30 +01:00
rustc_feature Rename pattern_complexity attr as pattern_complexity_limit. 2025-02-17 09:30:40 +11:00
rustc_fluent_macro use tracked_path in rustc_fluent_macro 2024-10-19 22:32:38 +08:00
rustc_fs_util Couple of changes to make it easier to compile rustc for wasm 2024-09-26 19:51:14 +00:00
rustc_graphviz Rustfmt 2025-02-08 22:12:13 +00:00
rustc_hashes Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_hir Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_hir_analysis Rollup merge of #137206 - estebank:e0599-structured, r=jieyouxu 2025-02-18 18:40:54 +01:00
rustc_hir_pretty Overhaul the intravisit::Map trait. 2025-02-17 13:21:35 +11:00
rustc_hir_typeck Rollup merge of #137206 - estebank:e0599-structured, r=jieyouxu 2025-02-18 18:40:54 +01:00
rustc_incremental Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_index Rustfmt 2025-02-08 22:12:13 +00:00
rustc_index_macros update rustc_index_macros feature handling 2024-12-19 20:32:12 +03:00
rustc_infer Rustfmt 2025-02-08 22:12:13 +00:00
rustc_interface Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_lexer Rustfmt 2025-02-08 22:12:13 +00:00
rustc_lint Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_lint_defs Implement lint for definition site item shadowing too 2025-02-13 05:45:53 +00:00
rustc_llvm Quote embedded codeview command line arguments 2025-02-14 11:02:28 +01:00
rustc_log Avoid naming variables str 2025-01-07 14:30:02 +02:00
rustc_macros Rustfmt 2025-02-08 22:12:13 +00:00
rustc_metadata Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_middle Auto merge of #136539 - matthewjasper:late-normalize-errors, r=compiler-errors 2025-02-19 07:49:08 +00:00
rustc_mir_build Rollup merge of #137213 - nnethercote:rm-rustc_middle-mir-tcx, r=compiler-errors 2025-02-19 01:30:13 +01:00
rustc_mir_dataflow Rollup merge of #137213 - nnethercote:rm-rustc_middle-mir-tcx, r=compiler-errors 2025-02-19 01:30:13 +01:00
rustc_mir_transform Rollup merge of #137251 - Zalathar:holes-visitor, r=jieyouxu 2025-02-19 18:52:10 +01:00
rustc_monomorphize Move some Map methods onto TyCtxt. 2025-02-17 13:21:02 +11:00
rustc_next_trait_solver Auto merge of #137235 - matthiaskrgr:rollup-2kjua2t, r=matthiaskrgr 2025-02-18 21:08:58 +00:00
rustc_parse Fix const items not being allowed to be called r#move or r#static 2025-02-16 18:21:40 +01:00
rustc_parse_format Rustfmt 2025-02-08 22:12:13 +00:00
rustc_passes Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_pattern_analysis Add pattern_complexity_limit to Limits. 2025-02-17 09:30:33 +11:00
rustc_privacy Move methods from Map to TyCtxt, part 2. 2025-02-18 10:17:44 +11:00
rustc_query_impl Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_query_system Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_resolve Enforce T: Hash for Interned<...> 2025-02-17 21:35:52 -05:00
rustc_sanitizers check the types in ty::Value to value conversion 2025-01-30 18:13:16 +01:00
rustc_serialize Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_session Auto merge of #137164 - matthiaskrgr:rollup-dj5826k, r=matthiaskrgr 2025-02-17 11:18:33 +00:00
rustc_smir Auto merge of #136593 - lukas-code:ty-value-perf, r=oli-obk 2025-02-13 15:27:30 +00:00
rustc_span add last std diagnostic items for clippy 2025-02-18 10:54:37 +01:00
rustc_symbol_mangling Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
rustc_target Rollup merge of #137094 - RalfJung:softfloat-means-no-simd, r=tgross35 2025-02-19 18:52:07 +01:00
rustc_trait_selection Rollup merge of #137232 - estebank:from-residual-note, r=petrochenkov 2025-02-19 18:52:08 +01:00
rustc_traits Clean up dropck code a bit 2025-02-17 11:33:07 +00:00
rustc_transmute transmutability: fix ICE when passing wrong ADT to ASSUME 2025-02-08 05:44:29 +01:00
rustc_ty_utils cosmetic changes 2025-02-18 13:22:46 +01:00
rustc_type_ir Rollup merge of #137211 - lcnr:alias-relate-accept-error, r=compiler-errors 2025-02-18 18:34:17 +01:00
rustc_type_ir_macros Rustfmt 2025-02-08 22:12:13 +00:00
stable_mir Extend the renaming to coerce_unsafe_ptr 2025-02-10 13:01:55 +00:00