Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: audit #174

Merged
merged 5 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions crates/storage/src/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl WithHash for BytesWithHash {
pub struct NoVersion;

pub struct InitiaModuleCache {
pub capacity: usize,
#[allow(clippy::type_complexity)]
pub(crate) module_cache: Mutex<CLruCache<Checksum, ModuleWrapper, RandomState, ModuleScale>>,
}
Expand All @@ -59,6 +60,7 @@ impl InitiaModuleCache {
pub fn new(cache_capacity: usize) -> Arc<InitiaModuleCache> {
let capacity = NonZeroUsize::new(cache_capacity * 1024 * 1024).unwrap();
Arc::new(InitiaModuleCache {
capacity: cache_capacity * 1024 * 1024,
module_cache: Mutex::new(CLruCache::with_config(
CLruCacheConfig::new(capacity).with_scale(ModuleScale),
)),
Expand All @@ -77,6 +79,11 @@ impl InitiaModuleCache {
extension: Arc<BytesWithHash>,
version: NoVersion,
) -> VMResult<()> {
// cache is too small to hold this module
if self.capacity < allocated_size {
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
return Ok(());
}

let mut module_cache = self.module_cache.lock();

match module_cache.get(&key) {
Expand All @@ -92,7 +99,11 @@ impl InitiaModuleCache {
module_cache
.put_with_weight(key, ModuleWrapper::new(module, allocated_size))
.unwrap_or_else(|_| {
eprintln!("WARNING: failed to insert module {:?} into cache; cache capacity might be too small", module_id.short_str_lossless().to_string());
// ignore cache errors
eprintln!(
"WARNING: failed to insert module {:?} into cache",
module_id.short_str_lossless().to_string()
);
None
});
Ok(())
Expand All @@ -116,12 +127,19 @@ impl InitiaModuleCache {
_ => {
let module_id = verified_code.self_id();
let module = Arc::new(ModuleCode::from_verified(verified_code, extension, version));
module_cache
.put_with_weight(key, ModuleWrapper::new(module.clone(), allocated_size))
.unwrap_or_else(|_| {
eprintln!("WARNING: failed to insert module {:?} into cache; cache capacity might be too small", module_id.short_str_lossless().to_string());
None
});
if self.capacity >= allocated_size {
module_cache
.put_with_weight(key, ModuleWrapper::new(module.clone(), allocated_size))
.unwrap_or_else(|_| {
// ignore cache errors
eprintln!(
"WARNING: failed to insert module {:?} into cache",
module_id.short_str_lossless().to_string()
);
None
});
}

Ok(module)
}
}
Expand Down Expand Up @@ -156,12 +174,19 @@ impl InitiaModuleCache {
}

let code_wrapper = ModuleWrapper::new(Arc::new(code), allocated_size);
module_cache
.put_with_weight(*checksum, code_wrapper.clone())
.unwrap_or_else(|_| {
eprintln!("WARNING: failed to insert module {:?} into cache; cache capacity might be too small", id.short_str_lossless().to_string());
None
});
if self.capacity >= allocated_size {
module_cache
.put_with_weight(*checksum, code_wrapper.clone())
.unwrap_or_else(|_| {
// ignore cache errors
eprintln!(
"WARNING: failed to insert module {:?} into cache",
id.short_str_lossless().to_string()
);
None
});
}

Some(code_wrapper)
}
None => None,
Expand Down
33 changes: 20 additions & 13 deletions crates/storage/src/script_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ use crate::{
};

pub struct InitiaScriptCache {
pub capacity: usize,
pub(crate) script_cache: Mutex<CLruCache<Checksum, ScriptWrapper, RandomState, ScriptScale>>,
}

impl InitiaScriptCache {
pub fn new(cache_capacity: usize) -> Arc<InitiaScriptCache> {
Arc::new(InitiaScriptCache {
capacity: cache_capacity * 1024 * 1024,
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
script_cache: Mutex::new(CLruCache::with_config(
CLruCacheConfig::new(NonZeroUsize::new(cache_capacity * 1024 * 1024).unwrap())
.with_scale(ScriptScale),
Expand All @@ -42,13 +44,15 @@ impl InitiaScriptCache {
let new_script = Code::from_deserialized(deserialized_script);
let deserialized_script = new_script.deserialized().clone();

// error occurs when the new script has a weight greater than the cache capacity
script_cache
.put_with_weight(key, ScriptWrapper::new(new_script, allocated_size))
.unwrap_or_else(|_| {
eprintln!("WARNING: failed to insert script into cache; cache capacity might be too small");
None
});
if self.capacity >= allocated_size {
script_cache
.put_with_weight(key, ScriptWrapper::new(new_script, allocated_size))
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or_else(|_| {
// ignore cache errors
eprintln!("WARNING: failed to insert script into cache");
None
});
}

Ok(deserialized_script)
}
Expand Down Expand Up @@ -81,12 +85,15 @@ impl InitiaScriptCache {
};

if let Some(new_script) = new_script {
script_cache
.put_with_weight(key, ScriptWrapper::new(new_script, allocated_size))
.unwrap_or_else(|_| {
eprintln!("WARNING: failed to insert script into cache; cache capacity might be too small");
None
});
if self.capacity >= allocated_size {
script_cache
.put_with_weight(key, ScriptWrapper::new(new_script, allocated_size))
.unwrap_or_else(|_| {
// ignore cache errors
eprintln!("WARNING: failed to insert script into cache");
None
});
}
}
Ok(verified_script)
}
Expand Down
Loading