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: ensure same mtime for js and code cache to prevent loading old code caches #1822

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Changes from all commits
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
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
Loading