Skip to content
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

UB: matching on const value with mir-opt-level=3 #107818

Closed
matthiaskrgr opened this issue Feb 8, 2023 · 2 comments · Fixed by #107851
Closed

UB: matching on const value with mir-opt-level=3 #107818

matthiaskrgr opened this issue Feb 8, 2023 · 2 comments · Fixed by #107851
Assignees
Labels
A-mir-opt Area: MIR optimizations C-bug Category: This is a bug.

Comments

@matthiaskrgr
Copy link
Member

I tried this code:

#[derive(PartialEq, Eq, Clone)]
struct Foo {
    field: i64
}

const FOO: Foo = Foo {
    field: 5,
};

fn main() {
    match FOO {
        FOO => unreachable!(),
        _ => unreachable!(),
    }
}

When I run miri on this with mir-opt-level=3, miri reports UB, maybe some of the mir-opts is a bit broken?

MIRIFLAGS="-Zmir-opt-level=3 -Zmiri-backtrace=full" cargo miri run

error: Undefined Behavior: constructing invalid value: encountered uninitialized memory, but expected an integer
  --> src/main.rs:11:11
   |
11 |     match FOO {
   |           ^^^ constructing invalid value: encountered uninitialized memory, but expected an integer
   |
   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
   = note: BACKTRACE:
   = note: inside `main` at src/main.rs:11:11: 11:14
   = note: inside `<fn() as std::ops::FnOnce<()>>::call_once - shim(fn())` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5: 250:71
   = note: inside `std::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:121:18: 121:21
   = note: inside closure at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:166:18: 166:82
   = note: inside `std::ops::function::impls::<impl std::ops::FnOnce<()> for &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>::call_once` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:287:13: 287:31
   = note: inside `std::panicking::r#try::do_call::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:483:40: 483:43
   = note: inside `std::panicking::r#try::<i32, &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:447:19: 447:81
   = note: inside `std::panic::catch_unwind::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:140:14: 140:33
   = note: inside closure at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:148:48: 148:73
   = note: inside `std::panicking::r#try::do_call::<[closure@std::rt::lang_start_internal::{closure#2}], isize>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:483:40: 483:43
   = note: inside `std::panicking::r#try::<isize, [closure@std::rt::lang_start_internal::{closure#2}]>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:447:19: 447:81
   = note: inside `std::panic::catch_unwind::<[closure@std::rt::lang_start_internal::{closure#2}], isize>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:140:14: 140:33
   = note: inside `std::rt::lang_start_internal` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:148:20: 148:98
   = note: inside `std::rt::lang_start::<()>` at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:165:17: 170:6

error: aborting due to previous error
@matthiaskrgr matthiaskrgr added C-bug Category: This is a bug. A-mir-opt Area: MIR optimizations labels Feb 8, 2023
@matthiaskrgr matthiaskrgr changed the title UB: matching on enum with mir-opt-level=3 UB: matching on const value with mir-opt-level=3 Feb 8, 2023
@matthiaskrgr
Copy link
Member Author

matthiaskrgr commented Feb 8, 2023

another one:

fn test<F>(f: F) -> usize where F: FnOnce(usize) -> usize {
    return f(22);
}

pub fn main() {
    let y = test(|x| 4 * x);
    assert_eq!(y, 88);
}
error: Undefined Behavior: constructing invalid value: encountered uninitialized memory, but expected an integer
 --> src/main.rs:2:12
  |
2 |     return f(22);
  |            ^^^^^ constructing invalid value: encountered uninitialized memory, but expected an integer
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
  = note: BACKTRACE:
  = note: inside `main` error: Undefined Behavior: constructing invalid value: encountered uninitialized memory, but expected an integer

@cjgillot cjgillot self-assigned this Feb 8, 2023
@bors bors closed this as completed in 585f3ee Feb 11, 2023
RalfJung pushed a commit to RalfJung/miri that referenced this issue Feb 15, 2023
Put deaggregated statements after original constant.

Fixes rust-lang/rust#107818
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants