Skip to content

Panic in nightly 1.83.0 and 1.84.0 with opt-level >= 1 when unwrapping Some variant #132353

Closed

Description

@Specy

When calling an unwrap on a value that should be Some, i instead get an unwrap on None error. Attaching a debugger seems to show an invalid memory error.

This issue happens only when opt-level is set to at least 1, (aka in dev profile, no panic happens, and in release it does), and only happens in rust nightly 1.83.0 and 1.84.0, it does not happen on stable and nightly 1.82.0.

I'm not sure if the issue is in my code or in the sprs crate, i've filed an issue there and also here just to make sure.
EDIT: managed to narrow down the bug by removing the sprs crate, this is only pure rust

Here is the repo for minimum example or in the playground, there is an even smaller reproduction in the comments

To reproduce, run cargo run --release which will panic, while if running cargo run it won't panic

I've tried to run this with miri, nothing there. Also tried to run the release mode with bounds checking turned on, but nothing changed

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (3f1be1ec7 2024-10-28)
binary: rustc
commit-hash: 3f1be1ec7ec3d8e80beb381ee82164a0aa3ca777
commit-date: 2024-10-28
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1
Backtrace

thread 'main' panicked at src/main.rs:122:27:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: rust_begin_unwind
             at /rustc/3f1be1ec7ec3d8e80beb381ee82164a0aa3ca777/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/3f1be1ec7ec3d8e80beb381ee82164a0aa3ca777/library/core/src/panicking.rs:75:14
   2: core::panicking::panic
             at /rustc/3f1be1ec7ec3d8e80beb381ee82164a0aa3ca777/library/core/src/panicking.rs:152:5
   3: core::option::unwrap_failed
             at /rustc/3f1be1ec7ec3d8e80beb381ee82164a0aa3ca777/library/core/src/option.rs:2008:5
   4: microlp::order_simple
   5: microlp::main
             at ./src/main.rs:152:5
   6: core::ops::function::FnOnce::call_once
             at /home/specy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 30, 2024
  2. changed the title [-]UB when using crate sprs in nightly 1.83.0 and 1.84.0 with opt-level >= 1[/-] [+]Panic in nightly 1.83.0 and 1.84.0 with opt-level >= 1 when unwrapping Some variant[/+] on Oct 30, 2024
  3. theemathas commented on Oct 30, 2024

    @theemathas
  4. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 30, 2024
  5. added
    I-miscompileIssue: Correct Rust code lowers to incorrect machine code
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 30, 2024
  6. tmiasko commented on Oct 30, 2024

    @tmiasko
    Contributor

    cargo-bisect-rustc suggest that regression is in e7386b3 (cc @dianqk)

  7. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-mir-optArea: MIR optimizations
    on Oct 30, 2024
  8. theemathas commented on Oct 30, 2024

    @theemathas
    Contributor

    Minimized:

    fn pop_min(mut score2head: Vec<Option<usize>>) -> Option<usize> {
        loop {
            if let Some(col) = score2head[0] {
                score2head[0] = None;
                return Some(col);
            }
        }
    }
    
    fn main() {
        let min = pop_min(vec![Some(1)]);
        println!("min: {:?}", min);
        // panic happens here on beta in release mode
        min.unwrap();
    }
  9. jieyouxu commented on Oct 30, 2024

    @jieyouxu
    Member

    Working on a revert for e7386b3, we can reland that mir-opt with this fixed.

  10. apiraino commented on Oct 30, 2024

    @apiraino
    Contributor

    WG-prioritization assigning priority (Zulip discussion).

    @rustbot label -I-prioritize +P-critical

  11. 32 remaining items

  12. matthiaskrgr commented on Nov 19, 2024

    @matthiaskrgr
    Member

    about the corpus, the rust repo and all its history are around 25 gb of code already, seems like a good start :)

    for i in `git rev-list --objects --all  | grep "\.rs"   | cut -d' ' -f1` ; do ; git cat-file -p $i > ${i}.rs ; done
    
  13. saethlin commented on Nov 19, 2024

    @saethlin
    Member

    This is now a discussion of how to fuzz rustc, which is much better had elsewhere.

    I believe that @cbeuw did that kind of fuzzing, but I don't think its still running?

    His dissertation https://dl.acm.org/doi/pdf/10.1145/3689780 says it took 10 CPU-years of fuzzing to uncover 22 new bugs. That's low enough yield that this needs to run on a server somewhere, not in someone's dev environment (much less a shared dev environment).

    Infra may be willing to help run it, but someone would need to do the work to maintain the codebase.

  14. RalfJung commented on Nov 22, 2024

    @RalfJung
    Member

    Sadly, for mir-opt bugs like this, the best defense we have is still to be really extra super careful when writing and reviewing them. Fuzzing and other tooling can catch some bugs, but not nearly enough.

    Infra may be willing to help run it, but someone would need to do the work to maintain the codebase.

    More importantly, someone needs to sift through the reports generated by the tool, and turn them into useful bug reports.

  15. added this to the 1.83.0 milestone on Nov 22, 2024
  16. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    and removed on Dec 3, 2024
  17. added 2 commits that reference this issue on Dec 4, 2024
    b09bd29
    bc3f26c
  18. dianqk commented on Dec 19, 2024

    @dianqk
    Member

    kani is not what I want. I'm writing an alive2-like tool for MIR, but I can't guarantee when it will be completed.

  19. added 2 commits that reference this issue on Dec 20, 2024
    cfae26d
    0754bc9
  20. added 2 commits that reference this issue on Apr 2, 2025
    7f4f652
    00095b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-mir-optArea: MIR optimizationsA-mir-opt-GVNArea: MIR opt Global Value Numbering (GVN)C-bugCategory: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions