diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2016-08-09 10:30:26 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2016-08-09 09:43:58 +0000 |
commit | 3cba637ea6b4109171e5cab678f5ccecaed40809 (patch) | |
tree | 7266d2b333e9993196ca54633a915c8e1252e484 /src | |
parent | 4c47c32c925f0725b57c61875bbb50864032bdd7 (diff) |
Separate header verification and mmap handling of CompiledData::Unit
...into a separate class. This will make it easier to specialize the
handling for Posix and Windows platforms.
Change-Id: I995538fa8239eac8586c96812086e50b4c19257f
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/compiler/compiler.pri | 6 | ||||
-rw-r--r-- | src/qml/compiler/qv4compilationunitmapper.cpp | 114 | ||||
-rw-r--r-- | src/qml/compiler/qv4compilationunitmapper_p.h | 82 | ||||
-rw-r--r-- | src/qml/compiler/qv4compileddata.cpp | 50 | ||||
-rw-r--r-- | src/qml/compiler/qv4compileddata_p.h | 3 |
5 files changed, 207 insertions, 48 deletions
diff --git a/src/qml/compiler/compiler.pri b/src/qml/compiler/compiler.pri index ad73c26b15..02288a8ad7 100644 --- a/src/qml/compiler/compiler.pri +++ b/src/qml/compiler/compiler.pri @@ -28,7 +28,8 @@ HEADERS += \ $$PWD/qv4isel_moth_p.h \ $$PWD/qv4instr_moth_p.h \ $$PWD/qqmlpropertycachecreator_p.h \ - $$PWD/qqmlpropertyvalidator_p.h + $$PWD/qqmlpropertyvalidator_p.h \ + $$PWD/qv4compilationunitmapper_p.h SOURCES += \ @@ -36,6 +37,7 @@ SOURCES += \ $$PWD/qv4instr_moth.cpp \ $$PWD/qv4isel_moth.cpp \ $$PWD/qqmlpropertycachecreator.cpp \ - $$PWD/qqmlpropertyvalidator.cpp + $$PWD/qqmlpropertyvalidator.cpp \ + $$PWD/qv4compilationunitmapper.cpp } diff --git a/src/qml/compiler/qv4compilationunitmapper.cpp b/src/qml/compiler/qv4compilationunitmapper.cpp new file mode 100644 index 0000000000..1d8be036c9 --- /dev/null +++ b/src/qml/compiler/qv4compilationunitmapper.cpp @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qv4compilationunitmapper_p.h" + +#include "qv4compileddata_p.h" +#include <QFileInfo> +#include <QDateTime> + +QT_BEGIN_NAMESPACE + +using namespace QV4; + +CompilationUnitMapper::CompilationUnitMapper() + : dataPtr(nullptr) +{ + +} + +CompiledData::Unit *CompilationUnitMapper::open(const QString &sourcePath, QString *errorString) +{ + close(); + + f.setFileName(sourcePath + QLatin1Char('c')); + if (!f.open(QIODevice::ReadOnly)) { + *errorString = f.errorString(); + return nullptr; + } + + CompiledData::Unit header; + qint64 bytesRead = f.read(reinterpret_cast<char *>(&header), sizeof(header)); + + if (bytesRead != sizeof(header)) { + *errorString = QStringLiteral("File too small for the header fields"); + return nullptr; + } + + if (strncmp(header.magic, CompiledData::magic_str, sizeof(header.magic))) { + *errorString = QStringLiteral("Magic bytes in the header do not match"); + return nullptr; + } + + if (header.version != quint32(QV4_DATA_STRUCTURE_VERSION)) { + *errorString = QString::fromUtf8("V4 data structure version mismatch. Found %1 expected %2").arg(header.version, 0, 16).arg(QV4_DATA_STRUCTURE_VERSION, 0, 16); + return nullptr; + } + + if (header.qtVersion != quint32(QT_VERSION)) { + *errorString = QString::fromUtf8("Qt version mismatch. Found %1 expected %2").arg(header.qtVersion, 0, 16).arg(QT_VERSION, 0, 16); + return nullptr; + } + + { + QFileInfo sourceCode(sourcePath); + if (sourceCode.exists() && sourceCode.lastModified().toMSecsSinceEpoch() != header.sourceTimeStamp) { + *errorString = QStringLiteral("QML source file has a different time stamp than cached file."); + return nullptr; + } + } + + // Data structure and qt version matched, so now we can access the rest of the file safely. + + dataPtr = f.map(/*offset*/0, f.size()); + if (!dataPtr) { + *errorString = f.errorString(); + return nullptr; + } + + return reinterpret_cast<CompiledData::Unit*>(dataPtr); +} + +void CompilationUnitMapper::close() +{ + f.close(); + dataPtr = nullptr; +} + +QT_END_NAMESPACE diff --git a/src/qml/compiler/qv4compilationunitmapper_p.h b/src/qml/compiler/qv4compilationunitmapper_p.h new file mode 100644 index 0000000000..06ca3d0e02 --- /dev/null +++ b/src/qml/compiler/qv4compilationunitmapper_p.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QV4COMPILATIONUNITMAPPER_H +#define QV4COMPILATIONUNITMAPPER_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <private/qv4global_p.h> +#include <QFile> + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +namespace CompiledData { +struct Unit; +} + +class CompilationUnitMapper +{ +public: + CompilationUnitMapper(); + + CompiledData::Unit *open(const QString &sourcePath, QString *errorString); + void close(); + +private: + QFile f; + uchar *dataPtr; +}; + +} + +QT_END_NAMESPACE + +#endif // QV4COMPILATIONUNITMAPPER_H diff --git a/src/qml/compiler/qv4compileddata.cpp b/src/qml/compiler/qv4compileddata.cpp index 98eb7188cc..cc1f8ae261 100644 --- a/src/qml/compiler/qv4compileddata.cpp +++ b/src/qml/compiler/qv4compileddata.cpp @@ -49,6 +49,7 @@ #include <private/qqmlpropertycache_p.h> #include <private/qqmltypeloader_p.h> #include <private/qqmlengine_p.h> +#include "qv4compilationunitmapper_p.h" #include <QQmlPropertyMap> #include <QDateTime> #include <QSaveFile> @@ -376,56 +377,15 @@ bool CompilationUnit::loadFromDisk(const QUrl &url, EvalISelFactory *iselFactory } const QString sourcePath = url.toLocalFile(); - QScopedPointer<QFile> cacheFile(new QFile(sourcePath + QLatin1Char('c'))); + QScopedPointer<CompilationUnitMapper> cacheFile(new CompilationUnitMapper()); - if (!cacheFile->open(QIODevice::ReadOnly)) { - *errorString = cacheFile->errorString(); - return false; - } - - { - CompiledData::Unit header; - qint64 bytesRead = cacheFile->read(reinterpret_cast<char *>(&header), sizeof(header)); - - if (bytesRead != sizeof(header)) { - *errorString = QStringLiteral("File too small for the header fields"); - return false; - } - - if (strncmp(header.magic, CompiledData::magic_str, sizeof(header.magic))) { - *errorString = QStringLiteral("Magic bytes in the header do not match"); - return false; - } - - if (header.version != quint32(QV4_DATA_STRUCTURE_VERSION)) { - *errorString = QString::fromUtf8("V4 data structure version mismatch. Found %1 expected %2").arg(header.version, 0, 16).arg(QV4_DATA_STRUCTURE_VERSION, 0, 16); - return false; - } - - if (header.qtVersion != quint32(QT_VERSION)) { - *errorString = QString::fromUtf8("Qt version mismatch. Found %1 expected %2").arg(header.qtVersion, 0, 16).arg(QT_VERSION, 0, 16); - return false; - } - - { - QFileInfo sourceCode(sourcePath); - if (sourceCode.exists() && sourceCode.lastModified().toMSecsSinceEpoch() != header.sourceTimeStamp) { - *errorString = QStringLiteral("QML source file has a different time stamp than cached file."); - return false; - } - } - - } - // Data structure and qt version matched, so now we can access the rest of the file safely. - - uchar *cacheData = cacheFile->map(/*offset*/0, cacheFile->size()); - if (!cacheData) { - *errorString = cacheFile->errorString(); + CompiledData::Unit *mappedUnit = cacheFile->open(sourcePath, errorString); + if (!mappedUnit) { return false; } const Unit * const oldDataPtr = (data && !(data->flags & QV4::CompiledData::Unit::StaticData)) ? data : nullptr; - QScopedValueRollback<const Unit *> dataPtrChange(data, reinterpret_cast<const Unit *>(cacheData)); + QScopedValueRollback<const Unit *> dataPtrChange(data, mappedUnit); { const QString foundArchitecture = stringAt(data->architectureIndex); diff --git a/src/qml/compiler/qv4compileddata_p.h b/src/qml/compiler/qv4compileddata_p.h index 0903654e51..a6ca1594a4 100644 --- a/src/qml/compiler/qv4compileddata_p.h +++ b/src/qml/compiler/qv4compileddata_p.h @@ -92,6 +92,7 @@ struct Function; struct Function; class EvalISelFactory; +class CompilationUnitMapper; namespace CompiledData { @@ -874,7 +875,7 @@ struct Q_QML_PRIVATE_EXPORT CompilationUnit : public QQmlRefCount int listMetaTypeId; bool isRegisteredWithEngine; - QScopedPointer<QIODevice> backingFile; + QScopedPointer<CompilationUnitMapper> backingFile; // --- interface for QQmlPropertyCacheCreator typedef Object CompiledObject; |