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: module cache delta overflow #3684

Merged
merged 4 commits into from
Mar 16, 2023
Merged

Fix: module cache delta overflow #3684

merged 4 commits into from
Mar 16, 2023

Conversation

dynamite-bud
Copy link
Contributor

@dynamite-bud dynamite-bud commented Mar 16, 2023

This PR makes a fix to the module cache.

In the function below we see that we have three points:

  1. We get the current platform time
  2. We get the read the cache
  3. We read the data getting the when_cached and then subtract it from the current platform time aka now
pub fn get_webc(&self, webc: &str, runtime: &dyn WasiRuntime) -> Option<BinaryPackage> {
        let name = webc.to_string();
        let now = platform_clock_time_get(Snapshot0Clockid::Monotonic, 1_000_000).unwrap() as u128; // -------- 1️⃣ 

        // Fast path
        {
            let cache = self.cache_webc.read().unwrap();  // -------- 2️⃣ 
            if let Some(data) = cache.get(&name) {
                if let Some(when_cached) = data.when_cached.as_ref() {
                    let delta = now - *when_cached;  // -------- 3️⃣ 
                    if delta <= self.cache_time.as_nanos() {
                        return Some(data.clone());
                    }
           ...

Issue:

Assuming two threads reading on same webc

Actions Series
|
| ---> 1️⃣ ----> 2️⃣ ---🔒(on cache_webc) ------------------------ 🔓 (on cache_webc and update when_cached) ------ 3️⃣ --- 🟥
| ---> 1️⃣ ----> 2️⃣ ----^--🕐 (wait for lock release) ---------^ 🔒 (on cache_webc and read updated when_cached) ----- 3️⃣ --- 🟥
|.....................................................................................................................Fails as when_cached is greater than now ^
________________________________________________________________________________________________________________________ Time

So this failure results in a value overflow in 3️⃣ and causes a crash in the tokio_runtime and thus fails the IP tests in wasmer_deploy_testing

@dynamite-bud dynamite-bud requested a review from theduke March 16, 2023 12:26
@dynamite-bud dynamite-bud self-assigned this Mar 16, 2023
@dynamite-bud dynamite-bud added the bug Something isn't working label Mar 16, 2023
Copy link
Contributor

@theduke theduke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Congratulations on your first Wasmer/WASIX bugfix, @dynamite-bud !

@theduke theduke enabled auto-merge (rebase) March 16, 2023 12:30
@theduke theduke merged commit 94c2bed into master Mar 16, 2023
@theduke theduke deleted the fix_module_cache branch March 16, 2023 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants