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

Data Race Unsoundness #1

Closed
WalterSmuts opened this issue Nov 23, 2022 · 1 comment
Closed

Data Race Unsoundness #1

WalterSmuts opened this issue Nov 23, 2022 · 1 comment

Comments

@WalterSmuts
Copy link
Owner

The following code results in a bug from miri:

use std::{cell::RefCell, ops::AddAssign};

use num_traits::{One, Zero};

fn generic_call_counter<T: Zero + One + Copy + AddAssign + 'static>() -> T {
    let mut count = generic_singleton::get_or_init!(|| RefCell::new(T::zero())).borrow_mut();
    *count += T::one();
    *count
}

fn main() {
    let mut handles = Vec::new();
    for _ in 0..2 {
        handles.push(std::thread::spawn(|| {
            println!("{}", generic_call_counter::<i32>())
        }));
    }
    for handle in handles {
        handle.join().unwrap();
    }
}

BUG:

[walter@cuddles generic_singleton_user]$ cargo miri run
Preparing a sysroot for Miri (target: x86_64-unknown-linux-gnu)... done
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `/home/walter/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo-miri runner target/miri/x86_64-unknown-linux-gnu/debug/generic_singleton_user`
1
error: Undefined Behavior: Data race detected between Read on thread `<unnamed>` and Write on thread `<unnamed>` at alloc3852
   --> /home/walter/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:452:18
    |
452 |         unsafe { *self.value.get() }
    |                  ^^^^^^^^^^^^^^^^^ Data race detected between Read on thread `<unnamed>` and Write on thread `<unnamed>` at alloc3852
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
    = note: BACKTRACE:
    = note: inside `std::cell::Cell::<isize>::get` at /home/walter/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:452:18
    = note: inside `std::cell::BorrowRefMut::<'_>::new` at /home/walter/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:1682:15
    = note: inside `std::cell::RefCell::<i32>::try_borrow_mut` at /home/walter/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:987:15
note: inside `generic_call_counter::<i32>` at src/main.rs:6:21
   --> src/main.rs:6:21
    |
6   |     let mut count = generic_singleton::get_or_init!(|| RefCell::new(T::zero())).borrow_mut();
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside closure at src/main.rs:15:28
   --> src/main.rs:15:28
    |
15  |             println!("{}", generic_call_counter::<i32>())
    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to previous error

The bug makes sense. Not sure why generic_singleton allows this usage :/

WalterSmuts added a commit that referenced this issue Jan 6, 2023
This is required to prevent:
#1
@WalterSmuts
Copy link
Owner Author

Fixed by e6110fa

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

No branches or pull requests

1 participant