From 5aee85ec23f5c28fdccd9bd9d6ac90882bda348e Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 20 Jan 2014 11:53:21 -0800 Subject: Don't allow the QLibraryStore to be recreated during shutdown When QtCore's global destructors are run, they delete the global QLibraryStore qt_library_data and set the pointer to null. If something happened to call QLibraryStore::instance() later, it would be recreated and then weird things might happen. So prevent that from happening. That usually cannot happen, since the only thing that can run after QtCore global destructors are other QtCore global destructors or global destructors from libraries that do not depend on QtCore. So we're reasonably safe. There are two conditions in which something could run after QLibraryStore::cleanup() and still try to access QLibraryStore: 1) indirect dependency, like a global destructor from a library that doesn't depend on QtCore running code from another module that does. 2) static builds of Qt modules. In that case, the order of the global destructors is totally arbitrary and we could get one from a module that depends on QtCore running after QtCore's. That is the case from the bug report. Task-number: QTBUG-36294 Change-Id: Id199671275fd2535acf2d158857ce46b474e579b Reviewed-by: Kai Koehne Reviewed-by: Tim Jenssen --- src/corelib/plugin/qlibrary.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp index 468f759189..9736950c89 100644 --- a/src/corelib/plugin/qlibrary.cpp +++ b/src/corelib/plugin/qlibrary.cpp @@ -374,6 +374,7 @@ private: static QBasicMutex qt_library_mutex; static QLibraryStore *qt_library_data = 0; +static bool qt_library_data_once; QLibraryStore::~QLibraryStore() { @@ -429,8 +430,11 @@ Q_DESTRUCTOR_FUNCTION(qlibraryCleanup) // must be called with a locked mutex QLibraryStore *QLibraryStore::instance() { - if (Q_UNLIKELY(!qt_library_data)) + if (Q_UNLIKELY(!qt_library_data_once && !qt_library_data)) { + // only create once per process lifetime qt_library_data = new QLibraryStore; + qt_library_data_once = true; + } return qt_library_data; } @@ -440,12 +444,15 @@ inline QLibraryPrivate *QLibraryStore::findOrCreate(const QString &fileName, con QLibraryStore *data = instance(); // check if this library is already loaded - QLibraryPrivate *lib = data->libraryMap.value(fileName); + QLibraryPrivate *lib = 0; + if (Q_LIKELY(data)) + lib = data->libraryMap.value(fileName); if (!lib) lib = new QLibraryPrivate(fileName, version); // track this library - data->libraryMap.insert(fileName, lib); + if (Q_LIKELY(data)) + data->libraryMap.insert(fileName, lib); lib->libraryRefCount.ref(); return lib; @@ -464,9 +471,11 @@ inline void QLibraryStore::releaseLibrary(QLibraryPrivate *lib) // no one else is using Q_ASSERT(lib->libraryUnloadCount.load() == 0); - QLibraryPrivate *that = data->libraryMap.take(lib->fileName); - Q_ASSERT(lib == that); - Q_UNUSED(that); + if (Q_LIKELY(data)) { + QLibraryPrivate *that = data->libraryMap.take(lib->fileName); + Q_ASSERT(lib == that); + Q_UNUSED(that); + } delete lib; } -- cgit v1.2.3