Skip to content

Commit 3d6e101

Browse files
authored
fix: ensure same mtime for js and code cache to prevent loading old code caches (#1822)
1 parent 023316f commit 3d6e101

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

test-app/runtime/src/main/cpp/ModuleInternal.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <libgen.h>
2424
#include <dlfcn.h>
2525
#include <sys/stat.h>
26+
#include <time.h>
27+
#include <utime.h>
2628

2729
using namespace v8;
2830
using namespace std;
@@ -478,8 +480,9 @@ ScriptCompiler::CachedData* ModuleInternal::TryLoadScriptCache(const std::string
478480
auto cacheLastModifiedTime = result.st_mtime;
479481
if (stat(path.c_str(), &result) == 0) {
480482
auto jsLastModifiedTime = result.st_mtime;
481-
if (jsLastModifiedTime > 0 && cacheLastModifiedTime > 0 && jsLastModifiedTime > cacheLastModifiedTime) {
482-
// The javascript file is more recent than the cache file => ignore the cache
483+
if (jsLastModifiedTime != cacheLastModifiedTime) {
484+
// files have different dates, ignore the cache file (this is enforced by the
485+
// SaveScriptCache function)
483486
return nullptr;
484487
}
485488
}
@@ -508,6 +511,16 @@ void ModuleInternal::SaveScriptCache(const Local<Script> script, const std::stri
508511
auto cachePath = path + ".cache";
509512
File::WriteBinary(cachePath, cachedData->data, length);
510513
delete cachedData;
514+
// make sure cache and js file have the same modification date
515+
struct stat result;
516+
struct utimbuf new_times;
517+
new_times.actime = time(nullptr);
518+
new_times.modtime = time(nullptr);
519+
if (stat(path.c_str(), &result) == 0) {
520+
auto jsLastModifiedTime = result.st_mtime;
521+
new_times.modtime = jsLastModifiedTime;
522+
}
523+
utime(cachePath.c_str(), &new_times);
511524
}
512525

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

0 commit comments

Comments
 (0)