2014-11-18 13:21:06 +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: */
|
|
|
|
/* 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/. */
|
2014-11-18 13:21:06 +03:00
|
|
|
|
|
|
|
#ifndef replace_malloc_bridge_h
|
|
|
|
#define replace_malloc_bridge_h
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// The replace-malloc bridge allows bidirectional method calls between
|
|
|
|
// a program and the replace-malloc library that has been loaded for it.
|
|
|
|
// In Firefox, this is used to allow method calls between code in libxul
|
|
|
|
// and code in the replace-malloc library, without libxul needing to link
|
|
|
|
// against that library or vice-versa.
|
|
|
|
//
|
|
|
|
// Subsystems can add methods for their own need. Replace-malloc libraries
|
|
|
|
// can decide to implement those methods or not.
|
|
|
|
//
|
|
|
|
// Replace-malloc libraries can provide such a bridge by implementing
|
|
|
|
// a ReplaceMallocBridge-derived class, and a replace_get_bridge function
|
|
|
|
// returning an instance of that class. The default methods in
|
|
|
|
// ReplaceMallocBridge are expected to return values that callers would
|
|
|
|
// understand as "the bridge doesn't implement this method", so that a
|
|
|
|
// replace-malloc library doesn't have to implement all methods.
|
|
|
|
//
|
|
|
|
// The ReplaceMallocBridge class contains definitions for methods for
|
|
|
|
// all replace-malloc libraries. Each library picks the methods it wants
|
|
|
|
// to reply to in its ReplaceMallocBridge-derived class instance.
|
|
|
|
// All methods of ReplaceMallocBridge must be virtual. Similarly,
|
|
|
|
// anything passed as an argument to those methods must be plain data, or
|
|
|
|
// an instance of a class with only virtual methods.
|
|
|
|
//
|
|
|
|
// Binary compatibility is expected to be maintained, such that a newer
|
|
|
|
// Firefox can be used with an old replace-malloc library, or an old
|
|
|
|
// Firefox can be used with a newer replace-malloc library. As such, only
|
|
|
|
// new virtual methods should be added to ReplaceMallocBridge, and
|
|
|
|
// each change should have a corresponding bump of the mVersion value.
|
|
|
|
// At the same time, each virtual method should have a corresponding
|
|
|
|
// wrapper calling the virtual method on the instance from
|
|
|
|
// ReplaceMallocBridge::Get(), giving it the version the virtual method
|
|
|
|
// was added.
|
|
|
|
//
|
|
|
|
// Parts that are not relevant to the replace-malloc library end of the
|
|
|
|
// bridge are hidden when REPLACE_MALLOC_IMPL is not defined, which is
|
|
|
|
// the case when including replace_malloc.h.
|
2014-11-18 13:21:06 +03:00
|
|
|
|
|
|
|
struct ReplaceMallocBridge;
|
|
|
|
|
|
|
|
#include "mozilla/Types.h"
|
|
|
|
|
2015-05-26 09:48:21 +03:00
|
|
|
MOZ_BEGIN_EXTERN_C
|
|
|
|
|
2014-11-18 13:21:06 +03:00
|
|
|
#ifndef REPLACE_MALLOC_IMPL
|
2017-11-01 11:15:12 +03:00
|
|
|
// Returns the replace-malloc bridge if there is one to be returned.
|
2017-11-01 11:20:54 +03:00
|
|
|
MFBT_API ReplaceMallocBridge* get_bridge();
|
2014-11-18 13:21:06 +03:00
|
|
|
#endif
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Table of malloc functions.
|
|
|
|
// e.g. void* (*malloc)(size_t), etc.
|
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
#define MALLOC_DECL(name, return_type, ...) \
|
|
|
|
typedef return_type(name##_impl_t)(__VA_ARGS__);
|
2015-05-26 09:48:21 +03:00
|
|
|
|
|
|
|
#include "malloc_decls.h"
|
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
#define MALLOC_DECL(name, return_type, ...) name##_impl_t* name;
|
2015-05-26 09:48:21 +03:00
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
typedef struct {
|
2015-05-26 09:48:21 +03:00
|
|
|
#include "malloc_decls.h"
|
|
|
|
} malloc_table_t;
|
|
|
|
|
2017-09-21 09:27:12 +03:00
|
|
|
MOZ_END_EXTERN_C
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2015-05-26 09:48:21 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Table of malloc hook functions.
|
|
|
|
// Those functions are called with the arguments and results of malloc
|
|
|
|
// functions after they are called.
|
|
|
|
// e.g. void* (*malloc_hook)(void*, size_t), etc.
|
|
|
|
// They can either return the result they're given, or alter it before
|
|
|
|
// returning it.
|
|
|
|
// The hooks corresponding to functions, like free(void*), that return no
|
|
|
|
// value, don't take an extra argument.
|
|
|
|
// The table must at least contain a pointer for malloc_hook and free_hook
|
|
|
|
// functions. They will be used as fallback if no pointer is given for
|
|
|
|
// other allocation functions, like calloc_hook.
|
2017-09-21 09:27:12 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace detail {
|
2017-11-01 11:20:54 +03:00
|
|
|
template <typename R, typename... Args>
|
|
|
|
struct AllocHookType {
|
2017-09-21 09:27:12 +03:00
|
|
|
using Type = R (*)(R, Args...);
|
|
|
|
};
|
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
template <typename... Args>
|
2017-09-21 09:27:12 +03:00
|
|
|
struct AllocHookType<void, Args...> {
|
|
|
|
using Type = void (*)(Args...);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
# define MALLOC_DECL(name, return_type, ...) \
|
|
|
|
typename mozilla::detail::AllocHookType<return_type, ##__VA_ARGS__>::Type \
|
|
|
|
name##_hook;
|
2015-05-26 09:48:21 +03:00
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
typedef struct {
|
2015-05-26 09:48:21 +03:00
|
|
|
# include "malloc_decls.h"
|
2017-11-01 11:15:12 +03:00
|
|
|
// Like free_hook, but called before realloc_hook. free_hook is called
|
|
|
|
// instead of not given.
|
2015-05-26 09:48:21 +03:00
|
|
|
void (*realloc_hook_before)(void* aPtr);
|
|
|
|
} malloc_hook_table_t;
|
|
|
|
|
2014-11-18 13:21:06 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dmd {
|
|
|
|
struct DMDFuncs;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dmd
|
2014-11-18 13:21:06 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Callbacks to register debug file handles for Poison IO interpose.
|
|
|
|
// See Mozilla(|Un)RegisterDebugHandle in xpcom/build/PoisonIOInterposer.h
|
2014-11-18 13:21:06 +03:00
|
|
|
struct DebugFdRegistry {
|
|
|
|
virtual void RegisterHandle(intptr_t aFd);
|
|
|
|
|
|
|
|
virtual void UnRegisterHandle(intptr_t aFd);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2014-11-18 13:21:06 +03:00
|
|
|
|
2014-11-18 13:21:06 +03:00
|
|
|
struct ReplaceMallocBridge {
|
2017-11-01 11:20:54 +03:00
|
|
|
ReplaceMallocBridge() : mVersion(3) {}
|
2014-11-18 13:21:06 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// This method was added in version 1 of the bridge.
|
2014-11-18 13:21:06 +03:00
|
|
|
virtual mozilla::dmd::DMDFuncs* GetDMDFuncs() { return nullptr; }
|
2014-11-18 13:21:06 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Send a DebugFdRegistry instance to the replace-malloc library so that
|
|
|
|
// it can register/unregister file descriptors whenever needed. The
|
|
|
|
// instance is valid until the process dies.
|
|
|
|
// This method was added in version 2 of the bridge.
|
2014-11-18 13:21:06 +03:00
|
|
|
virtual void InitDebugFd(mozilla::DebugFdRegistry&) {}
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
// Register a list of malloc functions and hook functions to the
|
|
|
|
// replace-malloc library so that it can choose to dispatch to them
|
|
|
|
// when needed. The details of what is dispatched when is left to the
|
|
|
|
// replace-malloc library.
|
|
|
|
// Passing a nullptr for either table will unregister a previously
|
|
|
|
// registered table under the same name.
|
|
|
|
// Returns nullptr if registration failed.
|
|
|
|
// If registration succeeded, a table of "pure" malloc functions is
|
|
|
|
// returned. Those "pure" malloc functions won't call hooks.
|
|
|
|
// /!\ Do not rely on registration/unregistration to be instantaneous.
|
|
|
|
// Functions from a previously registered table may still be called for
|
|
|
|
// a brief time after RegisterHook returns.
|
|
|
|
// This method was added in version 3 of the bridge.
|
2017-11-01 11:20:54 +03:00
|
|
|
virtual const malloc_table_t* RegisterHook(
|
|
|
|
const char* aName, const malloc_table_t* aTable,
|
|
|
|
const malloc_hook_table_t* aHookTable) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-26 09:48:21 +03:00
|
|
|
|
2014-11-18 13:21:06 +03:00
|
|
|
# ifndef REPLACE_MALLOC_IMPL
|
2017-11-01 11:15:12 +03:00
|
|
|
// Returns the replace-malloc bridge if its version is at least the
|
|
|
|
// requested one.
|
2017-11-01 11:20:54 +03:00
|
|
|
static ReplaceMallocBridge* Get(int aMinimumVersion) {
|
2014-11-18 13:21:06 +03:00
|
|
|
static ReplaceMallocBridge* sSingleton = get_bridge();
|
2017-11-01 11:20:54 +03:00
|
|
|
return (sSingleton && sSingleton->mVersion >= aMinimumVersion) ? sSingleton
|
|
|
|
: nullptr;
|
2014-11-18 13:21:06 +03:00
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const int mVersion;
|
|
|
|
};
|
|
|
|
|
|
|
|
# ifndef REPLACE_MALLOC_IMPL
|
2017-11-01 11:15:12 +03:00
|
|
|
// Class containing wrappers for calls to ReplaceMallocBridge methods.
|
|
|
|
// Those wrappers need to be static methods in a class because compilers
|
|
|
|
// complain about unused static global functions, and linkers complain
|
|
|
|
// about multiple definitions of non-static global functions.
|
|
|
|
// Using a separate class from ReplaceMallocBridge allows the function
|
|
|
|
// names to be identical.
|
2014-11-18 13:21:06 +03:00
|
|
|
struct ReplaceMalloc {
|
2017-11-01 11:15:12 +03:00
|
|
|
// Don't call this method from performance critical code. Use
|
|
|
|
// mozilla::dmd::DMDFuncs::Get() instead, it has less overhead.
|
2014-11-18 13:21:06 +03:00
|
|
|
static mozilla::dmd::DMDFuncs* GetDMDFuncs() {
|
|
|
|
auto singleton = ReplaceMallocBridge::Get(/* minimumVersion */ 1);
|
|
|
|
return singleton ? singleton->GetDMDFuncs() : nullptr;
|
|
|
|
}
|
2014-11-18 13:21:06 +03:00
|
|
|
|
|
|
|
static void InitDebugFd(mozilla::DebugFdRegistry& aRegistry) {
|
|
|
|
auto singleton = ReplaceMallocBridge::Get(/* minimumVersion */ 2);
|
|
|
|
if (singleton) {
|
|
|
|
singleton->InitDebugFd(aRegistry);
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 09:48:21 +03:00
|
|
|
|
2017-11-01 11:20:54 +03:00
|
|
|
static const malloc_table_t* RegisterHook(
|
|
|
|
const char* aName, const malloc_table_t* aTable,
|
|
|
|
const malloc_hook_table_t* aHookTable) {
|
2015-05-26 09:48:21 +03:00
|
|
|
auto singleton = ReplaceMallocBridge::Get(/* minimumVersion */ 3);
|
|
|
|
return singleton ? singleton->RegisterHook(aName, aTable, aHookTable)
|
|
|
|
: nullptr;
|
|
|
|
}
|
2014-11-18 13:21:06 +03:00
|
|
|
};
|
|
|
|
# endif
|
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
#endif // __cplusplus
|
2014-11-18 13:21:06 +03:00
|
|
|
|
2017-11-01 11:15:12 +03:00
|
|
|
#endif // replace_malloc_bridge_h
|