Skip to content

Commit

Permalink
Fix the caches mutating a deque node through a ptr derived
Browse files Browse the repository at this point in the history
from a shared ref

Add a test to `deque` module to reproduce the bug.
  • Loading branch information
tatsuya6502 committed Apr 27, 2023
1 parent 358a71e commit 03c75ff
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/common/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ impl<T> Deque<T> {

#[cfg(test)]
mod tests {
use std::ptr::NonNull;

use super::{CacheRegion::MainProbation, DeqNode, Deque};

#[test]
Expand Down Expand Up @@ -714,6 +716,32 @@ mod tests {
assert!(node2a.next_node().is_none());
}

#[test]
fn peek_and_move_to_back() {
let mut deque: Deque<String> = Deque::new(MainProbation);

let node1 = DeqNode::new("a".into());
deque.push_back(Box::new(node1));
let node2 = DeqNode::new("b".into());
let _ = deque.push_back(Box::new(node2));
let node3 = DeqNode::new("c".into());
let _ = deque.push_back(Box::new(node3));
// "a" -> "b" -> "c"

let node1a = deque.peek_front().unwrap();
assert_eq!(node1a.element, "a".to_string());
unsafe { deque.move_to_back(NonNull::from(node1a)) };
// "b" -> "c" -> "a"

let node2a = deque.peek_front().unwrap();
assert_eq!(node2a.element, "b".to_string());

let node3a = DeqNode::next_node(node2a).unwrap();
assert_eq!(node3a.element, "c".to_string());
unsafe { deque.move_to_back(NonNull::from(node3a)) };
// "b" -> "a" -> "c"
}

#[test]
fn drop() {
use std::{cell::RefCell, rc::Rc};
Expand Down

0 comments on commit 03c75ff

Please sign in to comment.