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
qv4compilationunitmapper_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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:significant
4
6
7#include <private/qv4compileddata_p.h>
8
9#include <QtCore/qdatetime.h>
10#include <QtCore/qfileinfo.h>
11#include <QtCore/qscopeguard.h>
12
13#include <qt_windows.h>
14
16
17using namespace QV4;
18
19CompiledData::Unit *CompilationUnitMapper::open(const QString &cacheFileName, const QDateTime &sourceTimeStamp, QString *errorString)
20{
21 close();
22
23 // ### TODO: fix up file encoding/normalization/unc handling once QFileSystemEntry
24 // is exported from QtCore.
25 HANDLE handle =
26 CreateFile(reinterpret_cast<const wchar_t*>(cacheFileName.constData()),
27 GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ,
28 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
29 nullptr);
30 if (handle == INVALID_HANDLE_VALUE) {
31 *errorString = qt_error_string(GetLastError());
32 return nullptr;
33 }
34
35 auto fileHandleCleanup = qScopeGuard([handle]{
36 CloseHandle(handle);
37 });
38
39 CompiledData::Unit header;
40 DWORD bytesRead;
41 if (!ReadFile(handle, reinterpret_cast<char *>(&header), sizeof(header), &bytesRead, nullptr)) {
42 *errorString = qt_error_string(GetLastError());
43 return nullptr;
44 }
45
46 if (bytesRead != sizeof(header)) {
47 *errorString = QStringLiteral("File too small for the header fields");
48 return nullptr;
49 }
50
51 if (!header.verifyHeader(sourceTimeStamp, errorString))
52 return nullptr;
53
54 // Data structure and qt version matched, so now we can access the rest of the file safely.
55
56 /* Error out early on file corruption. We assume we can read header.unitSize bytes
57 later (even before verifying the checksum), potentially causing out-of-bound
58 reads
59 Also, no need to wait until checksum verification if we know beforehand
60 that the cached unit is bogus
61 */
62 LARGE_INTEGER fileSize;
63 if (!GetFileSizeEx(handle, &fileSize)) {
64 *errorString = QStringLiteral("Could not determine file size");
65 return nullptr;
66 }
67 if (header.unitSize != fileSize.QuadPart) {
68 *errorString = QStringLiteral("Potential file corruption, file too small");
69 return nullptr;
70 }
71
72
73 HANDLE fileMappingHandle = CreateFileMapping(handle, 0, PAGE_READONLY, 0, 0, 0);
74 if (!fileMappingHandle) {
75 *errorString = qt_error_string(GetLastError());
76 return nullptr;
77 }
78
79 auto mappingCleanup = qScopeGuard([fileMappingHandle]{
80 CloseHandle(fileMappingHandle);
81 });
82
83 dataPtr = MapViewOfFile(fileMappingHandle, FILE_MAP_READ, 0, 0, 0);
84 if (!dataPtr) {
85 *errorString = qt_error_string(GetLastError());
86 return nullptr;
87 }
88
89 return reinterpret_cast<CompiledData::Unit*>(dataPtr);
90}
91
92void CompilationUnitMapper::close()
93{
94 if (dataPtr != nullptr) {
95 // Do not unmap cache files that are built with the StaticData flag. That's the majority of
96 // them and it's necessary to benefit from the QString literal optimization. There might
97 // still be QString instances around that point into that memory area. The memory is backed
98 // on the disk, so the kernel is free to release the pages and all that remains is the
99 // address space allocation.
100 if (!(reinterpret_cast<CompiledData::Unit*>(dataPtr)->flags & CompiledData::Unit::StaticData))
101 UnmapViewOfFile(dataPtr);
102 }
103 dataPtr = nullptr;
104}
105
106QT_END_NAMESPACE