Giter Site home page Giter Site logo

lifelink's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

lifelink's Issues

Forget unsoundness

When Deathtouch is forgotten (e.g. via mem::forget), this ends the borrow of the original variable without running the drop implementation of Deathtouch. Thus, the lifelink remains usable and this can lead to UB such as aliasing with &mut or use after free:

fn main() {
    use lifelink::{Lifelink, RefCtor};

    let mut value = Box::new(4u8);

    let (mut lifelink, deathtouch) = Lifelink::<RefCtor<u8>>::new(&*value);
    let guard = lifelink.get().unwrap();
    core::mem::forget(deathtouch);

    println!("{}", *guard);
    *value = 8; // guard aliases with mutable access
    println!("{}", *guard);
    drop(value);
    println!("{}", *guard); // use after free
}

Since forgetting is safe, the current API is unsound.

Potential Solutions

  • Have LifeLink::new accept a closure that is provided the lifelink. Then use the scopeguard crate (or the same techniques as scopeguard) to ensure that the code to reclaim the borrow from the lifelink always runs when that closure completes or panics.
  • Use a macro instead of LifeLink::new, that macro will place the Deathtouch in a local that is automatically dropped at the end of the current scope, with the local defined inside the macro, user code will not be able to forget it since it can't be named outside of the macro (e.g. similar to scopeguard::defer!).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.