From 0be5bf3e64cee329249c1174590d6a0c1a7a543f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 13 Sep 2021 20:29:48 -0700 Subject: QPlugin: don't use QFile::read() if map() fails on Unix If we can't mmap(), then libdl won't be able to either. Pick-to: 6.2 Change-Id: I42eb903a916645db9900fffd16a492a1ac25903f Reviewed-by: Lars Knoll --- src/corelib/plugin/qlibrary.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp index b67c3b6439..2c5940f8f2 100644 --- a/src/corelib/plugin/qlibrary.cpp +++ b/src/corelib/plugin/qlibrary.cpp @@ -254,16 +254,29 @@ static bool findPatternUnloaded(const QString &library, QLibraryPrivate *lib) constexpr qint64 MaxMemoryMapSize = Q_INT64_C(1) << (sizeof(qsizetype) > 4 ? 40 : 29); - QByteArray data; qsizetype fdlen = qMin(file.size(), MaxMemoryMapSize); const char *filedata = reinterpret_cast(file.map(0, fdlen)); +#ifdef Q_OS_UNIX if (filedata == nullptr) { - // Try reading the data into memory instead (up to 64 MB). + // If we can't mmap(), then the dynamic loader won't be able to either. + // This can't be used as a plugin. + if (qt_debug_component()) + qWarning("%s: failed to map to memory: %ls", QFile::encodeName(library).constData(), + qUtf16Printable(file.errorString())); + return false; + } +#else + QByteArray data; + if (filedata == nullptr) { + // It's unknown at this point whether Windows supports LoadLibrary() on + // files that fail to CreateFileMapping / MapViewOfFile, so we err on + // the side of doing a regular read into memory (up to 64 MB). data = file.read(64 * 1024 * 1024); filedata = data.constData(); fdlen = data.size(); } +#endif /* ELF and Mach-O binaries with GCC have .qplugin sections. -- cgit v1.2.3