From 62d636a666f38fb4483e2d528f1e175c90f5272a Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 28 Feb 2013 13:00:19 -0800 Subject: Add a Mach-O decoder to the QPluginLoader We already had an ELF decoder, which helped us greatly to find the metadata and that catches most Unix systems (Solaris, QNX, HP-UXi, and all of the free Unixes). On other Unix systems, aside from Mac OS X, we simply scanned the entire file for the signature. On Windows, even without a COFF-PE decoder, we use a LoadLibrary trick to load the plugin without loading the dependent libraries. In most cases, that works. Unfortunately, on Mac OS X we didn't have a decoder and nor could we do the file scan: because Mac OS X binaries could be fat binaries, we wouldn't know which architecture's signature we had found. No more. This adds a full Mach-O decoder to QtCore. It is also capable of finding the boundaries of the architecture's binary, but that functionality is disabled since all Qt 5 plugins have plugin metadata sections. Change-Id: I2d5c04c5ecf024864b8a43f31ab6b7e6c5eae9ce Reviewed-by: Oswald Buddenhagen Reviewed-by: Thiago Macieira --- .../plugin/qpluginloader/tst_qpluginloader.cpp | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp') diff --git a/tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp b/tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp index cef4f53101..989d311ebf 100644 --- a/tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp +++ b/tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp @@ -44,6 +44,10 @@ #include #include "theplugin/plugininterface.h" +#if defined(QT_BUILD_INTERNAL) && defined(Q_OF_MACH_O) +# include +#endif + // Helper macros to let us know if some suffixes are valid #define bundle_VALID false #define dylib_VALID false @@ -118,6 +122,8 @@ private slots: void deleteinstanceOnUnload(); void loadDebugObj(); void loadCorruptElf(); + void loadMachO_data(); + void loadMachO(); #if defined (Q_OS_UNIX) void loadGarbage(); #endif @@ -311,6 +317,73 @@ void tst_QPluginLoader::loadCorruptElf() #endif } +void tst_QPluginLoader::loadMachO_data() +{ +#ifdef Q_OF_MACH_O + QTest::addColumn("parseResult"); + + QTest::newRow("/dev/null") << int(QMachOParser::NotSuitable); + QTest::newRow("elftest/debugobj.so") << int(QMachOParser::NotSuitable); + QTest::newRow("tst_qpluginloader.cpp") << int(QMachOParser::NotSuitable); + QTest::newRow("tst_qpluginloader") << int(QMachOParser::NotSuitable); + +# ifdef Q_PROCESSOR_X86_64 + QTest::newRow("machtest/good.x86_64.dylib") << int(QMachOParser::QtMetaDataSection); + QTest::newRow("machtest/good.i386.dylib") << int(QMachOParser::NotSuitable); + QTest::newRow("machtest/good.fat.no-x86_64.dylib") << int(QMachOParser::NotSuitable); + QTest::newRow("machtest/good.fat.no-i386.dylib") << int(QMachOParser::QtMetaDataSection); +# elif defined(Q_PROCESSOR_X86_32) + QTest::newRow("machtest/good.i386.dylib") << int(QMachOParser::QtMetaDataSection); + QTest::newRow("machtest/good.x86_64.dylib") << int(QMachOParser::NotSuitable); + QTest::newRow("machtest/good.fat.no-i386.dylib") << int(QMachOParser::NotSuitable); + QTest::newRow("machtest/good.fat.no-x86_64.dylib") << int(QMachOParser::QtMetaDataSection); +# endif +# ifndef Q_PROCESSOR_POWER_64 + QTest::newRow("machtest/good.ppc64.dylib") << int(QMachOParser::NotSuitable); +# endif + + QTest::newRow("machtest/good.fat.all.dylib") << int(QMachOParser::QtMetaDataSection); + QTest::newRow("machtest/good.fat.stub-x86_64.dylib") << int(QMachOParser::NotSuitable); + QTest::newRow("machtest/good.fat.stub-i386.dylib") << int(QMachOParser::NotSuitable); +#endif +} + +void tst_QPluginLoader::loadMachO() +{ +#ifdef Q_OF_MACH_O + QFile f(QFINDTESTDATA(QTest::currentDataTag())); + QVERIFY(f.open(QIODevice::ReadOnly)); + QByteArray data = f.readAll(); + + long pos; + ulong len; + QString errorString; + int r = QMachOParser::parse(data.constData(), data.size(), f.fileName(), &errorString, &pos, &len); + + QFETCH(int, parseResult); + QCOMPARE(r, parseResult); + + if (r == QMachOParser::NotSuitable) + return; + + QVERIFY(pos > 0); + QVERIFY(len >= sizeof(void*)); + QVERIFY(pos + long(len) < data.size()); + QCOMPARE(pos & (sizeof(void*) - 1), 0UL); + + void *value = *(void**)(data.constData() + pos); + QCOMPARE(value, sizeof(void*) > 4 ? (void*)(0xc0ffeec0ffeeL) : (void*)0xc0ffee); + + // now that we know it's valid, let's try to make it invalid + ulong offeredlen = pos; + do { + --offeredlen; + r = QMachOParser::parse(data.constData(), offeredlen, f.fileName(), &errorString, &pos, &len); + QVERIFY2(r == QMachOParser::NotSuitable, qPrintable(QString("Failed at size 0x%1").arg(offeredlen, 0, 16))); + } while (offeredlen); +#endif +} + #if defined (Q_OS_UNIX) void tst_QPluginLoader::loadGarbage() { -- cgit v1.2.3