From 7003a4e3d82b4aeb913844ec1014602c1869055c Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Mon, 1 Feb 2016 11:13:31 -0800 Subject: [PATCH] node_contextify: do not incept debug context Currently a debug context is created for various calls to util. If the node debugger is being run the main context is the debug context. In this case node_contextify was freeing the debug context and causing everything to explode. This change moves around the logic and no longer frees the context. There is a concern about the dangling pointer The regression test was adapted from code submitted by @3y3 in #4815 Fixes: https://github.com/nodejs/node/issues/4440 Fixes: https://github.com/nodejs/node/issues/4815 Fixes: https://github.com/nodejs/node/issues/4597 Fixes: https://github.com/nodejs/node/issues/4952 PR-URL: https://github.com/nodejs/node/issues/4815 Reviewed-By: Fedor Indutny Reviewed-By: Ben Noordhuis Reviewed-By: Rich Trott --- src/node_contextify.cc | 31 +++------ .../debugger-util-regression-fixture.js | 4 ++ .../parallel/test-debugger-util-regression.js | 69 +++++++++++++++++++ 3 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 test/fixtures/debugger-util-regression-fixture.js create mode 100644 test/parallel/test-debugger-util-regression.js diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 6675e2d8a08f70..dc6695893cc609 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -239,37 +239,26 @@ class ContextifyContext { static void RunInDebugContext(const FunctionCallbackInfo& args) { - // Ensure that the debug context has an Environment assigned in case - // a fatal error is raised. The fatal exception handler in node.cc - // is not equipped to deal with contexts that don't have one and - // can't easily be taught that due to a deficiency in the V8 API: - // there is no way for the embedder to tell if the data index is - // in use. - struct ScopedEnvironment { - ScopedEnvironment(Local context, Environment* env) - : context_(context) { - const int index = Environment::kContextEmbedderDataIndex; - context->SetAlignedPointerInEmbedderData(index, env); - } - ~ScopedEnvironment() { - const int index = Environment::kContextEmbedderDataIndex; - context_->SetAlignedPointerInEmbedderData(index, nullptr); - } - Local context_; - }; - Local script_source(args[0]->ToString(args.GetIsolate())); if (script_source.IsEmpty()) return; // Exception pending. Local debug_context = Debug::GetDebugContext(); + Environment* env = Environment::GetCurrent(args); if (debug_context.IsEmpty()) { // Force-load the debug context. Debug::GetMirror(args.GetIsolate()->GetCurrentContext(), args[0]); debug_context = Debug::GetDebugContext(); CHECK(!debug_context.IsEmpty()); + // Ensure that the debug context has an Environment assigned in case + // a fatal error is raised. The fatal exception handler in node.cc + // is not equipped to deal with contexts that don't have one and + // can't easily be taught that due to a deficiency in the V8 API: + // there is no way for the embedder to tell if the data index is + // in use. + const int index = Environment::kContextEmbedderDataIndex; + debug_context->SetAlignedPointerInEmbedderData(index, env); } - Environment* env = Environment::GetCurrent(args); - ScopedEnvironment env_scope(debug_context, env); + Context::Scope context_scope(debug_context); Local