Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign up`Drop` and leaking `&mut` references #31567
Comments
nagisa
added
I-wrong
I-nominated
labels
Feb 11, 2016
This comment has been minimized.
This comment has been minimized.
|
The behaviour feels somewhat intuitive to me, but is certainly a memory safety issue. Nominating. cc @rust-lang/lang, I think? EDIT: affects MIR as well, without an obvious fix. |
arielb1
added
I-unsound 💥
and removed
I-wrong
labels
Feb 11, 2016
This comment has been minimized.
This comment has been minimized.
|
Minified: struct VecWrapper<'a>(&'a mut S);
struct S(Box<u32>);
fn get_dangling<'a>(v: VecWrapper<'a>) -> &'a u32 {
let s_inner: &'a S = &*v.0;
&s_inner.0
}
impl<'a> Drop for VecWrapper<'a> {
fn drop(&mut self) {
*self.0 = S(Box::new(0));
}
}
fn main() {
let mut s = S(Box::new(11));
let vw = VecWrapper(&mut s);
let dangling = get_dangling(vw);
println!("{}", dangling);
}MIR borrowck would of course catch
|
This comment has been minimized.
This comment has been minimized.
|
Here's another minification (playground) struct DropSet<'a>(&'a mut u8);
impl<'a> DropSet<'a> {
fn get(self) -> &'a u8 {
self.0
}
}
impl<'a> Drop for DropSet<'a> {
fn drop(&mut self) {
*self.0 = 0;
}
}
fn main() {
let mut x = 64;
let s = DropSet(&mut x);
let r = s.get();
println!("{}", r);
} |
This comment has been minimized.
This comment has been minimized.
|
I suppose we can leave this to fix-by-MIR-borrowck. |
pnkfelix
added
the
T-compiler
label
Feb 11, 2016
This comment has been minimized.
This comment has been minimized.
|
The problem (I think) is the special case rule in the borrow checker concerning |
This comment has been minimized.
This comment has been minimized.
|
triage: P-high |
rust-highfive
added
P-high
and removed
I-nominated
labels
Feb 11, 2016
nikomatsakis
assigned
pnkfelix
Feb 11, 2016
pnkfelix
referenced this issue
Apr 25, 2016
Closed
Tracking issue for borrows outliving owners with destructors #33206
This comment has been minimized.
This comment has been minimized.
|
so, just a quick note on some ways @nikomatsakis and I have talked about resolving this:
|
pnkfelix
added a commit
to pnkfelix/rust
that referenced
this issue
Apr 26, 2016
pnkfelix
added a commit
to pnkfelix/rust
that referenced
this issue
Apr 26, 2016
This comment has been minimized.
This comment has been minimized.
Surely you mean distinguish between a type that has a destructor and a type that may implement |
This comment has been minimized.
This comment has been minimized.
|
@arielb1 yes I just meant the more precise analysis needs to use the appropriate predicate (i.e. that the type implements |
This comment has been minimized.
This comment has been minimized.
|
I think we can just make it that a MIR |
This comment has been minimized.
This comment has been minimized.
|
@pnkfelix and I were talking. I think this can be cleanly integrated into regionck -- in fact, some of the rules that are enforced in borrowck (I suspect) ought to moved to regionck. I'm going to try and tinker a bit here. |
This comment has been minimized.
This comment has been minimized.
|
@rust-lang/compiler can you assign someone to this P-high bug? |
brson
added
the
regression-from-stable-to-nightly
label
Jun 23, 2016
This comment has been minimized.
This comment has been minimized.
|
I tagged this a regression from a quick scan, but not sure. |
This comment has been minimized.
This comment has been minimized.
|
Ah, felix is assigned. Ignore me. |
This comment has been minimized.
This comment has been minimized.
|
@brson "stable", "nightly" or "version" do not appear in any comment here and the only mention of a regression is @pnkfelix discussing a potential solution. |
This comment has been minimized.
This comment has been minimized.
|
I can reproduce this all the way back to 1.0.0, at least. |
brson
removed
the
regression-from-stable-to-nightly
label
Jun 23, 2016
This comment has been minimized.
This comment has been minimized.
|
I suspect I won't be able to address this myself in this cycle, so keeping this at P-high is not a great idea. @arielb1 suggests we should let this be fixed by switching to MIR-based borrowck. |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
modified the milestones:
NLL Future Compat Warnings,
NLL run-pass without ICEs,
NLL soundness violations
Jan 16, 2018
nikomatsakis
assigned
nikomatsakis
and unassigned
pnkfelix
Feb 27, 2018
This comment has been minimized.
This comment has been minimized.
|
Fixed by #48411 |
cuviper commentedFeb 11, 2016
There's a way to leak a
&mutmember borrowed from a struct, and then manipulate that member from theDrophandler -- even while it is immutably borrowed!playground output:
So this drop frees the vec's buffer, then an unrelated string allocation reuses the same memory, and those new values come out of the
vec::Iter.I think if the member was immutable
&, then this "leak" frominto_iter()would be fine since they can both share that reference, andDropcan't mutate it. But&mutmust be exclusive, of course...