-
Notifications
You must be signed in to change notification settings - Fork 22
Closed
Description
Filing to track the bug reported by @jDomantas in https://www.reddit.com/r/rust/comments/m42fjx/safetouse_procmacrofree_selfreferential_structs/gqt3i35/, which can result in UB in safe code.
use std::rc::Rc;
use once_self_cell::sync_once_self_cell;
struct Owner;
struct Borrowed<'a> {
owner: &'a Owner,
evil: Evil,
}
impl<'a> From<&'a Owner> for Borrowed<'a> {
fn from(owner: &'a Owner) -> Borrowed<'a> {
Borrowed {
owner,
evil: Evil::default(),
}
}
}
thread_local! {
static STR: Rc<str> = "hello".into();
}
#[derive(Clone)]
struct Evil {
rc: Rc<str>,
}
impl Drop for Evil {
fn drop(&mut self) {
let ptr = self.rc.as_ptr();
let thread_id = std::thread::current().id();
println!("drop rc at {:p} in thread {:?}", ptr, thread_id);
}
}
impl Default for Evil {
fn default() -> Evil {
Evil {
rc: STR.with(|x| x.clone()),
}
}
}
sync_once_self_cell!(
SelfRef,
Owner,
Borrowed<'_>,
);
fn main() {
let cell = SelfRef::new(Owner);
let _evil1 = cell.get_or_init_dependent().evil.clone();
std::thread::spawn(move || {
let _evil2 = cell.get_or_init_dependent().evil.clone();
});
}drop rc at 0x21b53dbd0d0 in thread ThreadId(1)
drop rc at 0x21b53dbd0d0 in thread ThreadId(2)
drop rc at 0x21b53dbd0d0 in thread ThreadId(2)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels