Skip to content

Commit 3a052ba

Browse files
committed
Use saturating increments for Stats fields
1 parent 479178d commit 3a052ba

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to
66

77
## [Unreleased]
88

9+
- cosmwasm-vm: Use saturating increments for `Stats` fields to ensure we don't
10+
run into overflow issues.
11+
912
## [1.2.2] - 2023-03-08
1013

1114
### Added

packages/vm/src/cache.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ const CACHE_DIR: &str = "cache";
2525
// Cacheable things.
2626
const MODULES_DIR: &str = "modules";
2727

28+
/// Statistics about the usage of a cache instance. Those values are node
29+
/// specific and must not be used in a consensus critical context.
30+
/// When a node is hit by a client for simulations or other queries, hits and misses
31+
/// increase. Also a node restart will reset the values.
32+
///
33+
/// All values should be increment using saturated addition to ensure the node does not
34+
/// crash in case the stats exceed the integer limit.
2835
#[derive(Debug, Default, Clone, Copy)]
2936
pub struct Stats {
3037
pub hits_pinned_memory_cache: u32,
@@ -228,7 +235,7 @@ where
228235

229236
// Try to get module from the memory cache
230237
if let Some(module) = cache.memory_cache.load(checksum)? {
231-
cache.stats.hits_memory_cache += 1;
238+
cache.stats.hits_memory_cache = cache.stats.hits_memory_cache.saturating_add(1);
232239
return cache
233240
.pinned_memory_cache
234241
.store(checksum, module.module, module.size);
@@ -237,7 +244,7 @@ where
237244
// Try to get module from file system cache
238245
let store = make_runtime_store(Some(cache.instance_memory_limit));
239246
if let Some(module) = cache.fs_cache.load(checksum, &store)? {
240-
cache.stats.hits_fs_cache += 1;
247+
cache.stats.hits_fs_cache = cache.stats.hits_fs_cache.saturating_add(1);
241248
let module_size = loupe::size_of_val(&module);
242249
return cache
243250
.pinned_memory_cache
@@ -295,20 +302,21 @@ where
295302
let mut cache = self.inner.lock().unwrap();
296303
// Try to get module from the pinned memory cache
297304
if let Some(module) = cache.pinned_memory_cache.load(checksum)? {
298-
cache.stats.hits_pinned_memory_cache += 1;
305+
cache.stats.hits_pinned_memory_cache =
306+
cache.stats.hits_pinned_memory_cache.saturating_add(1);
299307
return Ok(module);
300308
}
301309

302310
// Get module from memory cache
303311
if let Some(module) = cache.memory_cache.load(checksum)? {
304-
cache.stats.hits_memory_cache += 1;
312+
cache.stats.hits_memory_cache = cache.stats.hits_memory_cache.saturating_add(1);
305313
return Ok(module.module);
306314
}
307315

308316
// Get module from file system cache
309317
let store = make_runtime_store(Some(cache.instance_memory_limit));
310318
if let Some(module) = cache.fs_cache.load(checksum, &store)? {
311-
cache.stats.hits_fs_cache += 1;
319+
cache.stats.hits_fs_cache = cache.stats.hits_fs_cache.saturating_add(1);
312320
let module_size = loupe::size_of_val(&module);
313321
cache
314322
.memory_cache
@@ -322,7 +330,7 @@ where
322330
// serialization format. If you do not replay all transactions, previous calls of `save_wasm`
323331
// stored the old module format.
324332
let wasm = self.load_wasm_with_path(&cache.wasm_path, checksum)?;
325-
cache.stats.misses += 1;
333+
cache.stats.misses = cache.stats.misses.saturating_add(1);
326334
let module = compile(&wasm, Some(cache.instance_memory_limit), &[])?;
327335
cache.fs_cache.store(checksum, &module)?;
328336
let module_size = loupe::size_of_val(&module);

0 commit comments

Comments
 (0)