diff options
author | Daniel Bevenius <daniel.bevenius@gmail.com> | 2018-02-05 11:20:07 +0100 |
---|---|---|
committer | Daniel Bevenius <daniel.bevenius@gmail.com> | 2018-02-16 07:27:08 +0100 |
commit | 42c14c5c17c427aa82722fff41f3e723354239b9 (patch) | |
tree | c5430c97db3eb9ad1f081d0f5ee2182007272aef | |
parent | efb32592e1b78ec2559e1a409faa049e756a9501 (diff) | |
download | android-node-v8-42c14c5c17c427aa82722fff41f3e723354239b9.tar.gz android-node-v8-42c14c5c17c427aa82722fff41f3e723354239b9.tar.bz2 android-node-v8-42c14c5c17c427aa82722fff41f3e723354239b9.zip |
src: set thread local env in CreateEnvironment
This commit set the Environment as a thread local when CreateEnvironment
is called which is currently not being done. This would lead to a
segment fault if later node::AtExit is called without specifying the
environment parameter. This specific issue was reported by Electron.
If I recall correctly, back when this was implemented the motivation was
that if embedders have multiple environments per isolate they should be
using the AtExit functions that take an environment. This is not the
case with Electron which only create a single environment (as far as I
know), and if a native module calls AtExit this would lead to the
segment fault.
I was able to reproduce Electron issue and the provided test simulates
it. I was also able to use this patch and verify that it works for the
Electron issue as well.
PR-URL: https://github.com/nodejs/node/pull/18573
Refs: https://github.com/nodejs/node/pull/9163
Refs: https://github.com/electron/electron/issues/11299
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matheus Marchini <matheus@sthima.com>
-rw-r--r-- | src/env-inl.h | 4 | ||||
-rw-r--r-- | src/env.cc | 11 | ||||
-rw-r--r-- | src/env.h | 3 | ||||
-rw-r--r-- | src/node.cc | 8 | ||||
-rw-r--r-- | test/cctest/test_environment.cc | 10 |
5 files changed, 29 insertions, 7 deletions
diff --git a/src/env-inl.h b/src/env-inl.h index 5643fffb6f..1e64a0fda5 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -311,6 +311,10 @@ inline Environment* Environment::GetCurrent( info.Data().template As<v8::External>()->Value()); } +inline Environment* Environment::GetThreadLocalEnv() { + return static_cast<Environment*>(uv_key_get(&thread_local_env)); +} + inline Environment::Environment(IsolateData* isolate_data, v8::Local<v8::Context> context) : isolate_(context->GetIsolate()), diff --git a/src/env.cc b/src/env.cc index 07e37498c8..cb1f4a231b 100644 --- a/src/env.cc +++ b/src/env.cc @@ -80,6 +80,11 @@ v8::CpuProfiler* IsolateData::GetCpuProfiler() { return cpu_profiler_; } + +void InitThreadLocalOnce() { + CHECK_EQ(0, uv_key_create(&Environment::thread_local_env)); +} + void Environment::Start(int argc, const char* const* argv, int exec_argc, @@ -147,6 +152,10 @@ void Environment::Start(int argc, SetupProcessObject(this, argc, argv, exec_argc, exec_argv); LoadAsyncWrapperInfo(this); + + static uv_once_t init_once = UV_ONCE_INIT; + uv_once(&init_once, InitThreadLocalOnce); + uv_key_set(&thread_local_env, this); } void Environment::CleanupHandles() { @@ -471,4 +480,6 @@ void Environment::AsyncHooks::grow_async_ids_stack() { async_ids_stack_.GetJSArray()).FromJust(); } +uv_key_t Environment::thread_local_env = {}; + } // namespace node @@ -564,6 +564,9 @@ class Environment { static inline Environment* GetCurrent( const v8::PropertyCallbackInfo<T>& info); + static uv_key_t thread_local_env; + static inline Environment* GetThreadLocalEnv(); + inline Environment(IsolateData* isolate_data, v8::Local<v8::Context> context); inline ~Environment(); diff --git a/src/node.cc b/src/node.cc index 149d287950..0d62c89b03 100644 --- a/src/node.cc +++ b/src/node.cc @@ -4207,11 +4207,8 @@ uv_loop_t* GetCurrentEventLoop(v8::Isolate* isolate) { } -static uv_key_t thread_local_env; - - void AtExit(void (*cb)(void* arg), void* arg) { - auto env = static_cast<Environment*>(uv_key_get(&thread_local_env)); + auto env = Environment::GetThreadLocalEnv(); AtExit(env, cb, arg); } @@ -4342,8 +4339,6 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data, Local<Context> context = NewContext(isolate); Context::Scope context_scope(context); Environment env(isolate_data, context); - CHECK_EQ(0, uv_key_create(&thread_local_env)); - uv_key_set(&thread_local_env, &env); env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling); const char* path = argc > 1 ? argv[1] : nullptr; @@ -4393,7 +4388,6 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data, const int exit_code = EmitExit(&env); RunAtExit(&env); - uv_key_delete(&thread_local_env); v8_platform.DrainVMTasks(isolate); v8_platform.CancelVMTasks(isolate); diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index c559a21fda..07170ac267 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -33,6 +33,16 @@ TEST_F(EnvironmentTest, AtExitWithEnvironment) { EXPECT_TRUE(called_cb_1); } +TEST_F(EnvironmentTest, AtExitWithoutEnvironment) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env env {handle_scope, argv}; + + AtExit(at_exit_callback1); // No Environment is passed to AtExit. + RunAtExit(*env); + EXPECT_TRUE(called_cb_1); +} + TEST_F(EnvironmentTest, AtExitWithArgument) { const v8::HandleScope handle_scope(isolate_); const Argv argv; |