Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thread-safety issues in native bindings replacing Nan::Persistent with v8::Eternal #1419

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 48 additions & 24 deletions lib/protocol/crypto/src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,15 @@ class ChaChaPolyCipher : public ObjectWrap {
SetPrototypeMethod(tpl, "encrypt", Encrypt);
SetPrototypeMethod(tpl, "free", Free);

constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
Local<Context> context = Nan::GetCurrentContext();
v8::Isolate* isolate = context->GetIsolate();

constructor().Set(isolate, func);

Nan::Set(target,
Nan::New("ChaChaPolyCipher").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
func);
}

private:
Expand Down Expand Up @@ -387,8 +391,8 @@ class ChaChaPolyCipher : public ObjectWrap {
obj->clear();
}

static inline Nan::Persistent<Function> & constructor() {
static Nan::Persistent<Function> my_constructor;
static inline v8::Eternal<v8::Function> & constructor() {
static v8::Eternal<v8::Function> my_constructor;
return my_constructor;
}

Expand All @@ -414,11 +418,15 @@ class AESGCMCipher : public ObjectWrap {
SetPrototypeMethod(tpl, "encrypt", Encrypt);
SetPrototypeMethod(tpl, "free", Free);

constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
Local<Context> context = Nan::GetCurrentContext();
v8::Isolate* isolate = context->GetIsolate();

constructor().Set(isolate, func);

Nan::Set(target,
Nan::New("AESGCMCipher").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
func);
}

private:
Expand Down Expand Up @@ -633,8 +641,8 @@ class AESGCMCipher : public ObjectWrap {
obj->clear();
}

static inline Nan::Persistent<Function> & constructor() {
static Nan::Persistent<Function> my_constructor;
static inline v8::Eternal<v8::Function> & constructor() {
static v8::Eternal<v8::Function> my_constructor;
return my_constructor;
}

Expand All @@ -651,11 +659,15 @@ class GenericCipher : public ObjectWrap {
SetPrototypeMethod(tpl, "encrypt", Encrypt);
SetPrototypeMethod(tpl, "free", Free);

constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
Local<Context> context = Nan::GetCurrentContext();
v8::Isolate* isolate = context->GetIsolate();

constructor().Set(isolate, func);

Nan::Set(target,
Nan::New("GenericCipher").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
func);
}

private:
Expand Down Expand Up @@ -1014,8 +1026,8 @@ class GenericCipher : public ObjectWrap {
obj->clear();
}

static inline Nan::Persistent<Function> & constructor() {
static Nan::Persistent<Function> my_constructor;
static inline v8::Eternal<v8::Function> & constructor() {
static v8::Eternal<v8::Function> my_constructor;
return my_constructor;
}

Expand Down Expand Up @@ -1044,11 +1056,15 @@ class ChaChaPolyDecipher : public ObjectWrap {
SetPrototypeMethod(tpl, "decryptLen", DecryptLen);
SetPrototypeMethod(tpl, "free", Free);

constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
Local<Context> context = Nan::GetCurrentContext();
v8::Isolate* isolate = context->GetIsolate();

constructor().Set(isolate, func);

Nan::Set(target,
Nan::New("ChaChaPolyDecipher").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
func);
}

private:
Expand Down Expand Up @@ -1440,8 +1456,8 @@ class ChaChaPolyDecipher : public ObjectWrap {
obj->clear();
}

static inline Nan::Persistent<Function> & constructor() {
static Nan::Persistent<Function> my_constructor;
static inline v8::Eternal<v8::Function> & constructor() {
static v8::Eternal<v8::Function> my_constructor;
return my_constructor;
}

Expand All @@ -1468,11 +1484,15 @@ class AESGCMDecipher : public ObjectWrap {
SetPrototypeMethod(tpl, "decrypt", Decrypt);
SetPrototypeMethod(tpl, "free", Free);

constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
Local<Context> context = Nan::GetCurrentContext();
v8::Isolate* isolate = context->GetIsolate();

constructor().Set(isolate, func);

Nan::Set(target,
Nan::New("AESGCMDecipher").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
func);
}

private:
Expand Down Expand Up @@ -1697,8 +1717,8 @@ class AESGCMDecipher : public ObjectWrap {
obj->clear();
}

static inline Nan::Persistent<Function> & constructor() {
static Nan::Persistent<Function> my_constructor;
static inline v8::Eternal<v8::Function> & constructor() {
static v8::Eternal<v8::Function> my_constructor;
return my_constructor;
}

Expand All @@ -1716,11 +1736,15 @@ class GenericDecipher : public ObjectWrap {
SetPrototypeMethod(tpl, "decrypt", Decrypt);
SetPrototypeMethod(tpl, "free", Free);

constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Local<Function> func = Nan::GetFunction(tpl).ToLocalChecked();
Local<Context> context = Nan::GetCurrentContext();
v8::Isolate* isolate = context->GetIsolate();

constructor().Set(isolate, func);

Nan::Set(target,
Nan::New("GenericDecipher").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
func);
}

private:
Expand Down Expand Up @@ -2183,8 +2207,8 @@ class GenericDecipher : public ObjectWrap {
obj->clear();
}

static inline Nan::Persistent<Function> & constructor() {
static Nan::Persistent<Function> my_constructor;
static inline v8::Eternal<v8::Function> & constructor() {
static v8::Eternal<v8::Function> my_constructor;
return my_constructor;
}

Expand Down