Skip to content

NormalizeArrayLen MIR opt unsound due to missed writes #105929

@JakobDegen

Description

@JakobDegen
Contributor

The opt, as written today, will miscompile something that looks like this:

slice_ptr = Cast(array_ptr);
r = &mut slice_ptr;
*r = something_else;
Len(slice_ptr)

The bug is roughly on this line:

This makes an attempt to detect other writes to the slice_ptr local, but it misses a lot of cases. Taking a reference like in the example above is an easy one, but SetDiscriminant is also missed, as well as writes to any aliasing pointers.

As far as I can tell this needs Rvalue::Cast support in custom MIR to reproduce.

@rustbot label A-mir-opt T-compiler requires-nightly

Activity

added
A-mir-optArea: MIR optimizations
requires-nightlyThis issue requires a nightly compiler in some way.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Dec 20, 2022
shamatar

shamatar commented on Dec 20, 2022

@shamatar
Contributor

This optimization is more a workaround of unwillingness to add "len()" function to arrays, so method resolution would not need to use coercion to slice, but I'll check what can be done to fix it

added a commit that references this issue on Dec 20, 2022
c3af456
added a commit that references this issue on Jan 30, 2023
db97749
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.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @shamatar@rustbot@JakobDegen

      Issue actions

        `NormalizeArrayLen` MIR opt unsound due to missed writes · Issue #105929 · rust-lang/rust