fix: infinite recursion bug

This commit is contained in:
Shunsuke Shibayama 2024-08-11 14:17:04 +09:00
parent 50cfc43081
commit cd9973e800
2 changed files with 14 additions and 0 deletions

View file

@ -1,5 +1,6 @@
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicU32, Ordering};
use std::thread::ThreadId;
// use std::rc::Rc;
pub use parking_lot::{
@ -292,6 +293,7 @@ pub struct Forkable<T: Send + Clone> {
last_borrowed_at: Arc<ThreadLocal<RefCell<BorrowInfo>>>,
#[cfg(any(feature = "backtrace", feature = "debug"))]
last_mut_borrowed_at: Arc<ThreadLocal<RefCell<BorrowInfo>>>,
recursion_counter: Arc<AtomicU32>,
}
impl<T: fmt::Debug + Send + Clone> fmt::Debug for Forkable<T> {
@ -326,9 +328,18 @@ impl<T: Send + Clone> Forkable<T> {
last_borrowed_at: Arc::new(ThreadLocal::new()),
#[cfg(any(feature = "backtrace", feature = "debug"))]
last_mut_borrowed_at: Arc::new(ThreadLocal::new()),
recursion_counter: Arc::new(AtomicU32::new(0)),
}
}
/// return 1 if the recursion limit is reached.
pub fn dec_recursion_counter(&self) -> u32 {
if self.recursion_counter.load(Ordering::SeqCst) == 0 {
self.recursion_counter.store(256, Ordering::SeqCst);
}
self.recursion_counter.fetch_sub(1, Ordering::SeqCst)
}
pub fn update_init(&mut self) {
let clone = self.clone_inner();
// NG: self.init = Arc::new(clone);

View file

@ -551,6 +551,9 @@ pub struct Free<T: Send + Clone>(Forkable<FreeKind<T>>);
impl Hash for Free<Type> {
fn hash<H: Hasher>(&self, state: &mut H) {
if self.0.dec_recursion_counter() == 1 {
return;
}
if let Some(name) = self.unbound_name() {
name.hash(state);
}