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>
|
2012-01-20 12:48:44 +04:00
|
|
|
#include "Mappable.h"
|
|
|
|
#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"
|
2012-02-22 11:12:15 +04:00
|
|
|
#include "SeekableZStream.h"
|
2012-01-20 12:48:44 +04:00
|
|
|
#include "Logging.h"
|
|
|
|
|
2012-02-22 11:12:15 +04: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
|
|
|
}
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
MemoryRange
|
2012-01-20 12:48:44 +04:00
|
|
|
MappableFile::mmap(const void *addr, size_t length, int prot, 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;
|
|
|
|
|
2013-07-23 02:26:07 +04:00
|
|
|
return MemoryRange::mmap(const_cast<void *>(addr), length, prot, flags,
|
|
|
|
fd, 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;
|
|
|
|
}
|
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
Mappable *
|
|
|
|
MappableExtractFile::Create(const char *name, Zip *zip, Zip::Stream *stream)
|
2012-01-20 12:48:50 +04:00
|
|
|
{
|
|
|
|
const char *cachePath = getenv("MOZ_LINKER_CACHE");
|
|
|
|
if (!cachePath || !*cachePath) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("Warning: 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
|
|
|
}
|
2012-07-30 22:17:53 +04:00
|
|
|
mozilla::ScopedDeleteArray<char> path;
|
|
|
|
path = new char[strlen(cachePath) + strlen(name) + 2];
|
2012-01-20 12:48:50 +04:00
|
|
|
sprintf(path, "%s/%s", cachePath, name);
|
2012-02-22 11:12:15 +04:00
|
|
|
struct stat cacheStat;
|
|
|
|
if (stat(path, &cacheStat) == 0) {
|
|
|
|
struct stat zipStat;
|
|
|
|
stat(zip->GetName(), &zipStat);
|
|
|
|
if (cacheStat.st_mtime > zipStat.st_mtime) {
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("Reusing %s", static_cast<char *>(path));
|
2012-02-22 11:12:15 +04:00
|
|
|
return MappableFile::Create(path);
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("Extracting to %s", static_cast<char *>(path));
|
2012-07-30 22:17:53 +04:00
|
|
|
AutoCloseFD fd;
|
|
|
|
fd = open(path, O_TRUNC | O_RDWR | O_CREAT | O_NOATIME,
|
|
|
|
S_IRUSR | S_IWUSR);
|
2012-01-20 12:48:50 +04:00
|
|
|
if (fd == -1) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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
|
|
|
}
|
2012-07-30 22:17:53 +04:00
|
|
|
AutoUnlinkFile file;
|
|
|
|
file = path.forget();
|
2012-02-22 11:12:15 +04:00
|
|
|
if (stream->GetType() == Zip::Stream::DEFLATE) {
|
|
|
|
if (ftruncate(fd, stream->GetUncompressedSize()) == -1) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
z_stream zStream = stream->GetZStream(buffer);
|
|
|
|
|
|
|
|
/* Decompress */
|
|
|
|
if (inflateInit2(&zStream, -MAX_WBITS) != Z_OK) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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()) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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
|
|
|
}
|
|
|
|
} else if (stream->GetType() == Zip::Stream::STORE) {
|
|
|
|
SeekableZStream zStream;
|
2013-03-06 10:29:05 +04:00
|
|
|
if (!zStream.Init(stream->GetBuffer(), stream->GetSize())) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("Couldn't initialize SeekableZStream for %s", name);
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
if (ftruncate(fd, zStream.GetUncompressedSize()) == -1) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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
|
|
|
}
|
2013-11-11 23:15:46 +04:00
|
|
|
MappedPtr buffer(MemoryRange::mmap(nullptr, zStream.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) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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
|
|
|
}
|
|
|
|
|
|
|
|
if (!zStream.Decompress(buffer, 0, zStream.GetUncompressedSize())) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("%s: failed to decompress", name);
|
2013-11-11 23:15:46 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return new MappableExtractFile(fd.forget(), file.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
MappableExtractFile::~MappableExtractFile()
|
|
|
|
{
|
|
|
|
/* When destroying from a forked process, we don't want the file to be
|
|
|
|
* removed, as the main process is still using the file. Although it
|
|
|
|
* doesn't really matter, it helps e.g. valgrind that the file is there.
|
|
|
|
* The string still needs to be delete[]d, though */
|
|
|
|
if (pid != getpid())
|
|
|
|
delete [] path.forget();
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static _MappableBuffer *Create(const char *name, size_t length)
|
|
|
|
{
|
|
|
|
AutoCloseFD fd;
|
|
|
|
#ifdef ANDROID
|
|
|
|
/* On Android, initialize an ashmem region with the given length */
|
|
|
|
fd = open("/" ASHMEM_NAME_DEF, O_RDWR, 0600);
|
|
|
|
if (fd == -1)
|
2013-11-11 23:15:46 +04:00
|
|
|
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))
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
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__)
|
2013-11-11 23:15:46 +04:00
|
|
|
void *buf = ::mmap(nullptr, length + PAGE_SIZE, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
2013-09-26 19:17:05 +04:00
|
|
|
if (buf != MAP_FAILED) {
|
|
|
|
::mmap(AlignedEndPtr(reinterpret_cast<char *>(buf) + length, PAGE_SIZE),
|
|
|
|
PAGE_SIZE, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
DEBUG_LOG("Decompression buffer of size 0x%x in ashmem \"%s\", mapped @%p",
|
|
|
|
length, str, buf);
|
|
|
|
return new _MappableBuffer(fd.forget(), buf, length);
|
|
|
|
}
|
|
|
|
#elif defined(__i386__)
|
|
|
|
size_t anon_mapping_length = length + PAGE_SIZE;
|
2013-11-11 23:15:46 +04: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) {
|
2013-09-23 23:02:28 +04:00
|
|
|
char *first_page = reinterpret_cast<char *>(buf);
|
|
|
|
char *map_page = first_page + PAGE_SIZE;
|
|
|
|
|
2013-09-26 19:17:05 +04: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
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_LOG("Decompression buffer of size 0x%x in ashmem \"%s\", mapped @%p",
|
|
|
|
length, str, actual_buf);
|
|
|
|
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);
|
|
|
|
if (fd == -1)
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-01-20 12:48:44 +04:00
|
|
|
unlink(path);
|
|
|
|
ftruncate(fd, length);
|
|
|
|
|
2013-11-11 23:15:46 +04:00
|
|
|
void *buf = ::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
|
|
|
}
|
|
|
|
|
|
|
|
void *mmap(const void *addr, size_t length, int prot, int flags, 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
|
|
|
|
return ::mmap(const_cast<void *>(addr), length, prot, flags, fd, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
#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);
|
|
|
|
#elif defined(__i386__)
|
|
|
|
::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:
|
|
|
|
_MappableBuffer(int fd, void *buf, size_t length)
|
|
|
|
: MappedPtr(buf, length), fd(fd) { }
|
|
|
|
|
|
|
|
/* File descriptor for the temporary file or ashmem */
|
|
|
|
AutoCloseFD fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
Mappable *
|
2012-01-20 12:48:44 +04:00
|
|
|
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);
|
2012-01-20 12:48:44 +04:00
|
|
|
_MappableBuffer *buf = _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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
MemoryRange
|
2012-01-20 12:48:44 +04:00
|
|
|
MappableDeflate::mmap(const void *addr, size_t length, int prot, 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)) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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()) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("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();
|
|
|
|
}
|
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
Mappable *
|
2012-02-22 11:12:15 +04:00
|
|
|
MappableSeekableZStream::Create(const char *name, Zip *zip,
|
|
|
|
Zip::Stream *stream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(stream->GetType() == Zip::Stream::STORE);
|
2012-07-30 22:17:53 +04:00
|
|
|
mozilla::ScopedDeletePtr<MappableSeekableZStream> mappable;
|
|
|
|
mappable = new MappableSeekableZStream(zip);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-07-26 07:57:53 +04:00
|
|
|
pthread_mutexattr_t recursiveAttr;
|
|
|
|
pthread_mutexattr_init(&recursiveAttr);
|
|
|
|
pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
|
|
|
|
if (pthread_mutex_init(&mappable->mutex, &recursiveAttr))
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-03-06 10:29:05 +04:00
|
|
|
if (!mappable->zStream.Init(stream->GetBuffer(), stream->GetSize()))
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
mappable->buffer = _MappableBuffer::Create(name,
|
|
|
|
mappable->zStream.GetUncompressedSize());
|
|
|
|
if (!mappable->buffer)
|
2013-11-11 23:15:46 +04:00
|
|
|
return nullptr;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
mappable->chunkAvail = new unsigned char[mappable->zStream.GetChunksNum()];
|
|
|
|
memset(mappable->chunkAvail, 0, mappable->zStream.GetChunksNum());
|
|
|
|
|
|
|
|
return mappable.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
MappableSeekableZStream::MappableSeekableZStream(Zip *zip)
|
2012-02-22 11:12:15 +04:00
|
|
|
: zip(zip), chunkAvailNum(0) { }
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
MappableSeekableZStream::~MappableSeekableZStream()
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&mutex);
|
|
|
|
}
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
MemoryRange
|
2012-02-22 11:12:15 +04:00
|
|
|
MappableSeekableZStream::mmap(const void *addr, size_t length, int prot,
|
|
|
|
int flags, off_t offset)
|
|
|
|
{
|
|
|
|
/* Map with PROT_NONE so that accessing the mapping would segfault, and
|
|
|
|
* bring us to ensure() */
|
|
|
|
void *res = buffer->mmap(addr, length, PROT_NONE, flags, offset);
|
|
|
|
if (res == MAP_FAILED)
|
2013-06-27 04:35:49 +04:00
|
|
|
return MemoryRange(MAP_FAILED, 0);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/* Store the mapping, ordered by offset and length */
|
|
|
|
std::vector<LazyMap>::reverse_iterator it;
|
|
|
|
for (it = lazyMaps.rbegin(); it < lazyMaps.rend(); ++it) {
|
|
|
|
if ((it->offset < offset) ||
|
|
|
|
((it->offset == offset) && (it->length < length)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LazyMap map = { res, length, prot, offset };
|
|
|
|
lazyMaps.insert(it.base(), map);
|
2013-06-27 04:35:49 +04:00
|
|
|
return MemoryRange(res, length);
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MappableSeekableZStream::munmap(void *addr, size_t length)
|
|
|
|
{
|
|
|
|
std::vector<LazyMap>::iterator it;
|
|
|
|
for (it = lazyMaps.begin(); it < lazyMaps.end(); ++it)
|
|
|
|
if ((it->addr = addr) && (it->length == length)) {
|
|
|
|
lazyMaps.erase(it);
|
|
|
|
::munmap(addr, length);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("munmap called with unknown mapping");
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MappableSeekableZStream::finalize() { }
|
|
|
|
|
|
|
|
class AutoLock {
|
|
|
|
public:
|
|
|
|
AutoLock(pthread_mutex_t *mutex): mutex(mutex)
|
|
|
|
{
|
|
|
|
if (pthread_mutex_lock(mutex))
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("pthread_mutex_lock failed");
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
~AutoLock()
|
|
|
|
{
|
|
|
|
if (pthread_mutex_unlock(mutex))
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("pthread_mutex_unlock failed");
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
pthread_mutex_t *mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
MappableSeekableZStream::ensure(const void *addr)
|
|
|
|
{
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("ensure @%p", addr);
|
2013-06-27 04:35:49 +04:00
|
|
|
const void *addrPage = PageAlignedPtr(addr);
|
2012-02-22 11:12:15 +04:00
|
|
|
/* Find the mapping corresponding to the given page */
|
|
|
|
std::vector<LazyMap>::iterator map;
|
|
|
|
for (map = lazyMaps.begin(); map < lazyMaps.end(); ++map) {
|
|
|
|
if (map->Contains(addrPage))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (map == lazyMaps.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Find corresponding chunk */
|
|
|
|
off_t mapOffset = map->offsetOf(addrPage);
|
2013-01-15 13:12:56 +04:00
|
|
|
off_t chunk = mapOffset / zStream.GetChunkSize();
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/* In the typical case, we just need to decompress the chunk entirely. But
|
|
|
|
* when the current mapping ends in the middle of the chunk, we want to
|
2013-07-23 02:26:07 +04:00
|
|
|
* stop at the end of the corresponding page.
|
|
|
|
* However, if another mapping needs the last part of the chunk, we still
|
|
|
|
* need to continue. As mappings are ordered by offset and length, we don't
|
|
|
|
* need to scan the entire list of mappings.
|
2012-02-22 11:12:15 +04:00
|
|
|
* It is safe to run through lazyMaps here because the linker is never
|
|
|
|
* going to call mmap (which adds lazyMaps) while this function is
|
|
|
|
* called. */
|
|
|
|
size_t length = zStream.GetChunkSize(chunk);
|
2013-01-15 13:12:56 +04:00
|
|
|
off_t chunkStart = chunk * zStream.GetChunkSize();
|
|
|
|
off_t chunkEnd = chunkStart + length;
|
2012-02-22 11:12:15 +04:00
|
|
|
std::vector<LazyMap>::iterator it;
|
|
|
|
for (it = map; it < lazyMaps.end(); ++it) {
|
|
|
|
if (chunkEnd <= it->endOffset())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((it == lazyMaps.end()) || (chunkEnd > it->endOffset())) {
|
|
|
|
/* The mapping "it" points at now is past the interesting one */
|
|
|
|
--it;
|
|
|
|
length = it->endOffset() - chunkStart;
|
|
|
|
}
|
|
|
|
|
2013-07-23 02:26:07 +04:00
|
|
|
length = PageAlignedSize(length);
|
|
|
|
|
2013-07-26 07:57:53 +04:00
|
|
|
/* The following lock can be re-acquired by the thread holding it.
|
|
|
|
* If this happens, it means the following code is interrupted somehow by
|
|
|
|
* some signal, and ends up retriggering a chunk decompression for the
|
|
|
|
* same MappableSeekableZStream.
|
|
|
|
* If the chunk to decompress is different the second time, then everything
|
|
|
|
* is safe as the only common data touched below is chunkAvailNum, and it is
|
|
|
|
* atomically updated (leaving out any chance of an interruption while it is
|
|
|
|
* updated affecting the result). If the chunk to decompress is the same, the
|
|
|
|
* worst thing that can happen is chunkAvailNum being incremented one too
|
|
|
|
* many times, which doesn't affect functionality. The chances of it
|
|
|
|
* happening being pretty slim, and the effect being harmless, we can just
|
|
|
|
* ignore the issue. Other than that, we'd just be wasting time decompressing
|
|
|
|
* the same chunk twice. */
|
2012-02-22 11:12:15 +04:00
|
|
|
AutoLock lock(&mutex);
|
|
|
|
|
|
|
|
/* The very first page is mapped and accessed separately of the rest, and
|
|
|
|
* as such, only the first page of the first chunk is decompressed this way.
|
|
|
|
* When we fault in the remaining pages of that chunk, we want to decompress
|
|
|
|
* the complete chunk again. Short of doing that, we would end up with
|
2013-06-27 04:35:49 +04:00
|
|
|
* no data between PageSize() and chunkSize, which would effectively corrupt
|
2012-02-22 11:12:15 +04:00
|
|
|
* symbol resolution in the underlying library. */
|
2013-06-27 04:35:49 +04:00
|
|
|
if (chunkAvail[chunk] < PageNumber(length)) {
|
2012-02-22 11:12:15 +04:00
|
|
|
if (!zStream.DecompressChunk(*buffer + chunkStart, chunk, length))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#if defined(ANDROID) && defined(__arm__)
|
|
|
|
if (map->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 + chunkStart, *buffer + (chunkStart + length));
|
2012-02-22 11:12:15 +04:00
|
|
|
cacheflush(reinterpret_cast<uintptr_t>(*buffer + chunkStart),
|
|
|
|
reinterpret_cast<uintptr_t>(*buffer + (chunkStart + length)), 0);
|
|
|
|
}
|
|
|
|
#endif
|
2012-02-22 11:12:15 +04:00
|
|
|
/* Only count if we haven't already decompressed parts of the chunk */
|
|
|
|
if (chunkAvail[chunk] == 0)
|
|
|
|
chunkAvailNum++;
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
chunkAvail[chunk] = PageNumber(length);
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Flip the chunk mapping protection to the recorded flags. We could
|
|
|
|
* also flip the protection for other mappings of the same chunk,
|
|
|
|
* but it's easier to skip that and let further segfaults call
|
|
|
|
* ensure again. */
|
|
|
|
const void *chunkAddr = reinterpret_cast<const void *>
|
|
|
|
(reinterpret_cast<uintptr_t>(addrPage)
|
|
|
|
- mapOffset % zStream.GetChunkSize());
|
|
|
|
const void *chunkEndAddr = reinterpret_cast<const void *>
|
|
|
|
(reinterpret_cast<uintptr_t>(chunkAddr) + length);
|
|
|
|
|
|
|
|
const void *start = std::max(map->addr, chunkAddr);
|
|
|
|
const void *end = std::min(map->end(), chunkEndAddr);
|
|
|
|
length = reinterpret_cast<uintptr_t>(end)
|
|
|
|
- reinterpret_cast<uintptr_t>(start);
|
|
|
|
|
2014-01-15 11:21:45 +04:00
|
|
|
if (mprotect(const_cast<void *>(start), length, map->prot) == 0) {
|
|
|
|
DEBUG_LOG("mprotect @%p, 0x%" PRIxSize ", 0x%x", start, length, map->prot);
|
2012-02-22 11:12:15 +04:00
|
|
|
return true;
|
2014-01-15 11:21:45 +04:00
|
|
|
}
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2014-01-15 11:21:45 +04:00
|
|
|
LOG("mprotect @%p, 0x%" PRIxSize ", 0x%x failed with errno %d",
|
|
|
|
start, length, map->prot, errno);
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("mprotect failed");
|
2012-02-22 11:12:15 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
MappableSeekableZStream::stats(const char *when, const char *name) const
|
|
|
|
{
|
|
|
|
size_t nEntries = zStream.GetChunksNum();
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("%s: %s; %" PRIdSize "/%" PRIdSize " chunks decompressed",
|
2013-07-26 07:57:53 +04:00
|
|
|
name, when, static_cast<size_t>(chunkAvailNum), nEntries);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
size_t len = 64;
|
2012-07-30 22:17:53 +04:00
|
|
|
mozilla::ScopedDeleteArray<char> map;
|
|
|
|
map = new char[len + 3];
|
2012-02-22 11:12:15 +04:00
|
|
|
map[0] = '[';
|
|
|
|
|
|
|
|
for (size_t i = 0, j = 1; i < nEntries; i++, j++) {
|
|
|
|
map[j] = chunkAvail[i] ? '*' : '_';
|
|
|
|
if ((j == len) || (i == nEntries - 1)) {
|
|
|
|
map[j + 1] = ']';
|
|
|
|
map[j + 2] = '\0';
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("%s", static_cast<char *>(map));
|
2012-02-22 11:12:15 +04:00
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 12:23:12 +04:00
|
|
|
|
|
|
|
size_t
|
|
|
|
MappableSeekableZStream::GetLength() const
|
|
|
|
{
|
|
|
|
return buffer->GetLength();
|
|
|
|
}
|