| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#include <atomic> |
| 6 |
#include <array> |
| 7 |
#include <errno.h> |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" |
| 11 |
#include "mozilla/DebugOnly.h" |
| 12 |
|
| 13 |
#include "InterposerHelper.h" |
| 14 |
|
| 15 |
using mozilla::DebugOnly; |
| 16 |
|
| 17 |
#if defined(MOZ_ENABLE_FORKSERVER) && !defined(MOZ_TSAN) |
| 18 |
# include "mozilla/pthread_atfork.h" |
| 19 |
|
| 20 |
static constexpr const int maxHandlers = 32; |
| 21 |
static constexpr const int idxPreFork = 0; |
| 22 |
static constexpr const int idxPostForkParent = 1; |
| 23 |
static constexpr const int idxPostForkChild = 2; |
| 24 |
|
| 25 |
struct moz_pthread_atfork_handler { |
| 26 |
using fn_ptr = std::atomic<void (*)(void)>; |
| 27 |
using dso_handle = std::atomic<void*>; |
| 28 |
using pthread_handlers = std::array<fn_ptr, 3>; |
| 29 |
|
| 30 |
std::atomic<int> usedElems = 0; |
| 31 |
std::array<pthread_handlers, maxHandlers> handlers = {}; |
| 32 |
std::array<dso_handle, maxHandlers> dsos = {}; |
| 33 |
|
| 34 |
bool add(void (*aPrefork)(void), void (*aParent)(void), void (*aChild)(void), |
| 35 |
const void* const aHandle) { |
| 36 |
if (usedElems == maxHandlers) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
int elem = 0; |
| 41 |
for (elem = 0; elem < maxHandlers; ++elem) { |
| 42 |
if (dsos[elem] == nullptr) { |
| 43 |
handlers[elem][idxPreFork] = aPrefork; |
| 44 |
handlers[elem][idxPostForkParent] = aParent; |
| 45 |
handlers[elem][idxPostForkChild] = aChild; |
| 46 |
dsos[elem] = (void*)(aHandle); |
| 47 |
++usedElems; |
| 48 |
break; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
bool remove(void* aHandle) { |
| 56 |
int elem = 0; |
| 57 |
for (elem = 0; elem < maxHandlers; ++elem) { |
| 58 |
if (dsos[elem] == aHandle) { |
| 59 |
handlers[elem][idxPreFork] = nullptr; |
| 60 |
handlers[elem][idxPostForkParent] = nullptr; |
| 61 |
handlers[elem][idxPostForkChild] = nullptr; |
| 62 |
dsos[elem] = nullptr; |
| 63 |
--usedElems; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return true; |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
struct moz_pthread_atfork_handler mozPthreadHandlers; |
| 72 |
|
| 73 |
# if defined(LIBC_GLIBC) |
| 74 |
// On glibc the pthread_atfork may be available only from libc_nonshared.a |
| 75 |
// so prefer interposing the linker-resolved __register_atfork() |
| 76 |
|
| 77 |
extern const void* const __dso_handle; |
| 78 |
using register_atfork_t = int (*)(void (*)(), void (*)(), void (*)(), |
| 79 |
const void* const); |
| 80 |
static register_atfork_t real_register_atfork = nullptr; |
| 81 |
# else |
| 82 |
using pthread_atfork_t = int (*)(void (*)(), void (*)(), void (*)()); |
| 83 |
static pthread_atfork_t real_pthread_atfork = nullptr; |
| 84 |
# endif |
| 85 |
|
| 86 |
static int notReadyCount = 0; |
| 87 |
|
| 88 |
extern "C" { |
| 89 |
|
| 90 |
# if defined(LIBC_GLIBC) |
| 91 |
MFBT_API int __register_atfork(void (*aPrefork)(void), |
| 92 |
void (*aPostForkParent)(void), |
| 93 |
void (*aPostForkChild)(void), |
| 94 |
const void* const dso_handle) |
| 95 |
# else |
| 96 |
MFBT_API int pthread_atfork(void (*aPrefork)(void), |
| 97 |
void (*aPostForkParent)(void), |
| 98 |
void (*aPostForkChild)(void)) |
| 99 |
# endif |
| 100 |
{ |
| 101 |
# if defined(LIBC_GLIBC) |
| 102 |
MOZ_ASSERT(real_register_atfork != __register_atfork, |
| 103 |
"Found __register_atfork from libc"); |
| 104 |
# else |
| 105 |
MOZ_ASSERT(real_pthread_atfork != pthread_atfork, |
| 106 |
"Found pthread_atfork from libc"); |
| 107 |
# endif |
| 108 |
|
| 109 |
int rv = 0; |
| 110 |
# if defined(LIBC_GLIBC) |
| 111 |
if (real_register_atfork) { |
| 112 |
real_register_atfork(aPrefork, aPostForkParent, aPostForkChild, dso_handle); |
| 113 |
# else |
| 114 |
if (real_pthread_atfork) { |
| 115 |
real_pthread_atfork(aPrefork, aPostForkParent, aPostForkChild); |
| 116 |
# endif |
| 117 |
MOZ_ASSERT(rv == 0, "call to real_register_atfork() failed"); |
| 118 |
if (rv != 0) { |
| 119 |
return rv; |
| 120 |
} |
| 121 |
} else { |
| 122 |
++notReadyCount; |
| 123 |
} |
| 124 |
|
| 125 |
rv = mozPthreadHandlers.add(aPrefork, aPostForkParent, aPostForkChild |
| 126 |
# if defined(LIBC_GLIBC) |
| 127 |
, |
| 128 |
dso_handle |
| 129 |
# else |
| 130 |
, |
| 131 |
(void*)(1) |
| 132 |
# endif |
| 133 |
) |
| 134 |
? 0 |
| 135 |
: 1; |
| 136 |
MOZ_ASSERT(rv == 0, |
| 137 |
# if defined(LIBC_GLIBC) |
| 138 |
"Should have been able to add to __register_atfork() handlers" |
| 139 |
# else |
| 140 |
"Should have been able to add to pthread_atfork() handlers" |
| 141 |
# endif |
| 142 |
); |
| 143 |
|
| 144 |
if (rv > 0) { |
| 145 |
rv = ENOMEM; |
| 146 |
} |
| 147 |
|
| 148 |
return rv; |
| 149 |
} |
| 150 |
|
| 151 |
# if defined(LIBC_GLIBC) |
| 152 |
MFBT_API void __cxa_finalize(void* handle) { |
| 153 |
static const auto real_cxa_finalize = GET_REAL_SYMBOL(__cxa_finalize); |
| 154 |
real_cxa_finalize(handle); |
| 155 |
mozPthreadHandlers.remove(handle); |
| 156 |
} |
| 157 |
# endif |
| 158 |
} |
| 159 |
|
| 160 |
# if defined(LIBC_GLIBC) |
| 161 |
__attribute__((used)) __attribute__((constructor)) void register_atfork_setup( |
| 162 |
void) { |
| 163 |
real_register_atfork = GET_REAL_SYMBOL(__register_atfork); |
| 164 |
|
| 165 |
if (notReadyCount > 0) { |
| 166 |
for (int i = 0; i < notReadyCount; ++i) { |
| 167 |
real_register_atfork(mozPthreadHandlers.handlers[i][idxPreFork], |
| 168 |
mozPthreadHandlers.handlers[i][idxPostForkParent], |
| 169 |
mozPthreadHandlers.handlers[i][idxPostForkChild], |
| 170 |
__dso_handle); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
# else |
| 175 |
__attribute__((used)) __attribute__((constructor)) void pthread_atfork_setup( |
| 176 |
void) { |
| 177 |
real_pthread_atfork = GET_REAL_SYMBOL(pthread_atfork); |
| 178 |
|
| 179 |
if (notReadyCount > 0) { |
| 180 |
for (int i = 0; i < notReadyCount; ++i) { |
| 181 |
real_pthread_atfork(mozPthreadHandlers.handlers[i][idxPreFork], |
| 182 |
mozPthreadHandlers.handlers[i][idxPostForkParent], |
| 183 |
mozPthreadHandlers.handlers[i][idxPostForkChild]); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
# endif |
| 188 |
|
| 189 |
void run_moz_pthread_atfork_handlers(struct moz_pthread_atfork_handler* list, |
| 190 |
int handlerIdx, bool reverse) { |
| 191 |
MOZ_ASSERT(list, "moz_pthread_atfork_handler should not be nullptr"); |
| 192 |
for (int i = (reverse ? maxHandlers - 1 : 0); |
| 193 |
(reverse ? (i >= 0) : (i < maxHandlers)); (reverse ? --i : ++i)) { |
| 194 |
if (list->dsos[i]) { |
| 195 |
if (list->handlers[i][handlerIdx]) { |
| 196 |
(*list->handlers[i][handlerIdx])(); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
void run_moz_pthread_atfork_handlers_prefork() { |
| 203 |
run_moz_pthread_atfork_handlers(&mozPthreadHandlers, idxPreFork, true); |
| 204 |
} |
| 205 |
|
| 206 |
void run_moz_pthread_atfork_handlers_postfork_parent() { |
| 207 |
run_moz_pthread_atfork_handlers(&mozPthreadHandlers, idxPostForkParent, |
| 208 |
false); |
| 209 |
} |
| 210 |
|
| 211 |
void run_moz_pthread_atfork_handlers_postfork_child() { |
| 212 |
run_moz_pthread_atfork_handlers(&mozPthreadHandlers, idxPostForkChild, false); |
| 213 |
} |
| 214 |
#endif // defined(MOZ_ENABLE_FORKSERVER) |