Skip to content

CopyCell allows concurrent use of non-Sync types through references #12

@ammaraskar

Description

@ammaraskar

Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed that the CopyCell object implements Send as long as the underlying type implements Copy.

However, one potential problem with this is that (non-mutable) references actually implement the Copy trait: https://doc.rust-lang.org/std/marker/trait.Copy.html#impl-Copy-71

This makes it possible, for example, to share Cells across threads by wrapping them in a CopyCell:

#![forbid(unsafe_code)]

use toolshed::CopyCell;

use std::cell::Cell;
use crossbeam_utils::thread;

fn main() {
    let cell = Cell::new(42);
    let copy_cell = CopyCell::new(&cell);

    thread::scope(|s| {
        s.spawn(move |_| {
            let smuggled_cell_ref = copy_cell.get();
            println!("Other Thread: {:p}", smuggled_cell_ref);
        });

        println!("Main Thread:  {:p}", &cell);
    });
}

Output:

Main Thread:  0x7ffe19babd1c
Other Thread: 0x7ffe19babd1c

Indicating that the same Cell is now usable across threads, potentially allowing for data races.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions