From 3a663287fbaf87f37d056578f12347d7caf0f462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BB?= <49233942+hsqStephenZhang@users.noreply.github.com> Date: Fri, 10 Jan 2025 23:23:12 +0800 Subject: [PATCH] Fix miri (#25) --- src/lfu/wtinylfu.rs | 3 --- src/lru/adaptive.rs | 1 + src/lru/raw.rs | 56 ++++++++++++++++++++------------------------ src/lru/two_queue.rs | 1 + 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/lfu/wtinylfu.rs b/src/lfu/wtinylfu.rs index 6518729..08f9731 100644 --- a/src/lfu/wtinylfu.rs +++ b/src/lfu/wtinylfu.rs @@ -725,7 +725,6 @@ mod test { } #[test] - #[cfg_attr(miri, ignore)] fn test_wtinylfu_custom_hasher() { let mut cache: WTinyLFUCache< u64, @@ -823,7 +822,6 @@ mod test { } #[test] - #[cfg_attr(miri, ignore)] fn test_wtinylfu() { let mut cache = WTinyLFUCache::with_sizes(1, 2, 2, 5).unwrap(); assert_eq!(cache.cap(), 5); @@ -908,7 +906,6 @@ mod test { } #[test] - #[cfg_attr(miri, ignore)] fn test_wtinylfu_clone() { let mut cache = WTinyLFUCache::with_sizes(1, 2, 2, 5).unwrap(); assert_eq!(cache.cap(), 5); diff --git a/src/lru/adaptive.rs b/src/lru/adaptive.rs index fd7cc15..1ad1f66 100644 --- a/src/lru/adaptive.rs +++ b/src/lru/adaptive.rs @@ -1764,6 +1764,7 @@ mod test { use rand::seq::SliceRandom; use rand::{thread_rng, Rng}; + #[cfg_attr(miri, ignore)] #[test] fn test_arc_cache_random_ops() { let size = 128; diff --git a/src/lru/raw.rs b/src/lru/raw.rs index 9ec5551..60396ca 100644 --- a/src/lru/raw.rs +++ b/src/lru/raw.rs @@ -501,7 +501,7 @@ impl Cache for RawLRU { match self.map.get_mut(KeyWrapper::from_ref(k)) { None => None, - Some(node) => Some(unsafe { &mut *node.as_mut().val.as_mut_ptr() }), + Some(node) => Some(unsafe { &mut *(*node.as_ptr()).val.as_mut_ptr() }), } } @@ -557,10 +557,10 @@ impl Cache for RawLRU Some(old_node) => unsafe { let node_ptr = &mut *old_node.as_ptr(); self.detach(node_ptr); - let node = *Box::from_raw(old_node.as_ptr()); + let mut node = *Box::from_raw(old_node.as_ptr()); let val = node.val.assume_init(); - self.cb(&*node_ptr.key.as_ptr(), &val); - ptr::drop_in_place(node_ptr.key.as_mut_ptr()); + self.cb(&*node.key.as_ptr(), &val); + ptr::drop_in_place(node.key.assume_init_mut()); Some(val) }, } @@ -1363,7 +1363,7 @@ impl RawLRU { { match self.map.get_mut(KeyWrapper::from_ref(k)) { None => None, - Some(node) => Some(unsafe { &mut *node.as_mut().val.as_mut_ptr() }), + Some(node) => Some(unsafe { &mut *(*node.as_ptr()).val.as_mut_ptr() }), } } @@ -1375,7 +1375,7 @@ impl RawLRU { { self.map .get(KeyWrapper::from_ref(k)) - .map(|node| unsafe { &*node.as_ref().val.as_ptr() }) + .map(|node| unsafe { &*(*node.as_ptr()).val.as_ptr() }) } // used for avoiding borrow checker issues @@ -1414,22 +1414,22 @@ impl RawLRU { } } - pub(crate) fn put_nonnull(&mut self, mut bks: NonNull>) -> PutResult { + // let old_key = KeyRef { + // k: unsafe { &(*(*(*self.tail).prev).key.as_ptr()) }, + // }; + // let old_node = self.map.remove(&old_key).unwrap(); + pub(crate) fn put_nonnull(&mut self, bks: NonNull>) -> PutResult { if self.len() >= self.cap() { unsafe { // Safety: the cache length is not zero, so the cache must have a tail node. - let node = ((*self.tail).prev).as_mut().unwrap(); - self.detach(node); - + let old_key = KeyRef { + k: &(*(*(*self.tail).prev).key.as_ptr()), + }; // Safety: the node is in cache, so the cache map must have the node. - let node = self - .map - .remove(&KeyRef { - k: node.key.as_ptr(), - }) - .unwrap(); - - self.attach(bks.as_mut()); + let node = self.map.remove(&old_key).unwrap(); + self.detach(node.as_ptr()); + + self.attach(bks.as_ptr()); self.map.insert( KeyRef { k: bks.as_ref().key.as_ptr(), @@ -1464,23 +1464,19 @@ impl RawLRU { pub(crate) fn put_or_evict_nonnull( &mut self, - mut bks: NonNull>, + bks: NonNull>, ) -> Option>> { if self.len() >= self.cap() { unsafe { // Safety: the cache length is not zero, so the cache must have a tail node. - let node = ((*self.tail).prev).as_mut().unwrap(); - self.detach(node); - + let old_key = KeyRef { + k: &(*(*(*self.tail).prev).key.as_ptr()), + }; // Safety: the node is in cache, so the cache map must have the node. - let node = self - .map - .remove(&KeyRef { - k: node.key.as_ptr(), - }) - .unwrap(); - - self.attach(bks.as_mut()); + let node = self.map.remove(&old_key).unwrap(); + self.detach(node.as_ptr()); + + self.attach(bks.as_ptr()); self.map.insert( KeyRef { k: bks.as_ref().key.as_ptr(), diff --git a/src/lru/two_queue.rs b/src/lru/two_queue.rs index 69e1de8..bdac307 100644 --- a/src/lru/two_queue.rs +++ b/src/lru/two_queue.rs @@ -1618,6 +1618,7 @@ mod test { use rand::{thread_rng, Rng}; use std::format; + #[cfg_attr(miri, ignore)] #[test] fn test_2q_cache_random_ops() { let size = 128;