mirror of
https://github.com/nodejs/node.git
synced 2024-11-30 23:43:09 +01:00
42c14c5c17
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>
83 lines
1.9 KiB
C++
83 lines
1.9 KiB
C++
#include "node_internals.h"
|
|
#include "libplatform/libplatform.h"
|
|
|
|
#include <string>
|
|
#include "gtest/gtest.h"
|
|
#include "node_test_fixture.h"
|
|
|
|
using node::AtExit;
|
|
using node::RunAtExit;
|
|
|
|
static bool called_cb_1 = false;
|
|
static bool called_cb_2 = false;
|
|
static void at_exit_callback1(void* arg);
|
|
static void at_exit_callback2(void* arg);
|
|
static std::string cb_1_arg; // NOLINT(runtime/string)
|
|
|
|
class EnvironmentTest : public EnvironmentTestFixture {
|
|
private:
|
|
virtual void TearDown() {
|
|
NodeTestFixture::TearDown();
|
|
called_cb_1 = false;
|
|
called_cb_2 = false;
|
|
}
|
|
};
|
|
|
|
TEST_F(EnvironmentTest, AtExitWithEnvironment) {
|
|
const v8::HandleScope handle_scope(isolate_);
|
|
const Argv argv;
|
|
Env env {handle_scope, argv};
|
|
|
|
AtExit(*env, at_exit_callback1);
|
|
RunAtExit(*env);
|
|
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;
|
|
Env env {handle_scope, argv};
|
|
|
|
std::string arg{"some args"};
|
|
AtExit(*env, at_exit_callback1, static_cast<void*>(&arg));
|
|
RunAtExit(*env);
|
|
EXPECT_EQ(arg, cb_1_arg);
|
|
}
|
|
|
|
TEST_F(EnvironmentTest, MultipleEnvironmentsPerIsolate) {
|
|
const v8::HandleScope handle_scope(isolate_);
|
|
const Argv argv;
|
|
Env env1 {handle_scope, argv};
|
|
Env env2 {handle_scope, argv};
|
|
|
|
AtExit(*env1, at_exit_callback1);
|
|
AtExit(*env2, at_exit_callback2);
|
|
RunAtExit(*env1);
|
|
EXPECT_TRUE(called_cb_1);
|
|
EXPECT_FALSE(called_cb_2);
|
|
|
|
RunAtExit(*env2);
|
|
EXPECT_TRUE(called_cb_2);
|
|
}
|
|
|
|
static void at_exit_callback1(void* arg) {
|
|
called_cb_1 = true;
|
|
if (arg) {
|
|
cb_1_arg = *static_cast<std::string*>(arg);
|
|
}
|
|
}
|
|
|
|
static void at_exit_callback2(void* arg) {
|
|
called_cb_2 = true;
|
|
}
|