Skip to content

Commit

Permalink
Bug 1420353 - Change how replace-malloc initializes, part 2. r=njn
Browse files Browse the repository at this point in the history
Because one entry point is simpler than two, we make replace_init fulfil
both the roles of replace_init and replace_get_bridge.

Note this should be binary compatible with older replace-malloc
libraries, albeit not detecting their bridge (and with the
previous change, they do not register anyways). So loading older
replace-malloc libraries should do nothing, but not crash in awful ways.

--HG--
extra : rebase_source : aaf83e706ee34f45cfa75551a2f0998e5c5b8726
  • Loading branch information
glandium committed Nov 24, 2017
1 parent 845f9c5 commit 32ecc64
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 36 deletions.
14 changes: 5 additions & 9 deletions memory/build/malloc_decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
(MALLOC_FUNCS_MALLOC_BASE | MALLOC_FUNCS_MALLOC_EXTRA)
#define MALLOC_FUNCS_JEMALLOC 4
#define MALLOC_FUNCS_INIT 8
#define MALLOC_FUNCS_BRIDGE 16
#define MALLOC_FUNCS_ARENA_BASE 32
#define MALLOC_FUNCS_ARENA_ALLOC 64
#define MALLOC_FUNCS_ARENA_BASE 16
#define MALLOC_FUNCS_ARENA_ALLOC 32
#define MALLOC_FUNCS_ARENA (MALLOC_FUNCS_ARENA_BASE | MALLOC_FUNCS_ARENA_ALLOC)
#define MALLOC_FUNCS_ALL \
(MALLOC_FUNCS_INIT | MALLOC_FUNCS_BRIDGE | MALLOC_FUNCS_MALLOC | \
MALLOC_FUNCS_JEMALLOC | MALLOC_FUNCS_ARENA)
(MALLOC_FUNCS_INIT | MALLOC_FUNCS_MALLOC | MALLOC_FUNCS_JEMALLOC | \
MALLOC_FUNCS_ARENA)

#endif // malloc_decls_h

Expand All @@ -38,10 +37,7 @@

#ifdef MALLOC_DECL
#if MALLOC_FUNCS & MALLOC_FUNCS_INIT
MALLOC_DECL(init, void, malloc_table_t*)
#endif
#if MALLOC_FUNCS & MALLOC_FUNCS_BRIDGE
MALLOC_DECL(get_bridge, struct ReplaceMallocBridge*)
MALLOC_DECL(init, void, malloc_table_t*, struct ReplaceMallocBridge**)
#endif
#if MALLOC_FUNCS & MALLOC_FUNCS_MALLOC_BASE
MALLOC_DECL(malloc, void*, size_t)
Expand Down
12 changes: 5 additions & 7 deletions memory/build/mozjemalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4830,7 +4830,7 @@ static malloc_table_t gReplaceMallocTable = {
#define MALLOC_DECL(name, return_type, ...) \
typedef return_type(name##_impl_t)(__VA_ARGS__); \
name##_impl_t* replace_##name = nullptr;
#define MALLOC_FUNCS (MALLOC_FUNCS_INIT | MALLOC_FUNCS_BRIDGE)
#define MALLOC_FUNCS MALLOC_FUNCS_INIT
#include "malloc_decls.h"
#endif

Expand Down Expand Up @@ -4878,6 +4878,7 @@ replace_malloc_init_funcs();
// Below is the malloc implementation overriding jemalloc and calling the
// replacement functions if they exist.
static bool gReplaceMallocInitialized = false;
static ReplaceMallocBridge* gReplaceMallocBridge = nullptr;
static void
init()
{
Expand All @@ -4887,7 +4888,7 @@ init()
#define MALLOC_DECL(name, ...) \
replace_##name = REPLACE_MALLOC_GET_FUNC(handle, name);

#define MALLOC_FUNCS (MALLOC_FUNCS_INIT | MALLOC_FUNCS_BRIDGE)
#define MALLOC_FUNCS MALLOC_FUNCS_INIT
#include "malloc_decls.h"
}
#endif
Expand All @@ -4896,7 +4897,7 @@ init()
// malloc() we'll get an infinite loop.
gReplaceMallocInitialized = true;
if (replace_init) {
replace_init(&gReplaceMallocTable);
replace_init(&gReplaceMallocTable, &gReplaceMallocBridge);
}
replace_malloc_init_funcs();
}
Expand All @@ -4919,10 +4920,7 @@ get_bridge(void)
if (MOZ_UNLIKELY(!gReplaceMallocInitialized)) {
init();
}
if (MOZ_LIKELY(!replace_get_bridge)) {
return nullptr;
}
return replace_get_bridge();
return gReplaceMallocBridge;
}

// posix_memalign, aligned_alloc, memalign and valloc all implement some kind
Expand Down
12 changes: 8 additions & 4 deletions memory/build/replace_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// An initialization function is called before any malloc replacement
// function, and has the following declaration:
//
// void replace_init(malloc_table_t *)
// void replace_init(malloc_table_t*, ReplaceMallocBridge**)
//
// The malloc_table_t pointer given to that function is a table containing
// pointers to the original allocator implementation, so that replacement
Expand All @@ -28,9 +28,13 @@
// If it needs the original implementation, it thus needs a copy of the
// original table.
//
// The ReplaceMallocBridge* pointer is an outparam that allows the
// replace_init function to return a pointer to its ReplaceMallocBridge
// (see replace_malloc_bridge.h).
//
// The functions to be implemented in the external library are of the form:
//
// void *replace_malloc(size_t size)
// void* replace_malloc(size_t size)
// {
// // Fiddle with the size if necessary.
// // orig->malloc doesn't have to be called if the external library
Expand Down Expand Up @@ -85,11 +89,11 @@ MOZ_BEGIN_EXTERN_C
#define MOZ_REPLACE_WEAK
#endif

// Export replace_init and replace_get_bridge.
// Export replace_init.
#define MALLOC_DECL(name, return_type, ...) \
MOZ_EXPORT return_type replace_##name(__VA_ARGS__) MOZ_REPLACE_WEAK;

#define MALLOC_FUNCS (MALLOC_FUNCS_INIT | MALLOC_FUNCS_BRIDGE)
#define MALLOC_FUNCS MALLOC_FUNCS_INIT
#include "malloc_decls.h"

// Define the remaining replace_* functions as not exported.
Expand Down
9 changes: 2 additions & 7 deletions memory/replace/dmd/DMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1265,18 +1265,13 @@ static void Init(malloc_table_t* aMallocTable);
} // namespace mozilla

void
replace_init(malloc_table_t* aMallocTable)
replace_init(malloc_table_t* aMallocTable, ReplaceMallocBridge** aBridge)
{
mozilla::dmd::Init(aMallocTable);
#define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
#define MALLOC_DECL(name, ...) aMallocTable->name = replace_ ## name;
#include "malloc_decls.h"
}

ReplaceMallocBridge*
replace_get_bridge()
{
return mozilla::dmd::gDMDBridge;
*aBridge = mozilla::dmd::gDMDBridge;
}

void*
Expand Down
11 changes: 3 additions & 8 deletions memory/replace/logalloc/LogAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ class LogAllocBridge : public ReplaceMallocBridge
};

void
replace_init(malloc_table_t* aTable)
replace_init(malloc_table_t* aTable, ReplaceMallocBridge** aBridge)
{
static LogAllocBridge bridge;
sFuncs = *aTable;
#define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
#define MALLOC_DECL(name, ...) aTable->name = replace_ ## name;
Expand All @@ -86,6 +87,7 @@ replace_init(malloc_table_t* aTable)
aTable->aligned_alloc = replace_aligned_alloc;
aTable->valloc = replace_valloc;
#endif
*aBridge = &bridge;

#ifndef _WIN32
/* When another thread has acquired a lock before forking, the child
Expand Down Expand Up @@ -168,13 +170,6 @@ replace_init(malloc_table_t* aTable)
}
}

ReplaceMallocBridge*
replace_get_bridge()
{
static LogAllocBridge bridge;
return &bridge;
}

/* Do a simple, text-form, log of all calls to replace-malloc functions.
* Use locking to guarantee that an allocation that did happen is logged
* before any other allocation/free happens.
Expand Down
2 changes: 1 addition & 1 deletion mozglue/build/replace_malloc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

ifeq (Darwin_1,$(OS_TARGET)_$(MOZ_REPLACE_MALLOC))
MK_LDFLAGS = -Wl,-U,_replace_init -Wl,-U,_replace_get_bridge
MK_LDFLAGS = -Wl,-U,_replace_init
endif

0 comments on commit 32ecc64

Please sign in to comment.