bacon-rajan-cc icon indicating copy to clipboard operation
bacon-rajan-cc copied to clipboard

trace Rc, rc::Weak, Arc, sync::Weak, Mutex

Open mio-19 opened this issue 4 years ago • 1 comments

mio-19 avatar Mar 29 '20 10:03 mio-19

Tracing the content of Rc, Arc, etc. is unsound and will lead to double frees, since it is like tracing the same field twice or more:

struct A {
    rc1: Rc<Cc<B>>,
    rc2: Rc<Cc<B>>,
}

struct B {
    ...
}

impl Trace for A {
    fn trace(&self, tracer: &mut Tracer) {
        self.rc1.trace(tracer);
        self.rc2.trace(tracer);
    }
}

fn main() {
    let b = Rc::new(Cc::new(B { ... }));
    let a = Cc::new(A {
        rc1: b.clone(),
        rc2: b.clone(),
    });
    // Now tracing a is unsound, since b strong counter is 1 and b will be traced twice
}

This is also why in rust-cc there isn't a Trace impl for Rc.

frengor avatar Mar 17 '23 16:03 frengor