Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Revert "fix: allocate memory of Buffer with V8's allocator"
This reverts commit e66aa77.
  • Loading branch information
deepak1556 committed Jul 10, 2019
commit 982406ee4e5848d73e9a6a81496fecb379cc43cc
28 changes: 11 additions & 17 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ bool zero_fill_all_buffers = false;

namespace {

inline void* BufferMalloc(v8::Isolate* isolate, size_t length) {
auto* allocator = isolate->GetArrayBufferAllocator();
return zero_fill_all_buffers ? allocator->Allocate(length) :
allocator->AllocateUninitialized(length);
inline void* BufferMalloc(size_t length) {
return zero_fill_all_buffers ? node::UncheckedCalloc(length) :
node::UncheckedMalloc(length);
}

} // namespace
Expand Down Expand Up @@ -246,7 +245,7 @@ MaybeLocal<Object> New(Isolate* isolate,
char* data = nullptr;

if (length > 0) {
data = static_cast<char*>(BufferMalloc(isolate, length));
data = static_cast<char*>(BufferMalloc(length));

if (data == nullptr)
return Local<Object>();
Expand All @@ -255,14 +254,10 @@ MaybeLocal<Object> New(Isolate* isolate,
CHECK(actual <= length);

if (actual == 0) {
isolate->GetArrayBufferAllocator()->Free(data, length);
free(data);
data = nullptr;
} else if (actual < length) {
auto* allocator = isolate->GetArrayBufferAllocator();
auto* excessive_data = data;
data = static_cast<char*>(allocator->AllocateUninitialized(actual));
memcpy(data, excessive_data, actual);
allocator->Free(excessive_data, length);
data = node::Realloc(data, actual);
}
}

Expand All @@ -271,7 +266,7 @@ MaybeLocal<Object> New(Isolate* isolate,
return scope.Escape(buf);

// Object failed to be created. Clean up resources.
isolate->GetArrayBufferAllocator()->Free(data, length);
free(data);
return Local<Object>();
}

Expand All @@ -297,7 +292,7 @@ MaybeLocal<Object> New(Environment* env, size_t length) {

void* data;
if (length > 0) {
data = BufferMalloc(env->isolate(), length);
data = BufferMalloc(length);
if (data == nullptr)
return Local<Object>();
} else {
Expand All @@ -313,7 +308,7 @@ MaybeLocal<Object> New(Environment* env, size_t length) {

if (ui.IsEmpty()) {
// Object failed to be created. Clean up resources.
env->isolate()->GetArrayBufferAllocator()->Free(data, length);
free(data);
}

return scope.Escape(ui.FromMaybe(Local<Uint8Array>()));
Expand All @@ -339,11 +334,10 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
return Local<Object>();
}

auto* allocator = env->isolate()->GetArrayBufferAllocator();
void* new_data;
if (length > 0) {
CHECK_NOT_NULL(data);
new_data = allocator->AllocateUninitialized(length);
new_data = node::UncheckedMalloc(length);
if (new_data == nullptr)
return Local<Object>();
memcpy(new_data, data, length);
Expand All @@ -360,7 +354,7 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {

if (ui.IsEmpty()) {
// Object failed to be created. Clean up resources.
allocator->Free(new_data, length);
free(new_data);
}

return scope.Escape(ui.FromMaybe(Local<Uint8Array>()));
Expand Down
77 changes: 27 additions & 50 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1867,8 +1867,7 @@ void SSLWrap<Base>::GetFinished(const FunctionCallbackInfo<Value>& args) {
if (len == 0)
return;

auto* allocator = env->isolate()->GetArrayBufferAllocator();
char* buf = static_cast<char*>(allocator->AllocateUninitialized(len));
char* buf = Malloc(len);
CHECK_EQ(len, SSL_get_finished(w->ssl_.get(), buf, len));
args.GetReturnValue().Set(Buffer::New(env, buf, len).ToLocalChecked());
}
Expand All @@ -1891,8 +1890,7 @@ void SSLWrap<Base>::GetPeerFinished(const FunctionCallbackInfo<Value>& args) {
if (len == 0)
return;

auto* allocator = env->isolate()->GetArrayBufferAllocator();
char* buf = static_cast<char*>(allocator->AllocateUninitialized(len));
char* buf = Malloc(len);
CHECK_EQ(len, SSL_get_peer_finished(w->ssl_.get(), buf, len));
args.GetReturnValue().Set(Buffer::New(env, buf, len).ToLocalChecked());
}
Expand All @@ -1912,8 +1910,7 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
int slen = i2d_SSL_SESSION(sess, nullptr);
CHECK_GT(slen, 0);

auto* allocator = env->isolate()->GetArrayBufferAllocator();
char* sbuf = static_cast<char*>(allocator->AllocateUninitialized(slen));
char* sbuf = Malloc(slen);
unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
i2d_SSL_SESSION(sess, &p);
args.GetReturnValue().Set(Buffer::New(env, sbuf, slen).ToLocalChecked());
Expand Down Expand Up @@ -2334,12 +2331,11 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
size_t len = Buffer::Length(obj);

// OpenSSL takes control of the pointer after accepting it
auto* allocator = env->isolate()->GetArrayBufferAllocator();
uint8_t* data = static_cast<uint8_t*>(allocator->AllocateUninitialized(len));
char* data = node::Malloc(len);
memcpy(data, resp, len);

if (!SSL_set_tlsext_status_ocsp_resp(s, data, len))
allocator->Free(data, len);
free(data);
w->ocsp_response_.Reset();

return SSL_TLSEXT_ERR_OK;
Expand Down Expand Up @@ -3054,8 +3050,7 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
return kErrorState;
}

auto* allocator = env()->isolate()->GetArrayBufferAllocator();
*out = static_cast<unsigned char*>(allocator->AllocateUninitialized(buff_len));
*out = Malloc<unsigned char>(buff_len);
int r = EVP_CipherUpdate(ctx_.get(),
*out,
out_len,
Expand Down Expand Up @@ -3098,8 +3093,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
}

if (r != kSuccess) {
auto* allocator = env->isolate()->GetArrayBufferAllocator();
allocator->Free(out, out_len);
free(out);
if (r == kErrorState) {
ThrowCryptoError(env, ERR_get_error(),
"Trying to add data in unsupported state");
Expand Down Expand Up @@ -3138,9 +3132,8 @@ bool CipherBase::Final(unsigned char** out, int* out_len) {

const int mode = EVP_CIPHER_CTX_mode(ctx_.get());

auto* allocator = env()->isolate()->GetArrayBufferAllocator();
*out = static_cast<unsigned char*>(allocator->AllocateUninitialized(
EVP_CIPHER_CTX_block_size(ctx_.get())));
*out = Malloc<unsigned char>(
static_cast<size_t>(EVP_CIPHER_CTX_block_size(ctx_.get())));

if (kind_ == kDecipher && IsSupportedAuthenticatedMode(mode)) {
MaybePassAuthTagToOpenSSL();
Expand Down Expand Up @@ -3189,8 +3182,7 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
bool r = cipher->Final(&out_value, &out_len);

if (out_len <= 0 || !r) {
auto* allocator = env->isolate()->GetArrayBufferAllocator();
allocator->Free(out_value, out_len);
free(out_value);
out_value = nullptr;
out_len = 0;
if (!r) {
Expand Down Expand Up @@ -3839,8 +3831,7 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
template <PublicKeyCipher::Operation operation,
PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
bool PublicKeyCipher::Cipher(Environment* env,
const char* key_pem,
bool PublicKeyCipher::Cipher(const char* key_pem,
int key_pem_len,
const char* passphrase,
int padding,
Expand All @@ -3849,7 +3840,6 @@ bool PublicKeyCipher::Cipher(Environment* env,
unsigned char** out,
size_t* out_len) {
EVPKeyPointer pkey;
auto* allocator = env->isolate()->GetArrayBufferAllocator();

// Check if this is a PKCS#8 or RSA public key before trying as X.509 and
// private key.
Expand Down Expand Up @@ -3882,7 +3872,7 @@ bool PublicKeyCipher::Cipher(Environment* env,
if (EVP_PKEY_cipher(ctx.get(), nullptr, out_len, data, len) <= 0)
return false;

*out = static_cast<unsigned char*>(allocator->AllocateUninitialized(*out_len));
*out = Malloc<unsigned char>(*out_len);

if (EVP_PKEY_cipher(ctx.get(), *out, out_len, data, len) <= 0)
return false;
Expand Down Expand Up @@ -3916,7 +3906,6 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
ClearErrorOnReturn clear_error_on_return;

bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
env,
kbuf,
klen,
args.Length() >= 4 && !args[3]->IsNull() ? *passphrase : nullptr,
Expand All @@ -3927,8 +3916,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
&out_len);

if (out_len == 0 || !r) {
auto* allocator = env->isolate()->GetArrayBufferAllocator();
allocator->Free(out_value, out_len);
free(out_value);
out_value = nullptr;
out_len = 0;
if (!r) {
Expand Down Expand Up @@ -4141,8 +4129,7 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
const BIGNUM* pub_key;
DH_get0_key(diffieHellman->dh_.get(), &pub_key, nullptr);
size_t size = BN_num_bytes(pub_key);
auto* allocator = env->isolate()->GetArrayBufferAllocator();
char* data = static_cast<char*>(allocator->AllocateUninitialized(size));
char* data = Malloc(size);
BN_bn2bin(pub_key, reinterpret_cast<unsigned char*>(data));
args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
}
Expand All @@ -4161,8 +4148,7 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
if (num == nullptr) return env->ThrowError(err_if_null);

size_t size = BN_num_bytes(num);
auto* allocator = env->isolate()->GetArrayBufferAllocator();
char* data = static_cast<char*>(allocator->AllocateUninitialized(size));
char* data = Malloc(size);
BN_bn2bin(num, reinterpret_cast<unsigned char*>(data));
args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
}
Expand Down Expand Up @@ -4226,8 +4212,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
Buffer::Length(args[0]),
0));

auto* allocator = env->isolate()->GetArrayBufferAllocator();
MallocedBuffer<char> data(DH_size(diffieHellman->dh_.get()), allocator);
MallocedBuffer<char> data(DH_size(diffieHellman->dh_.get()));

int size = DH_compute_key(reinterpret_cast<unsigned char*>(data.data),
key.get(),
Expand Down Expand Up @@ -4447,14 +4432,13 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
}

// NOTE: field_size is in bits
auto* allocator = env->isolate()->GetArrayBufferAllocator();
int field_size = EC_GROUP_get_degree(ecdh->group_);
size_t out_len = (field_size + 7) / 8;
char* out = static_cast<char*>(allocator->AllocateUninitialized(out_len));
char* out = node::Malloc(out_len);

int r = ECDH_compute_key(out, out_len, pub.get(), ecdh->key_.get(), nullptr);
if (!r) {
allocator->Free(out, out_len);
free(out);
return env->ThrowError("Failed to compute ECDH key");
}

Expand Down Expand Up @@ -4485,13 +4469,11 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
if (size == 0)
return env->ThrowError("Failed to get public key length");

auto* allocator = env->isolate()->GetArrayBufferAllocator();
unsigned char* out =
static_cast<unsigned char*>(allocator->AllocateUninitialized(size));
unsigned char* out = node::Malloc<unsigned char>(size);

int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr);
if (r != size) {
allocator->Free(out, size);
free(out);
return env->ThrowError("Failed to get public key");
}

Expand All @@ -4511,13 +4493,11 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
if (b == nullptr)
return env->ThrowError("Failed to get ECDH private key");

auto* allocator = env->isolate()->GetArrayBufferAllocator();
int size = BN_num_bytes(b);
unsigned char* out =
static_cast<unsigned char*>(allocator->AllocateUninitialized(size));
unsigned char* out = node::Malloc<unsigned char>(size);

if (size != BN_bn2bin(b, out)) {
allocator->Free(out, size);
free(out);
return env->ThrowError("Failed to convert ECDH private key to Buffer");
}

Expand Down Expand Up @@ -4986,9 +4966,8 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
}


char* ExportPublicKey(Environment* env, const char* data, int len, size_t* size) {
char* ExportPublicKey(const char* data, int len, size_t* size) {
char* buf = nullptr;
auto* allocator = env->isolate()->GetArrayBufferAllocator();

BIOPointer bio(BIO_new(BIO_s_mem()));
if (!bio)
Expand All @@ -5009,7 +4988,7 @@ char* ExportPublicKey(Environment* env, const char* data, int len, size_t* size)
BIO_get_mem_ptr(bio.get(), &ptr);

*size = ptr->length;
buf = static_cast<char*>(allocator->AllocateUninitialized(*size));
buf = Malloc(*size);
memcpy(buf, ptr->data, *size);

return buf;
Expand All @@ -5027,7 +5006,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
CHECK_NOT_NULL(data);

size_t pkey_size;
char* pkey = ExportPublicKey(env, data, length, &pkey_size);
char* pkey = ExportPublicKey(data, length, &pkey_size);
if (pkey == nullptr)
return args.GetReturnValue().SetEmptyString();

Expand Down Expand Up @@ -5109,13 +5088,11 @@ void ConvertKey(const FunctionCallbackInfo<Value>& args) {
if (size == 0)
return env->ThrowError("Failed to get public key length");

auto* allocator = env->isolate()->GetArrayBufferAllocator();
unsigned char* out =
static_cast<unsigned char*>(allocator->AllocateUninitialized(size));
unsigned char* out = node::Malloc<unsigned char>(size);

int r = EC_POINT_point2oct(group.get(), pub.get(), form, out, size, nullptr);
if (r != size) {
allocator->Free(out, size);
free(out);
return env->ThrowError("Failed to get public key");
}

Expand Down
3 changes: 1 addition & 2 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,7 @@ class PublicKeyCipher {
template <Operation operation,
EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
EVP_PKEY_cipher_t EVP_PKEY_cipher>
static bool Cipher(Environment* env,
const char* key_pem,
static bool Cipher(const char* key_pem,
int key_pem_len,
const char* passphrase,
int padding,
Expand Down
Loading