8#include "private/qcoreapplication_p.h"
9#include "private/qloggingregistry_p.h"
22#include <qtcore_tracepoints_p.h>
26using namespace Qt::StringLiterals;
39 InvalidMetaDataVersion = -2,
40 InvalidTopLevelItem = -3,
41 InvalidHeaderItem = -4,
44 QCborError error = { QCborError::NoError };
46 Q_IMPLICIT IterationResult(Result r) : result(r) {}
47 Q_IMPLICIT IterationResult(QCborError e) : result(ParsingError), error(e) {}
50struct QFactoryLoaderIidSearch
52 QLatin1StringView iid;
53 bool matchesIid =
false;
54 QFactoryLoaderIidSearch(QLatin1StringView iid) : iid(iid)
55 { Q_ASSERT(!iid.isEmpty()); }
57 static IterationResult::Result skip(QCborStreamReader &reader)
61 return IterationResult::ContinueSearch;
64 IterationResult::Result operator()(QtPluginMetaDataKeys key, QCborStreamReader &reader)
66 if (key != QtPluginMetaDataKeys::IID)
68 matchesIid = (reader.readAllString() == iid);
69 return IterationResult::FinishedSearch;
71 IterationResult::Result operator()(QUtf8StringView, QCborStreamReader &reader)
77struct QFactoryLoaderMetaDataKeysExtractor : QFactoryLoaderIidSearch
80 QFactoryLoaderMetaDataKeysExtractor(QLatin1StringView iid)
81 : QFactoryLoaderIidSearch(iid)
84 IterationResult::Result operator()(QtPluginMetaDataKeys key, QCborStreamReader &reader)
86 if (key == QtPluginMetaDataKeys::IID) {
87 QFactoryLoaderIidSearch::operator()(key, reader);
88 return IterationResult::ContinueSearch;
90 if (key != QtPluginMetaDataKeys::MetaData)
94 return IterationResult::FinishedSearch;
95 if (!reader.isMap() || !reader.isLengthKnown())
96 return IterationResult::InvalidHeaderItem;
97 if (!reader.enterContainer())
98 return IterationResult::ParsingError;
99 while (reader.isValid()) {
101 QByteArray key = reader.readAllUtf8String();
103 if (!reader.isArray() || !reader.isLengthKnown())
104 return IterationResult::InvalidHeaderItem;
105 keys = QCborValue::fromCbor(reader).toArray();
111 return IterationResult::FinishedSearch;
113 using QFactoryLoaderIidSearch::operator();
120 Q_ASSERT(raw.size() >= qsizetype(
sizeof(header)));
121 memcpy(&header, raw.data(),
sizeof(header));
122 if (Q_UNLIKELY(header.version > QPluginMetaData::CurrentMetaDataVersion))
123 return IterationResult::InvalidMetaDataVersion;
126 raw = raw.sliced(
sizeof(header));
127 QByteArray ba = QByteArray::fromRawData(raw.data(), raw.size());
128 QCborStreamReader reader(ba);
129 if (reader.isInvalid())
130 return reader.lastError();
132 return IterationResult::InvalidTopLevelItem;
133 if (!reader.enterContainer())
134 return reader.lastError();
135 while (reader.isValid()) {
136 IterationResult::Result r;
137 if (reader.isInteger()) {
139 qint64 value = reader.toInteger();
140 auto key = QtPluginMetaDataKeys(value);
141 if (qint64(key) != value)
142 return IterationResult::InvalidHeaderItem;
144 return reader.lastError();
146 }
else if (reader.isString()) {
147 QByteArray key = reader.readAllUtf8String();
149 return reader.lastError();
150 r = f(QUtf8StringView(key), reader);
152 return IterationResult::InvalidTopLevelItem;
155 if (QCborError e = reader.lastError())
157 if (r != IterationResult::ContinueSearch)
161 if (!reader.leaveContainer())
162 return reader.lastError();
163 return IterationResult::FinishedSearch;
166static bool isIidMatch(QByteArrayView raw, QLatin1StringView iid)
168 QFactoryLoaderIidSearch search(iid);
169 iterateInPluginMetaData(raw, search);
170 return search.matchesIid;
173bool QPluginParsedMetaData::parse(QByteArrayView raw)
176 auto r = iterateInPluginMetaData(raw, [&](
const auto &key, QCborStreamReader &reader) {
177 QCborValue item = QCborValue::fromCbor(reader);
178 if (item.isInvalid())
179 return IterationResult::ParsingError;
180 if constexpr (std::is_enum_v<std::decay_t<
decltype(key)>>)
181 map[
int(key)] = item;
183 map[QString::fromUtf8(key)] = item;
184 return IterationResult::ContinueSearch;
188 case IterationResult::FinishedSearch:
189 case IterationResult::ContinueSearch:
193 case IterationResult::ParsingError:
194 return setError(QFactoryLoader::tr(
"Metadata parsing error: %1").arg(r.error.toString()));
195 case IterationResult::InvalidMetaDataVersion:
196 return setError(QFactoryLoader::tr(
"Invalid metadata version"));
197 case IterationResult::InvalidTopLevelItem:
198 case IterationResult::InvalidHeaderItem:
199 return setError(QFactoryLoader::tr(
"Unexpected metadata contents"));
203 auto header = qFromUnaligned<QPluginMetaData::Header>(raw.data());
205 DecodedArchRequirements archReq =
206 header.version == 0 ? decodeVersion0ArchRequirements(header.plugin_arch_requirements)
207 : decodeVersion1ArchRequirements(header.plugin_arch_requirements);
210 map[
int(QtPluginMetaDataKeys::QtVersion)] =
211 QT_VERSION_CHECK(header.qt_major_version, header.qt_minor_version, 0);
212 map[
int(QtPluginMetaDataKeys::IsDebug)] = archReq.isDebug;
213 map[
int(QtPluginMetaDataKeys::Requirements)] = archReq.level;
215 data = std::move(map);
219QJsonObject QPluginParsedMetaData::toJson()
const
223 for (
auto it : data.toMap()) {
225 if (it.first.isInteger()) {
226 switch (it.first.toInteger()) {
227#define CONVERT_TO_STRING(IntKey, StringKey, Description)
228 case int(IntKey): key = QStringLiteral(StringKey); break;
229 QT_PLUGIN_FOREACH_METADATA(CONVERT_TO_STRING)
232 key = it.first.toString();
236 o.insert(key, it.second.toJsonValue());
241#if QT_CONFIG(library)
243Q_STATIC_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(lcFactoryLoader,
"QT_DEBUG_PLUGINS",
244 "qt.core.plugin.factoryloader")
247struct QFactoryLoaderGlobals
251 QRecursiveMutex mutex;
252 QList<QFactoryLoader *> loaders;
256Q_GLOBAL_STATIC(QFactoryLoaderGlobals, qt_factoryloader_global)
258inline void QFactoryLoader::Private::updateSinglePath(
const QString &path)
260 struct LibraryReleaser {
261 void operator()(QLibraryPrivate *library)
262 {
if (library) library->release(); }
266 if (loadedPaths.hasSeen(path))
269 qCDebug(lcFactoryLoader) <<
"checking directory path" << path <<
"...";
271 QDirListing plugins(path,
272#if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
273 QStringList(QStringLiteral(
"*.dll")),
274#elif defined(Q_OS_ANDROID)
275 QStringList(
"libplugins_%1_*.so"_L1.arg(suffix)),
277 QDirListing::IteratorFlag::FilesOnly | QDirListing::IteratorFlag::ResolveSymlinks);
279 auto versionFromLib = [](
const QLibraryPrivate *lib) {
280 return lib->metaData.value(QtPluginMetaDataKeys::QtVersion).toInteger();
283 for (
const auto &dirEntry : plugins) {
284 const QString &fileName = dirEntry.fileName();
285#if defined(Q_PROCESSOR_X86)
286 if (fileName.endsWith(
".avx2"_L1) || fileName.endsWith(
".avx512"_L1)) {
291 qCDebug(lcFactoryLoader) <<
"looking at" << fileName;
293 Q_TRACE(QFactoryLoader_update, fileName);
295 QLibraryPrivate::UniquePtr library;
296 library.reset(QLibraryPrivate::findOrCreate(dirEntry.canonicalFilePath()));
297 if (!library->isPlugin()) {
298 qCDebug(lcFactoryLoader) << library->errorString << Qt::endl
304 bool metaDataOk =
false;
306 QString iid = library->metaData.value(QtPluginMetaDataKeys::IID).toString();
307 if (iid == QLatin1StringView(
this->iid.constData(),
this->iid.size())) {
308 QCborMap object = library->metaData.value(QtPluginMetaDataKeys::MetaData).toMap();
311 const QCborArray k = object.value(
"Keys"_L1).toArray();
312 for (QCborValueConstRef v : k)
313 keys += cs ? v.toString() : v.toString().toLower();
315 qCDebug(lcFactoryLoader) <<
"Got keys from plugin meta data" << keys;
320 static constexpr qint64 QtVersionNoPatch = QT_VERSION_CHECK(QT_VERSION_MAJOR, QT_VERSION_MINOR, 0);
321 qint64 thisVersion = versionFromLib(library.get());
322 if (iid.startsWith(QStringLiteral(
"org.qt-project.Qt.QPA"))) {
324 if (thisVersion != QtVersionNoPatch) {
325 qCDebug(lcFactoryLoader) <<
"Ignoring QPA plugin due to mismatching Qt versions" << QtVersionNoPatch << thisVersion;
330 int keyUsageCount = 0;
331 for (
const QString &key : std::as_const(keys)) {
332 QLibraryPrivate *&keyMapEntry = keyMap[key];
333 if (QLibraryPrivate *existingLibrary = keyMapEntry) {
334 static constexpr bool QtBuildIsDebug = QT_CONFIG(debug);
335 bool existingIsDebug = existingLibrary->metaData.value(QtPluginMetaDataKeys::IsDebug).toBool();
336 bool thisIsDebug = library->metaData.value(QtPluginMetaDataKeys::IsDebug).toBool();
337 bool configsAreDifferent = thisIsDebug != existingIsDebug;
338 bool thisConfigDoesNotMatchQt = thisIsDebug != QtBuildIsDebug;
339 if (configsAreDifferent && thisConfigDoesNotMatchQt)
345 qint64 existingVersion = versionFromLib(existingLibrary);
346 if (existingVersion == QtVersionNoPatch)
348 if (existingVersion < QtVersionNoPatch && thisVersion > QtVersionNoPatch)
350 if (existingVersion < QtVersionNoPatch && thisVersion < existingVersion)
354 keyMapEntry = library.get();
357 if (keyUsageCount || keys.isEmpty()) {
358 library->setLoadHints(QLibrary::PreventUnloadHint);
359 QMutexLocker locker(&mutex);
360 libraries.push_back(std::move(library));
364 loadedLibraries.resize(libraries.size());
367void QFactoryLoader::setLoadHints(QLibrary::LoadHints loadHints)
369 d->loadHints = loadHints;
372void QFactoryLoader::update()
375 if (!d->extraSearchPath.isEmpty())
376 d->updateSinglePath(d->extraSearchPath);
378 const QStringList paths = QCoreApplication::libraryPaths();
379 for (
const QString &pluginDir : paths) {
381 QString path = pluginDir;
383 QString path = pluginDir + d->suffix;
385 d->updateSinglePath(path);
388 qCDebug(lcFactoryLoader) <<
"ignoring" << d->iid
389 <<
"since plugins are disabled in static builds";
393QFactoryLoader::~QFactoryLoader()
395 if (!qt_factoryloader_global.isDestroyed()) {
396 QMutexLocker locker(&qt_factoryloader_global->mutex);
397 qt_factoryloader_global->loaders.removeOne(
this);
400#if QT_CONFIG(library)
401 for (qsizetype i = 0; i < d->loadedLibraries.size(); ++i) {
402 if (d->loadedLibraries.at(i)) {
403 auto &plugin = d->libraries[i];
404 delete plugin->inst.data();
410 for (QtPluginInstanceFunction staticInstance : d->usedStaticInstances) {
412 delete staticInstance();
416#if defined(Q_OS_UNIX) && !defined (Q_OS_DARWIN)
417QLibraryPrivate *QFactoryLoader::library(
const QString &key)
const
419 const auto it = d->keyMap.find(d->cs ? key : key.toLower());
420 if (it == d->keyMap.cend())
426void QFactoryLoader::refreshAll()
428 if (qt_factoryloader_global.exists()) {
429 QMutexLocker locker(&qt_factoryloader_global->mutex);
430 for (QFactoryLoader *loader : std::as_const(qt_factoryloader_global->loaders))
437QFactoryLoader::QFactoryLoader(
const char *iid,
438 const QString &suffix,
439 Qt::CaseSensitivity cs)
441 Q_ASSERT_X(suffix.startsWith(u'/'),
"QFactoryLoader",
442 "For historical reasons, the suffix must start with '/' (and it can't be empty)");
445#if QT_CONFIG(library)
449 if (!d->suffix.isEmpty() && d->suffix.at(0) == u'/')
450 d->suffix.remove(0, 1);
453 QMutexLocker locker(&qt_factoryloader_global->mutex);
455 qt_factoryloader_global->loaders.append(
this);
462void QFactoryLoader::setExtraSearchPath(
const QString &path)
464#if QT_CONFIG(library)
465 if (d->extraSearchPath == path)
468 QMutexLocker locker(&qt_factoryloader_global->mutex);
469 QString oldPath = std::exchange(d->extraSearchPath, path);
470 if (oldPath.isEmpty()) {
472 d->updateSinglePath(d->extraSearchPath);
475 for (qsizetype i = 0; i < d->loadedLibraries.size(); ++i) {
476 if (d->loadedLibraries.at(i)) {
477 auto &plugin = d->libraries[i];
478 delete plugin->inst.data();
481 d->loadedLibraries.fill(
false);
482 d->loadedPaths.clear();
483 d->libraries.clear();
492QFactoryLoader::MetaDataList QFactoryLoader::metaData()
const
494 QList<QPluginParsedMetaData> metaData;
495#if QT_CONFIG(library)
496 QMutexLocker locker(&d->mutex);
497 metaData.reserve(qsizetype(d->libraries.size()));
498 for (
const auto &library : d->libraries)
499 metaData.append(library->metaData);
503 QLatin1StringView iid(d->iid.constData(), d->iid.size());
504 const auto staticPlugins = QPluginLoader::staticPlugins();
505 for (
const QStaticPlugin &plugin : staticPlugins) {
506 QByteArrayView pluginData(
static_cast<
const char *>(plugin.rawMetaData), plugin.rawMetaDataSize);
507 QPluginParsedMetaData parsed(pluginData);
508 if (parsed.isError() || parsed.value(QtPluginMetaDataKeys::IID) != iid)
510 metaData.append(std::move(parsed));
514 Q_ASSERT(metaData.size() <= std::numeric_limits<
int>::max());
518QList<QCborArray> QFactoryLoader::metaDataKeys()
const
520 QList<QCborArray> metaData;
521#if QT_CONFIG(library)
522 QMutexLocker locker(&d->mutex);
523 metaData.reserve(qsizetype(d->libraries.size()));
524 for (
const auto &library : d->libraries) {
525 const QCborValue md = library->metaData.value(QtPluginMetaDataKeys::MetaData);
526 metaData.append(md[
"Keys"_L1].toArray());
531 QLatin1StringView iid(d->iid.constData(), d->iid.size());
532 const auto staticPlugins = QPluginLoader::staticPlugins();
533 for (
const QStaticPlugin &plugin : staticPlugins) {
534 QByteArrayView pluginData(
static_cast<
const char *>(plugin.rawMetaData),
535 plugin.rawMetaDataSize);
536 QFactoryLoaderMetaDataKeysExtractor extractor{ iid };
537 iterateInPluginMetaData(pluginData, extractor);
538 if (extractor.matchesIid)
539 metaData += std::move(extractor.keys);
543 Q_ASSERT(metaData.size() <= std::numeric_limits<
int>::max());
547QObject *QFactoryLoader::instance(
int index)
const
552 QMutexLocker lock(&d->mutex);
553 QObject *obj = instanceHelper_locked(index);
555 if (obj && !obj->parent())
556 obj->moveToThread(QCoreApplicationPrivate::mainThread());
560inline QObject *QFactoryLoader::instanceHelper_locked(
int index)
const
562#if QT_CONFIG(library)
563 if (size_t(index) < d->libraries.size()) {
564 QLibraryPrivate *library = d->libraries[index].get();
565 d->loadedLibraries[index] =
true;
566 library->setLoadHints(d->loadHints);
567 return library->pluginInstance();
570 index -=
static_cast<
int>(d->libraries.size());
573 QLatin1StringView iid(d->iid.constData(), d->iid.size());
574 const QList<QStaticPlugin> staticPlugins = QPluginLoader::staticPlugins();
576 for (QStaticPlugin plugin : staticPlugins) {
577 QByteArrayView pluginData(
static_cast<
const char *>(plugin.rawMetaData), plugin.rawMetaDataSize);
578 if (!isIidMatch(pluginData, iid))
582 if (d->usedStaticInstances.size() <= i)
583 d->usedStaticInstances.resize(i + 1);
584 d->usedStaticInstances[i] = plugin.instance;
585 return plugin.instance();
593QMultiMap<
int, QString> QFactoryLoader::keyMap()
const
595 QMultiMap<
int, QString> result;
596 const QList<QCborArray> metaDataList = metaDataKeys();
597 for (
int i = 0; i <
int(metaDataList.size()); ++i) {
598 const QCborArray &keys = metaDataList[i];
599 for (QCborValueConstRef key : keys)
600 result.insert(i, key.toString());
605int QFactoryLoader::indexOf(
const QString &needle)
const
607 const QList<QCborArray> metaDataList = metaDataKeys();
608 for (
int i = 0; i <
int(metaDataList.size()); ++i) {
609 const QCborArray &keys = metaDataList[i];
610 for (QCborValueConstRef key : keys) {
611 if (key.toString().compare(needle, Qt::CaseInsensitive) == 0)
Q_TRACE_POINT(qtcore, QFactoryLoader_update, const QString &fileName)
static bool isIidMatch(QByteArrayView raw, QLatin1StringView iid)
static IterationResult iterateInPluginMetaData(QByteArrayView raw, F &&f)