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
This should probably only be for Sync types (this is the same limitation that Rust applies to static types) otherwise there is a soundness issue for multithreaded programs . It is possible to wrap a type that isn't safe to use across threads like Cell in a LateStatic and then cause data races like so:
use late_static::LateStatic;use std::cell::Cell;use std::thread;#[derive(Debug,Clone,Copy)]enumRefOrInt<'a>{Ref(&'a u64),Int(u64)}staticSOME_INT:u64 = 123;staticSTATIC_CELL:LateStatic<Cell<RefOrInt>> = LateStatic::new();fnmain(){unsafe{LateStatic::assign(&STATIC_CELL,Cell::new(RefOrInt::Ref(&SOME_INT)));}
thread::spawn(move || {loop{// Repeatedly write Ref(&addr) and Int(0xdeadbeef) into the cell.STATIC_CELL.set(RefOrInt::Ref(&SOME_INT));STATIC_CELL.set(RefOrInt::Int(0xdeadbeef));}});loop{ifletRefOrInt::Ref(addr) = STATIC_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)
Currently LateStatic implements
Send
andSync
unconditionally for all types.late-static/src/lib.rs
Lines 29 to 30 in 3b72ba5
This should probably only be for
Sync
types (this is the same limitation that Rust applies to static types) otherwise there is a soundness issue for multithreaded programs . It is possible to wrap a type that isn't safe to use across threads likeCell
in aLateStatic
and then cause data races like so:This outputs:
(Issue found by @sslab-gatech's Rust group)
The text was updated successfully, but these errors were encountered: