Issue #1053 - Remove android support from memory

This commit is contained in:
Matt A. Tobin 2020-02-23 00:26:22 -05:00 committed by Roy Tam
commit f1182eaec9
15 changed files with 19 additions and 374 deletions

View file

@ -36,6 +36,6 @@ if CONFIG['MOZ_GLUE_IN_PROGRAM']:
DIST_INSTALL = True
# Keep jemalloc separated when mozglue is statically linked
if CONFIG['MOZ_MEMORY'] and CONFIG['OS_TARGET'] in ('WINNT', 'Darwin', 'Android'):
if CONFIG['MOZ_MEMORY'] and CONFIG['OS_TARGET'] in ('WINNT', 'Darwin'):
FINAL_LIBRARY = 'mozglue'

View file

@ -88,60 +88,6 @@ strdup_impl(const char *src)
}
#endif /* XP_DARWIN */
#ifdef ANDROID
#include <stdarg.h>
#include <stdio.h>
MOZ_MEMORY_API int
vasprintf_impl(char **str, const char *fmt, va_list ap)
{
char* ptr, *_ptr;
int ret;
if (str == NULL || fmt == NULL) {
return -1;
}
ptr = (char*)malloc_impl(128);
if (ptr == NULL) {
*str = NULL;
return -1;
}
ret = vsnprintf(ptr, 128, fmt, ap);
if (ret < 0) {
free_impl(ptr);
*str = NULL;
return -1;
}
_ptr = realloc_impl(ptr, ret + 1);
if (_ptr == NULL) {
free_impl(ptr);
*str = NULL;
return -1;
}
*str = _ptr;
return ret;
}
MOZ_MEMORY_API int
asprintf_impl(char **str, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vasprintf_impl(str, fmt, ap);
va_end(ap);
return ret;
}
#endif
#ifdef XP_WIN
/*
* There's a fun allocator mismatch in (at least) the VS 2010 CRT

View file

@ -58,13 +58,8 @@
* zone allocator anyways. Jemalloc-specific functions are also left
* unprefixed.
*
* - On Android and Gonk, all functions are left unprefixed. Additionally,
* C++ allocation functions (operator new/delete) are also exported and
* unprefixed.
*
* - On other systems (mostly Linux), all functions are left unprefixed.
*
* Only Android and Gonk add C++ allocation functions.
*
* Proper exporting of the various functions is done with the MOZ_MEMORY_API
* and MOZ_JEMALLOC_API macros. MOZ_MEMORY_API is meant to be used for malloc
@ -72,14 +67,6 @@
* dedicated to jemalloc specific functions.
*
*
* All these functions are meant to be called with no prefix from Gecko code.
* In most cases, this is because that's how they are available at runtime.
* However, on Android, this relies on faulty.lib (the custom dynamic linker)
* resolving mozglue symbols before libc symbols, which is guaranteed by the
* way faulty.lib works (it respects the DT_NEEDED order, and libc always
* appears after mozglue ; which we double check when building anyways)
*
*
* Within libmozglue (when MOZ_MEMORY_IMPL is defined), all the functions
* should be suffixed with "_impl" both for declarations and use.
* That is, the implementation declaration for e.g. strdup would look like:
@ -133,9 +120,6 @@
# endif
# else
# define MOZ_MEMORY_API MFBT_API
# if defined(MOZ_WIDGET_ANDROID)
# define MOZ_WRAP_NEW_DELETE
# endif
# endif
# endif
# ifdef XP_WIN
@ -184,15 +168,6 @@
# define wcsdup_impl mozmem_dup_impl(wcsdup)
#endif
/* String functions */
#ifdef ANDROID
/* Bug 801571 and Bug 879668, libstagefright uses vasprintf, causing malloc()/
* free() to be mismatched between bionic and mozglue implementation.
*/
#define vasprintf_impl mozmem_dup_impl(vasprintf)
#define asprintf_impl mozmem_dup_impl(asprintf)
#endif
/* Jemalloc specific function */
#define jemalloc_stats_impl mozmem_jemalloc_impl(jemalloc_stats)
#define jemalloc_purge_freed_pages_impl mozmem_jemalloc_impl(jemalloc_purge_freed_pages)

View file

@ -24,14 +24,11 @@
* LD_PRELOAD or DYLD_INSERT_LIBRARIES on Linux/OSX. On this platform,
* the replacement functions are defined as variable pointers to the
* function resolved with GetProcAddress() instead of weak definitions
* of functions. On Android, the same needs to happen as well, because
* the Android linker doesn't handle weak linking with non LD_PRELOADed
* libraries, but LD_PRELOADing is not very convenient on Android, with
* the zygote.
* of functions.
*/
#ifdef XP_DARWIN
# define MOZ_REPLACE_WEAK __attribute__((weak_import))
#elif defined(XP_WIN) || defined(MOZ_WIDGET_ANDROID)
#elif defined(XP_WIN)
# define MOZ_NO_REPLACE_FUNC_DECL
#elif defined(__GNUC__)
# define MOZ_REPLACE_WEAK __attribute__((weak))
@ -66,24 +63,6 @@ replace_malloc_init_funcs()
#define MALLOC_DECL(name, ...) \
replace_ ## name = (replace_ ## name ## _impl_t *) GetProcAddress(handle, "replace_" # name);
# define MALLOC_FUNCS MALLOC_FUNCS_ALL
#include "malloc_decls.h"
}
}
}
# elif defined(MOZ_WIDGET_ANDROID)
# include <dlfcn.h>
# include <stdlib.h>
static void
replace_malloc_init_funcs()
{
const char *replace_malloc_lib = getenv("MOZ_REPLACE_MALLOC_LIB");
if (replace_malloc_lib && *replace_malloc_lib) {
void *handle = dlopen(replace_malloc_lib, RTLD_LAZY);
if (handle) {
#define MALLOC_DECL(name, ...) \
replace_ ## name = (replace_ ## name ## _impl_t *) dlsym(handle, "replace_" # name);
# define MALLOC_FUNCS MALLOC_FUNCS_ALL
#include "malloc_decls.h"
}

View file

@ -13,7 +13,7 @@
* environment variables to the library path:
* - LD_PRELOAD on Linux,
* - DYLD_INSERT_LIBRARIES on OSX,
* - MOZ_REPLACE_MALLOC_LIB on Windows and Android.
* - MOZ_REPLACE_MALLOC_LIB on Windows.
*
* An initialization function is called before any malloc replacement
* function, and has the following declaration:

View file

@ -160,16 +160,7 @@ MFBT_API void* moz_xvalloc(size_t size)
# define MOZALLOC_EXPORT_NEW
#endif
#if defined(ANDROID)
/*
* It's important to always specify 'throw()' in GCC because it's used to tell
* GCC that 'new' may return null. That makes GCC null-check the result before
* potentially initializing the memory to zero.
* Also, the Android minimalistic headers don't include std::bad_alloc.
*/
#define MOZALLOC_THROW_IF_HAS_EXCEPTIONS throw()
#define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS
#elif defined(_MSC_VER)
#if defined(_MSC_VER)
/*
* Suppress build warning spam (bug 578546).
*/

View file

@ -7,9 +7,6 @@
#include "mozilla/mozalloc_abort.h"
#ifdef ANDROID
# include <android/log.h>
#endif
#ifdef MOZ_WIDGET_ANDROID
# include "APKOpen.h"
# include "dlfcn.h"
@ -21,40 +18,11 @@
void
mozalloc_abort(const char* const msg)
{
#ifndef ANDROID
fputs(msg, stderr);
fputs("\n", stderr);
#else
__android_log_print(ANDROID_LOG_ERROR, "Gecko", "mozalloc_abort: %s", msg);
#endif
#ifdef MOZ_WIDGET_ANDROID
abortThroughJava(msg);
#endif
MOZ_CRASH();
}
#ifdef MOZ_WIDGET_ANDROID
template <size_t N>
void fillAbortMessage(char (&msg)[N], uintptr_t retAddress) {
/*
* On Android, we often don't have reliable backtrace when crashing inside
* abort(). Therefore, we try to find out who is calling abort() and add
* that to the message.
*/
Dl_info info = {};
dladdr(reinterpret_cast<void*>(retAddress), &info);
const char* const module = info.dli_fname ? info.dli_fname : "";
const char* const base_module = strrchr(module, '/');
const void* const module_offset =
reinterpret_cast<void*>(retAddress - uintptr_t(info.dli_fbase));
const char* const sym = info.dli_sname ? info.dli_sname : "";
snprintf(msg, sizeof(msg), "abort() called from %s:%p (%s)",
base_module ? base_module + 1 : module, module_offset, sym);
}
#endif
#if defined(XP_UNIX) && !defined(MOZ_ASAN)
// Define abort() here, so that it is used instead of the system abort(). This
// lets us control the behavior when aborting, in order to get better results
@ -70,12 +38,7 @@ void fillAbortMessage(char (&msg)[N], uintptr_t retAddress) {
// result, ASan will just exit(1) instead of aborting.
extern "C" void abort(void)
{
#ifdef MOZ_WIDGET_ANDROID
char msg[64] = {};
fillAbortMessage(msg, uintptr_t(__builtin_return_address(0)));
#else
const char* const msg = "Redirecting call to abort() to mozalloc_abort\n";
#endif
mozalloc_abort(msg);

View file

@ -100,11 +100,6 @@
*******************************************************************************
*/
#ifdef MOZ_MEMORY_ANDROID
#define NO_TLS
#define _pthread_self() pthread_self()
#endif
/*
* On Linux, we use madvise(MADV_DONTNEED) to release memory back to the
* operating system. If we release 1MB of live pages with MADV_DONTNEED, our
@ -174,7 +169,7 @@
# define MALLOC_SYSV
#endif
#if defined(MOZ_MEMORY_LINUX) && !defined(MOZ_MEMORY_ANDROID)
#ifdef MOZ_MEMORY_LINUX
#define _GNU_SOURCE /* For mremap(2). */
#endif
@ -501,7 +496,7 @@ static bool malloc_initialized = false;
/* No init lock for Windows. */
#elif defined(MOZ_MEMORY_DARWIN)
static malloc_mutex_t init_lock = {OS_SPINLOCK_INIT};
#elif defined(MOZ_MEMORY_LINUX) && !defined(MOZ_MEMORY_ANDROID)
#elif defined(MOZ_MEMORY_LINUX)
static malloc_mutex_t init_lock = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
#else
static malloc_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
@ -1377,12 +1372,6 @@ void (*_malloc_message)(const char *p1, const char *p2, const char *p3,
# define assert(e)
#endif
#ifdef MOZ_MEMORY_ANDROID
// Android's pthread.h does not declare pthread_atfork() until SDK 21.
extern MOZ_EXPORT
int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));
#endif
#if defined(MOZ_JEMALLOC_HARD_ASSERTS)
# define RELEASE_ASSERT(assertion) do { \
if (!(assertion)) { \
@ -1408,7 +1397,7 @@ malloc_mutex_init(malloc_mutex_t *mutex)
return (true);
#elif defined(MOZ_MEMORY_DARWIN)
mutex->lock = OS_SPINLOCK_INIT;
#elif defined(MOZ_MEMORY_LINUX) && !defined(MOZ_MEMORY_ANDROID)
#elif defined(MOZ_MEMORY_LINUX)
pthread_mutexattr_t attr;
if (pthread_mutexattr_init(&attr) != 0)
return (true);
@ -1462,7 +1451,7 @@ malloc_spin_init(malloc_spinlock_t *lock)
return (true);
#elif defined(MOZ_MEMORY_DARWIN)
lock->lock = OS_SPINLOCK_INIT;
#elif defined(MOZ_MEMORY_LINUX) && !defined(MOZ_MEMORY_ANDROID)
#elif defined(MOZ_MEMORY_LINUX)
pthread_mutexattr_t attr;
if (pthread_mutexattr_init(&attr) != 0)
return (true);

View file

@ -54,16 +54,6 @@ GetTid()
#endif
}
#ifdef ANDROID
/* See mozglue/android/APKOpen.cpp */
extern "C" MOZ_EXPORT __attribute__((weak))
void* __dso_handle;
/* Android doesn't have pthread_atfork defined in pthread.h */
extern "C" MOZ_EXPORT
int pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
#endif
class LogAllocBridge : public ReplaceMallocBridge
{
virtual void InitDebugFd(mozilla::DebugFdRegistry& aRegistry) override {

View file

@ -20,22 +20,10 @@ DEFINES['DEBUG'] = False
# Use locking code from the chromium stack.
if CONFIG['OS_TARGET'] == 'WINNT':
SOURCES += [
'../../../ipc/chromium/src/base/lock_impl_win.cc',
]
SOURCES += ['../../../ipc/chromium/src/base/lock_impl_win.cc']
else:
SOURCES += [
'../../../ipc/chromium/src/base/lock_impl_posix.cc',
]
SOURCES += ['../../../ipc/chromium/src/base/lock_impl_posix.cc']
include('/ipc/chromium/chromium-config.mozbuild')
# Android doesn't have pthread_atfork, but we have our own in mozglue.
if CONFIG['OS_TARGET'] == 'Android':
USE_LIBS += [
'mozglue',
]
DIRS += [
'replay',
]
DIRS += ['replay']

View file

@ -290,23 +290,6 @@ MOZ_BEGIN_EXTERN_C
void malloc_init_hard(void);
#endif
#ifdef ANDROID
/* mozjemalloc uses MozTagAnonymousMemory, which doesn't have an inline
* implementation on Android */
void
MozTagAnonymousMemory(const void* aPtr, size_t aLength, const char* aTag) {}
/* mozjemalloc and jemalloc use pthread_atfork, which Android doesn't have.
* While gecko has one in libmozglue, the replay program can't use that.
* Since we're not going to fork anyways, make it a dummy function. */
int
pthread_atfork(void (*aPrepare)(void), void (*aParent)(void),
void (*aChild)(void))
{
return 0;
}
#endif
MOZ_END_EXTERN_C
size_t parseNumber(Buffer aBuf)

View file

@ -76,9 +76,7 @@ private:
void* mBuf;
size_t mSize;
int mLockCount;
#if defined(ANDROID)
int mFd;
#elif defined(XP_DARWIN)
#ifdef XP_DARWIN
bool mHeap;
#elif defined(XP_WIN)
bool mHeap;

View file

@ -1,139 +0,0 @@
/* 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 "VolatileBuffer.h"
#include "mozilla/Assertions.h"
#include "mozilla/mozalloc.h"
#include <fcntl.h>
#include <linux/ashmem.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef MOZ_MEMORY
extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size);
#endif
#define MIN_VOLATILE_ALLOC_SIZE 8192
namespace mozilla {
VolatileBuffer::VolatileBuffer()
: mMutex("VolatileBuffer")
, mBuf(nullptr)
, mSize(0)
, mLockCount(0)
, mFd(-1)
{
}
bool
VolatileBuffer::Init(size_t aSize, size_t aAlignment)
{
MOZ_ASSERT(!mSize && !mBuf, "Init called twice");
MOZ_ASSERT(!(aAlignment % sizeof(void *)),
"Alignment must be multiple of pointer size");
mSize = aSize;
if (aSize < MIN_VOLATILE_ALLOC_SIZE) {
goto heap_alloc;
}
mFd = open("/" ASHMEM_NAME_DEF, O_RDWR);
if (mFd < 0) {
goto heap_alloc;
}
if (ioctl(mFd, ASHMEM_SET_SIZE, mSize) < 0) {
goto heap_alloc;
}
mBuf = mmap(nullptr, mSize, PROT_READ | PROT_WRITE, MAP_SHARED, mFd, 0);
if (mBuf != MAP_FAILED) {
return true;
}
heap_alloc:
mBuf = nullptr;
if (mFd >= 0) {
close(mFd);
mFd = -1;
}
#ifdef MOZ_MEMORY
posix_memalign(&mBuf, aAlignment, aSize);
#else
mBuf = memalign(aAlignment, aSize);
#endif
return !!mBuf;
}
VolatileBuffer::~VolatileBuffer()
{
MOZ_ASSERT(mLockCount == 0, "Being destroyed with non-zero lock count?");
if (OnHeap()) {
free(mBuf);
} else {
munmap(mBuf, mSize);
close(mFd);
}
}
bool
VolatileBuffer::Lock(void** aBuf)
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer");
*aBuf = mBuf;
if (++mLockCount > 1 || OnHeap()) {
return true;
}
// Zero offset and zero length means we want to pin/unpin the entire thing.
struct ashmem_pin pin = { 0, 0 };
return ioctl(mFd, ASHMEM_PIN, &pin) == ASHMEM_NOT_PURGED;
}
void
VolatileBuffer::Unlock()
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mLockCount > 0, "VolatileBuffer unlocked too many times!");
if (--mLockCount || OnHeap()) {
return;
}
struct ashmem_pin pin = { 0, 0 };
ioctl(mFd, ASHMEM_UNPIN, &pin);
}
bool
VolatileBuffer::OnHeap() const
{
return mFd < 0;
}
size_t
VolatileBuffer::HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
return OnHeap() ? aMallocSizeOf(mBuf) : 0;
}
size_t
VolatileBuffer::NonHeapSizeOfExcludingThis() const
{
if (OnHeap()) {
return 0;
}
return (mSize + (PAGE_SIZE - 1)) & PAGE_MASK;
}
} // namespace mozilla

View file

@ -5,26 +5,14 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
NO_VISIBILITY_FLAGS = True
EXPORTS.mozilla += [
'VolatileBuffer.h',
]
EXPORTS.mozilla += ['VolatileBuffer.h']
if CONFIG['OS_TARGET'] == 'Android':
UNIFIED_SOURCES += [
'VolatileBufferAshmem.cpp',
]
elif CONFIG['OS_TARGET'] == 'Darwin':
UNIFIED_SOURCES += [
'VolatileBufferOSX.cpp',
]
if CONFIG['OS_TARGET'] == 'Darwin':
UNIFIED_SOURCES += ['VolatileBufferOSX.cpp']
elif CONFIG['OS_TARGET'] == 'WINNT':
UNIFIED_SOURCES += [
'VolatileBufferWindows.cpp',
]
UNIFIED_SOURCES += ['VolatileBufferWindows.cpp']
else:
UNIFIED_SOURCES += [
'VolatileBufferFallback.cpp',
]
UNIFIED_SOURCES += ['VolatileBufferFallback.cpp']
FINAL_LIBRARY = 'xul'

View file

@ -6,13 +6,7 @@
#include "mozilla/VolatileBuffer.h"
#include <string.h>
#if defined(ANDROID)
#include <fcntl.h>
#include <linux/ashmem.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#elif defined(XP_DARWIN)
#ifdef XP_DARWIN
#include <mach/mach.h>
#endif