tests/auto/symbols/tst_symbols.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtCore/QtCore>
       
    44 #include <QtTest/QtTest>
       
    45 
       
    46 #ifdef QT_NAMESPACE
       
    47 #define STRINGIFY_HELPER(s) #s
       
    48 #define STRINGIFY(s) STRINGIFY_HELPER(s)
       
    49 QString ns = STRINGIFY(QT_NAMESPACE) + QString("::");
       
    50 #else
       
    51 QString ns;
       
    52 #endif
       
    53 
       
    54 class tst_Symbols: public QObject
       
    55 {
       
    56     Q_OBJECT
       
    57 private slots:
       
    58     void prefix();
       
    59     void globalObjects();
       
    60 };
       
    61 
       
    62 /* Computes the line number from a symbol */
       
    63 static QString symbolToLine(const QString &symbol, const QString &lib)
       
    64 {
       
    65     // nm outputs the symbol name, the type, the address and the size
       
    66     QRegExp re("global constructors keyed to ([a-zA-Z_0-9.]*) (.) ([0-f]*) ([0-f]*)");
       
    67     if (re.indexIn(symbol) == -1)
       
    68         return QString();
       
    69 
       
    70     // address and symbolSize are in hex. Convert to integers
       
    71     bool ok;
       
    72     int symbolAddress = re.cap(3).toInt(&ok, 16);
       
    73     if (!ok)
       
    74         return QString();
       
    75     int symbolSize = re.cap(4).toInt(&ok, 16);
       
    76     if (!ok)
       
    77         return QString();
       
    78 
       
    79     // now, find the start address, which is the address - size
       
    80     QString startAddress = QString::number(symbolAddress - symbolSize, 16);
       
    81 
       
    82     QProcess proc;
       
    83     proc.start("addr2line", QStringList() << "-e" << lib << startAddress);
       
    84     if (!proc.waitForFinished())
       
    85         return QString();
       
    86 
       
    87     QString result = QString::fromLocal8Bit(proc.readLine());
       
    88     result.chop(1); // chop tailing newline
       
    89     return result;
       
    90 }
       
    91 
       
    92 /* This test searches through all Qt libraries and searches for symbols
       
    93    starting with "global constructors keyed to "
       
    94 
       
    95    These indicate static global objects, which should not be used in shared
       
    96    libraries - use Q_GLOBAL_STATIC instead.
       
    97 */
       
    98 void tst_Symbols::globalObjects()
       
    99 {
       
   100 #ifndef Q_OS_LINUX
       
   101     QSKIP("Linux-specific test", SkipAll);
       
   102 #endif
       
   103     QSKIP("Test disabled, we're not fixing these issues in this Qt version", SkipAll);
       
   104 
       
   105     // these are regexps for global objects that are allowed in Qt
       
   106     QStringList whitelist = QStringList()
       
   107         // ignore qInitResources - they are safe to use
       
   108         << "^_Z[0-9]*qInitResources_"
       
   109         << "qrc_.*\\.cpp"
       
   110         // ignore qRegisterGuiVariant - it's a safe fallback to register GUI Variants
       
   111         << "qRegisterGuiVariant";
       
   112 
       
   113     bool isFailed = false;
       
   114 
       
   115     QDir dir(QLibraryInfo::location(QLibraryInfo::LibrariesPath), "*.so");
       
   116     QStringList files = dir.entryList();
       
   117     QVERIFY(!files.isEmpty());
       
   118 
       
   119     foreach (QString lib, files) {
       
   120         if (lib == "libQtCLucene.so") {
       
   121             // skip this library, it's 3rd-party C++
       
   122             continue;
       
   123         }
       
   124         if (lib == "libQt3Support.so") {
       
   125             // we're not going to fix these issues anyway, so skip this library
       
   126             continue;
       
   127         }
       
   128 
       
   129         QProcess proc;
       
   130         proc.start("nm",
       
   131            QStringList() << "-C" << "--format=posix"
       
   132                          << dir.absolutePath() + "/" + lib);
       
   133         QVERIFY(proc.waitForFinished());
       
   134         QCOMPARE(proc.exitCode(), 0);
       
   135         QCOMPARE(QString::fromLocal8Bit(proc.readAllStandardError()), QString());
       
   136 
       
   137         QStringList symbols = QString::fromLocal8Bit(proc.readAll()).split("\n");
       
   138         QVERIFY(!symbols.isEmpty());
       
   139         foreach (QString symbol, symbols) {
       
   140             if (symbol.isEmpty())
       
   141                 continue;
       
   142 
       
   143             if (!symbol.startsWith("global constructors keyed to "))
       
   144                 continue;
       
   145 
       
   146             QRegExp re("global constructors keyed to ([a-zA-Z_0-9.]*)");
       
   147             QVERIFY(re.indexIn(symbol) != -1);
       
   148 
       
   149             QString cap = re.cap(1);
       
   150 
       
   151             bool whitelisted = false;
       
   152             foreach (QString white, whitelist) {
       
   153                 if (cap.indexOf(QRegExp(white)) != -1) {
       
   154                     whitelisted = true;
       
   155                     break;
       
   156                 }
       
   157             }
       
   158             if (whitelisted)
       
   159                 continue;
       
   160 
       
   161             QString line = symbolToLine(symbol, dir.absolutePath() + "/" + lib);
       
   162 
       
   163             if (cap.contains('.'))
       
   164                 QWARN(qPrintable("Static global object(s) found in " + lib + " in file " + cap + " (" + line + ")"));
       
   165             else
       
   166                 QWARN(qPrintable("Static global object found in " + lib + " near symbol " + cap + " (" + line + ")"));
       
   167 
       
   168             isFailed = true;
       
   169         }
       
   170     }
       
   171 
       
   172     if (isFailed) {
       
   173 #if QT_VERSION >= 0x040600
       
   174         QVERIFY2(!isFailed, "Libraries contain static global objects. See Debug output above.");
       
   175 #else
       
   176         QSKIP("Libraries contains static global objects. See Debug output above. [These errors cannot be fixed in 4.5 in time]", SkipSingle);
       
   177 #endif
       
   178     }
       
   179 }
       
   180 
       
   181 void tst_Symbols::prefix()
       
   182 {
       
   183 #if defined(QT_CROSS_COMPILED)
       
   184     QSKIP("Probably no compiler on the target", SkipAll);
       
   185 #elif defined(Q_OS_LINUX)
       
   186     QStringList qtTypes;
       
   187     qtTypes << "QString" << "QChar" << "QWidget" << "QObject" << "QVariant" << "QList"
       
   188             << "QMap" << "QHash" << "QVector" << "QRect" << "QSize" << "QPoint"
       
   189             << "QTextFormat" << "QTextLength" << "QPen" << "QFont" << "QIcon"
       
   190             << "QPixmap" << "QImage" << "QRegion" << "QPolygon";
       
   191     QStringList qAlgorithmFunctions;
       
   192     qAlgorithmFunctions << "qBinaryFind" << "qLowerBound" << "qUpperBound"
       
   193                         << "qAbs" << "qMin" << "qMax" << "qBound" << "qSwap"
       
   194                         << "qHash" << "qDeleteAll" << "qCopy" << "qSort";
       
   195 
       
   196     QStringList exceptionalSymbols;
       
   197     exceptionalSymbols << "XRectangle::~XRectangle()"
       
   198                        << "XChar2b::~XChar2b()"
       
   199                        << "XPoint::~XPoint()"
       
   200                        << "glyph_metrics_t::"; // #### Qt 4.2
       
   201 
       
   202     QStringList stupidCSymbols;
       
   203     stupidCSymbols << "Add_Glyph_Property"
       
   204                    << "Check_Property"
       
   205                    << "Coverage_Index"
       
   206                    << "Get_Class"
       
   207                    << "Get_Device"
       
   208                    << "rcsid3"
       
   209                    << "sfnt_module_class"
       
   210                    << "t1cid_driver_class"
       
   211                    << "t42_driver_class"
       
   212                    << "winfnt_driver_class"
       
   213                    << "pshinter_module_class"
       
   214                    << "psnames_module_class"
       
   215                    ;
       
   216 
       
   217     QHash<QString,QStringList> excusedPrefixes;
       
   218     excusedPrefixes[QString()] =
       
   219         QStringList() << "Ui_Q"; // uic generated, like Ui_QPrintDialog
       
   220 
       
   221     excusedPrefixes["QtCore"] =
       
   222         QStringList() << "hb_"
       
   223                       << "HB_"
       
   224                       // zlib symbols, for -qt-zlib ;(
       
   225                       << "deflate"
       
   226                       << "compress"
       
   227                       << "uncompress"
       
   228                       << "adler32"
       
   229                       << "gz"
       
   230                       << "inflate"
       
   231                       << "zlib"
       
   232                       << "zError"
       
   233                       << "get_crc_table"
       
   234                       << "crc32";
       
   235 
       
   236     excusedPrefixes["QtGui"] =
       
   237         QStringList() << "ftglue_"
       
   238                       << "Load_"
       
   239                       << "otl_"
       
   240                       << "TT_"
       
   241                       << "tt_"
       
   242                       << "t1_"
       
   243                       << "Free_"
       
   244                       << "FT_"
       
   245                       << "FTC_"
       
   246                       << "ft_"
       
   247                       << "ftc_"
       
   248                       << "af_autofitter"
       
   249                       << "af_dummy"
       
   250                       << "af_latin"
       
   251                       << "autofit_"
       
   252                       << "XPanorami"
       
   253                       << "Xinerama"
       
   254                       << "bdf_"
       
   255                       << "ccf_"
       
   256                       << "gray_raster"
       
   257                       << "pcf_"
       
   258                       << "cff_"
       
   259                       << "otv_"
       
   260                       << "pfr_"
       
   261                       << "ps_"
       
   262                       << "psaux"
       
   263                       << "png_";
       
   264 
       
   265     excusedPrefixes["QtSql"] =
       
   266         QStringList() << "sqlite3";
       
   267 
       
   268     excusedPrefixes["QtWebKit"] =
       
   269         QStringList() << "WebCore::"
       
   270                       << "KJS::"
       
   271                       << "kjs"
       
   272                       << "kJS"
       
   273                       << "JS"
       
   274 //                      << "OpaqueJS"
       
   275                       << "WTF"
       
   276                       << "wtf_"
       
   277                       << "SVG::"
       
   278                       << "NPN_"
       
   279                       << "cti"  // ctiTrampoline and ctiVMThrowTrampoline from the JIT
       
   280 #ifdef QT_NAMESPACE
       
   281                       << "QWeb" // Webkit is only 'namespace aware'
       
   282 #endif
       
   283         ;
       
   284 
       
   285     excusedPrefixes["phonon"] =
       
   286         QStringList() << ns + "Phonon";
       
   287 
       
   288     QDir dir(qgetenv("QTDIR") + "/lib", "*.so");
       
   289     QStringList files = dir.entryList();
       
   290     QVERIFY(!files.isEmpty());
       
   291 
       
   292     bool isFailed = false;
       
   293     foreach (QString lib, files) {
       
   294         if (lib.contains("Designer") || lib.contains("QtCLucene") || lib.contains("XmlPatternsSDK"))
       
   295             continue;
       
   296 
       
   297         bool isPhonon = lib.contains("phonon");
       
   298 
       
   299         QProcess proc;
       
   300         proc.start("nm",
       
   301            QStringList() << "-g" << "-C" << "-D" << "--format=posix"
       
   302                          << "--defined-only" << dir.absolutePath() + "/" + lib);
       
   303         QVERIFY(proc.waitForFinished());
       
   304         QCOMPARE(proc.exitCode(), 0);
       
   305         QCOMPARE(QString::fromLocal8Bit(proc.readAllStandardError()), QString());
       
   306 
       
   307         QStringList symbols = QString::fromLocal8Bit(proc.readAll()).split("\n");
       
   308         QVERIFY(!symbols.isEmpty());
       
   309         foreach (QString symbol, symbols) {
       
   310             if (symbol.isEmpty())
       
   311                 continue;
       
   312 
       
   313             if (symbol.startsWith("unsigned "))
       
   314                 // strip modifiers
       
   315                 symbol = symbol.mid(symbol.indexOf(' ') + 1);
       
   316             if (symbol.startsWith("long long ")) {
       
   317                 symbol = symbol.mid(10);
       
   318             } else if (symbol.startsWith("bool ") || symbol.startsWith("bool* ")
       
   319                 || symbol.startsWith("char ") || symbol.startsWith("char* ")
       
   320                 || symbol.startsWith("int ") || symbol.startsWith("int* ") || symbol.startsWith("int*&")
       
   321                 || symbol.startsWith("short") || symbol.startsWith("long ")
       
   322                 || symbol.startsWith("void ") || symbol.startsWith("void* ")
       
   323                 || symbol.startsWith("double ") || symbol.startsWith("double* ")
       
   324                 || symbol.startsWith("float ") || symbol.startsWith("float* ")) {
       
   325                 // partial templates have the return type in their demangled name, strip it
       
   326                 symbol = symbol.mid(symbol.indexOf(' ') + 1);
       
   327             }
       
   328             if (symbol.startsWith("const ") || symbol.startsWith("const* ") ||
       
   329                 symbol.startsWith("const& ")) {
       
   330                 // strip modifiers
       
   331                 symbol = symbol.mid(symbol.indexOf(' ') + 1);
       
   332             }
       
   333 
       
   334             if (symbol.startsWith("_") || symbol.startsWith("std::"))
       
   335                 continue;
       
   336             if (symbol.startsWith("vtable ") || symbol.startsWith("VTT for ") ||
       
   337                 symbol.startsWith("construction vtable for"))
       
   338                 continue;
       
   339             if (symbol.startsWith("typeinfo "))
       
   340                 continue;
       
   341             if (symbol.startsWith("non-virtual thunk ") || symbol.startsWith("virtual thunk"))
       
   342                 continue;
       
   343             if (symbol.startsWith(ns + "operator"))
       
   344                 continue;
       
   345             if (symbol.startsWith("guard variable for "))
       
   346                 continue;
       
   347             if (symbol.contains("(" + ns + "QTextStream"))
       
   348                 // QTextStream is excused.
       
   349                 continue;
       
   350             if (symbol.contains("(" + ns + "Q3TextStream"))
       
   351                 // Q3TextStream is excused.
       
   352                 continue;
       
   353             if (symbol.startsWith(ns + "bitBlt") || symbol.startsWith(ns + "copyBlt"))
       
   354                 // you're excused, too
       
   355                 continue;
       
   356 
       
   357             bool symbolOk = false;
       
   358 
       
   359             QHash<QString,QStringList>::ConstIterator it = excusedPrefixes.constBegin();
       
   360             for ( ; it != excusedPrefixes.constEnd(); ++it) {
       
   361                 if (!lib.contains(it.key()))
       
   362                     continue;
       
   363                 foreach (QString prefix, it.value())
       
   364                     if (symbol.startsWith(prefix)) {
       
   365                         symbolOk = true;
       
   366                         break;
       
   367                     }
       
   368             }
       
   369 
       
   370             if (symbolOk)
       
   371                 continue;
       
   372 
       
   373             foreach (QString cSymbolPattern, stupidCSymbols)
       
   374                 if (symbol.contains(cSymbolPattern)) {
       
   375                     symbolOk = true;
       
   376                     break;
       
   377                 }
       
   378 
       
   379             if (symbolOk)
       
   380                 continue;
       
   381 
       
   382             QStringList fields = symbol.split(' ');
       
   383             // the last two fields are address and size and the third last field is the symbol type
       
   384             QVERIFY(fields.count() > 3);
       
   385             QString type = fields.at(fields.count() - 3);
       
   386             // weak symbol
       
   387             if (type == QLatin1String("W")) {
       
   388                 if (symbol.contains("qAtomic"))
       
   389                     continue;
       
   390 
       
   391                 if (symbol.contains("fstat")
       
   392                     || symbol.contains("lstat")
       
   393                     || symbol.contains("stat64")
       
   394                    )
       
   395                     continue;
       
   396 
       
   397                 foreach (QString acceptedPattern, qAlgorithmFunctions + exceptionalSymbols)
       
   398                     if (symbol.contains(acceptedPattern)) {
       
   399                         symbolOk = true;
       
   400                         break;
       
   401                     }
       
   402 
       
   403                 if (symbolOk)
       
   404                     continue;
       
   405 
       
   406                 QString plainSymbol;
       
   407                 for (int i = 0; i < fields.count() - 3; ++i) {
       
   408                     if (i > 0)
       
   409                         plainSymbol += QLatin1Char(' ');
       
   410                     plainSymbol += fields.at(i);
       
   411                 }
       
   412                 foreach (QString qtType, qtTypes)
       
   413                     if (plainSymbol.contains(qtType)) {
       
   414                         symbolOk = true;
       
   415                         break;
       
   416                     }
       
   417 
       
   418                 if (symbolOk)
       
   419                     continue;
       
   420             }
       
   421 
       
   422             QString prefix = ns + "q";
       
   423             if (!symbol.startsWith(prefix, Qt::CaseInsensitive)
       
   424                 && !(isPhonon && symbol.startsWith("Phonon"))) {
       
   425                 qDebug("symbol in '%s' does not start with prefix '%s': '%s'",
       
   426                     qPrintable(lib), qPrintable(prefix), qPrintable(symbol));
       
   427                 isFailed = true;
       
   428             }
       
   429         }
       
   430     }
       
   431 
       
   432 #  if defined(Q_OS_LINUX) && defined(Q_CC_INTEL)
       
   433     QEXPECT_FAIL("", "linux-icc* incorrectly exports some QtWebkit symbols, waiting for a fix from Intel.", Continue);
       
   434 #  endif
       
   435     QVERIFY2(!isFailed, "Libraries contain non-prefixed symbols. See Debug output :)");
       
   436 #else
       
   437     QSKIP("Linux-specific test", SkipAll);
       
   438 #endif
       
   439 }
       
   440 
       
   441 QTEST_MAIN(tst_Symbols)
       
   442 #include "tst_symbols.moc"