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: #18573
Refs: #9163
Refs: electron/electron#11299
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
  • Loading branch information
danbev committed Feb 16, 2018
1 parent efb3259 commit 42c14c5
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
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
11 changes: 11 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 1 addition & 7 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions test/cctest/test_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 42c14c5

Please sign in to comment.