2017-11-01 10:56:27 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2017-01-18 05:39:29 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2017-11-01 10:56:27 +03:00
|
|
|
* 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/. */
|
2017-01-18 05:39:29 +03:00
|
|
|
|
|
|
|
#include "mozmemory_wrap.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2017-01-18 07:50:35 +03:00
|
|
|
#include <mach/mach_types.h>
|
2017-01-18 05:39:29 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Malloc implementation functions are MOZ_MEMORY_API, and jemalloc
|
|
|
|
// specific functions MOZ_JEMALLOC_API; see mozmemory_wrap.h
|
|
|
|
|
2017-01-18 05:39:29 +03:00
|
|
|
#define MALLOC_DECL(name, return_type, ...) \
|
|
|
|
MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
|
|
|
|
#define MALLOC_FUNCS MALLOC_FUNCS_MALLOC
|
|
|
|
#include "malloc_decls.h"
|
|
|
|
|
|
|
|
#define MALLOC_DECL(name, return_type, ...) \
|
|
|
|
MOZ_JEMALLOC_API return_type name##_impl(__VA_ARGS__);
|
|
|
|
#define MALLOC_FUNCS MALLOC_FUNCS_JEMALLOC
|
|
|
|
#include "malloc_decls.h"
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Definitions of the following structs in malloc/malloc.h might be too old
|
|
|
|
// for the built binary to run on newer versions of OSX. So use the newest
|
|
|
|
// possible version of those structs.
|
|
|
|
|
2017-01-18 07:50:35 +03:00
|
|
|
typedef struct _malloc_zone_t {
|
|
|
|
void* reserved1;
|
|
|
|
void* reserved2;
|
|
|
|
size_t (*size)(struct _malloc_zone_t*, const void*);
|
|
|
|
void* (*malloc)(struct _malloc_zone_t*, size_t);
|
|
|
|
void* (*calloc)(struct _malloc_zone_t*, size_t, size_t);
|
|
|
|
void* (*valloc)(struct _malloc_zone_t*, size_t);
|
|
|
|
void (*free)(struct _malloc_zone_t*, void*);
|
|
|
|
void* (*realloc)(struct _malloc_zone_t*, void*, size_t);
|
|
|
|
void (*destroy)(struct _malloc_zone_t*);
|
|
|
|
const char* zone_name;
|
|
|
|
unsigned (*batch_malloc)(struct _malloc_zone_t*, size_t, void**, unsigned);
|
|
|
|
void (*batch_free)(struct _malloc_zone_t*, void**, unsigned);
|
|
|
|
struct malloc_introspection_t* introspect;
|
|
|
|
unsigned version;
|
|
|
|
void* (*memalign)(struct _malloc_zone_t*, size_t, size_t);
|
|
|
|
void (*free_definite_size)(struct _malloc_zone_t*, void*, size_t);
|
|
|
|
size_t (*pressure_relief)(struct _malloc_zone_t*, size_t);
|
|
|
|
} malloc_zone_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
vm_address_t address;
|
|
|
|
vm_size_t size;
|
|
|
|
} vm_range_t;
|
|
|
|
|
|
|
|
typedef struct malloc_statistics_t {
|
|
|
|
unsigned blocks_in_use;
|
|
|
|
size_t size_in_use;
|
|
|
|
size_t max_size_in_use;
|
|
|
|
size_t size_allocated;
|
|
|
|
} malloc_statistics_t;
|
|
|
|
|
|
|
|
typedef kern_return_t memory_reader_t(task_t, vm_address_t, vm_size_t, void**);
|
2017-11-01 11:20:54 +03:00
|
|
|
|
2017-01-18 07:50:35 +03:00
|
|
|
typedef void vm_range_recorder_t(task_t, void*, unsigned type, vm_range_t*,
|
|
|
|
unsigned);
|
2017-11-01 11:20:54 +03:00
|
|
|
|
2017-01-18 07:50:35 +03:00
|
|
|
typedef struct malloc_introspection_t {
|
|
|
|
kern_return_t (*enumerator)(task_t, void*, unsigned, vm_address_t,
|
|
|
|
memory_reader_t, vm_range_recorder_t);
|
|
|
|
size_t (*good_size)(malloc_zone_t*, size_t);
|
|
|
|
boolean_t (*check)(malloc_zone_t*);
|
|
|
|
void (*print)(malloc_zone_t*, boolean_t);
|
|
|
|
void (*log)(malloc_zone_t*, void*);
|
|
|
|
void (*force_lock)(malloc_zone_t*);
|
|
|
|
void (*force_unlock)(malloc_zone_t*);
|
|
|
|
void (*statistics)(malloc_zone_t*, malloc_statistics_t*);
|
|
|
|
boolean_t (*zone_locked)(malloc_zone_t*);
|
|
|
|
boolean_t (*enable_discharge_checking)(malloc_zone_t*);
|
|
|
|
boolean_t (*disable_discharge_checking)(malloc_zone_t*);
|
|
|
|
void (*discharge)(malloc_zone_t*, void*);
|
|
|
|
#ifdef __BLOCKS__
|
|
|
|
void (*enumerate_discharged_pointers)(malloc_zone_t*, void (^)(void*, void*));
|
|
|
|
#else
|
|
|
|
void* enumerate_unavailable_without_blocks;
|
|
|
|
#endif
|
|
|
|
void (*reinit_lock)(malloc_zone_t*);
|
|
|
|
} malloc_introspection_t;
|
|
|
|
|
|
|
|
extern kern_return_t malloc_get_all_zones(task_t, memory_reader_t,
|
|
|
|
vm_address_t**, unsigned*);
|
|
|
|
|
|
|
|
extern malloc_zone_t* malloc_default_zone(void);
|
|
|
|
|
|
|
|
extern void malloc_zone_register(malloc_zone_t* zone);
|
|
|
|
|
|
|
|
extern void malloc_zone_unregister(malloc_zone_t* zone);
|
|
|
|
|
|
|
|
extern malloc_zone_t* malloc_default_purgeable_zone(void);
|
|
|
|
|
2017-07-04 09:01:50 +03:00
|
|
|
extern malloc_zone_t* malloc_zone_from_ptr(const void* ptr);
|
|
|
|
|
|
|
|
extern void malloc_zone_free(malloc_zone_t* zone, void* ptr);
|
|
|
|
|
|
|
|
extern void* malloc_zone_realloc(malloc_zone_t* zone, void* ptr, size_t size);
|
2017-01-18 07:50:35 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// The following is a OSX zone allocator implementation.
|
|
|
|
// /!\ WARNING. It assumes the underlying malloc implementation's
|
|
|
|
// malloc_usable_size returns 0 when the given pointer is not owned by
|
|
|
|
// the allocator. Sadly, OSX does call zone_size with pointers not
|
|
|
|
// owned by the allocator.
|
2017-01-18 05:39:29 +03:00
|
|
|
|
2017-01-18 07:50:35 +03:00
|
|
|
static size_t zone_size(malloc_zone_t* zone, const void* ptr) {
|
2017-01-18 05:39:29 +03:00
|
|
|
return malloc_usable_size_impl(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* zone_malloc(malloc_zone_t* zone, size_t size) {
|
|
|
|
return malloc_impl(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* zone_calloc(malloc_zone_t* zone, size_t num, size_t size) {
|
|
|
|
return calloc_impl(num, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* zone_realloc(malloc_zone_t* zone, void* ptr, size_t size) {
|
|
|
|
if (malloc_usable_size_impl(ptr)) return realloc_impl(ptr, size);
|
2017-07-04 09:01:50 +03:00
|
|
|
|
|
|
|
// Sometimes, system libraries call malloc_zone_* functions with the wrong
|
|
|
|
// zone (e.g. CoreFoundation does). In that case, we need to find the real
|
|
|
|
// one. We can't call libSystem's realloc directly because we're exporting
|
|
|
|
// realloc from libmozglue and we'd pick that one, so we manually find the
|
|
|
|
// right zone and realloc with it.
|
|
|
|
malloc_zone_t* real_zone = malloc_zone_from_ptr(ptr);
|
|
|
|
// The system allocator crashes voluntarily by default when a pointer can't
|
|
|
|
// be traced back to a zone. Do the same.
|
|
|
|
MOZ_RELEASE_ASSERT(real_zone);
|
|
|
|
MOZ_RELEASE_ASSERT(real_zone != zone);
|
|
|
|
return malloc_zone_realloc(real_zone, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void other_zone_free(malloc_zone_t* original_zone, void* ptr) {
|
|
|
|
// Sometimes, system libraries call malloc_zone_* functions with the wrong
|
|
|
|
// zone (e.g. CoreFoundation does). In that case, we need to find the real
|
|
|
|
// one. We can't call libSystem's free directly because we're exporting
|
|
|
|
// free from libmozglue and we'd pick that one, so we manually find the
|
|
|
|
// right zone and free with it.
|
Bug 1396361 - Avoid crashing when some system library calls malloc_zone_free(zone, NULL). r=njn
Some system libraries call malloc_zone_free directly instead of free,
and sometimes they do that with the wrong zone. When that happens, we
circle back, trying to find the right zone, and call malloc_zone_free
with the right one, but when we can't find one, we crash, which matches
what the system free() would do. Except in one case where the pointer
we're being passed is NULL, in which case we can't trace it back to any
zone, but shouldn't crash (system free() explicitly doesn't crash in
that case).
--HG--
extra : rebase_source : 17efdcd80f1a53be7ab6b7293bfb6060a9aa4a48
2017-09-04 01:32:42 +03:00
|
|
|
if (!ptr) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-04 09:01:50 +03:00
|
|
|
malloc_zone_t* zone = malloc_zone_from_ptr(ptr);
|
|
|
|
// The system allocator crashes voluntarily by default when a pointer can't
|
|
|
|
// be traced back to a zone. Do the same.
|
|
|
|
MOZ_RELEASE_ASSERT(zone);
|
|
|
|
MOZ_RELEASE_ASSERT(zone != original_zone);
|
|
|
|
return malloc_zone_free(zone, ptr);
|
2017-01-18 05:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void zone_free(malloc_zone_t* zone, void* ptr) {
|
|
|
|
if (malloc_usable_size_impl(ptr)) {
|
|
|
|
free_impl(ptr);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-04 09:01:50 +03:00
|
|
|
other_zone_free(zone, ptr);
|
2017-01-18 05:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void zone_free_definite_size(malloc_zone_t* zone, void* ptr,
|
|
|
|
size_t size) {
|
|
|
|
size_t current_size = malloc_usable_size_impl(ptr);
|
|
|
|
if (current_size) {
|
|
|
|
MOZ_ASSERT(current_size == size);
|
|
|
|
free_impl(ptr);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-04 09:01:50 +03:00
|
|
|
other_zone_free(zone, ptr);
|
2017-01-18 05:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* zone_memalign(malloc_zone_t* zone, size_t alignment, size_t size) {
|
|
|
|
void* ptr;
|
|
|
|
if (posix_memalign_impl(&ptr, alignment, size) == 0) return ptr;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* zone_valloc(malloc_zone_t* zone, size_t size) {
|
|
|
|
return valloc_impl(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zone_destroy(malloc_zone_t* zone) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// This function should never be called.
|
2017-01-18 05:39:29 +03:00
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
static unsigned zone_batch_malloc(malloc_zone_t* zone, size_t size,
|
|
|
|
void** results, unsigned num_requested) {
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_requested; i++) {
|
|
|
|
results[i] = malloc_impl(size);
|
|
|
|
if (!results[i]) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zone_batch_free(malloc_zone_t* zone, void** to_be_freed,
|
|
|
|
unsigned num_to_be_freed) {
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_to_be_freed; i++) {
|
|
|
|
zone_free(zone, to_be_freed[i]);
|
|
|
|
to_be_freed[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t zone_pressure_relief(malloc_zone_t* zone, size_t goal) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:39:29 +03:00
|
|
|
static size_t zone_good_size(malloc_zone_t* zone, size_t size) {
|
|
|
|
return malloc_good_size_impl(size);
|
|
|
|
}
|
|
|
|
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
static kern_return_t zone_enumerator(task_t task, void* data,
|
|
|
|
unsigned type_mask,
|
|
|
|
vm_address_t zone_address,
|
|
|
|
memory_reader_t reader,
|
|
|
|
vm_range_recorder_t recorder) {
|
|
|
|
return KERN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean_t zone_check(malloc_zone_t* zone) { return true; }
|
|
|
|
|
|
|
|
static void zone_print(malloc_zone_t* zone, boolean_t verbose) {}
|
|
|
|
|
|
|
|
static void zone_log(malloc_zone_t* zone, void* address) {}
|
|
|
|
|
2017-01-18 05:39:29 +03:00
|
|
|
extern void _malloc_prefork(void);
|
2017-01-20 04:06:41 +03:00
|
|
|
extern void _malloc_postfork_child(void);
|
2017-01-18 05:39:29 +03:00
|
|
|
|
|
|
|
static void zone_force_lock(malloc_zone_t* zone) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// /!\ This calls into mozjemalloc. It works because we're linked in the
|
|
|
|
// same library.
|
2017-01-18 05:39:29 +03:00
|
|
|
_malloc_prefork();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zone_force_unlock(malloc_zone_t* zone) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// /!\ This calls into mozjemalloc. It works because we're linked in the
|
|
|
|
// same library.
|
2017-01-20 04:06:41 +03:00
|
|
|
_malloc_postfork_child();
|
2017-01-18 05:39:29 +03:00
|
|
|
}
|
|
|
|
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
static void zone_statistics(malloc_zone_t* zone, malloc_statistics_t* stats) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// We make no effort to actually fill the values
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
stats->blocks_in_use = 0;
|
|
|
|
stats->size_in_use = 0;
|
|
|
|
stats->max_size_in_use = 0;
|
|
|
|
stats->size_allocated = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean_t zone_locked(malloc_zone_t* zone) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Pretend no lock is being held
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zone_reinit_lock(malloc_zone_t* zone) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// As of OSX 10.12, this function is only used when force_unlock would
|
|
|
|
// be used if the zone version were < 9. So just use force_unlock.
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
zone_force_unlock(zone);
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:39:29 +03:00
|
|
|
static malloc_zone_t zone;
|
|
|
|
static struct malloc_introspection_t zone_introspect;
|
|
|
|
|
|
|
|
static malloc_zone_t* get_default_zone() {
|
|
|
|
malloc_zone_t** zones = NULL;
|
|
|
|
unsigned int num_zones = 0;
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// On OSX 10.12, malloc_default_zone returns a special zone that is not
|
|
|
|
// present in the list of registered zones. That zone uses a "lite zone"
|
|
|
|
// if one is present (apparently enabled when malloc stack logging is
|
|
|
|
// enabled), or the first registered zone otherwise. In practice this
|
|
|
|
// means unless malloc stack logging is enabled, the first registered
|
|
|
|
// zone is the default.
|
|
|
|
// So get the list of zones to get the first one, instead of relying on
|
|
|
|
// malloc_default_zone.
|
2017-01-18 05:39:29 +03:00
|
|
|
if (KERN_SUCCESS !=
|
|
|
|
malloc_get_all_zones(0, NULL, (vm_address_t**)&zones, &num_zones)) {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Reset the value in case the failure happened after it was set.
|
2017-01-18 05:39:29 +03:00
|
|
|
num_zones = 0;
|
|
|
|
}
|
|
|
|
if (num_zones) {
|
|
|
|
return zones[0];
|
|
|
|
}
|
|
|
|
return malloc_default_zone();
|
|
|
|
}
|
|
|
|
|
Bug 1399921 - Register zone allocator independently, and delay jemalloc initialization on mac. r=njn
In bug 1361258, we unified the initialization sequence on mac, and
chose to make the zone registration happen after jemalloc
initialization.
The order between jemalloc init and zone registration shouldn't actually
matter, because jemalloc initializes the first time the allocator is
actually used.
On the other hand, in some build setups (e.g. with light optimization),
the initialization of the thread_arena thread local variable can happen
after the forced jemalloc initialization because of the order the
corresponding static initializers run. In some levels of optimization,
the thread_arena initializer resets the value the jemalloc
initialization has set, which subsequently makes choose_arena() return
a bogus value (or hit an assertion in ThreadLocal.h on debug builds).
So instead of initializing jemalloc from a static initializer, which
then registers the zone, we instead register the zone and let jemalloc
initialize itself when used, which increases the chances of the
thread_arena initializer running first.
--HG--
extra : rebase_source : 4d9a5340d097ac8528dc4aaaf0c05bbef40b59bb
2017-09-15 01:34:48 +03:00
|
|
|
__attribute__((constructor)) static void register_zone(void) {
|
2017-01-18 05:39:29 +03:00
|
|
|
malloc_zone_t* default_zone = get_default_zone();
|
|
|
|
|
2017-01-18 07:50:35 +03:00
|
|
|
zone.size = zone_size;
|
|
|
|
zone.malloc = zone_malloc;
|
|
|
|
zone.calloc = zone_calloc;
|
|
|
|
zone.valloc = zone_valloc;
|
|
|
|
zone.free = zone_free;
|
|
|
|
zone.realloc = zone_realloc;
|
|
|
|
zone.destroy = zone_destroy;
|
2017-01-18 05:45:45 +03:00
|
|
|
#ifdef MOZ_REPLACE_MALLOC
|
2017-01-18 05:39:29 +03:00
|
|
|
zone.zone_name = "replace_malloc_zone";
|
2017-01-18 05:45:45 +03:00
|
|
|
#else
|
|
|
|
zone.zone_name = "jemalloc_zone";
|
|
|
|
#endif
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
zone.batch_malloc = zone_batch_malloc;
|
|
|
|
zone.batch_free = zone_batch_free;
|
2017-01-18 05:39:29 +03:00
|
|
|
zone.introspect = &zone_introspect;
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
zone.version = 9;
|
2017-01-18 05:39:29 +03:00
|
|
|
zone.memalign = zone_memalign;
|
|
|
|
zone.free_definite_size = zone_free_definite_size;
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
zone.pressure_relief = zone_pressure_relief;
|
|
|
|
zone_introspect.enumerator = zone_enumerator;
|
2017-01-18 07:50:35 +03:00
|
|
|
zone_introspect.good_size = zone_good_size;
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
zone_introspect.check = zone_check;
|
|
|
|
zone_introspect.print = zone_print;
|
|
|
|
zone_introspect.log = zone_log;
|
2017-01-18 07:50:35 +03:00
|
|
|
zone_introspect.force_lock = zone_force_lock;
|
|
|
|
zone_introspect.force_unlock = zone_force_unlock;
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
zone_introspect.statistics = zone_statistics;
|
|
|
|
zone_introspect.zone_locked = zone_locked;
|
2017-01-18 05:39:29 +03:00
|
|
|
zone_introspect.enable_discharge_checking = NULL;
|
|
|
|
zone_introspect.disable_discharge_checking = NULL;
|
|
|
|
zone_introspect.discharge = NULL;
|
|
|
|
#ifdef __BLOCKS__
|
|
|
|
zone_introspect.enumerate_discharged_pointers = NULL;
|
|
|
|
#else
|
|
|
|
zone_introspect.enumerate_unavailable_without_blocks = NULL;
|
|
|
|
#endif
|
Bug 1286613 - Add dummy implementations for most remaining OSX zone allocator functions. r=njn
Some system libraries are using malloc_default_zone() and then using
some of the malloc_zone_* API. Under normal conditions, those functions
check the malloc_zone_t/malloc_introspection_t struct for the values
that are allowed to be NULL, so that a NULL deref doesn't happen.
As of OSX 10.12, malloc_default_zone() doesn't return the actual default
zone anymore, but returns a fake, wrapper zone. The wrapper zone defines
all the possible functions in the malloc_zone_t/malloc_introspection_t
struct (almost), and calls the function from the registered default zone
(jemalloc in our case) on its own. Without checking whether the pointers
are NULL.
This means that a system library that calls e.g.
malloc_zone_batch_malloc(malloc_default_zone(), ...) ends up trying to
call jemalloc_zone.batch_malloc, which is NULL, and crash follows.
So as of OSX 10.12, the default zone is required to have all the
functions available (really, the same as the wrapper zone), even if they
do nothing.
This is arguably a bug in libsystem_malloc in OSX 10.12, but jemalloc
still needs to work in that case.
[Adapted from
https://github.com/jemalloc/jemalloc/commit/c6943acb3c56d1b3d1e82dd43b3fcfeae7771990]
--HG--
extra : rebase_source : 7d7a5b47fa18f56183e99c3655aee003c9be161e
2017-01-18 08:35:11 +03:00
|
|
|
zone_introspect.reinit_lock = zone_reinit_lock;
|
2017-01-18 05:39:29 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// The default purgeable zone is created lazily by OSX's libc. It uses
|
|
|
|
// the default zone when it is created for "small" allocations
|
|
|
|
// (< 15 KiB), but assumes the default zone is a scalable_zone. This
|
|
|
|
// obviously fails when the default zone is the jemalloc zone, so
|
|
|
|
// malloc_default_purgeable_zone is called beforehand so that the
|
|
|
|
// default purgeable zone is created when the default zone is still
|
|
|
|
// a scalable_zone.
|
2017-01-18 05:39:29 +03:00
|
|
|
malloc_zone_t* purgeable_zone = malloc_default_purgeable_zone();
|
|
|
|
|
2018-01-31 11:18:01 +03:00
|
|
|
// There is a problem related to the above with the system nano zone, which
|
|
|
|
// is hard to work around from here, and that is instead worked around by
|
|
|
|
// disabling the nano zone through an environment variable
|
|
|
|
// (MallocNanoZone=0). In Firefox, we do that through
|
|
|
|
// browser/app/macbuild/Contents/Info.plist.in.
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Register the custom zone. At this point it won't be the default.
|
2017-01-18 05:39:29 +03:00
|
|
|
malloc_zone_register(&zone);
|
|
|
|
|
|
|
|
do {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Unregister and reregister the default zone. On OSX >= 10.6,
|
|
|
|
// unregistering takes the last registered zone and places it at the
|
|
|
|
// location of the specified zone. Unregistering the default zone thus
|
|
|
|
// makes the last registered one the default. On OSX < 10.6,
|
|
|
|
// unregistering shifts all registered zones. The first registered zone
|
|
|
|
// then becomes the default.
|
2017-01-18 05:39:29 +03:00
|
|
|
malloc_zone_unregister(default_zone);
|
|
|
|
malloc_zone_register(default_zone);
|
2017-11-01 11:15:12 +03:00
|
|
|
|
|
|
|
// On OSX 10.6, having the default purgeable zone appear before the default
|
|
|
|
// zone makes some things crash because it thinks it owns the default
|
|
|
|
// zone allocated pointers. We thus unregister/re-register it in order to
|
|
|
|
// ensure it's always after the default zone. On OSX < 10.6, as
|
|
|
|
// unregistering shifts registered zones, this simply removes the purgeable
|
|
|
|
// zone from the list and adds it back at the end, after the default zone.
|
|
|
|
// On OSX >= 10.6, unregistering replaces the purgeable zone with the last
|
|
|
|
// registered zone above, i.e the default zone. Registering it again then
|
|
|
|
// puts it at the end, obviously after the default zone.
|
2017-01-18 05:39:29 +03:00
|
|
|
malloc_zone_unregister(purgeable_zone);
|
|
|
|
malloc_zone_register(purgeable_zone);
|
|
|
|
default_zone = get_default_zone();
|
|
|
|
} while (default_zone != &zone);
|
|
|
|
}
|