From 34a4642dc29f0302f4a82acf22f40a72c5df35b4 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sun, 26 Apr 2015 14:27:46 +0200 Subject: [PATCH] crypto: track external memory for SSL structures Ensure that GC kicks in at the right times and the RSS does not blow up. Fix: https://github.com/iojs/io.js/issues/1522 --- src/node_crypto.cc | 1 + src/node_crypto.h | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index af69cb73677584..9e62e1e61fbdac 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -1878,6 +1878,7 @@ void SSLWrap::DestroySSL() { return; SSL_free(ssl_); + env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize); ssl_ = nullptr; } diff --git a/src/node_crypto.h b/src/node_crypto.h index 8fec4bb6253c2e..a623ccbf2637a5 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -64,6 +64,7 @@ class SecureContext : public BaseObject { static const int kMaxSessionSize = 10 * 1024; protected: + static const int64_t kExternalSize = sizeof(SSL_CTX); static void New(const v8::FunctionCallbackInfo& args); static void Init(const v8::FunctionCallbackInfo& args); @@ -97,10 +98,12 @@ class SecureContext : public BaseObject { cert_(nullptr), issuer_(nullptr) { MakeWeak(this); + env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize); } void FreeCTXMem() { if (ctx_) { + env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize); if (ctx_->cert_store == root_cert_store) { // SSL_CTX_free() will attempt to free the cert_store as well. // Since we want our root_cert_store to stay around forever @@ -140,6 +143,7 @@ class SSLWrap { session_callbacks_(false), new_session_wait_(false) { ssl_ = SSL_new(sc->ctx_); + env_->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize); CHECK_NE(ssl_, nullptr); } @@ -166,6 +170,12 @@ class SSLWrap { inline bool is_waiting_new_session() const { return new_session_wait_; } protected: + // Size allocated by OpenSSL: one for SSL structure, one for SSL3_STATE and + // some for buffers. + // NOTE: Actually it is much more than this + static const int64_t kExternalSize = + sizeof(SSL) + sizeof(SSL3_STATE) + 42 * 1024; + static void InitNPN(SecureContext* sc); static void AddMethods(Environment* env, v8::Handle t);