Skip to content

Commit

Permalink
src: set thread local env in CreateEnvironment
Browse files Browse the repository at this point in the history
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: nodejs#18573
Refs: nodejs#9163
Refs: electron/electron#11299
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matheus Marchini <matheus@sthima.com>
  • Loading branch information
danbev authored and BridgeAR committed Apr 30, 2018
1 parent 181d522 commit 2008a8c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/env-inl.h
Expand Up @@ -305,6 +305,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()),
Expand Down
11 changes: 11 additions & 0 deletions src/env.cc
Expand Up @@ -75,6 +75,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,
Expand Down Expand Up @@ -142,6 +147,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() {
Expand Down Expand Up @@ -373,4 +382,6 @@ void Environment::AsyncHooks::grow_async_ids_stack() {
async_ids_stack_.GetJSArray()).FromJust();
}

uv_key_t Environment::thread_local_env = {};

} // namespace node
3 changes: 3 additions & 0 deletions src/env.h
Expand Up @@ -563,6 +563,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();

Expand Down
8 changes: 1 addition & 7 deletions src/node.cc
Expand Up @@ -4646,11 +4646,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);
}

Expand Down Expand Up @@ -4781,8 +4778,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;
Expand Down Expand Up @@ -4832,7 +4827,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);
Expand Down
10 changes: 10 additions & 0 deletions test/cctest/test_environment.cc
Expand Up @@ -39,6 +39,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;
Expand Down

0 comments on commit 2008a8c

Please sign in to comment.