From eb2a97a6f9069312f219505a77d9c265e7471596 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sat, 2 Jun 2018 18:10:34 +0200 Subject: [PATCH 1/2] src: store pointer to Environment on DestroyParam MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To avoid a potential segfault when inside WeakCallback, store a reference to Environment inside DestroyParam. Co-authored-by: Yang Guo Co-authored-by: Michaƫl Zasso --- src/async_wrap.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 11ce5a387a950c..c5ca1fb0b443ab 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -398,6 +398,7 @@ static void DisablePromiseHook(const FunctionCallbackInfo& args) { class DestroyParam { public: double asyncId; + Environment* env; Persistent target; Persistent propBag; }; @@ -406,13 +407,12 @@ class DestroyParam { void AsyncWrap::WeakCallback(const v8::WeakCallbackInfo& info) { HandleScope scope(info.GetIsolate()); - Environment* env = Environment::GetCurrent(info.GetIsolate()); std::unique_ptr p{info.GetParameter()}; Local prop_bag = PersistentToLocal(info.GetIsolate(), p->propBag); - Local val = prop_bag->Get(env->destroyed_string()); + Local val = prop_bag->Get(p->env->destroyed_string()); if (val->IsFalse()) { - AsyncWrap::EmitDestroy(env, p->asyncId); + AsyncWrap::EmitDestroy(p->env, p->asyncId); } // unique_ptr goes out of scope here and pointer is deleted. } @@ -426,6 +426,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); DestroyParam* p = new DestroyParam(); p->asyncId = args[1].As()->Value(); + p->env = Environment::GetCurrent(isolate); p->target.Reset(isolate, args[0].As()); p->propBag.Reset(isolate, args[2].As()); p->target.SetWeak( From b3a86f30d6a44643822d9408908427414452f2c3 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sun, 3 Jun 2018 17:31:40 +0200 Subject: [PATCH 2/2] fixup: addaleax nit --- src/async_wrap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index c5ca1fb0b443ab..87c50950faadcc 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -426,7 +426,7 @@ static void RegisterDestroyHook(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); DestroyParam* p = new DestroyParam(); p->asyncId = args[1].As()->Value(); - p->env = Environment::GetCurrent(isolate); + p->env = Environment::GetCurrent(args); p->target.Reset(isolate, args[0].As()); p->propBag.Reset(isolate, args[2].As()); p->target.SetWeak(