From 4ef8c5c47d61a8b5f5e4b9b32e419ecad737a041 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bennet=20Ble=C3=9Fmann?= Date: Thu, 27 Jul 2023 23:35:57 +0200 Subject: [PATCH] =?utf8?q?detect=20and=20prevent=C2=B2=20concurrent=20Atom?= =?utf8?q?Table=20use?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit ²in the case of `#[cfg(not(test))]` there is still a toctou race as I am not sufficently familiar with Atomics --- src/atom_table.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/atom_table.rs b/src/atom_table.rs index b925273a..9f480139 100644 --- a/src/atom_table.rs +++ b/src/atom_table.rs @@ -54,7 +54,12 @@ static mut ATOM_TABLE_BUF_BASE: *const u8 = ptr::null_mut(); #[cfg(test)] fn set_atom_tbl_buf_base(ptr: *const u8) { ATOM_TABLE_BUF_BASE.with(|atom_table_buf_base| { - *atom_table_buf_base.borrow_mut() = ptr; + let mut borrow = atom_table_buf_base.borrow_mut(); + assert!( + borrow.is_null() || ptr.is_null(), + "Overwriting atom table base pointer!" + ); + *borrow = ptr; }); } @@ -66,6 +71,11 @@ pub(crate) fn get_atom_tbl_buf_base() -> *const u8 { #[cfg(not(test))] fn set_atom_tbl_buf_base(ptr: *const u8) { unsafe { + // FIXME: to prevent a toctou race-condition an atomic compare_exchange or a global lock should be used + assert!( + ATOM_TABLE_BUF_BASE.is_null() || ptr.is_null(), + "Overwriting atom table base pointer!" + ); ATOM_TABLE_BUF_BASE = ptr; } } @@ -75,6 +85,13 @@ pub(crate) fn get_atom_tbl_buf_base() -> *const u8 { unsafe { ATOM_TABLE_BUF_BASE } } +#[test] +#[should_panic(expected = "Overwriting atom table base pointer!")] +fn atomtable_is_not_concurrency_safe() { + let table_a = AtomTable::new(); + let table_b = AtomTable::new(); +} + impl RawBlockTraits for AtomTable { #[inline] fn init_size() -> usize { @@ -241,6 +258,7 @@ pub struct AtomTable { impl Drop for AtomTable { fn drop(&mut self) { + set_atom_tbl_buf_base(ptr::null()); self.block.deallocate(); } } -- 2.54.0