moc: Initialize staticMetaObject with the highest user-settable priority
The referenced static meta object for the superclass might be in a different DLL. In this case, the whole QMetaObject can't be initialized all via preinitialized data in the data section of the binary, but must run code at runtime to fill in the value of the dllimported pointer. In these cases, both GCC and MSVC initialize as much as possible statically, while only filling in the dllimported values (QMetaObject::d::superdata) at runtime. Clang, on the other side, initializes the whole struct at runtime if some part of it needs runtime initialization, leaving the struct completely uninitialized before constructors are run. In C++, there are no guarantees for in what order constructors in different translation units are executed. This in particular means that there are no guarantees as to whether qRegisterWidgetsVariant() in qwidgetsvariants.cpp runs before or after the runtime initialization of QWidget::staticMetaObject. With GCC and MSVC, this doesn't seem to have mattered since only the superdata pointer of the staticMetaObject was uninitialized - everything else was initialized, and the superdata pointer doesn't seem to be accessed during qRegisterWidgetsVariant. With clang, the whole staticMetaObject is uninitialized, unless the staticMetaObject has been initialized before (and the initialization order is undefined). By setting a manual priority (which is a GCC extension that also clang supports) for the staticMetaObjects, we can be sure that these are initialized before the actual explicit constructor invocations (without any explicit initialization priority) that can access the staticMetaObjects. Change-Id: I64a82f12d690528567509791bae088b6304e189b Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
parent
50deb8cf70
commit
74118a4784
@ -508,6 +508,12 @@ using qsizetype = QIntegerForSizeof<std::size_t>::Signed;
|
||||
# define Q_ALWAYS_INLINE inline
|
||||
#endif
|
||||
|
||||
#ifdef Q_CC_GNU
|
||||
# define QT_INIT_METAOBJECT __attribute__((init_priority(101)))
|
||||
#else
|
||||
# define QT_INIT_METAOBJECT
|
||||
#endif
|
||||
|
||||
//defines the type for the WNDPROC on windows
|
||||
//the alignment needs to be forced for sse2 to not crash with mingw
|
||||
#if defined(Q_OS_WIN)
|
||||
|
@ -523,9 +523,9 @@ void Generator::generateCode()
|
||||
// Finally create and initialize the static meta object
|
||||
//
|
||||
if (isQt)
|
||||
fprintf(out, "const QMetaObject QObject::staticQtMetaObject = {\n");
|
||||
fprintf(out, "QT_INIT_METAOBJECT const QMetaObject QObject::staticQtMetaObject = {\n");
|
||||
else
|
||||
fprintf(out, "const QMetaObject %s::staticMetaObject = {\n", cdef->qualified.constData());
|
||||
fprintf(out, "QT_INIT_METAOBJECT const QMetaObject %s::staticMetaObject = {\n", cdef->qualified.constData());
|
||||
|
||||
if (isQObject)
|
||||
fprintf(out, " { nullptr, ");
|
||||
|
Loading…
Reference in New Issue
Block a user