Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4compileddata.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical data-parser
4
6
7#include <private/inlinecomponentutils_p.h>
8#include <private/qqmlscriptdata_p.h>
9#include <private/qqmltypenamecache_p.h>
10#include <private/qv4resolvedtypereference_p.h>
11
12#include <QtQml/qqmlfile.h>
13
14#include <QtCore/qdir.h>
15#include <QtCore/qscopeguard.h>
16#include <QtCore/qstandardpaths.h>
17#include <QtCore/qxpfunctional.h>
18
20
21namespace QV4 {
22namespace CompiledData {
23
24
25bool Unit::verifyHeader(QDateTime expectedSourceTimeStamp, QString *errorString) const
26{
27 if (strncmp(magic, CompiledData::magic_str, sizeof(magic))) {
28 *errorString = QStringLiteral("Magic bytes in the header do not match");
29 return false;
30 }
31
32 if (version != quint32(QV4_DATA_STRUCTURE_VERSION)) {
33 *errorString = QString::fromUtf8("V4 data structure version mismatch. Found %1 expected %2")
34 .arg(quint32(version), 0, 16).arg(QV4_DATA_STRUCTURE_VERSION, 0, 16);
35 return false;
36 }
37
38 if (sourceTimeStamp) {
39 // Files from the resource system do not have any time stamps, so fall back to the application
40 // executable.
41 if (!expectedSourceTimeStamp.isValid()) {
42 expectedSourceTimeStamp = QFileInfo(QCoreApplication::applicationFilePath()).lastModified();
43 if (!expectedSourceTimeStamp.isValid()) {
44 *errorString = QStringLiteral("Failed to get valid timestamp from application executable");
45 return false;
46 }
47 }
48 if (expectedSourceTimeStamp.toMSecsSinceEpoch() != sourceTimeStamp) {
49 *errorString = QStringLiteral("QML source file has a different time stamp than cached file.");
50 return false;
51 }
52 }
53
54 return true;
55}
56
57/*!
58 \internal
59 This function creates a temporary key vector and sorts it to guarantuee a stable
60 hash. This is used to calculate a check-sum on dependent meta-objects.
61 */
63 QCryptographicHash *hash, QHash<quintptr, QByteArray> *checksums) const
64{
65 std::vector<int> keys (size());
66 int i = 0;
67 for (auto it = constBegin(), end = constEnd(); it != end; ++it) {
68 keys[i] = it.key();
69 ++i;
70 }
71 std::sort(keys.begin(), keys.end());
72 for (int key: keys) {
73 if (!this->operator[](key)->addToHash(hash, checksums))
74 return false;
75 }
76
77 return true;
78}
79
80CompilationUnit::CompilationUnit(
81 const Unit *unitData, const QString &fileName, const QString &finalUrlString)
82{
83 setUnitData(unitData, nullptr, fileName, finalUrlString);
84}
85
87{
88 qDeleteAll(resolvedTypes);
89
90 if (data) {
91 if (data->qmlUnit() != qmlData)
92 free(const_cast<QmlUnit *>(qmlData));
93 qmlData = nullptr;
94
95 if (!(data->flags & QV4::CompiledData::Unit::StaticData))
96 free(const_cast<Unit *>(data));
97 }
98 data = nullptr;
99#if Q_BYTE_ORDER == Q_BIG_ENDIAN
100 delete [] constants;
101 constants = nullptr;
102#endif
103}
104
105QString CompilationUnit::localCacheFilePath(const QUrl &url)
106{
107 static const QByteArray envCachePath = qgetenv("QML_DISK_CACHE_PATH");
108
109 const QString localSourcePath = QQmlFile::urlToLocalFileOrQrc(url);
110 const QString cacheFileSuffix
111 = QFileInfo(localSourcePath + QLatin1Char('c')).completeSuffix();
112 QCryptographicHash fileNameHash(QCryptographicHash::Sha1);
113 fileNameHash.addData(localSourcePath.toUtf8());
114 QString directory = envCachePath.isEmpty()
115 ? QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
116 + QLatin1String("/qmlcache/")
117 : QString::fromLocal8Bit(envCachePath) + QLatin1String("/");
118 QDir::root().mkpath(directory);
119 return directory + QString::fromUtf8(fileNameHash.result().toHex())
120 + QLatin1Char('.') + cacheFileSuffix;
121}
122
124 const QUrl &url, const QDateTime &sourceTimeStamp, QString *errorString)
125{
126 if (!QQmlFile::isLocalFile(url)) {
127 *errorString = QStringLiteral("File has to be a local file.");
128 return false;
129 }
130
131 const QString sourcePath = QQmlFile::urlToLocalFileOrQrc(url);
132 auto cacheFile = std::make_unique<CompilationUnitMapper>();
133
134 const QStringList cachePaths = { sourcePath + QLatin1Char('c'), localCacheFilePath(url) };
135 for (const QString &cachePath : cachePaths) {
136 Unit *mappedUnit = cacheFile->get(cachePath, sourceTimeStamp, errorString);
137 if (!mappedUnit)
138 continue;
139
140 const Unit *oldData = unitData();
141 const Unit * const oldDataPtr
142 = (oldData && !(oldData->flags & Unit::StaticData))
143 ? oldData
144 : nullptr;
145
146 auto dataPtrRevert = qScopeGuard([this, oldData](){
147 setUnitData(oldData);
148 });
149 setUnitData(mappedUnit);
150
151 if (mappedUnit->sourceFileIndex != 0) {
152 if (mappedUnit->sourceFileIndex >=
153 mappedUnit->stringTableSize + dynamicStrings.size()) {
154 *errorString = QStringLiteral("QML source file index is invalid.");
155 continue;
156 }
157 if (sourcePath !=
158 QQmlFile::urlToLocalFileOrQrc(stringAt(mappedUnit->sourceFileIndex))) {
159 *errorString = QStringLiteral("QML source file has moved to a different location.");
160 continue;
161 }
162 }
163
164 dataPtrRevert.dismiss();
165 free(const_cast<Unit*>(oldDataPtr));
166 backingFile = std::move(cacheFile);
167 return true;
168 }
169
170 return false;
171}
172
173bool CompilationUnit::saveToDisk(const QUrl &unitUrl, QString *errorString) const
174{
175 if (unitData()->sourceTimeStamp == 0) {
176 *errorString = QStringLiteral("Missing time stamp for source file");
177 return false;
178 }
179
180 if (!QQmlFile::isLocalFile(unitUrl)) {
181 *errorString = QStringLiteral("File has to be a local file.");
182 return false;
183 }
184
185 return SaveableUnitPointer(unitData()).saveToDisk<char>(
186 [&unitUrl, errorString](const char *data, quint32 size) {
187 const QString cachePath = localCacheFilePath(unitUrl);
188 if (SaveableUnitPointer::writeDataToFile(
189 cachePath, data, size, errorString)) {
190 CompilationUnitMapper::invalidate(cachePath);
191 return true;
192 }
193
194 return false;
195 });
196}
197
198QStringList CompilationUnit::moduleRequests() const
199{
200 QStringList requests;
201 requests.reserve(data->moduleRequestTableSize);
202 for (uint i = 0; i < data->moduleRequestTableSize; ++i)
203 requests << stringAt(data->moduleRequestTable()[i]);
204 return requests;
205}
206
208{
209 for (ResolvedTypeReference *ref : std::as_const(resolvedTypes)) {
210 if (ref->type().typeId() == type)
211 return ref;
212 }
213 return nullptr;
214
215}
216
218{
219 // Add to type registry of composites
220 if (propertyCaches.needsVMEMetaObject(/*root object*/0)) {
221 // qmlType is only valid for types that have references to themselves.
222 if (type.isValid()) {
223 qmlType = type;
224 } else {
225 qmlType = QQmlMetaType::findCompositeType(
226 url(), this, (unitData()->flags & CompiledData::Unit::IsSingleton)
227 ? QQmlMetaType::Singleton
228 : QQmlMetaType::NonSingleton);
229 }
230
231 QQmlMetaType::registerInternalCompositeType(this);
232 } else {
233 const QV4::CompiledData::Object *obj = objectAt(/*root object*/0);
234 auto *typeRef = resolvedTypes.value(obj->inheritedTypeNameIndex);
235 Q_ASSERT(typeRef);
236 qmlType = typeRef->type();
237 }
238}
239
240bool CompilationUnit::verifyChecksum(const DependentTypesHasher &dependencyHasher) const
241{
242 if (!dependencyHasher) {
243 for (size_t i = 0; i < sizeof(data->dependencyMD5Checksum); ++i) {
244 if (data->dependencyMD5Checksum[i] != 0)
245 return false;
246 }
247 return true;
248 }
249 const QByteArray checksum = dependencyHasher();
250 return checksum.size() == sizeof(data->dependencyMD5Checksum)
251 && memcmp(data->dependencyMD5Checksum, checksum.constData(),
252 sizeof(data->dependencyMD5Checksum)) == 0;
253}
254
255QQmlType CompilationUnit::qmlTypeForComponent(const QString &inlineComponentName) const
256{
257 if (inlineComponentName.isEmpty())
258 return qmlType;
259 return inlineComponentData[inlineComponentName].qmlType;
260}
261
262} // namespace CompiledData
263} // namespace QV4
264
265QT_END_NAMESPACE
Combined button and popup list for selecting options.
static const char magic_str[]
Definition qjsvalue.h:24
#define QV4_DATA_STRUCTURE_VERSION
Q_QML_EXPORT bool saveToDisk(const QUrl &unitUrl, QString *errorString) const
void finalizeCompositeType(const QQmlType &type)
ResolvedTypeReference * resolvedType(int id) const
ResolvedTypeReferenceMap resolvedTypes
bool verifyChecksum(const CompiledData::DependentTypesHasher &dependencyHasher) const
QQmlType qmlTypeForComponent(const QString &inlineComponentName=QString()) const
const CompiledObject * objectAt(int index) const
Q_QML_EXPORT bool loadFromDisk(const QUrl &url, const QDateTime &sourceTimeStamp, QString *errorString)
bool addToHash(QCryptographicHash *hash, QHash< quintptr, QByteArray > *checksums) const
bool verifyHeader(QDateTime expectedSourceTimeStamp, QString *errorString) const