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

Doc: remove a "safety note" made obsolete by dropck for TypedArena #24282

Merged
merged 1 commit into from
Apr 11, 2015

Conversation

SimonSapin
Copy link
Contributor

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@pnkfelix
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Apr 10, 2015

📌 Commit c2fa1f7 has been approved by pnkfelix

@bors bors merged commit c2fa1f7 into rust-lang:master Apr 11, 2015
@SimonSapin
Copy link
Contributor Author

@pnkfelix, thinking about this a bit more, I was wondering if dropck really applied here or if unsoundess was still possible with TypedArena. After all, the lifetime of &mut T references returned by TypedArena::alloc are set artificially with transmute.

So I tried to see if I could deliberately cause a use-after-free in safe code:

#![feature(rustc_private)]
extern crate arena;

use std::cell::Cell;

struct Foo<'a> {
    data: Box<u32>,
    other: Cell<Option<&'a Foo<'a>>>
}

impl<'a> Drop for Foo<'a> {
    fn drop(&mut self) {
        let other = self.other.get().unwrap();
        println!("Accessing other item’s data = {}", *other.data);  // Use after free?
        println!("Dropping with data = {}", *self.data);
    }
}

fn main() {
    let arena = arena::TypedArena::new();
    // Create a reference cycle within the arena:
    let a = arena.alloc(Foo {
        data: Box::new(42),
        other: Cell::new(None),
    });
    let b = arena.alloc(Foo {
        data: Box::new(6),
        other: Cell::new(None)
    });
    a.other.set(Some(b));
    b.other.set(Some(a));
}

This fails to compile with error: arena does not live long enough, but compiles fine if I remove the Drop impl. So I assume this is dropck doing its job correctly, although the error message does not mention Drop at all. It looks like my arena-tree crate only works because nodes (which have &T cycles within an arena) don’t have a destructor.

Just because I didn’t manage to find a problem doesn’t prove it doesn’t exist, but I’m slightly more confident about removing this warning now.

@SimonSapin SimonSapin deleted the patch-6 branch June 5, 2015 13:58
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 this pull request may close these issues.

5 participants