Skip to content

Commit d049f5d

Browse files
committed
Auto merge of #141759 - 1c3t3a:discriminants-query, r=saethlin
Insert checks for enum discriminants when debug assertions are enabled Similar to the existing null-pointer 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 rust-lang/rust#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@. r? `@saethlin`
2 parents a28aa95 + ac8ce3e commit d049f5d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/base.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,18 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
407407
source_info.span,
408408
)
409409
}
410+
AssertKind::InvalidEnumConstruction(source) => {
411+
let source = codegen_operand(fx, source).load_scalar(fx);
412+
let location = fx.get_caller_location(source_info).load_scalar(fx);
413+
414+
codegen_panic_inner(
415+
fx,
416+
rustc_hir::LangItem::PanicInvalidEnumConstruction,
417+
&[source, location],
418+
*unwind,
419+
source_info.span,
420+
)
421+
}
410422
_ => {
411423
let location = fx.get_caller_location(source_info).load_scalar(fx);
412424

0 commit comments

Comments
 (0)