Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qv4compilationunitmapper_unix.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
5
6#include <private/qcore_unix_p.h>
7#include <private/qv4compileddata_p.h>
8
9#include <QtCore/qscopeguard.h>
10#include <QtCore/qdatetime.h>
11
12#include <functional>
13#include <sys/mman.h>
14
16
17using namespace QV4;
18
19CompiledData::Unit *CompilationUnitMapper::open(const QString &cacheFileName, const QDateTime &sourceTimeStamp, QString *errorString)
20{
21 close();
22
23 int fd = qt_safe_open(QFile::encodeName(cacheFileName).constData(), O_RDONLY);
24 if (fd == -1) {
26 return nullptr;
27 }
28
29 auto cleanup = qScopeGuard([fd]{
31 });
32
34 qint64 bytesRead = qt_safe_read(fd, reinterpret_cast<char *>(&header), sizeof(header));
35
36 if (bytesRead != sizeof(header)) {
37 *errorString = QStringLiteral("File too small for the header fields");
38 return nullptr;
39 }
40
41 if (!header.verifyHeader(sourceTimeStamp, errorString))
42 return nullptr;
43
44 // Data structure and qt version matched, so now we can access the rest of the file safely.
45
46 length = static_cast<size_t>(lseek(fd, 0, SEEK_END));
47 /* Error out early on file corruption. We assume we can read header.unitSize bytes
48 later (even before verifying the checksum), potentially causing out-of-bound
49 reads
50 Also, no need to wait until checksum verification if we know beforehand
51 that the cached unit is bogus
52 */
53 if (length != header.unitSize) {
54 *errorString = QStringLiteral("Potential file corruption, file too small");
55 return nullptr;
56 }
57
58 void *ptr = mmap(nullptr, length, PROT_READ, MAP_SHARED, fd, /*offset*/0);
59 if (ptr == MAP_FAILED) {
61 return nullptr;
62 }
63 dataPtr = ptr;
64
65 return reinterpret_cast<CompiledData::Unit*>(dataPtr);
66}
67
68void CompilationUnitMapper::close()
69{
70 // Do not unmap the data here.
71 if (dataPtr != nullptr) {
72 // Do not unmap cache files that are built with the StaticData flag. That's the majority of
73 // them and it's necessary to benefit from the QString literal optimization. There might
74 // still be QString instances around that point into that memory area. The memory is backed
75 // on the disk, so the kernel is free to release the pages and all that remains is the
76 // address space allocation.
77 if (!(reinterpret_cast<CompiledData::Unit*>(dataPtr)->flags & CompiledData::Unit::StaticData))
78 munmap(dataPtr, length);
79 }
80 dataPtr = nullptr;
81}
82
\inmodule QtCore\reentrant
Definition qdatetime.h:283
static QByteArray encodeName(const QString &fileName)
Converts fileName to an 8-bit encoding that you can use in native APIs.
Definition qfile.h:158
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
Q_MULTIMEDIA_EXPORT QString errorString(HRESULT hr)
static int qt_safe_open(const char *pathname, int flags, mode_t mode=0777)
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
static int qt_safe_close(int fd)
static QString header(const QString &name)
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
static ControlElement< T > * ptr(QWidget *widget)
GLenum GLuint GLenum GLsizei length
GLbitfield flags
GLuint64 GLenum GLint fd
#define MAP_FAILED
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
#define QStringLiteral(str)
long long qint64
Definition qtypes.h:60