Skip to content

Commit 8670613

Browse files
committed
node_crypto_bio: adjust external memory size
Adjust V8's external memory size when allocating buffers for TLS data to ensure that V8 has enough information to trigger the GC at right time. PR-URL: #1085 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent f8c893d commit 8670613

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/node_crypto_bio.cc

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ BIO* NodeBIO::New() {
2727
}
2828

2929

30+
void NodeBIO::AssignEnvironment(Environment* env) {
31+
env_ = env;
32+
}
33+
34+
3035
int NodeBIO::New(BIO* bio) {
3136
bio->ptr = new NodeBIO();
3237

@@ -399,7 +404,7 @@ void NodeBIO::TryAllocateForWrite(size_t hint) {
399404
kThroughputBufferLength;
400405
if (len < hint)
401406
len = hint;
402-
Buffer* next = new Buffer(len);
407+
Buffer* next = new Buffer(env_, len);
403408

404409
if (w == nullptr) {
405410
next->next_ = next;

src/node_crypto_bio.h

+18-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
#define SRC_NODE_CRYPTO_BIO_H_
33

44
#include "openssl/bio.h"
5+
#include "env.h"
6+
#include "env-inl.h"
57
#include "util.h"
68
#include "util-inl.h"
9+
#include "v8.h"
710

811
namespace node {
912

1013
class NodeBIO {
1114
public:
12-
NodeBIO() : initial_(kInitialBufferLength),
15+
NodeBIO() : env_(nullptr),
16+
initial_(kInitialBufferLength),
1317
length_(0),
1418
read_head_(nullptr),
1519
write_head_(nullptr) {
@@ -19,6 +23,8 @@ class NodeBIO {
1923

2024
static BIO* New();
2125

26+
void AssignEnvironment(Environment* env);
27+
2228
// Move read head to next buffer if needed
2329
void TryMoveReadHead();
2430

@@ -89,24 +95,31 @@ class NodeBIO {
8995

9096
class Buffer {
9197
public:
92-
explicit Buffer(size_t len) : read_pos_(0),
93-
write_pos_(0),
94-
len_(len),
95-
next_(nullptr) {
98+
Buffer(Environment* env, size_t len) : env_(env),
99+
read_pos_(0),
100+
write_pos_(0),
101+
len_(len),
102+
next_(nullptr) {
96103
data_ = new char[len];
104+
if (env_ != nullptr)
105+
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
97106
}
98107

99108
~Buffer() {
100109
delete[] data_;
110+
if (env_ != nullptr)
111+
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len_);
101112
}
102113

114+
Environment* env_;
103115
size_t read_pos_;
104116
size_t write_pos_;
105117
size_t len_;
106118
Buffer* next_;
107119
char* data_;
108120
};
109121

122+
Environment* env_;
110123
size_t initial_;
111124
size_t length_;
112125
Buffer* read_head_;

src/tls_wrap.cc

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ void TLSWrap::InitSSL() {
127127
// Initialize SSL
128128
enc_in_ = NodeBIO::New();
129129
enc_out_ = NodeBIO::New();
130+
NodeBIO::FromBIO(enc_in_)->AssignEnvironment(env());
131+
NodeBIO::FromBIO(enc_out_)->AssignEnvironment(env());
130132

131133
SSL_set_bio(ssl_, enc_in_, enc_out_);
132134

@@ -162,6 +164,7 @@ void TLSWrap::InitSSL() {
162164

163165
// Initialize ring for queud clear data
164166
clear_in_ = new NodeBIO();
167+
clear_in_->AssignEnvironment(env());
165168
}
166169

167170

0 commit comments

Comments
 (0)