Skip to content

Commit 74060bb

Browse files
committed
crypto: track external memory for SSL structures
Ensure that GC kicks in at the right times and the RSS does not blow up. Fix: #1522 PR-URL: #1529 Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
1 parent 7ada680 commit 74060bb

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/node_crypto.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,7 @@ void SSLWrap<Base>::DestroySSL() {
18781878
return;
18791879

18801880
SSL_free(ssl_);
1881+
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
18811882
ssl_ = nullptr;
18821883
}
18831884

src/node_crypto.h

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class SecureContext : public BaseObject {
6464
static const int kMaxSessionSize = 10 * 1024;
6565

6666
protected:
67+
static const int64_t kExternalSize = sizeof(SSL_CTX);
6768

6869
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
6970
static void Init(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -97,10 +98,12 @@ class SecureContext : public BaseObject {
9798
cert_(nullptr),
9899
issuer_(nullptr) {
99100
MakeWeak<SecureContext>(this);
101+
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
100102
}
101103

102104
void FreeCTXMem() {
103105
if (ctx_) {
106+
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
104107
if (ctx_->cert_store == root_cert_store) {
105108
// SSL_CTX_free() will attempt to free the cert_store as well.
106109
// Since we want our root_cert_store to stay around forever
@@ -140,6 +143,7 @@ class SSLWrap {
140143
session_callbacks_(false),
141144
new_session_wait_(false) {
142145
ssl_ = SSL_new(sc->ctx_);
146+
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
143147
CHECK_NE(ssl_, nullptr);
144148
}
145149

@@ -166,6 +170,12 @@ class SSLWrap {
166170
inline bool is_waiting_new_session() const { return new_session_wait_; }
167171

168172
protected:
173+
// Size allocated by OpenSSL: one for SSL structure, one for SSL3_STATE and
174+
// some for buffers.
175+
// NOTE: Actually it is much more than this
176+
static const int64_t kExternalSize =
177+
sizeof(SSL) + sizeof(SSL3_STATE) + 42 * 1024;
178+
169179
static void InitNPN(SecureContext* sc);
170180
static void AddMethods(Environment* env, v8::Handle<v8::FunctionTemplate> t);
171181

0 commit comments

Comments
 (0)