diff options
| author | Bastian Kersting <bkersting@google.com> | 2025-05-15 19:09:13 +0000 |
|---|---|---|
| committer | Bastian Kersting <bkersting@google.com> | 2025-06-27 09:37:36 +0000 |
| commit | 1087042e22ae3d14ff856f621a84eaa3e15cc537 (patch) | |
| tree | 628081274946cecb6c9cb17ab87a2185ccc40e12 /compiler/rustc_mir_transform/src/lib.rs | |
| parent | 40daf23eeb711dadf140b2536e67e3ff4c999196 (diff) | |
| download | rust-1087042e22ae3d14ff856f621a84eaa3e15cc537.tar.gz rust-1087042e22ae3d14ff856f621a84eaa3e15cc537.zip | |
Insert checks for enum discriminants when debug assertions are enabled
Similar to the existing nullpointer and alignment checks, this checks
for valid enum discriminants on creation of enums through unsafe
transmutes. Essentially this sanitizes patterns like the following:
```rust
let val: MyEnum = unsafe { std::mem::transmute<u32, MyEnum>(42) };
```
An extension of this check will be done in a follow-up that explicitly
sanitizes for extern enum values that come into Rust from e.g. C/C++.
This check is similar to Miri's capabilities of checking for valid
construction of enum values.
This PR is inspired by saethlin@'s PR
https://github.com/rust-lang/rust/pull/104862. Thank you so much for
keeping this code up and the detailed comments!
I also pair-programmed large parts of this together with vabr-g@.
Diffstat (limited to 'compiler/rustc_mir_transform/src/lib.rs')
| -rw-r--r-- | compiler/rustc_mir_transform/src/lib.rs | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 572ad585c8c..6b32254b051 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -117,6 +117,7 @@ declare_passes! { mod check_inline : CheckForceInline; mod check_call_recursion : CheckCallRecursion, CheckDropRecursion; mod check_alignment : CheckAlignment; + mod check_enums : CheckEnums; mod check_const_item_mutation : CheckConstItemMutation; mod check_null : CheckNull; mod check_packed_ref : CheckPackedRef; @@ -666,6 +667,7 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<' // Add some UB checks before any UB gets optimized away. &check_alignment::CheckAlignment, &check_null::CheckNull, + &check_enums::CheckEnums, // Before inlining: trim down MIR with passes to reduce inlining work. // Has to be done before inlining, otherwise actual call will be almost always inlined. |
