| 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 | |
| 4 | #include "qv4compilationunitmapper_p.h" |
| 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 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | using namespace QV4; |
| 18 | |
| 19 | CompiledData::Unit *CompilationUnitMapper::open(const QString &cacheFileName, const QDateTime &sourceTimeStamp, QString *errorString) |
| 20 | { |
| 21 | close(); |
| 22 | |
| 23 | int fd = qt_safe_open(pathname: QFile::encodeName(fileName: cacheFileName).constData(), O_RDONLY); |
| 24 | if (fd == -1) { |
| 25 | *errorString = qt_error_string(errno); |
| 26 | return nullptr; |
| 27 | } |
| 28 | |
| 29 | auto cleanup = qScopeGuard(f: [fd]{ |
| 30 | qt_safe_close(fd) ; |
| 31 | }); |
| 32 | |
| 33 | CompiledData::Unit ; |
| 34 | qint64 bytesRead = qt_safe_read(fd, data: reinterpret_cast<char *>(&header), maxlen: 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(expectedSourceTimeStamp: 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: fd, offset: 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(addr: nullptr, len: length, PROT_READ, MAP_SHARED, fd: fd, /*offset*/0); |
| 59 | if (ptr == MAP_FAILED) { |
| 60 | *errorString = qt_error_string(errno); |
| 61 | return nullptr; |
| 62 | } |
| 63 | dataPtr = ptr; |
| 64 | |
| 65 | return reinterpret_cast<CompiledData::Unit*>(dataPtr); |
| 66 | } |
| 67 | |
| 68 | void 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(addr: dataPtr, len: length); |
| 79 | } |
| 80 | dataPtr = nullptr; |
| 81 | } |
| 82 | |
| 83 | QT_END_NAMESPACE |
| 84 | |