1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qv4compilationunitmapper_p.h"
41
42 #include "qv4executablecompilationunit_p.h"
43 #include <QScopeGuard>
44 #include <QFileInfo>
45 #include <QDateTime>
46 #include <qt_windows.h>
47
48 QT_BEGIN_NAMESPACE
49
50 using namespace QV4;
51
open(const QString & cacheFileName,const QDateTime & sourceTimeStamp,QString * errorString)52 CompiledData::Unit *CompilationUnitMapper::open(const QString &cacheFileName, const QDateTime &sourceTimeStamp, QString *errorString)
53 {
54 close();
55
56 // ### TODO: fix up file encoding/normalization/unc handling once QFileSystemEntry
57 // is exported from QtCore.
58 HANDLE handle =
59 #if defined(Q_OS_WINRT)
60 CreateFile2(reinterpret_cast<const wchar_t*>(cacheFileName.constData()),
61 GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ,
62 OPEN_EXISTING, nullptr);
63 #else
64 CreateFile(reinterpret_cast<const wchar_t*>(cacheFileName.constData()),
65 GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ,
66 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
67 nullptr);
68 #endif
69 if (handle == INVALID_HANDLE_VALUE) {
70 *errorString = qt_error_string(GetLastError());
71 return nullptr;
72 }
73
74 auto fileHandleCleanup = qScopeGuard([handle]{
75 CloseHandle(handle);
76 });
77
78 CompiledData::Unit header;
79 DWORD bytesRead;
80 if (!ReadFile(handle, reinterpret_cast<char *>(&header), sizeof(header), &bytesRead, nullptr)) {
81 *errorString = qt_error_string(GetLastError());
82 return nullptr;
83 }
84
85 if (bytesRead != sizeof(header)) {
86 *errorString = QStringLiteral("File too small for the header fields");
87 return nullptr;
88 }
89
90 if (!ExecutableCompilationUnit::verifyHeader(&header, sourceTimeStamp, errorString))
91 return nullptr;
92
93 // Data structure and qt version matched, so now we can access the rest of the file safely.
94
95 HANDLE fileMappingHandle = CreateFileMapping(handle, 0, PAGE_READONLY, 0, 0, 0);
96 if (!fileMappingHandle) {
97 *errorString = qt_error_string(GetLastError());
98 return nullptr;
99 }
100
101 auto mappingCleanup = qScopeGuard([fileMappingHandle]{
102 CloseHandle(fileMappingHandle);
103 });
104
105 dataPtr = MapViewOfFile(fileMappingHandle, FILE_MAP_READ, 0, 0, 0);
106 if (!dataPtr) {
107 *errorString = qt_error_string(GetLastError());
108 return nullptr;
109 }
110
111 return reinterpret_cast<CompiledData::Unit*>(dataPtr);
112 }
113
close()114 void CompilationUnitMapper::close()
115 {
116 if (dataPtr != nullptr) {
117 // Do not unmap cache files that are built with the StaticData flag. That's the majority of
118 // them and it's necessary to benefit from the QString literal optimization. There might
119 // still be QString instances around that point into that memory area. The memory is backed
120 // on the disk, so the kernel is free to release the pages and all that remains is the
121 // address space allocation.
122 if (!(reinterpret_cast<CompiledData::Unit*>(dataPtr)->flags & CompiledData::Unit::StaticData))
123 UnmapViewOfFile(dataPtr);
124 }
125 dataPtr = nullptr;
126 }
127
128 QT_END_NAMESPACE
129