Skip to content

emit fake reads for indexing after every bounds-check - #161857

Open
dianne wants to merge 1 commit into
rust-lang:mainfrom
dianne:quadratic-fake-reads
Open

emit fake reads for indexing after every bounds-check#161857
dianne wants to merge 1 commit into
rust-lang:mainfrom
dianne:quadratic-fake-reads

Conversation

@dianne

@dianne dianne commented Aug 27, 2026

Copy link
Copy Markdown
Member

To fix #161852, we need fake borrows on indexed-into slice pointers to live through subsequent bounds-checks. This PR does that.

The following is now rejected:

fn main() {
    let mut x: &[&[&[u32]]] = &[&[&[0]]];
    let y: &[&[&[u32]]] = &[];
    x[0][{ x = y; 0 }][{ return; 0 }];
    //~^ ERROR: cannot assign `x` in indexing expression
}

This does not affect locations from which bounds-checks are unreachable. The following was already and is still accepted:

fn main() {
    let mut x: &[&[&[u32]]] = &[&[&[0]]];
    let y: &[&[&[u32]]] = &[];
    x[0][{ x = y; return; 0 }];
}

Similar to #161581, I imagine if we want to be stricter about that, we'll either need fake reads at any point we diverge or some other mechanism for keeping fake borrows alive that isn't as sensitive to CFG structure.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 27, 2026
Comment on lines -648 to +651
if is_outermost_index {
self.read_fake_borrows(block, fake_borrow_temps, source_info)
} else {
// Keep all fake borrows we've collected so far alive. If we only emitted fake reads at the
// end, diverging within an index expression could make them unreachable. This would allow
// bounds checks to perform out-of-bounds accesses (#161852).
self.read_fake_borrows(block, fake_borrow_temps, source_info);

@dianne dianne Aug 27, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes the total number of fake reads quadratic in how much nested slice indexing we do, but hopefully that'd only matter in stress tests? Surprisingly, no MIR building tests needed blessing, so I don't have an example to point to.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs 3 or more nested array/slice indexes, so it's not that surprising that no tests are affected. It definitely doesn't seem likely that there's any real code that's going to have large enough numbers of nested indexes for this to have a significant impact.

@dianne

dianne commented Aug 27, 2026

Copy link
Copy Markdown
Member Author

r? @matthewjasper

Like #161581, I'm not fully sure where this falls between lang and types in terms of design responsibility, but I'll start by lang-nominating it alongside that. The ask is equivalent: from a user's perspective, what's the language property we're expressing with fake borrows in indexing expressions? Does this approach fit that, or should we be stricter?

@dianne
dianne marked this pull request as ready for review August 27, 2026 09:25
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 27, 2026
@dianne dianne added the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 27, 2026

@matthewjasper matthewjasper left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code here looks good. I'll wait for lang to discuss.

The original motivation for the fake borrows here was to make a minimal change to (try to) fix the soundness issue. I considered mimicking the borrows done by the Index trait, but I think that was too restrictive and broke too much code.

View changes since this review

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

Labels

I-lang-nominated Nominated for discussion during a lang team meeting. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

indexing expressions can ignore fake borrows when an index diverges

3 participants