Commit 03b4d391 authored by Mythri's avatar Mythri Committed by Commit Bot

Fix a race condition when accessing code_cache_map_ in d8

code_cache_map_ is used to store the serialized code that could
be consumed in the subsequent runs. This is a std::map which is not
thread safe. So protect this with mutex to avoid any race conditions
when executing in multiple isolates in parallel.

Bug: chromium:783124
Change-Id: Ie58402b8547cf3e83716b8d97d98a298745e487f
Reviewed-on: https://chromium-review.googlesource.com/806334Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Commit-Queue: Mythri Alle <mythria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49837}
parent 1ce7f0e8
...@@ -506,6 +506,7 @@ std::vector<Worker*> Shell::workers_; ...@@ -506,6 +506,7 @@ std::vector<Worker*> Shell::workers_;
std::vector<ExternalizedContents> Shell::externalized_contents_; std::vector<ExternalizedContents> Shell::externalized_contents_;
base::LazyMutex Shell::isolate_status_lock_; base::LazyMutex Shell::isolate_status_lock_;
std::map<v8::Isolate*, bool> Shell::isolate_status_; std::map<v8::Isolate*, bool> Shell::isolate_status_;
base::LazyMutex Shell::cached_code_mutex_;
std::map<std::string, std::unique_ptr<ScriptCompiler::CachedData>> std::map<std::string, std::unique_ptr<ScriptCompiler::CachedData>>
Shell::cached_code_map_; Shell::cached_code_map_;
...@@ -570,6 +571,7 @@ class BackgroundCompileThread : public base::Thread { ...@@ -570,6 +571,7 @@ class BackgroundCompileThread : public base::Thread {
ScriptCompiler::CachedData* Shell::LookupCodeCache(Isolate* isolate, ScriptCompiler::CachedData* Shell::LookupCodeCache(Isolate* isolate,
Local<Value> source) { Local<Value> source) {
base::LockGuard<base::Mutex> lock_guard(cached_code_mutex_.Pointer());
CHECK(source->IsString()); CHECK(source->IsString());
v8::String::Utf8Value key(isolate, source); v8::String::Utf8Value key(isolate, source);
DCHECK(*key); DCHECK(*key);
...@@ -587,6 +589,7 @@ ScriptCompiler::CachedData* Shell::LookupCodeCache(Isolate* isolate, ...@@ -587,6 +589,7 @@ ScriptCompiler::CachedData* Shell::LookupCodeCache(Isolate* isolate,
void Shell::StoreInCodeCache(Isolate* isolate, Local<Value> source, void Shell::StoreInCodeCache(Isolate* isolate, Local<Value> source,
const ScriptCompiler::CachedData* cache_data) { const ScriptCompiler::CachedData* cache_data) {
base::LockGuard<base::Mutex> lock_guard(cached_code_mutex_.Pointer());
CHECK(source->IsString()); CHECK(source->IsString());
if (cache_data == nullptr) return; if (cache_data == nullptr) return;
v8::String::Utf8Value key(isolate, source); v8::String::Utf8Value key(isolate, source);
...@@ -3435,7 +3438,6 @@ int Shell::Main(int argc, char* argv[]) { ...@@ -3435,7 +3438,6 @@ int Shell::Main(int argc, char* argv[]) {
result = RunMain(isolate2, argc, argv, true); result = RunMain(isolate2, argc, argv, true);
} }
cached_code_map_.clear();
isolate2->Dispose(); isolate2->Dispose();
} else { } else {
bool last_run = true; bool last_run = true;
...@@ -3454,6 +3456,7 @@ int Shell::Main(int argc, char* argv[]) { ...@@ -3454,6 +3456,7 @@ int Shell::Main(int argc, char* argv[]) {
} }
// Shut down contexts and collect garbage. // Shut down contexts and collect garbage.
cached_code_map_.clear();
evaluation_context_.Reset(); evaluation_context_.Reset();
stringify_function_.Reset(); stringify_function_.Reset();
CollectGarbage(isolate); CollectGarbage(isolate);
......
...@@ -513,6 +513,8 @@ class Shell : public i::AllStatic { ...@@ -513,6 +513,8 @@ class Shell : public i::AllStatic {
// the isolate_status_ needs to be concurrency-safe. // the isolate_status_ needs to be concurrency-safe.
static base::LazyMutex isolate_status_lock_; static base::LazyMutex isolate_status_lock_;
static std::map<Isolate*, bool> isolate_status_; static std::map<Isolate*, bool> isolate_status_;
static base::LazyMutex cached_code_mutex_;
static std::map<std::string, std::unique_ptr<ScriptCompiler::CachedData>> static std::map<std::string, std::unique_ptr<ScriptCompiler::CachedData>>
cached_code_map_; cached_code_map_;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment