Skip to content

InstCombine introduces an incorrect use of a local after its storage has ended #78192

Closed

Description

@tmiasko
pub fn f<T>(a: &T) -> *const T {
    let b: &*const T = &(a as *const T);
    *b
}
--- mir_dump/a.f.005-003.InstCombine.before.mir
+++ mir_dump/a.f.005-003.InstCombine.after.mir
@@ -1,2 +1,2 @@
-// MIR for `f` before InstCombine
+// MIR for `f` after InstCombine
 
@@ -18,5 +18,5 @@
         _3 = &_4;                        // scope 0 at a.rs:2:24: 2:40
-        _2 = &(*_3);                     // scope 0 at a.rs:2:24: 2:40
+        _2 = _3;                         // scope 0 at a.rs:2:24: 2:40
         StorageDead(_3);                 // scope 0 at a.rs:2:40: 2:41
-        _0 = (*_2);                      // scope 1 at a.rs:3:5: 3:7
+        _0 = (*_3);                      // scope 1 at a.rs:3:5: 3:7
         StorageDead(_4);                 // scope 0 at a.rs:4:1: 4:2

Found by MIR validator #77369 & #78147.

cc @simonvandel #76683

Activity

  1. added
    A-mir-optArea: MIR optimizations
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 21, 2020
  2. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 21, 2020
  3. tmiasko commented on Oct 21, 2020

    @tmiasko
    ContributorAuthor

    I will open a PR to disable problematic part of InstCombine for now.

  4. LeSeulArtichaut commented on Oct 21, 2020

    @LeSeulArtichaut
    Contributor

    Did you bisect the regression to #76683?

  5. added a commit that references this issue on Oct 21, 2020
  6. spastorino commented on Oct 21, 2020

    @spastorino
    Member

    I see that we've disabled the optimization, can't we also add a regression test for it? cc @tmiasko

    EDIT (camelid): See the relevant discussion in wg-prioritization.

  7. added and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 21, 2020
  8. spastorino commented on Oct 21, 2020

    @spastorino
    Member

    Assigning P-critical as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

  9. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Oct 21, 2020
  10. camelid commented on Oct 21, 2020

    @camelid
    Member

    Is the cause of the unsoundness in this code? I haven't done MIR stuff before, but it looks like to me like it's not checking that the storage is live...

    for stmt in statements_to_look_in {
    match &stmt.kind {
    // Exhaustive match on statements to detect conditions that warrant we bail out of the optimization.
    rustc_middle::mir::StatementKind::Assign(box (l, r))
    if l.local == local_being_derefed =>
    {
    match r {
    // Looking for immutable reference e.g _local_being_deref = &_1;
    Rvalue::Ref(
    _,
    // Only apply the optimization if it is an immutable borrow.
    BorrowKind::Shared,
    place_taken_address_of,
    ) => {
    self.optimizations
    .unneeded_deref
    .insert(location, *place_taken_address_of);
    return Some(());
    }

  11. 24 remaining items

  12. simonvandel commented on Oct 27, 2020

    @simonvandel
    Contributor

    I will have time to investigate this after today 1700(utc+1). If anyone finds the cause or arrives at a solution before, go ahead and open a PR.

  13. jonas-schievink commented on Oct 27, 2020

    @jonas-schievink
    Contributor

    This optimization should stay disabled by default until we're sure that it is sound.

  14. jonas-schievink commented on Oct 27, 2020

    @jonas-schievink
    Contributor

    Opened #78434 to disable it

  15. added a commit that references this issue on Oct 27, 2020
  16. added
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    and removed on Oct 27, 2020
  17. added a commit that references this issue on Nov 3, 2020
    c19620c
  18. camelid commented on Jan 23, 2021

    @camelid
    Member

    Why was this reopened and then closed?

  19. LeSeulArtichaut commented on Jan 23, 2021

    @LeSeulArtichaut
    Contributor

    I reopened it after #78434 since the bug wasn't fixed, only disabled so it doesn't hit stable. Is it fixed now @tmiasko?

  20. tmiasko commented on Jan 23, 2021

    @tmiasko
    ContributorAuthor

    I closed it because the code responsible is no longer there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions