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

Trait object allowed to escape its lifetime #14285

Closed
brandonw opened this issue May 19, 2014 · 12 comments
Closed

Trait object allowed to escape its lifetime #14285

brandonw opened this issue May 19, 2014 · 12 comments

Comments

@brandonw
Copy link
Contributor

Updated description

This code should be rejected from the compiler, but it is not:

trait Foo {}

struct A;

impl Foo for A {}

impl Drop for A {
    fn drop(&mut self) { println!("dropping"); }
}

struct B<'a>(&'a Foo);

fn foo<'a>(a: &Foo) -> B<'a> { B(a) }

fn main() {
    let _test = foo(&A);
    println!("test");
}

Original Issue

Crash when reading from stdin

Depending on whether you let the compiler infer traits for you or not, you can make a program reading input from stdin crash in a couple different ways.

Repro here: https://gist.github.com/brandonw/3a3d040b7c6213740794

@alexcrichton
Copy link
Member

Can you provide some contextual information such as rustc version and platform you're running on? I cannot reproduce the segfault you've mentioned on either OSX or ubuntu.

@BurntSushi
Copy link
Member

This fails: https://gist.github.com/1f36060fd4e533ae0d8b (I'm surprised this one compiles actually.)

So does this: https://gist.github.com/6ee5ffe1de090ff3a2d0

Version:

[andrew@Liger ~] rustc --version
rustc 0.11.0-pre (3da5a5c 2014-05-17 05:06:27 -0700)
host: x86_64-unknown-linux-gnu

@BurntSushi
Copy link
Member

Try with:

rustc scratch.rs
echo test | ./scratch

@brandonw
Copy link
Contributor Author

Alex,

Sorry if I didn't make it clear enough. The gist has the working version uncommented, and the bug code is actually commented out. That was probably a poor decision on my part.

@alexcrichton alexcrichton changed the title Crash when reading from stdin Trait object allowed to escape its lifetime May 19, 2014
@alexcrichton
Copy link
Member

Updating to a more minimal description. Nominating and cc @nikomatsakis

@alexcrichton
Copy link
Member

It appears the bug is that the argument taken in the buggy examples is &mut std::io::Reader instead of &'a mut std::io::Reader

@huonw
Copy link
Member

huonw commented May 19, 2014

Seems similar/identical to #5723, #9745 and/or #11971.

@brandonw
Copy link
Contributor Author

Is it relevant that different errors would be encountered based on whether you piped data in or not? Or is that a red herring?

The assertion error is encountered in the original gist if you pipe data in, while if you do not pipe data in then the program segfaults before exiting.

@alexcrichton
Copy link
Member

The problem appears to be that the I/O object is being dropped, and then being used. This basically amounts to use-after-free, and LLVM optimizations can do really weird things to that, causing segfaults/asserts and whatnot.

@edwardw
Copy link
Contributor

edwardw commented May 19, 2014

The root cause lies in variance.rs#L744-L748, where the lifetime parameter of a possible trait reference should be accounted for but isn't. The fix is rather straightforward and had been tried before, but unfortunately never landed. It invalidates json.rs#L379-L388.

BTW, with that fix rustc does reject the code in question:

// error: cannot infer an appropriate lifetime
fn foo<'a>(a: &Foo) -> B<'a> { B(a) }

@edwardw
Copy link
Contributor

edwardw commented May 19, 2014

The #12470 shares the same root cause with this one, but they are manifested in quite different ways. I'd suggest to keep both tickets open and since #12470 has been tagged as 1.0 milestone so probably this one doesn't need to.

@pnkfelix
Copy link
Member

Treating as dupe of #12470. Removing I-nominated tag; it should be treated with whatever severity #12470 is.

(And if someone can convince me that the code example here adds no value beyond what is documented in #12470, then maybe we should close this.)

edwardw added a commit to edwardw/rust that referenced this issue Jun 23, 2014
The rust-lang#14869 removed `TraitStore` from `ty_trait` and represented trait
reference as regular `ty_rptr`. An old bug of the missing constraint
upon lifetime parameter of trait reference then is fixed as a side
effect. Adds tests for affected bugs and closes them.

Closes rust-lang#12470.
Closes rust-lang#14285.
alexcrichton added a commit to alexcrichton/rust that referenced this issue Jun 25, 2014
Closes rust-lang#14482 (std: Bring back half of Add on String)
Closes rust-lang#15026 (librustc: Remove the fallback to `int` from typechecking.)
Closes rust-lang#15119 (Add more description to c_str::unwrap().)
Closes rust-lang#15120 (Add tests for rust-lang#12470 and rust-lang#14285)
Closes rust-lang#15122 (Remove the cheat sheet.)
Closes rust-lang#15126 (rustc: Always include the morestack library)
Closes rust-lang#15127 (Improve ambiguous pronoun.)
Closes rust-lang#15130 (Fix rust-lang#15129)
Closes rust-lang#15131 (Add the Guide, add warning to tutorial.)
Closes rust-lang#15134 (Xfailed tests for hygiene, etc.)
Closes rust-lang#15135 (core: Add stability attributes to Clone)
Closes rust-lang#15136 (Some minor improvements to core::bool)
Closes rust-lang#15137 (std: Add stability attributes to primitive numeric modules)
Closes rust-lang#15141 (Fix grammar in tutorial)
Closes rust-lang#15143 (Remove few FIXMEs)
Closes rust-lang#15145 (Avoid unnecessary temporary on assignments)
Closes rust-lang#15147 (Small improvements for metaprogramming)
Closes rust-lang#15153 (librustc: Check function argument patterns for legality of by-move)
Closes rust-lang#15154 (test: Add a test for regions, traits, and variance.)
Closes rust-lang#15159 (rustc: Don't register syntax crates twice)
Closes rust-lang#13816 (Stabilize version output for rustc and rustdoc)
lnicola pushed a commit to lnicola/rust that referenced this issue Mar 13, 2023
lnicola pushed a commit to lnicola/rust that referenced this issue Mar 13, 2023
lnicola pushed a commit to lnicola/rust that referenced this issue Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants