From bf95f65ce1501644719947ba07317fab94206a4f Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Fri, 2 Feb 2024 10:10:43 -0800 Subject: [PATCH] Fix: decayed_counter can overflow if shifted more than 63 (#35054) --- program-runtime/src/loaded_programs.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/program-runtime/src/loaded_programs.rs b/program-runtime/src/loaded_programs.rs index 1e92944ca8c75a..e8e3b9ee325c2c 100644 --- a/program-runtime/src/loaded_programs.rs +++ b/program-runtime/src/loaded_programs.rs @@ -464,7 +464,8 @@ impl LoadedProgram { pub fn decayed_usage_counter(&self, now: Slot) -> u64 { let last_access = self.latest_access_slot.load(Ordering::Relaxed); - let decaying_for = now.saturating_sub(last_access); + // Shifting the u64 value for more than 63 will cause an overflow. + let decaying_for = std::cmp::min(63, now.saturating_sub(last_access)); self.tx_usage_counter.load(Ordering::Relaxed) >> decaying_for } } @@ -1359,6 +1360,10 @@ mod tests { assert_eq!(program.decayed_usage_counter(19), 16); assert_eq!(program.decayed_usage_counter(20), 8); assert_eq!(program.decayed_usage_counter(21), 4); + + // Decay for 63 or more slots + assert_eq!(program.decayed_usage_counter(18 + 63), 0); + assert_eq!(program.decayed_usage_counter(100), 0); } #[test]