AppleUnifiedLogger: Remove manually managed os_log_t cache

The os_log_create API documents that the logging subsystem will
maintain a global list of logs, and will return existing logs
for subsequent calls for the same category and subsystem, so
we don't need to maintain our own log.

This also avoids a crash were we would end up in the logging
backend during application shutdown, when destructor functions
run, for example via QLibraryStore::cleanup().

In this case local static such as our log cache had already been
destroyed, leading to use after free.

Pick-to: 6.2 6.3 5.15
Change-Id: I91abfef6c56f8ca6dd8d602ac83aa163ce3d6860
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
bb10
Tor Arne Vestbø 2022-04-28 14:25:53 +02:00
parent b41b1a3f6f
commit d5113384c9
1 changed files with 1 additions and 24 deletions

View File

@ -133,7 +133,7 @@ bool AppleUnifiedLogger::messageHandler(QtMsgType msgType, const QMessageLogCont
const bool isDefault = !context.category || !strcmp(context.category, "default");
os_log_t log = isDefault ? OS_LOG_DEFAULT :
cachedLog(subsystem, QString::fromLatin1(context.category));
os_log_create(subsystem.toLatin1().constData(), context.category);
os_log_type_t logType = logTypeForMessageType(msgType);
if (!os_log_type_enabled(log, logType))
@ -169,29 +169,6 @@ os_log_type_t AppleUnifiedLogger::logTypeForMessageType(QtMsgType msgType)
return OS_LOG_TYPE_DEFAULT;
}
os_log_t AppleUnifiedLogger::cachedLog(const QString &subsystem, const QString &category)
{
static QBasicMutex mutex;
const auto locker = qt_scoped_lock(mutex);
static QHash<QPair<QString, QString>, os_log_t> logs;
const auto cacheKey = qMakePair(subsystem, category);
os_log_t log = logs.value(cacheKey);
if (!log) {
log = os_log_create(subsystem.toLatin1().constData(),
category.toLatin1().constData());
logs.insert(cacheKey, log);
// Technically we should release the os_log_t resource when done
// with it, but since we don't know when a category is disabled
// we keep all cached os_log_t instances until shutdown, where
// the OS will clean them up for us.
}
return log;
}
#endif // QT_USE_APPLE_UNIFIED_LOGGING
// -------------------------------------------------------------------------