You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed that Singleton implements Send and Sync unconiditionally:
// The Singleton need to implement Send & Sync to ensure cross core compile check mechanics
// this is safe as the inner RWLock ensures cross core safety
unsafeimpl<T>SyncforSingleton<T>{}
unsafeimpl<T>SendforSingleton<T>{}
This is guarded by a RwLock but violates RwLock's own assumptions that Send will have T: Send and Sync will be T: Send + Sync. The way this is structured right now allows for data races from safe Rust code, for example:
#![forbid(unsafe_code)]use ruspiro_singleton::Singleton;use std::{cell::Cell, thread};#[derive(Debug,Clone,Copy)]enumRefOrInt<'a>{Ref(&'a u64),Int(u64)}staticSOME_INT:u64 = 123;staticSTATIC_CELL:Singleton<Cell<RefOrInt>> = Singleton::lazy(&|| {Cell::new(RefOrInt::Ref(&SOME_INT))});fnmain(){
thread::spawn(move || {loop{STATIC_CELL.with_ref(|cell| {// Repeatedly write Ref(&addr) and Int(0xdeadbeef) into the cell.
cell.set(RefOrInt::Ref(&SOME_INT));
cell.set(RefOrInt::Int(0xdeadbeef));});}});STATIC_CELL.with_ref(|cell| {loop{ifletRefOrInt::Ref(addr) = cell.get(){// Hope that between the time we pattern match the object as a// `Ref`, it gets written to by the other thread.if addr as*constu64 == &SOME_INTas*constu64{continue;}// Due to the data race, obtaining Ref(0xdeadbeef) is possibleprintln!("Pointer is now: {:p}", addr);println!("Dereferencing addr will now segfault: {}", *addr);}}});}
This outputs:
Pointer is now: 0xdeadbeef
Return Code: -11 (SIGSEGV)
The text was updated successfully, but these errors were encountered:
Thanks for your investigation and support. I actually did not considered the case someone would use types with interior mutability as types wrapped with a Singleton.
Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed that
Singleton
implements Send and Sync unconiditionally:ruspiro-singleton/src/lib.rs
Lines 82 to 85 in 203f1ae
This is guarded by a
RwLock
but violatesRwLock
's own assumptions thatSend
will haveT: Send
andSync
will beT: Send + Sync
. The way this is structured right now allows for data races from safe Rust code, for example:This outputs:
The text was updated successfully, but these errors were encountered: