From 0ad99cb2f6608587f0933ee7d252f944e891385d Mon Sep 17 00:00:00 2001 From: Sandor Molnar Date: Tue, 20 Dec 2022 10:31:04 +0200 Subject: [PATCH] Backed out 3 changesets (bug 1752703) for causing spider-monkey bustages. Backed out changeset 6b8d96806ae6 (bug 1752703) Backed out changeset afbd8f714804 (bug 1752703) Backed out changeset 1adb41e2eb9b (bug 1752703) --- browser/app/moz.build | 5 ++ ipc/app/moz.build | 5 ++ js/xpconnect/shell/moz.build | 5 ++ mozglue/interposers/InterposerHelper.h | 41 ------------ mozglue/interposers/env_interposer.cpp | 62 ------------------- mozglue/moz.build | 3 - toolkit/crashreporter/moz.build | 1 + .../pthread_create_interposer}/moz.build | 13 +--- .../pthread_create_interposer.cpp | 27 ++++++-- 9 files changed, 41 insertions(+), 121 deletions(-) delete mode 100644 mozglue/interposers/InterposerHelper.h delete mode 100644 mozglue/interposers/env_interposer.cpp rename {mozglue/interposers => toolkit/crashreporter/pthread_create_interposer}/moz.build (59%) rename {mozglue/interposers => toolkit/crashreporter/pthread_create_interposer}/pthread_create_interposer.cpp (76%) diff --git a/browser/app/moz.build b/browser/app/moz.build index 9356538bc080..61824afe34d9 100644 --- a/browser/app/moz.build +++ b/browser/app/moz.build @@ -53,6 +53,11 @@ LOCAL_INCLUDES += [ "/xpcom/build", ] +# The pthred_create() interposer needs to be linked as early as possible so +# that it will appear before libpthread when resolving symbols. +if CONFIG["OS_ARCH"] == "Linux" and CONFIG["MOZ_CRASHREPORTER"]: + USE_LIBS += ["pthread_create_interposer"] + if CONFIG["LIBFUZZER"]: USE_LIBS += ["fuzzer"] LOCAL_INCLUDES += [ diff --git a/ipc/app/moz.build b/ipc/app/moz.build index afe1df122b27..de426b54499b 100644 --- a/ipc/app/moz.build +++ b/ipc/app/moz.build @@ -16,6 +16,11 @@ else: "MozillaRuntimeMain.cpp", ] +# The pthred_create() interposer needs to be linked as early as possible so +# that it will appear before libpthread when resolving symbols. +if CONFIG["OS_ARCH"] == "Linux" and CONFIG["MOZ_CRASHREPORTER"]: + USE_LIBS += ["pthread_create_interposer"] + include("/ipc/chromium/chromium-config.mozbuild") LOCAL_INCLUDES += [ diff --git a/js/xpconnect/shell/moz.build b/js/xpconnect/shell/moz.build index de3b050b7972..8a270fecfb08 100644 --- a/js/xpconnect/shell/moz.build +++ b/js/xpconnect/shell/moz.build @@ -10,6 +10,11 @@ SOURCES += [ "xpcshell.cpp", ] +# The pthred_create() interposer needs to be linked as early as possible so +# that it will appear before libpthread when resolving symbols. +if CONFIG["OS_ARCH"] == "Linux" and CONFIG["MOZ_CRASHREPORTER"]: + USE_LIBS += ["pthread_create_interposer"] + if CONFIG["LIBFUZZER"]: USE_LIBS += ["fuzzer"] diff --git a/mozglue/interposers/InterposerHelper.h b/mozglue/interposers/InterposerHelper.h deleted file mode 100644 index df833eb9d2d9..000000000000 --- a/mozglue/interposers/InterposerHelper.h +++ /dev/null @@ -1,41 +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/. */ - -#ifndef InterposerHelper_h -#define InterposerHelper_h - -#include - -#include - -#include "mozilla/Assertions.h" - -template -static T get_real_symbol(const char* aName, T aReplacementSymbol) { - // T can only be a function pointer - static_assert(std::is_function::type>::value); - - // Find the corresponding function in the linked libraries - T real_symbol = reinterpret_cast(dlsym(RTLD_NEXT, aName)); - - if (real_symbol == nullptr) { - MOZ_CRASH_UNSAFE_PRINTF( - "%s() interposition failed but the interposer function is " - "still being called, this won't work!", - aName); - } - - if (real_symbol == aReplacementSymbol) { - MOZ_CRASH_UNSAFE_PRINTF( - "We could not obtain the real %s(). Calling the symbol we " - "got would make us enter an infinite loop so stop here instead.", - aName); - } - - return real_symbol; -} - -#define GET_REAL_SYMBOL(name) get_real_symbol(#name, name) - -#endif // InterposerHelper_h diff --git a/mozglue/interposers/env_interposer.cpp b/mozglue/interposers/env_interposer.cpp deleted file mode 100644 index ae245acebcca..000000000000 --- a/mozglue/interposers/env_interposer.cpp +++ /dev/null @@ -1,62 +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 - -#include "InterposerHelper.h" - -// The interposers in this file cover all the functions used to access the -// environment (getenv(), putenv(), setenv(), unsetenv() and clearenv()). They -// all use the mutex below for synchronization to prevent races that caused -// startup crashes, see bug 1752703. -static pthread_mutex_t gEnvLock = PTHREAD_MUTEX_INITIALIZER; - -extern "C" { - -MFBT_API char* getenv(const char* name) { - static const auto real_getenv = GET_REAL_SYMBOL(getenv); - - pthread_mutex_lock(&gEnvLock); - char* result = real_getenv(name); - pthread_mutex_unlock(&gEnvLock); - return result; -} - -MFBT_API int putenv(char* string) { - static const auto real_putenv = GET_REAL_SYMBOL(putenv); - - pthread_mutex_lock(&gEnvLock); - int result = real_putenv(string); - pthread_mutex_unlock(&gEnvLock); - return result; -} - -MFBT_API int setenv(const char* name, const char* value, int replace) { - static const auto real_setenv = GET_REAL_SYMBOL(setenv); - - pthread_mutex_lock(&gEnvLock); - int result = real_setenv(name, value, replace); - pthread_mutex_unlock(&gEnvLock); - return result; -} - -MFBT_API int unsetenv(const char* name) { - static const auto real_unsetenv = GET_REAL_SYMBOL(unsetenv); - - pthread_mutex_lock(&gEnvLock); - int result = real_unsetenv(name); - pthread_mutex_unlock(&gEnvLock); - return result; -} - -MFBT_API int clearenv(void) { - static const auto real_clearenv = GET_REAL_SYMBOL(clearenv); - - pthread_mutex_lock(&gEnvLock); - int result = real_clearenv(); - pthread_mutex_unlock(&gEnvLock); - return result; -} - -} // extern "C" diff --git a/mozglue/moz.build b/mozglue/moz.build index a8288b0ce3d9..75e14589ca57 100644 --- a/mozglue/moz.build +++ b/mozglue/moz.build @@ -13,9 +13,6 @@ if CONFIG["MOZ_LINKER"] or CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": if CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": DIRS += ["android"] -if CONFIG["OS_ARCH"] == "Linux": - DIRS += ["interposers"] - DIRS += [ "baseprofiler", "build", diff --git a/toolkit/crashreporter/moz.build b/toolkit/crashreporter/moz.build index d274da182d7f..411d51093135 100644 --- a/toolkit/crashreporter/moz.build +++ b/toolkit/crashreporter/moz.build @@ -53,6 +53,7 @@ if CONFIG["MOZ_CRASHREPORTER"]: "google-breakpad/src/common", "google-breakpad/src/common/linux", "google-breakpad/src/processor", + "pthread_create_interposer", ] if CONFIG["MOZ_OXIDIZED_BREAKPAD"]: diff --git a/mozglue/interposers/moz.build b/toolkit/crashreporter/pthread_create_interposer/moz.build similarity index 59% rename from mozglue/interposers/moz.build rename to toolkit/crashreporter/pthread_create_interposer/moz.build index 1212f27e5e2d..d0ff4cae007a 100644 --- a/mozglue/interposers/moz.build +++ b/toolkit/crashreporter/pthread_create_interposer/moz.build @@ -3,17 +3,10 @@ # 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/. -Library("interposers") +Library("pthread_create_interposer") -DEFINES["IMPL_MFBT"] = True +NoVisibilityFlags() UNIFIED_SOURCES += [ - "env_interposer.cpp", + "pthread_create_interposer.cpp", ] - -if CONFIG["MOZ_CRASHREPORTER"]: - UNIFIED_SOURCES += [ - "pthread_create_interposer.cpp", - ] - -FINAL_LIBRARY = "mozglue" diff --git a/mozglue/interposers/pthread_create_interposer.cpp b/toolkit/crashreporter/pthread_create_interposer/pthread_create_interposer.cpp similarity index 76% rename from mozglue/interposers/pthread_create_interposer.cpp rename to toolkit/crashreporter/pthread_create_interposer/pthread_create_interposer.cpp index 65f60c2d1bfd..e3ba6b164f70 100644 --- a/mozglue/interposers/pthread_create_interposer.cpp +++ b/toolkit/crashreporter/pthread_create_interposer/pthread_create_interposer.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include @@ -12,8 +13,6 @@ #include "mozilla/Assertions.h" #include "mozilla/DebugOnly.h" -#include "InterposerHelper.h" - using mozilla::DebugOnly; struct SigAltStack { @@ -84,12 +83,30 @@ void* set_alt_signal_stack_and_start(PthreadCreateParams* params) { return thread_rv; } +using pthread_create_func_t = int (*)(pthread_t*, const pthread_attr_t*, + void* (*)(void*), void*); + extern "C" { // This interposer replaces libpthread's pthread_create() so that we can // inject an alternate signal stack in every new thread. -MFBT_API int pthread_create(pthread_t* thread, const pthread_attr_t* attr, - void* (*start_routine)(void*), void* arg) { - static const auto real_pthread_create = GET_REAL_SYMBOL(pthread_create); +__attribute__((visibility("default"))) int pthread_create( + pthread_t* thread, const pthread_attr_t* attr, + void* (*start_routine)(void*), void* arg) { + // static const pthread_create_func_t real_pthread_create = + static const pthread_create_func_t real_pthread_create = + (pthread_create_func_t)dlsym(RTLD_NEXT, "pthread_create"); + + if (real_pthread_create == nullptr) { + MOZ_CRASH( + "pthread_create() interposition failed but the interposer function is " + "still being called, this won't work!"); + } + + if (real_pthread_create == pthread_create) { + MOZ_CRASH( + "We could not obtain the real pthread_create(). Calling the symbol we " + "got would make us enter an infinte loop so stop here instead."); + } PthreadCreateParams* params = (PthreadCreateParams*)malloc(sizeof(PthreadCreateParams));