Allocate memory of Buffer with V8's allocator · electron/node@f61bae3
@@ -1890,7 +1890,8 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
18901890int slen = i2d_SSL_SESSION(sess, nullptr);
18911891CHECK_GT(slen, 0);
189218921893-char* sbuf = Malloc(slen);
1893+auto* allocator = env->isolate()->GetArrayBufferAllocator();
1894+char* sbuf = static_cast<char*>(allocator->AllocateUninitialized(slen));
18941895unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
18951896i2d_SSL_SESSION(sess, &p);
18961897 args.GetReturnValue().Set(Buffer::New(env, sbuf, slen).ToLocalChecked());
@@ -3011,7 +3012,8 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
30113012return kErrorState;
30123013 }
301330143014- *out = Malloc<unsigned char>(buff_len);
3015+auto* allocator = env()->isolate()->GetArrayBufferAllocator();
3016+ *out = static_cast<unsigned char*>(allocator->AllocateUninitialized(buff_len));
30153017int r = EVP_CipherUpdate(ctx_.get(),
30163018 *out,
30173019 out_len,
@@ -3053,7 +3055,8 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
30533055 }
3054305630553057if (r != kSuccess) {
3056-free(out);
3058+auto* allocator = env->isolate()->GetArrayBufferAllocator();
3059+ allocator->Free(out, out_len);
30573060if (r == kErrorState) {
30583061ThrowCryptoError(env, ERR_get_error(),
30593062"Trying to add data in unsupported state");
@@ -3091,8 +3094,9 @@ bool CipherBase::Final(unsigned char** out, int* out_len) {
3091309430923095const int mode = EVP_CIPHER_CTX_mode(ctx_.get());
309330963094- *out = Malloc<unsigned char>(
3095-static_cast<size_t>(EVP_CIPHER_CTX_block_size(ctx_.get())));
3097+auto* allocator = env()->isolate()->GetArrayBufferAllocator();
3098+ *out = static_cast<unsigned char*>(allocator->AllocateUninitialized(
3099+EVP_CIPHER_CTX_block_size(ctx_.get())));
3096310030973101// In CCM mode, final() only checks whether authentication failed in update().
30983102// EVP_CipherFinal_ex must not be called and will fail.
@@ -3135,7 +3139,8 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
31353139bool r = cipher->Final(&out_value, &out_len);
3136314031373141if (out_len <= 0 || !r) {
3138-free(out_value);
3142+auto* allocator = env->isolate()->GetArrayBufferAllocator();
3143+ allocator->Free(out_value, out_len);
31393144 out_value = nullptr;
31403145 out_len = 0;
31413146if (!r) {
@@ -3781,7 +3786,8 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
37813786template <PublicKeyCipher::Operation operation,
37823787 PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
37833788 PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
3784-bool PublicKeyCipher::Cipher(const char* key_pem,
3789+bool PublicKeyCipher::Cipher(Environment* env,
3790+const char* key_pem,
37853791int key_pem_len,
37863792const char* passphrase,
37873793int padding,
@@ -3790,6 +3796,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
37903796unsigned char** out,
37913797size_t* out_len) {
37923798 EVPKeyPointer pkey;
3799+auto* allocator = env->isolate()->GetArrayBufferAllocator();
3793380037943801 BIOPointer bp(BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len));
37953802if (!bp)
@@ -3837,7 +3844,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
38373844if (EVP_PKEY_cipher(ctx.get(), nullptr, out_len, data, len) <= 0)
38383845return false;
383938463840- *out = Malloc<unsigned char>(*out_len);
3847+ *out = static_cast<unsigned char*>(allocator->AllocateUninitialized(*out_len));
3841384838423849if (EVP_PKEY_cipher(ctx.get(), *out, out_len, data, len) <= 0)
38433850return false;
@@ -3870,6 +3877,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
38703877 ClearErrorOnReturn clear_error_on_return;
3871387838723879bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
3880+ env,
38733881 kbuf,
38743882 klen,
38753883 args.Length() >= 3 && !args[2]->IsNull() ? *passphrase : nullptr,
@@ -3880,7 +3888,8 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
38803888&out_len);
3881388938823890if (out_len == 0 || !r) {
3883-free(out_value);
3891+auto* allocator = env->isolate()->GetArrayBufferAllocator();
3892+ allocator->Free(out_value, out_len);
38843893 out_value = nullptr;
38853894 out_len = 0;
38863895if (!r) {
@@ -4085,7 +4094,8 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
40854094const BIGNUM* pub_key;
40864095DH_get0_key(diffieHellman->dh_.get(), &pub_key, nullptr);
40874096size_t size = BN_num_bytes(pub_key);
4088-char* data = Malloc(size);
4097+auto* allocator = env->isolate()->GetArrayBufferAllocator();
4098+char* data = static_cast<char*>(allocator->AllocateUninitialized(size));
40894099BN_bn2bin(pub_key, reinterpret_cast<unsigned char*>(data));
40904100 args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
40914101}
@@ -4104,7 +4114,8 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
41044114if (num == nullptr) return env->ThrowError(err_if_null);
4105411541064116size_t size = BN_num_bytes(num);
4107-char* data = Malloc(size);
4117+auto* allocator = env->isolate()->GetArrayBufferAllocator();
4118+char* data = static_cast<char*>(allocator->AllocateUninitialized(size));
41084119BN_bn2bin(num, reinterpret_cast<unsigned char*>(data));
41094120 args.GetReturnValue().Set(Buffer::New(env, data, size).ToLocalChecked());
41104121}
@@ -4168,7 +4179,8 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
41684179Buffer::Length(args[0]),
416941800));
417041814171- MallocedBuffer<char> data(DH_size(diffieHellman->dh_.get()));
4182+auto* allocator = env->isolate()->GetArrayBufferAllocator();
4183+ MallocedBuffer<char> data(DH_size(diffieHellman->dh_.get()), allocator);
4172418441734185int size = DH_compute_key(reinterpret_cast<unsigned char*>(data.data),
41744186 key.get(),
@@ -4388,13 +4400,14 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
43884400 }
4389440143904402// NOTE: field_size is in bits
4403+auto* allocator = env->isolate()->GetArrayBufferAllocator();
43914404int field_size = EC_GROUP_get_degree(ecdh->group_);
43924405size_t out_len = (field_size + 7) / 8;
4393-char* out = node::Malloc(out_len);
4406+char* out = static_cast<char*>(allocator->AllocateUninitialized(out_len));
4394440743954408int r = ECDH_compute_key(out, out_len, pub.get(), ecdh->key_.get(), nullptr);
43964409if (!r) {
4397-free(out);
4410+allocator->Free(out, out_len);
43984411return env->ThrowError("Failed to compute ECDH key");
43994412 }
44004413@@ -4424,11 +4437,13 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
44244437if (size == 0)
44254438return env->ThrowError("Failed to get public key length");
442644394427-unsigned char* out = node::Malloc<unsigned char>(size);
4440+auto* allocator = env->isolate()->GetArrayBufferAllocator();
4441+unsigned char* out =
4442+static_cast<unsigned char*>(allocator->AllocateUninitialized(size));
4428444344294444int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr);
44304445if (r != size) {
4431-free(out);
4446+allocator->Free(out, size);
44324447return env->ThrowError("Failed to get public key");
44334448 }
44344449@@ -4448,11 +4463,13 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
44484463if (b == nullptr)
44494464return env->ThrowError("Failed to get ECDH private key");
445044654466+auto* allocator = env->isolate()->GetArrayBufferAllocator();
44514467int size = BN_num_bytes(b);
4452-unsigned char* out = node::Malloc<unsigned char>(size);
4468+unsigned char* out =
4469+static_cast<unsigned char*>(allocator->AllocateUninitialized(size));
4453447044544471if (size != BN_bn2bin(b, out)) {
4455-free(out);
4472+allocator->Free(out, size);
44564473return env->ThrowError("Failed to convert ECDH private key to Buffer");
44574474 }
44584475@@ -4572,7 +4589,7 @@ class PBKDF2Request : public AsyncWrap, public ThreadPoolWork {
45724589 success_(false),
45734590 pass_(std::move(pass)),
45744591 salt_(std::move(salt)),
4575- key_(keylen),
4592+ key_(keylen, env->isolate()->GetArrayBufferAllocator()),
45764593 iteration_count_(iteration_count) {
45774594 }
45784595@@ -4634,6 +4651,7 @@ void PBKDF2Request::AfterThreadPoolWork(int status) {
4634465146354652void PBKDF2(const FunctionCallbackInfo<Value>& args) {
46364653 Environment* env = Environment::GetCurrent(args);
4654+auto* allocator = env->isolate()->GetArrayBufferAllocator();
4637465546384656const EVP_MD* digest = nullptr;
46394657int keylen = -1;
@@ -4642,12 +4660,12 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
4642466046434661int passlen = Buffer::Length(args[0]);
464446624645- MallocedBuffer<char> pass(passlen);
4663+ MallocedBuffer<char> pass(passlen, allocator);
46464664memcpy(pass.data, Buffer::Data(args[0]), passlen);
4647466546484666int saltlen = Buffer::Length(args[1]);
464946674650- MallocedBuffer<char> salt(saltlen);
4668+ MallocedBuffer<char> salt(saltlen, allocator);
46514669memcpy(salt.data, Buffer::Data(args[1]), saltlen);
4652467046534671 iteration_count = args[2]->Int32Value(env->context()).FromJust();
@@ -4724,9 +4742,10 @@ class RandomBytesRequest : public AsyncWrap, public ThreadPoolWork {
47244742 }
4725474347264744inline void release() {
4745+size_t free_size = size_;
47274746 size_ = 0;
47284747if (free_mode_ == FREE_DATA) {
4729-free(data_);
4748+env()->isolate()->GetArrayBufferAllocator()->Free(data_, free_size);
47304749 data_ = nullptr;
47314750 }
47324751 }
@@ -4840,7 +4859,8 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
4840485948414860 Local<Object> obj = env->randombytes_constructor_template()->
48424861NewInstance(env->context()).ToLocalChecked();
4843-char* data = node::Malloc(size);
4862+char* data = static_cast<char*>(
4863+ env->isolate()->GetArrayBufferAllocator()->AllocateUninitialized(size));
48444864 std::unique_ptr<RandomBytesRequest> req(
48454865new RandomBytesRequest(env,
48464866 obj,
@@ -5015,8 +5035,9 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
50155035}
50165036501750375018-char* ExportPublicKey(const char* data, int len, size_t* size) {
5038+char* ExportPublicKey(Environment* env, const char* data, int len, size_t* size) {
50195039char* buf = nullptr;
5040+auto* allocator = env->isolate()->GetArrayBufferAllocator();
5020504150215042 BIOPointer bio(BIO_new(BIO_s_mem()));
50225043if (!bio)
@@ -5037,7 +5058,7 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
50375058BIO_get_mem_ptr(bio.get(), &ptr);
5038505950395060 *size = ptr->length;
5040- buf = Malloc(*size);
5061+ buf = static_cast<char*>(allocator->AllocateUninitialized(*size));
50415062memcpy(buf, ptr->data, *size);
5042506350435064return buf;
@@ -5055,7 +5076,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
50555076CHECK_NE(data, nullptr);
5056507750575078size_t pkey_size;
5058-char* pkey = ExportPublicKey(data, length, &pkey_size);
5079+char* pkey = ExportPublicKey(env, data, length, &pkey_size);
50595080if (pkey == nullptr)
50605081return args.GetReturnValue().SetEmptyString();
50615082