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

-Zvalidate-mir: check that all referenced locals exist #73356

Closed
jonas-schievink opened this issue Jun 14, 2020 · 11 comments · Fixed by #79031
Closed

-Zvalidate-mir: check that all referenced locals exist #73356

jonas-schievink opened this issue Jun 14, 2020 · 11 comments · Fixed by #79031
Assignees
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jonas-schievink
Copy link
Contributor

jonas-schievink commented Jun 14, 2020

When encountering a Local, there must be a corresponding LocalDecl in the MIR body. If there isn't, later passes can panic due to indexing out of bounds of the LocalDecl array. The validation pass could detect this problem earlier.

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html labels Jun 14, 2020
@jonas-schievink
Copy link
Contributor Author

Instructions:

Extend the validation pass in src/librustc_mir/transform/validate.rs by implementing visit_local for the TypeChecker MIR visitor:

impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {

The Visitor trait is documented here.

The method should check that the local exists in self.body.local_decls, and fail with a descriptive error message if not (by calling self.fail, not by panicking directly).

@jonas-schievink jonas-schievink added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jun 14, 2020
@yerke
Copy link
Contributor

yerke commented Jun 14, 2020

@rustbot claim

@rustbot rustbot self-assigned this Jun 14, 2020
@jonas-schievink
Copy link
Contributor Author

@yerke FYI, the validation pass currently fails on even libcore until #73175 is merged, so you need to either wait for that, or cherry-pick some commits from there to get it to work.

You can test your changes by running RUSTFLAGS_NOT_BOOTSTRAP="-Zvalidate-mir" ./x.py test --stage=1 src/test/ui (the validator only runs on very little code by default, -Zvalidate-mir turns it on for everything).

@yerke
Copy link
Contributor

yerke commented Jun 15, 2020

@jonas-schievink I wanted to double check, running RUSTFLAGS_NOT_BOOTSTRAP="-Zvalidate-mir" ./x.py test --stage=1 src/test/ui should fail when I run it, right? It actually passed for me after I merged in your branch from #73359.
I wonder if I should try clean and build stage 1 again. :\

@jonas-schievink
Copy link
Contributor Author

It should pass with #73359 applied, but fail on current master.

@yerke
Copy link
Contributor

yerke commented Jun 15, 2020

@jonas-schievink So your PR fixes this issue (#73356) as well?

@jonas-schievink
Copy link
Contributor Author

Ah, no, this issue is just about adding additional validation. It shouldn't actually detect any locals that don't exist. So the command should work both before and after implementing this. If it fails after implementing this, something else is going on that we should look into.

@pitaj
Copy link
Contributor

pitaj commented Oct 15, 2020

@yerke are you still working on this?

@yerke
Copy link
Contributor

yerke commented Oct 15, 2020

No, sorry. Didn’t have time for it. Go ahead.

@jonas-schievink
Copy link
Contributor Author

@rustbot release-assignment

@rustbot rustbot removed their assignment Nov 7, 2020
@natsukagami
Copy link

Would like to work on this, but it seems there is another check, not on the declaration like this one, but on the memory liveness. This should cover the case that the local is not declared (I think), or would it not?

// Uses of locals must occur while the local's storage is allocated.
self.storage_liveness.seek_after_primary_effect(location);
let locals_with_storage = self.storage_liveness.get();
if !locals_with_storage.contains(*local) {
self.fail(location, format!("use of local {:?}, which has no storage here", local));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants