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(sandbox): Concurrent file operations in embedded executor cache #4325

Merged
merged 12 commits into from
Nov 19, 2024
Merged
94 changes: 91 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ directories = "5.0.1" # utils/key-finder
num-traits = { version = "0.2", default-features = false } # gear-core
glob = "0.3.1" # cargo-gbuild
smallvec = "1.13.2" # utils/node-wrapper
fs4 = "0.11.1" # utils/gear-wasmer-cache
bytes = "1.8.0" # utils/gear-wasmer-cache
loom = "0.7.2" # utils/gear-wasmer-cache

[profile.dev.package.corosensei]
opt-level = 3
Expand Down
7 changes: 2 additions & 5 deletions sandbox/host/src/sandbox/wasmer_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ use crate::{

pub type StoreRefCell = store_refcell::StoreRefCell<wasmer::Store>;

#[cfg(feature = "gear-wasmer-cache")]
use gear_wasmer_cache::*;

environmental::environmental!(SupervisorContextStore: trait SupervisorContext);

mod store_refcell_ctx {
Expand Down Expand Up @@ -156,7 +153,7 @@ pub fn invoke(
}

#[cfg(feature = "gear-wasmer-cache")]
fn fs_cache() -> PathBuf {
fn cache_base_path() -> PathBuf {
use std::sync::OnceLock;
use tempfile::TempDir;

Expand All @@ -178,7 +175,7 @@ pub fn instantiate(
supervisor_context: &mut dyn SupervisorContext,
) -> std::result::Result<SandboxInstance, InstantiationError> {
#[cfg(feature = "gear-wasmer-cache")]
let module = get_or_compile_with_cache(wasm, context.store().borrow().engine(), fs_cache)
let module = gear_wasmer_cache::get(context.store().borrow().engine(), wasm, cache_base_path())
.map_err(|_| InstantiationError::ModuleDecoding)?;

#[cfg(not(feature = "gear-wasmer-cache"))]
Expand Down
14 changes: 5 additions & 9 deletions sandbox/sandbox/src/embedded_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use crate::{
};
use alloc::string::String;
use gear_sandbox_env::GLOBAL_NAME_GAS;
use gear_wasmer_cache::get_or_compile_with_cache;
use sp_wasm_interface_common::HostPointer;
use std::{
collections::btree_map::BTreeMap, fs, marker::PhantomData, path::PathBuf, ptr::NonNull,
Expand All @@ -41,15 +40,13 @@ use wasmer::{
};
use wasmer_types::{ExternType, Target};

fn fs_cache() -> PathBuf {
fn cache_base_path() -> PathBuf {
static CACHE_DIR: OnceLock<PathBuf> = OnceLock::new();
CACHE_DIR
.get_or_init(|| {
let out_dir = PathBuf::from(env!("OUT_DIR"));
let cache = out_dir.join("wasmer-cache");
if !cache.exists() {
fs::create_dir(&cache).unwrap();
}
fs::create_dir_all(&cache).unwrap();
cache
})
.into()
Expand Down Expand Up @@ -432,10 +429,9 @@ impl<State: Send + 'static> super::SandboxInstance<State> for Instance<State> {
code: &[u8],
env_def_builder: &Self::EnvironmentBuilder,
) -> Result<Instance<State>, Error> {
let module = get_or_compile_with_cache(code, store.engine(), fs_cache).map_err(|e| {
log::trace!(target: TARGET, "Failed to create module: {e}");
Error::Module
})?;
let module = gear_wasmer_cache::get(store.engine(), code, cache_base_path())
.inspect_err(|e| log::trace!(target: TARGET, "Failed to create module: {e}"))
.map_err(|_e| Error::Module)?;
let mut imports = Imports::new();

for import in module.imports() {
Expand Down
1 change: 1 addition & 0 deletions scripts/src/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ doc_test() {
time_consuming_tests() {
$CARGO test -p demo-fungible-token --no-fail-fast --release -- --nocapture --ignored
$CARGO test -p gear-wasm-builder --no-fail-fast "$@" -- --nocapture --ignored
LOOM_MAX_PREEMPTIONS=3 RUSTFLAGS="--cfg loom" $CARGO test -p gear-wasmer-cache --no-fail-fast --release -- --nocapture
}

typo_tests() {
Expand Down
14 changes: 14 additions & 0 deletions utils/gear-wasmer-cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@ homepage.workspace = true
repository.workspace = true
rust-version.workspace = true

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)'] }

[dependencies]
wasmer.workspace = true
wasmer-cache.workspace = true
log.workspace = true
uluru.workspace = true
fs4.workspace = true
derive_more.workspace = true
bytes.workspace = true

[dev-dependencies]
tempfile.workspace = true
demo-constructor = { workspace = true, features = ["std"] }
env_logger.workspace = true

[target.'cfg(loom)'.dev-dependencies]
loom.workspace = true
Loading
Loading