2012-01-20 12:48:44 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
2012-01-20 12:48:50 +04:00
|
|
|
#include <sys/stat.h>
|
2012-01-20 12:48:44 +04:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
2012-02-07 13:35:00 +04:00
|
|
|
#include <cstdio>
|
2016-11-10 21:38:27 +03:00
|
|
|
#include <string>
|
2013-12-30 23:34:54 +04:00
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
#include "Mappable.h"
|
2013-12-30 23:34:54 +04:00
|
|
|
|
2017-05-17 20:06:22 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2013-12-30 23:34:54 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
# include <linux/ashmem.h>
|
|
|
|
#endif
|
2012-02-22 11:12:15 +04:00
|
|
|
#include <sys/stat.h>
|
2014-01-15 11:21:45 +04:00
|
|
|
#include <errno.h>
|
2012-01-20 12:48:50 +04:00
|
|
|
#include "ElfLoader.h"
|
2016-08-23 17:01:14 +03:00
|
|
|
#include "XZStream.h"
|
2012-01-20 12:48:44 +04:00
|
|
|
#include "Logging.h"
|
|
|
|
|
2014-07-22 02:14:11 +04:00
|
|
|
using mozilla::MakeUnique;
|
|
|
|
using mozilla::UniquePtr;
|
|
|
|
|
2016-08-17 01:54:28 +03:00
|
|
|
class CacheValidator {
|
|
|
|
public:
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheValidator(const char* aCachedLibPath, Zip* aZip, Zip::Stream* aStream)
|
2016-11-10 21:38:27 +03:00
|
|
|
: mCachedLibPath(aCachedLibPath) {
|
2016-08-17 01:54:28 +03:00
|
|
|
static const char kChecksumSuffix[] = ".crc";
|
|
|
|
|
|
|
|
mCachedChecksumPath =
|
|
|
|
MakeUnique<char[]>(strlen(aCachedLibPath) + sizeof(kChecksumSuffix));
|
|
|
|
sprintf(mCachedChecksumPath.get(), "%s%s", aCachedLibPath, kChecksumSuffix);
|
|
|
|
DEBUG_LOG("mCachedChecksumPath: %s", mCachedChecksumPath.get());
|
|
|
|
|
|
|
|
mChecksum = aStream->GetCRC32();
|
|
|
|
DEBUG_LOG("mChecksum: %x", mChecksum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether the cache is valid and up-to-date.
|
|
|
|
bool IsValid() const {
|
|
|
|
// Validate based on checksum.
|
|
|
|
RefPtr<Mappable> checksumMap =
|
|
|
|
MappableFile::Create(mCachedChecksumPath.get());
|
|
|
|
if (!checksumMap) {
|
|
|
|
// Force caching if checksum is missing in cache.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_LOG("Comparing %x with %s", mChecksum, mCachedChecksumPath.get());
|
|
|
|
MappedPtr checksumBuf = checksumMap->mmap(nullptr, checksumMap->GetLength(),
|
|
|
|
PROT_READ, MAP_PRIVATE, 0);
|
|
|
|
if (checksumBuf == MAP_FAILED) {
|
|
|
|
WARN("Couldn't map %s to validate checksum", mCachedChecksumPath.get());
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-10 21:38:27 +03:00
|
|
|
if (memcmp(checksumBuf, &mChecksum, sizeof(mChecksum))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !access(mCachedLibPath.c_str(), R_OK);
|
2016-08-17 01:54:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Caches the APK-provided checksum used in future cache validations.
|
|
|
|
void CacheChecksum() const {
|
|
|
|
AutoCloseFD fd(open(mCachedChecksumPath.get(),
|
|
|
|
O_TRUNC | O_RDWR | O_CREAT | O_NOATIME,
|
|
|
|
S_IRUSR | S_IWUSR));
|
|
|
|
if (fd == -1) {
|
|
|
|
WARN("Couldn't open %s to update checksum", mCachedChecksumPath.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_LOG("Updating checksum %s", mCachedChecksumPath.get());
|
|
|
|
|
|
|
|
const size_t size = sizeof(mChecksum);
|
|
|
|
size_t written = 0;
|
|
|
|
while (written < size) {
|
|
|
|
ssize_t ret =
|
2019-05-01 11:47:10 +03:00
|
|
|
write(fd, reinterpret_cast<const uint8_t*>(&mChecksum) + written,
|
2016-08-17 01:54:28 +03:00
|
|
|
size - written);
|
|
|
|
if (ret >= 0) {
|
|
|
|
written += ret;
|
|
|
|
} else if (errno != EINTR) {
|
|
|
|
WARN("Writing checksum %s failed with errno %d",
|
|
|
|
mCachedChecksumPath.get(), errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-10 21:38:27 +03:00
|
|
|
const std::string mCachedLibPath;
|
2016-08-17 01:54:28 +03:00
|
|
|
UniquePtr<char[]> mCachedChecksumPath;
|
|
|
|
uint32_t mChecksum;
|
|
|
|
};
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Mappable* MappableFile::Create(const char* path) {
|
2012-01-20 12:48:44 +04:00
|
|
|
int fd = open(path, O_RDONLY);
|
|
|
|
if (fd != -1) return new MappableFile(fd);
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
MemoryRange MappableFile::mmap(const void* addr, size_t length, int prot,
|
2012-01-20 12:48:44 +04:00
|
|
|
int flags, off_t offset) {
|
2012-02-13 18:49:45 +04:00
|
|
|
MOZ_ASSERT(fd != -1);
|
|
|
|
MOZ_ASSERT(!(flags & MAP_SHARED));
|
2012-01-20 12:48:44 +04:00
|
|
|
flags |= MAP_PRIVATE;
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
return MemoryRange::mmap(const_cast<void*>(addr), length, prot, flags, fd,
|
2013-07-23 02:26:07 +04:00
|
|
|
offset);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappableFile::finalize() {
|
|
|
|
/* Close file ; equivalent to close(fd.forget()) */
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
|
2013-04-12 12:23:12 +04:00
|
|
|
size_t MappableFile::GetLength() const {
|
|
|
|
struct stat st;
|
|
|
|
return fstat(fd, &st) ? 0 : st.st_size;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Mappable* MappableExtractFile::Create(const char* name, Zip* zip,
|
|
|
|
Zip::Stream* stream) {
|
2016-08-17 01:54:28 +03:00
|
|
|
MOZ_ASSERT(zip && stream);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* cachePath = getenv("MOZ_LINKER_CACHE");
|
2012-01-20 12:48:50 +04:00
|
|
|
if (!cachePath || !*cachePath) {
|
2014-06-13 03:45:58 +04:00
|
|
|
WARN(
|
|
|
|
"MOZ_LINKER_EXTRACT is set, but not MOZ_LINKER_CACHE; "
|
2012-01-20 12:48:50 +04:00
|
|
|
"not extracting");
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-01-20 12:48:50 +04:00
|
|
|
}
|
2017-02-08 21:49:20 +03:00
|
|
|
|
|
|
|
// Ensure that the cache dir is private.
|
|
|
|
chmod(cachePath, 0770);
|
|
|
|
|
2014-07-22 02:14:11 +04:00
|
|
|
UniquePtr<char[]> path =
|
|
|
|
MakeUnique<char[]>(strlen(cachePath) + strlen(name) + 2);
|
2013-12-30 23:34:54 +04:00
|
|
|
sprintf(path.get(), "%s/%s", cachePath, name);
|
2016-08-17 01:54:28 +03:00
|
|
|
|
|
|
|
CacheValidator validator(path.get(), zip, stream);
|
|
|
|
if (validator.IsValid()) {
|
2019-05-01 11:47:10 +03:00
|
|
|
DEBUG_LOG("Reusing %s", static_cast<char*>(path.get()));
|
2016-08-17 01:54:28 +03:00
|
|
|
return MappableFile::Create(path.get());
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
2019-05-01 11:47:10 +03:00
|
|
|
DEBUG_LOG("Extracting to %s", static_cast<char*>(path.get()));
|
2012-07-30 22:17:53 +04:00
|
|
|
AutoCloseFD fd;
|
2013-12-30 23:34:54 +04:00
|
|
|
fd = open(path.get(), O_TRUNC | O_RDWR | O_CREAT | O_NOATIME,
|
|
|
|
S_IRUSR | S_IWUSR);
|
2012-01-20 12:48:50 +04:00
|
|
|
if (fd == -1) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't open %s to decompress library", path.get());
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-01-20 12:48:50 +04:00
|
|
|
}
|
2014-07-22 02:14:11 +04:00
|
|
|
AutoUnlinkFile file(path.release());
|
2012-02-22 11:12:15 +04:00
|
|
|
if (stream->GetType() == Zip::Stream::DEFLATE) {
|
|
|
|
if (ftruncate(fd, stream->GetUncompressedSize()) == -1) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't ftruncate %s to decompress library", file.get());
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
/* Map the temporary file for use as inflate buffer */
|
2013-11-11 23:15:46 +04:00
|
|
|
MappedPtr buffer(MemoryRange::mmap(nullptr, stream->GetUncompressedSize(),
|
2013-06-27 04:35:49 +04:00
|
|
|
PROT_WRITE, MAP_SHARED, fd, 0));
|
2012-02-22 11:12:15 +04:00
|
|
|
if (buffer == MAP_FAILED) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't map %s to decompress library", file.get());
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
2012-01-20 12:48:50 +04:00
|
|
|
|
2018-05-10 05:45:23 +03:00
|
|
|
z_stream zStream = stream->GetZStream(buffer);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/* Decompress */
|
|
|
|
if (inflateInit2(&zStream, -MAX_WBITS) != Z_OK) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("inflateInit failed: %s", zStream.msg);
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
if (inflate(&zStream, Z_FINISH) != Z_STREAM_END) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("inflate failed: %s", zStream.msg);
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
if (inflateEnd(&zStream) != Z_OK) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("inflateEnd failed: %s", zStream.msg);
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
if (zStream.total_out != stream->GetUncompressedSize()) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("File not fully uncompressed! %ld / %d", zStream.total_out,
|
2012-02-22 11:12:15 +04:00
|
|
|
static_cast<unsigned int>(stream->GetUncompressedSize()));
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
2016-08-23 17:01:14 +03:00
|
|
|
} else if (XZStream::IsXZ(stream->GetBuffer(), stream->GetSize())) {
|
|
|
|
XZStream xzStream(stream->GetBuffer(), stream->GetSize());
|
|
|
|
|
|
|
|
if (!xzStream.Init()) {
|
|
|
|
ERROR("Couldn't initialize XZ decoder");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-05-17 20:06:22 +03:00
|
|
|
DEBUG_LOG("XZStream created, compressed=%" PRIuPTR
|
|
|
|
", uncompressed=%" PRIuPTR,
|
2016-08-23 17:01:14 +03:00
|
|
|
xzStream.Size(), xzStream.UncompressedSize());
|
|
|
|
|
|
|
|
if (ftruncate(fd, xzStream.UncompressedSize()) == -1) {
|
|
|
|
ERROR("Couldn't ftruncate %s to decompress library", file.get());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MappedPtr buffer(MemoryRange::mmap(nullptr, xzStream.UncompressedSize(),
|
|
|
|
PROT_WRITE, MAP_SHARED, fd, 0));
|
|
|
|
if (buffer == MAP_FAILED) {
|
|
|
|
ERROR("Couldn't map %s to decompress library", file.get());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const size_t written = xzStream.Decode(buffer, buffer.GetLength());
|
2017-05-17 20:06:22 +03:00
|
|
|
DEBUG_LOG("XZStream decoded %" PRIuPTR, written);
|
2016-08-23 17:01:14 +03:00
|
|
|
if (written != buffer.GetLength()) {
|
|
|
|
ERROR("Error decoding XZ file %s", file.get());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-02-22 11:12:15 +04:00
|
|
|
} else {
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-01-20 12:48:50 +04:00
|
|
|
}
|
|
|
|
|
2016-08-17 01:54:28 +03:00
|
|
|
validator.CacheChecksum();
|
2016-08-17 22:33:18 +03:00
|
|
|
return new MappableExtractFile(fd.forget(), file.release());
|
2012-01-20 12:48:50 +04:00
|
|
|
}
|
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
/**
|
|
|
|
* _MappableBuffer is a buffer which content can be mapped at different
|
|
|
|
* locations in the virtual address space.
|
|
|
|
* On Linux, uses a (deleted) temporary file on a tmpfs for sharable content.
|
|
|
|
* On Android, uses ashmem.
|
|
|
|
*/
|
|
|
|
class _MappableBuffer : public MappedPtr {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns a _MappableBuffer instance with the given name and the given
|
|
|
|
* length.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
static _MappableBuffer* Create(const char* name, size_t length) {
|
2012-01-20 12:48:44 +04:00
|
|
|
AutoCloseFD fd;
|
|
|
|
#ifdef ANDROID
|
|
|
|
/* On Android, initialize an ashmem region with the given length */
|
|
|
|
fd = open("/" ASHMEM_NAME_DEF, O_RDWR, 0600);
|
2013-11-11 23:15:46 +04:00
|
|
|
if (fd == -1) return nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
char str[ASHMEM_NAME_LEN];
|
|
|
|
strlcpy(str, name, sizeof(str));
|
|
|
|
ioctl(fd, ASHMEM_SET_NAME, str);
|
|
|
|
if (ioctl(fd, ASHMEM_SET_SIZE, length)) return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-01-20 12:48:44 +04:00
|
|
|
/* The Gecko crash reporter is confused by adjacent memory mappings of
|
2013-09-23 23:02:28 +04:00
|
|
|
* the same file and chances are we're going to map from the same file
|
2013-01-11 15:24:31 +04:00
|
|
|
* descriptor right away. To avoid problems with the crash reporter,
|
2013-09-26 19:17:05 +04:00
|
|
|
* create an empty anonymous page before or after the ashmem mapping,
|
|
|
|
* depending on how mappings grow in the address space.
|
2013-09-23 23:02:28 +04:00
|
|
|
*/
|
2013-09-26 19:17:05 +04:00
|
|
|
# if defined(__arm__)
|
2017-05-17 20:06:22 +03:00
|
|
|
// Address increases on ARM.
|
2019-05-01 11:47:10 +03:00
|
|
|
void* buf = ::mmap(nullptr, length + PAGE_SIZE, PROT_READ | PROT_WRITE,
|
2013-11-11 23:15:46 +04:00
|
|
|
MAP_SHARED, fd, 0);
|
2013-09-26 19:17:05 +04:00
|
|
|
if (buf != MAP_FAILED) {
|
2019-05-01 11:47:10 +03:00
|
|
|
::mmap(AlignedEndPtr(reinterpret_cast<char*>(buf) + length, PAGE_SIZE),
|
2013-09-26 19:17:05 +04:00
|
|
|
PAGE_SIZE, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1,
|
|
|
|
0);
|
2017-05-17 20:06:22 +03:00
|
|
|
DEBUG_LOG("Decompression buffer of size 0x%" PRIxPTR
|
|
|
|
" in ashmem \"%s\", mapped @%p",
|
2013-09-26 19:17:05 +04:00
|
|
|
length, str, buf);
|
|
|
|
return new _MappableBuffer(fd.forget(), buf, length);
|
|
|
|
}
|
2018-09-13 19:09:25 +03:00
|
|
|
# elif defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
|
|
|
|
// Address decreases on x86, x86-64, and AArch64.
|
2013-09-26 19:17:05 +04:00
|
|
|
size_t anon_mapping_length = length + PAGE_SIZE;
|
2019-05-01 11:47:10 +03:00
|
|
|
void* buf = ::mmap(nullptr, anon_mapping_length, PROT_NONE,
|
2013-09-23 23:02:28 +04:00
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2012-01-20 12:48:44 +04:00
|
|
|
if (buf != MAP_FAILED) {
|
2019-05-01 11:47:10 +03:00
|
|
|
char* first_page = reinterpret_cast<char*>(buf);
|
|
|
|
char* map_page = first_page + PAGE_SIZE;
|
2013-09-23 23:02:28 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void* actual_buf = ::mmap(map_page, length, PROT_READ | PROT_WRITE,
|
2013-09-23 23:02:28 +04:00
|
|
|
MAP_FIXED | MAP_SHARED, fd, 0);
|
|
|
|
if (actual_buf == MAP_FAILED) {
|
|
|
|
::munmap(buf, anon_mapping_length);
|
|
|
|
DEBUG_LOG("Fixed allocation of decompression buffer at %p failed",
|
|
|
|
map_page);
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2013-09-23 23:02:28 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 20:06:22 +03:00
|
|
|
DEBUG_LOG("Decompression buffer of size 0x%" PRIxPTR
|
|
|
|
" in ashmem \"%s\", mapped @%p",
|
|
|
|
length, str, actual_buf);
|
2013-09-23 23:02:28 +04:00
|
|
|
return new _MappableBuffer(fd.forget(), actual_buf, length);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
2013-09-26 19:17:05 +04:00
|
|
|
# else
|
|
|
|
# error need to add a case for your CPU
|
|
|
|
# endif
|
2012-01-20 12:48:44 +04:00
|
|
|
#else
|
|
|
|
/* On Linux, use /dev/shm as base directory for temporary files, assuming
|
|
|
|
* it's on tmpfs */
|
|
|
|
/* TODO: check that /dev/shm is tmpfs */
|
|
|
|
char path[256];
|
|
|
|
sprintf(path, "/dev/shm/%s.XXXXXX", name);
|
|
|
|
fd = mkstemp(path);
|
2013-11-11 23:15:46 +04:00
|
|
|
if (fd == -1) return nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
unlink(path);
|
|
|
|
ftruncate(fd, length);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void* buf =
|
2013-11-11 23:15:46 +04:00
|
|
|
::mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
2012-01-20 12:48:44 +04:00
|
|
|
if (buf != MAP_FAILED) {
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("Decompression buffer of size %ld in \"%s\", mapped @%p",
|
|
|
|
length, path, buf);
|
2012-01-20 12:48:44 +04:00
|
|
|
return new _MappableBuffer(fd.forget(), buf, length);
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void* mmap(const void* addr, size_t length, int prot, int flags,
|
2012-01-20 12:48:44 +04:00
|
|
|
off_t offset) {
|
2012-02-13 18:49:45 +04:00
|
|
|
MOZ_ASSERT(fd != -1);
|
2012-01-20 12:48:44 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
/* Mapping ashmem MAP_PRIVATE is like mapping anonymous memory, even when
|
|
|
|
* there is content in the ashmem */
|
|
|
|
if (flags & MAP_PRIVATE) {
|
|
|
|
flags &= ~MAP_PRIVATE;
|
|
|
|
flags |= MAP_SHARED;
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-01 11:47:10 +03:00
|
|
|
return ::mmap(const_cast<void*>(addr), length, prot, flags, fd, offset);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
~_MappableBuffer() {
|
2013-09-26 19:17:05 +04:00
|
|
|
/* Free the additional page we allocated. See _MappableBuffer::Create */
|
|
|
|
# if defined(__arm__)
|
|
|
|
::munmap(AlignedEndPtr(*this + GetLength(), PAGE_SIZE), PAGE_SIZE);
|
2018-09-13 19:09:25 +03:00
|
|
|
# elif defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
|
2013-09-26 19:17:05 +04:00
|
|
|
::munmap(*this - PAGE_SIZE, GetLength() + PAGE_SIZE);
|
|
|
|
# else
|
|
|
|
# error need to add a case for your CPU
|
|
|
|
# endif
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2019-05-01 11:47:10 +03:00
|
|
|
_MappableBuffer(int fd, void* buf, size_t length)
|
2012-01-20 12:48:44 +04:00
|
|
|
: MappedPtr(buf, length), fd(fd) {}
|
|
|
|
|
|
|
|
/* File descriptor for the temporary file or ashmem */
|
|
|
|
AutoCloseFD fd;
|
|
|
|
};
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Mappable* MappableDeflate::Create(const char* name, Zip* zip,
|
|
|
|
Zip::Stream* stream) {
|
2012-02-13 18:49:45 +04:00
|
|
|
MOZ_ASSERT(stream->GetType() == Zip::Stream::DEFLATE);
|
2019-05-01 11:47:10 +03:00
|
|
|
_MappableBuffer* buf =
|
2012-01-20 12:48:44 +04:00
|
|
|
_MappableBuffer::Create(name, stream->GetUncompressedSize());
|
|
|
|
if (buf) return new MappableDeflate(buf, zip, stream);
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
MappableDeflate::MappableDeflate(_MappableBuffer* buf, Zip* zip,
|
|
|
|
Zip::Stream* stream)
|
2012-01-20 12:48:50 +04:00
|
|
|
: zip(zip), buffer(buf), zStream(stream->GetZStream(*buf)) {}
|
2012-01-20 12:48:44 +04:00
|
|
|
|
2012-01-20 12:48:50 +04:00
|
|
|
MappableDeflate::~MappableDeflate() {}
|
2012-01-20 12:48:44 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
MemoryRange MappableDeflate::mmap(const void* addr, size_t length, int prot,
|
2012-01-20 12:48:44 +04:00
|
|
|
int flags, off_t offset) {
|
2012-02-13 18:49:45 +04:00
|
|
|
MOZ_ASSERT(buffer);
|
|
|
|
MOZ_ASSERT(!(flags & MAP_SHARED));
|
2012-01-20 12:48:44 +04:00
|
|
|
flags |= MAP_PRIVATE;
|
|
|
|
|
|
|
|
/* The deflate stream is uncompressed up to the required offset + length, if
|
|
|
|
* it hasn't previously been uncompressed */
|
|
|
|
ssize_t missing = offset + length + zStream.avail_out - buffer->GetLength();
|
|
|
|
if (missing > 0) {
|
|
|
|
uInt avail_out = zStream.avail_out;
|
|
|
|
zStream.avail_out = missing;
|
|
|
|
if ((*buffer == zStream.next_out) &&
|
|
|
|
(inflateInit2(&zStream, -MAX_WBITS) != Z_OK)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("inflateInit failed: %s", zStream.msg);
|
2013-06-27 04:35:49 +04:00
|
|
|
return MemoryRange(MAP_FAILED, 0);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
int ret = inflate(&zStream, Z_SYNC_FLUSH);
|
|
|
|
if (ret < 0) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("inflate failed: %s", zStream.msg);
|
2013-06-27 04:35:49 +04:00
|
|
|
return MemoryRange(MAP_FAILED, 0);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
if (ret == Z_NEED_DICT) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("zstream requires a dictionary. %s", zStream.msg);
|
2013-06-27 04:35:49 +04:00
|
|
|
return MemoryRange(MAP_FAILED, 0);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
zStream.avail_out = avail_out - missing + zStream.avail_out;
|
|
|
|
if (ret == Z_STREAM_END) {
|
|
|
|
if (inflateEnd(&zStream) != Z_OK) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("inflateEnd failed: %s", zStream.msg);
|
2013-06-27 04:35:49 +04:00
|
|
|
return MemoryRange(MAP_FAILED, 0);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
if (zStream.total_out != buffer->GetLength()) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("File not fully uncompressed! %ld / %d", zStream.total_out,
|
2012-01-20 12:48:44 +04:00
|
|
|
static_cast<unsigned int>(buffer->GetLength()));
|
2013-06-27 04:35:49 +04:00
|
|
|
return MemoryRange(MAP_FAILED, 0);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if defined(ANDROID) && defined(__arm__)
|
|
|
|
if (prot & PROT_EXEC) {
|
|
|
|
/* We just extracted data that may be executed in the future.
|
|
|
|
* We thus need to ensure Instruction and Data cache coherency. */
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("cacheflush(%p, %p)", *buffer + offset,
|
|
|
|
*buffer + (offset + length));
|
2012-01-20 12:48:44 +04:00
|
|
|
cacheflush(reinterpret_cast<uintptr_t>(*buffer + offset),
|
|
|
|
reinterpret_cast<uintptr_t>(*buffer + (offset + length)), 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
return MemoryRange(buffer->mmap(addr, length, prot, flags, offset), length);
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappableDeflate::finalize() {
|
2013-01-10 11:39:07 +04:00
|
|
|
/* Free zlib internal buffers */
|
|
|
|
inflateEnd(&zStream);
|
2012-01-20 12:48:44 +04:00
|
|
|
/* Free decompression buffer */
|
2013-11-11 23:15:46 +04:00
|
|
|
buffer = nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
/* Remove reference to Zip archive */
|
2013-11-11 23:15:46 +04:00
|
|
|
zip = nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
}
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-04-12 12:23:12 +04:00
|
|
|
size_t MappableDeflate::GetLength() const { return buffer->GetLength(); }
|