Skip to content

Commit 2008a8c

Browse files
danbevBridgeAR
authored andcommitted
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: nodejs#18573 Refs: nodejs#9163 Refs: electron/electron#11299 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Matheus Marchini <[email protected]>
1 parent 181d522 commit 2008a8c

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

src/env-inl.h

+4
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ inline Environment* Environment::GetCurrent(
305305
info.Data().template As<v8::External>()->Value());
306306
}
307307

308+
inline Environment* Environment::GetThreadLocalEnv() {
309+
return static_cast<Environment*>(uv_key_get(&thread_local_env));
310+
}
311+
308312
inline Environment::Environment(IsolateData* isolate_data,
309313
v8::Local<v8::Context> context)
310314
: isolate_(context->GetIsolate()),

src/env.cc

+11
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ v8::CpuProfiler* IsolateData::GetCpuProfiler() {
7575
return cpu_profiler_;
7676
}
7777

78+
79+
void InitThreadLocalOnce() {
80+
CHECK_EQ(0, uv_key_create(&Environment::thread_local_env));
81+
}
82+
7883
void Environment::Start(int argc,
7984
const char* const* argv,
8085
int exec_argc,
@@ -142,6 +147,10 @@ void Environment::Start(int argc,
142147

143148
SetupProcessObject(this, argc, argv, exec_argc, exec_argv);
144149
LoadAsyncWrapperInfo(this);
150+
151+
static uv_once_t init_once = UV_ONCE_INIT;
152+
uv_once(&init_once, InitThreadLocalOnce);
153+
uv_key_set(&thread_local_env, this);
145154
}
146155

147156
void Environment::CleanupHandles() {
@@ -373,4 +382,6 @@ void Environment::AsyncHooks::grow_async_ids_stack() {
373382
async_ids_stack_.GetJSArray()).FromJust();
374383
}
375384

385+
uv_key_t Environment::thread_local_env = {};
386+
376387
} // namespace node

src/env.h

+3
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@ class Environment {
563563
static inline Environment* GetCurrent(
564564
const v8::PropertyCallbackInfo<T>& info);
565565

566+
static uv_key_t thread_local_env;
567+
static inline Environment* GetThreadLocalEnv();
568+
566569
inline Environment(IsolateData* isolate_data, v8::Local<v8::Context> context);
567570
inline ~Environment();
568571

src/node.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -4646,11 +4646,8 @@ uv_loop_t* GetCurrentEventLoop(v8::Isolate* isolate) {
46464646
}
46474647

46484648

4649-
static uv_key_t thread_local_env;
4650-
4651-
46524649
void AtExit(void (*cb)(void* arg), void* arg) {
4653-
auto env = static_cast<Environment*>(uv_key_get(&thread_local_env));
4650+
auto env = Environment::GetThreadLocalEnv();
46544651
AtExit(env, cb, arg);
46554652
}
46564653

@@ -4781,8 +4778,6 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
47814778
Local<Context> context = NewContext(isolate);
47824779
Context::Scope context_scope(context);
47834780
Environment env(isolate_data, context);
4784-
CHECK_EQ(0, uv_key_create(&thread_local_env));
4785-
uv_key_set(&thread_local_env, &env);
47864781
env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);
47874782

47884783
const char* path = argc > 1 ? argv[1] : nullptr;
@@ -4832,7 +4827,6 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
48324827

48334828
const int exit_code = EmitExit(&env);
48344829
RunAtExit(&env);
4835-
uv_key_delete(&thread_local_env);
48364830

48374831
v8_platform.DrainVMTasks(isolate);
48384832
v8_platform.CancelVMTasks(isolate);

test/cctest/test_environment.cc

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ TEST_F(EnvironmentTest, AtExitWithEnvironment) {
3939
EXPECT_TRUE(called_cb_1);
4040
}
4141

42+
TEST_F(EnvironmentTest, AtExitWithoutEnvironment) {
43+
const v8::HandleScope handle_scope(isolate_);
44+
const Argv argv;
45+
Env env {handle_scope, argv};
46+
47+
AtExit(at_exit_callback1); // No Environment is passed to AtExit.
48+
RunAtExit(*env);
49+
EXPECT_TRUE(called_cb_1);
50+
}
51+
4252
TEST_F(EnvironmentTest, AtExitWithArgument) {
4353
const v8::HandleScope handle_scope(isolate_);
4454
const Argv argv;

0 commit comments

Comments
 (0)