QTlsBackendOpenSSL: Early return from ensureCiphersAndCertsLoaded()

Add an atomic state variable to perform early return without taking
a recursive lock after ensureCiphersAndCertsLoaded() is complete.

Make related mutex and state variable function-local static because
they are not used anywhere else.

Taks-number: QTBUG-103559
Change-Id: I1e4c9c4f73204885bce82ba7f2b5e64548c3aac3
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Ievgenii Meshcheriakov 2022-08-31 10:25:23 +02:00
parent 42c1e3c334
commit 8bada697d6
2 changed files with 16 additions and 7 deletions

View File

@ -33,8 +33,6 @@ using namespace Qt::StringLiterals;
Q_LOGGING_CATEGORY(lcTlsBackend, "qt.tlsbackend.ossl");
Q_GLOBAL_STATIC(QRecursiveMutex, qt_opensslInitMutex)
static void q_loadCiphersForConnection(SSL *connection, QList<QSslCipher> &ciphers,
QList<QSslCipher> &defaultCiphers)
{
@ -59,7 +57,6 @@ static void q_loadCiphersForConnection(SSL *connection, QList<QSslCipher> &ciphe
}
}
bool QTlsBackendOpenSSL::s_loadedCiphersAndCerts = false;
int QTlsBackendOpenSSL::s_indexForSSLExtraData = -1;
QString QTlsBackendOpenSSL::getErrorsFromOpenSsl()
@ -172,11 +169,24 @@ void QTlsBackendOpenSSL::ensureInitialized() const
void QTlsBackendOpenSSL::ensureCiphersAndCertsLoaded() const
{
const QMutexLocker locker(qt_opensslInitMutex());
Q_CONSTINIT static bool initializationStarted = false;
Q_CONSTINIT static QAtomicInt initialized = Q_BASIC_ATOMIC_INITIALIZER(0);
Q_CONSTINIT static QRecursiveMutex initMutex;
if (s_loadedCiphersAndCerts)
if (initialized.loadAcquire())
return;
s_loadedCiphersAndCerts = true;
const QMutexLocker locker(&initMutex);
if (initializationStarted || initialized.loadAcquire())
return;
// Indicate that the initialization has already started in the current
// thread in case of recursive calls. The atomic variable cannot be used
// for this because it is checked without holding the init mutex.
initializationStarted = true;
auto guard = qScopeGuard([] { initialized.storeRelease(1); });
resetDefaultCiphers();
resetDefaultEllipticCurves();

View File

@ -42,7 +42,6 @@ public:
static void clearErrorQueue();
// Index used in SSL_get_ex_data to get the matching TlsCryptographerOpenSSL:
static bool s_loadedCiphersAndCerts;
static int s_indexForSSLExtraData;
static QString msgErrorsDuringHandshake();