Skip to content

Insert checks for enum discriminants when debug assertions are enabled #141759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

1c3t3a
Copy link
Member

@1c3t3a 1c3t3a commented May 30, 2025

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:

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
#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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 30, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @vakaras

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@rustbot

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 6d3fe75 to a7dd718 Compare May 30, 2025 09:46
@rustbot

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from a7dd718 to 4f3342e Compare May 30, 2025 09:59
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 54b6e74 to b03960e Compare May 30, 2025 13:33
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from b03960e to 228b656 Compare May 30, 2025 13:59
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 228b656 to d1d8f88 Compare June 2, 2025 14:34
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from d1d8f88 to 93b24d7 Compare June 2, 2025 20:23
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 93b24d7 to c2a8415 Compare June 3, 2025 12:31
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from c2a8415 to d769d6b Compare June 4, 2025 01:51
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from d769d6b to 68665ad Compare June 4, 2025 02:32
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 68665ad to 1225079 Compare June 6, 2025 15:35
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 1225079 to c52f534 Compare June 6, 2025 19:54
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from c52f534 to 9983dc0 Compare June 6, 2025 20:26
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 9983dc0 to 63e93e1 Compare June 7, 2025 04:33
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 63e93e1 to 4006db6 Compare June 7, 2025 05:23
@rustbot
Copy link
Collaborator

rustbot commented Jun 7, 2025

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch 2 times, most recently from 28c4056 to 15dc498 Compare June 8, 2025 19:09
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 15dc498 to 06f752d Compare June 8, 2025 20:07
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 06f752d to db4a7f9 Compare June 10, 2025 09:32
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from db4a7f9 to 33e8914 Compare June 10, 2025 09:47
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 33e8914 to 8fd6814 Compare June 10, 2025 11:41
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the discriminants-query branch from 8fd6814 to d37a37e Compare June 10, 2025 14:35
@rust-log-analyzer

This comment has been minimized.

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
rust-lang#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@.
@1c3t3a 1c3t3a force-pushed the discriminants-query branch from d37a37e to 33890a1 Compare June 10, 2025 15:18
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-19 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#18 exporting to docker image format
#18 sending tarball 27.3s done
#18 DONE 35.1s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-19]
[CI_JOB_NAME=x86_64-gnu-llvm-19]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Listening on address 127.0.0.1:4226
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-19', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'build.print-step-timings', '--enable-verbose-tests', '--set', 'build.metrics', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--set', 'gcc.download-ci-gcc=true', '--enable-new-symbol-mangling']
configure: build.build          := x86_64-unknown-linux-gnu
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-19/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
test [ui] tests/ui/mir/enable_passes_validation.rs#all_known ... ok
test [ui] tests/ui/mir/enable_passes_validation.rs#unprefixed ... ok
test [ui] tests/ui/mir/enable_passes_validation.rs#all_unknown ... ok
test [ui] tests/ui/mir/enable_passes_validation.rs#mixed ... ok
test [ui] tests/ui/mir/enum/convert_non_enum_niche_ok.rs ... ok
test [ui] tests/ui/mir/enum/convert_non_enum_ok.rs ... ok
test [ui] tests/ui/mir/enum/niche_option_tuple_break.rs ... ok
test [ui] tests/ui/mir/enum/niche_option_tuple_ok.rs ... ok
test [ui] tests/ui/mir/enum/numbered_variants_ok.rs ... ok
test [ui] tests/ui/mir/enum/convert_non_enum_niche_break.rs ... ok
test [ui] tests/ui/mir/enum/convert_non_enum_break.rs ... ok
test [ui] tests/ui/mir/enum/option_with_bigger_niche_break.rs ... ok
test [ui] tests/ui/mir/enum/option_with_bigger_niche_ok.rs ... ok
test [ui] tests/ui/mir/enum/single_ok.rs ... ok
test [ui] tests/ui/mir/enum/plain_no_data_ok.rs ... ok
test [ui] tests/ui/mir/enum/numbered_variants_break.rs ... ok
test [ui] tests/ui/mir/enum/single_with_repr_ok.rs ... ok
test [ui] tests/ui/mir/enum/with_niche_int_ok.rs ... ok
test [ui] tests/ui/mir/enum/plain_no_data_break.rs ... ok
test [ui] tests/ui/mir/enum/with_niche_ptr_ok.rs ... FAILED
test [ui] tests/ui/mir/enum/wrap_ok.rs ... ok
test [ui] tests/ui/mir/enum/with_niche_int_break.rs ... ok
test [ui] tests/ui/mir/enum/single_with_repr_break.rs ... ok
test [ui] tests/ui/mir/field-projection-invariant.rs ... ok
test [ui] tests/ui/mir/enum/wrap_break.rs ... ok
---
test [ui] tests/ui/zero-sized/zero-sized-tuple-struct.rs ... ok

failures:

---- [ui] tests/ui/mir/enum/with_niche_ptr_ok.rs stdout ----

error: test compilation failed although it shouldn't!
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/mir/enum/with_niche_ptr_ok.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=i686-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/mir/enum/with_niche_ptr_ok" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/i686-unknown-linux-gnu/native/rust-test-helpers" "-Clinker=x86_64-linux-gnu-gcc" "-C" "debug-assertions"
stdout: none
--- stderr -------------------------------
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
##[error]  --> /checkout/tests/ui/mir/enum/with_niche_ptr_ok.rs:21:25
   |
LL |     let _val = unsafe { std::mem::transmute::<u64, Option<unsafe extern "C" fn()>>(0) };
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: source type: `u64` (64 bits)
   = note: target type: `Option<unsafe extern "C" fn()>` (32 bits)

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
##[error]  --> /checkout/tests/ui/mir/enum/with_niche_ptr_ok.rs:22:25
   |
LL |     let _val = unsafe { std::mem::transmute::<u64, Option<unsafe extern "C" fn()>>(1) };
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: source type: `u64` (64 bits)
   = note: target type: `Option<unsafe extern "C" fn()>` (32 bits)

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
##[error]  --> /checkout/tests/ui/mir/enum/with_niche_ptr_ok.rs:23:25
   |
LL |     let _val = unsafe { std::mem::transmute::<u64, Option<unsafe extern "C" fn()>>(u64::MAX) };
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: source type: `u64` (64 bits)
   = note: target type: `Option<unsafe extern "C" fn()>` (32 bits)

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0512`.
------------------------------------------



failures:
    [ui] tests/ui/mir/enum/with_niche_ptr_ok.rs

test result: FAILED. 18878 passed; 1 failed; 366 ignored; 0 measured; 0 filtered out; finished in 538.02s

Some tests failed in compiletest suite=ui pass_mode=check mode=ui host=x86_64-unknown-linux-gnu target=i686-unknown-linux-gnu
Build completed unsuccessfully in 0:10:37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants