-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Kill the storage for all locals on returning terminators #46100
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks nice! I left a few minor nits as well as a request for some new tests.
} | ||
} | ||
} | ||
_ => {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'd prefer to see the variants here written out exhaustively. My rule of thumb: if a new terminator kind is added, what is the change this code might want to change? If it is not "rather low", make it exhaustive. I rate this chance as "moderate".
mir::TerminatorKind::Resume | | ||
mir::TerminatorKind::Return | | ||
mir::TerminatorKind::GeneratorDrop => { | ||
for (borrow_index, borrow_data) in self.borrows.iter_enumerated() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a good place for a comment. Something like:
// When we return from the function, then all `ReScope`-style regions are guaranteed to have ended.
// Normally, there would be `EndRegion` statements that come before, and hence most of these loans
// will already be dead -- but, in some cases like unwind paths, we do not always emit `EndRegion`
// statements, so we add some kills here as a "backup" and to avoid spurious error messages.
src/librustc_mir/borrow_check.rs
Outdated
TerminatorKind::Resume | | ||
TerminatorKind::Return | | ||
TerminatorKind::GeneratorDrop | | ||
TerminatorKind::GeneratorDrop => { | ||
for local in self.mir.local_decls.indices() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good place for a comment, me thinks.
// Returning from the function implicitly kills storage for all locals.
// Often, the storage will already have been killed by an explicit
// StorageDead, but we don't always emit those (notably on unwind paths),
// so this "extra check" serves as a kind of backup.
src/librustc_mir/borrow_check.rs
Outdated
(&Lvalue::Local(local), span), | ||
(Shallow(None), Write(WriteKind::StorageDeadOrDrop)), | ||
flow_state | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is also aiming to check for in-scope loans of "thread-local statics" and report an error. It may be that it already prevents those. To check, can you add the following test as src/test/compile-fail/borrowck/thread-local-static-borrow-outlives-fn.rs
(I don't believe we have any such test already):
// Test that borrows of `#[thread_local]` statics cannot
// outlive the function. See also #17954.
// revisions: ast mir
#![feature(thread_local)]
#[thread_local]
static FOO: u8 = 3;
fn assert_static(_t: &'static u8) {}
fn main() {
assert_static(&FOO);
}
This currently gives a somewhat obscure error about the lifetime of a temporary value, which makes me think that it will likely generate an error under your PR as well, presuming MIR introduces a temporary at the same spot. I'm actually a bit unsure just why we are generating a temporary here -- presumably something to do with how we lower access to #[thread_local]
statics. Anyway, let's add the test. If it gets an error in both checkers, great, otherwise, we'll have to add a bit of code right around here I think to check for outstanding borrows of #[thread_local]
statics.
let x = 2; | ||
let y = &x; | ||
panic!("panic 1"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add the test that @arielb1 added to the issue and check that it generates two errors (one for each function) -- both in AST and MIR mode? A quick test with play seems to suggest that it does not generate two errors today, but it'd be good to verify that your test is not working before your patch, and works after while you are at it.
fn cplusplus_mode(x: isize) -> &'static isize { &x }
fn cplusplus_mode_exceptionally_unsafe(x: &mut Option<&'static mut isize>) {
let z = 0;
*x = Some(&mut z);
panic!("catch me for a dangling pointer!")
}
15223c1
to
15494cd
Compare
Ok, so I added two tests, both of which are failing; |
15494cd
to
768a89a
Compare
The latest force-push contains changes to the logic in checking for |
let mut z = 0; | ||
*x = Some(&mut z); //[ast]~ ERROR `z` does not live long enough | ||
//[mir]~^ ERROR `z` does not live long enough (Ast) | ||
//[mir]~| ERROR `z` does not live long enough (Mir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only problem left here now is this - MIR borrowck doesn't generate an error for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here is TerminatorKind::Call
with a None
unwind terminator, which just means that the call immediately jumps to a resume.
I feel it's a good idea to have MIR construction instead link all None
call terminators to a resume block, and have a later pass (e.g. SimplifyBranches
) clean up unwind paths that don't actually do anything so they won't choke LLVM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose you can remove this test and I'll handle the change myself.
self.access_lvalue( | ||
ContextKind::StorageDead.new(loc), | ||
(&root_lvalue, self.mir.source_info(borrow.location).span), | ||
(Deep, Write(WriteKind::StorageDeadOrDrop)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other thing I'm not sure about is whether this should be a shallow write instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a drop - basically a shallow write for structs without a destructor, and a deep write for structs with one.
This is because you want to avoid the likes of #31567 while still permitting borrowing the interior of types such as &mut
references, as in:
fn foo<'a>(x: &'a mut u32) -> &'a mut u32 {
let result : &'a mut u32 = &mut *x;
if true { result } else {
panic!() // this destroys `x`, but not `*x`
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah so this code only runs for thread-locals. In that case, I suppose there won't be much harm from it remaining a deep write.
nice PR! r=me with the cplusplus_mode_exceptionally_unsafe test removed - I'll fix it myself. |
768a89a
to
99d44aa
Compare
@bors r=arielb1 |
📌 Commit 99d44aa has been approved by |
⌛ Testing commit 99d44aa353d2dc399b695624b5138ca56c089f60 with merge ae47198f7629abf0b226f7f1565fd734213e5ffb... |
💔 Test failed - status-travis |
|
99d44aa
to
0996bb0
Compare
Rebased. |
Same error as above - I wonder if my patch is causing the temporary rvalue to be dropped too soon... |
@KiChjang rebasing won't fix the problem, it just exposes the problem on the CI early on instead of wait until |
// except according to those terms. | ||
|
||
// revisions: ast mir | ||
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the emit-end-regions flag any more btw, thanks to this very nice PR: #44717. My PR #46106 is removing them everywhere but I guess I should start spreading the word so that the line doesn't keep getting added :p.
The comment applies to the other places where you have -Z emit-end-regions as well, but I won't comment on each of them.
How are you doing debugging this? Should I check in and try to get this working? |
@bors r=nikomatsakis |
📌 Commit efa3ed2 has been approved by |
Kill the storage for all locals on returning terminators Fixes #45704.
☀️ Test successful - status-appveyor, status-travis |
Fixes #45704.