Commit e9089c4a authored by Sean Cannella's avatar Sean Cannella Committed by Praveen Kumar Ramakrishnan

Lazily initialize AsyncSSLSocket EorBioMethod

Summary:
Address another Android initialization crash by delaying
initialization of the EorBio method until first AsyncSSLSocket
construction.

Test Plan: existing tests

Reviewed By: pgriess@fb.com

Subscribers: net-systems@, ssl-diffs@, folly-diffs@, yfeldblum, chalfant, #csti

FB internal diff: D2036329

Tasks: 6925575, 6925570

Signature: t1:2036329:1430444665:a3201f90860a34808a3cf3b42d530608c8a619a8
parent 9a797d91
...@@ -224,8 +224,7 @@ void setup_SSL_CTX(SSL_CTX *ctx) { ...@@ -224,8 +224,7 @@ void setup_SSL_CTX(SSL_CTX *ctx) {
BIO_METHOD eorAwareBioMethod; BIO_METHOD eorAwareBioMethod;
__attribute__((__constructor__)) void* initEorBioMethod(void) {
void initEorBioMethod(void) {
memcpy(&eorAwareBioMethod, BIO_s_socket(), sizeof(eorAwareBioMethod)); memcpy(&eorAwareBioMethod, BIO_s_socket(), sizeof(eorAwareBioMethod));
// override the bwrite method for MSG_EOR support // override the bwrite method for MSG_EOR support
eorAwareBioMethod.bwrite = AsyncSSLSocket::eorAwareBioWrite; eorAwareBioMethod.bwrite = AsyncSSLSocket::eorAwareBioWrite;
...@@ -234,6 +233,10 @@ void initEorBioMethod(void) { ...@@ -234,6 +233,10 @@ void initEorBioMethod(void) {
// set here. openssl code seems to be checking ".type == BIO_TYPE_SOCKET" and // set here. openssl code seems to be checking ".type == BIO_TYPE_SOCKET" and
// then have specific handlings. The eorAwareBioWrite should be compatible // then have specific handlings. The eorAwareBioWrite should be compatible
// with the one in openssl. // with the one in openssl.
// Return something here to enable AsyncSSLSocket to call this method using
// a function-scoped static.
return nullptr;
} }
} // anonymous namespace } // anonymous namespace
...@@ -254,7 +257,7 @@ AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext> &ctx, ...@@ -254,7 +257,7 @@ AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext> &ctx,
AsyncSocket(evb), AsyncSocket(evb),
ctx_(ctx), ctx_(ctx),
handshakeTimeout_(this, evb) { handshakeTimeout_(this, evb) {
setup_SSL_CTX(ctx_->getSSLCtx()); init();
} }
/** /**
...@@ -266,7 +269,7 @@ AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext>& ctx, ...@@ -266,7 +269,7 @@ AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext>& ctx,
server_(server), server_(server),
ctx_(ctx), ctx_(ctx),
handshakeTimeout_(this, evb) { handshakeTimeout_(this, evb) {
setup_SSL_CTX(ctx_->getSSLCtx()); init();
if (server) { if (server) {
SSL_CTX_set_info_callback(ctx_->getSSLCtx(), SSL_CTX_set_info_callback(ctx_->getSSLCtx(),
AsyncSSLSocket::sslInfoCallback); AsyncSSLSocket::sslInfoCallback);
...@@ -281,11 +284,8 @@ AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext>& ctx, ...@@ -281,11 +284,8 @@ AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext>& ctx,
AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext> &ctx, AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext> &ctx,
EventBase* evb, EventBase* evb,
const std::string& serverName) : const std::string& serverName) :
AsyncSocket(evb), AsyncSSLSocket(ctx, evb) {
ctx_(ctx), tlsextHostname_ = serverName;
handshakeTimeout_(this, evb),
tlsextHostname_(serverName) {
setup_SSL_CTX(ctx_->getSSLCtx());
} }
/** /**
...@@ -295,11 +295,8 @@ AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext> &ctx, ...@@ -295,11 +295,8 @@ AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext> &ctx,
AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext>& ctx, AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext>& ctx,
EventBase* evb, int fd, EventBase* evb, int fd,
const std::string& serverName) : const std::string& serverName) :
AsyncSocket(evb, fd), AsyncSSLSocket(ctx, evb, fd, false) {
ctx_(ctx), tlsextHostname_ = serverName;
handshakeTimeout_(this, evb),
tlsextHostname_(serverName) {
setup_SSL_CTX(ctx_->getSSLCtx());
} }
#endif #endif
...@@ -310,6 +307,13 @@ AsyncSSLSocket::~AsyncSSLSocket() { ...@@ -310,6 +307,13 @@ AsyncSSLSocket::~AsyncSSLSocket() {
<< sslState_ << ", events=" << eventFlags_ << ")"; << sslState_ << ", events=" << eventFlags_ << ")";
} }
void AsyncSSLSocket::init() {
// Do this here to ensure we initialize this once before any use of
// AsyncSSLSocket instances and not as part of library load.
static const auto eorAwareBioMethodInitializer = initEorBioMethod();
setup_SSL_CTX(ctx_->getSSLCtx());
}
void AsyncSSLSocket::closeNow() { void AsyncSSLSocket::closeNow() {
// Close the SSL connection. // Close the SSL connection.
if (ssl_ != nullptr && fd_ != -1) { if (ssl_ != nullptr && fd_ != -1) {
......
...@@ -648,6 +648,10 @@ class AsyncSSLSocket : public virtual AsyncSocket { ...@@ -648,6 +648,10 @@ class AsyncSSLSocket : public virtual AsyncSocket {
return minWriteSize_; return minWriteSize_;
} }
private:
void init();
protected: protected:
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment