Skip to content

Commit

Permalink
fix: ensure same mtime for js and code cache to prevent loading old c…
Browse files Browse the repository at this point in the history
…ode caches (#1822)
  • Loading branch information
edusperoni authored Sep 6, 2024
1 parent 023316f commit 3d6e101
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions test-app/runtime/src/main/cpp/ModuleInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <libgen.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include <time.h>
#include <utime.h>

using namespace v8;
using namespace std;
Expand Down Expand Up @@ -478,8 +480,9 @@ ScriptCompiler::CachedData* ModuleInternal::TryLoadScriptCache(const std::string
auto cacheLastModifiedTime = result.st_mtime;
if (stat(path.c_str(), &result) == 0) {
auto jsLastModifiedTime = result.st_mtime;
if (jsLastModifiedTime > 0 && cacheLastModifiedTime > 0 && jsLastModifiedTime > cacheLastModifiedTime) {
// The javascript file is more recent than the cache file => ignore the cache
if (jsLastModifiedTime != cacheLastModifiedTime) {
// files have different dates, ignore the cache file (this is enforced by the
// SaveScriptCache function)
return nullptr;
}
}
Expand Down Expand Up @@ -508,6 +511,16 @@ void ModuleInternal::SaveScriptCache(const Local<Script> script, const std::stri
auto cachePath = path + ".cache";
File::WriteBinary(cachePath, cachedData->data, length);
delete cachedData;
// make sure cache and js file have the same modification date
struct stat result;
struct utimbuf new_times;
new_times.actime = time(nullptr);
new_times.modtime = time(nullptr);
if (stat(path.c_str(), &result) == 0) {
auto jsLastModifiedTime = result.st_mtime;
new_times.modtime = jsLastModifiedTime;
}
utime(cachePath.c_str(), &new_times);
}

ModuleInternal::ModulePathKind ModuleInternal::GetModulePathKind(const std::string& path) {
Expand Down

0 comments on commit 3d6e101

Please sign in to comment.