From 5a612662d2fa26e8ed822b5c7e3873a7ebf5f29a Mon Sep 17 00:00:00 2001 From: Gabriele Svelto Date: Fri, 12 May 2023 15:03:47 +0000 Subject: [PATCH] Bug 1832526 - If we can't interpose a libc function assume libc isn't loaded and open it manually r=gerard-majax Differential Revision: https://phabricator.services.mozilla.com/D177887 --- mozglue/interposers/InterposerHelper.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/mozglue/interposers/InterposerHelper.h b/mozglue/interposers/InterposerHelper.h index 3277dd6a2d37..d26cab3c7c33 100644 --- a/mozglue/interposers/InterposerHelper.h +++ b/mozglue/interposers/InterposerHelper.h @@ -36,11 +36,16 @@ static T get_real_symbol(const char* aName, T aReplacementSymbol) { if (real_symbol == nullptr) { // On old versions of Android the application runtime links in libc before // we get a chance to link libmozglue, so its symbols don't appear when - // resolving them with RTLD_NEXT but rather with RTLD_DEFAULT. If RTLD_NEXT - // failed to find a symbol we try again with RTLD_DEFAULT. The checks below - // make sure that we crash in case the symbol we get matches the - // replacement one so this is safe albeit a bit weird. - real_symbol = dlsym_wrapper(RTLD_DEFAULT, aName); + // resolving them with RTLD_NEXT. This behavior differ between the + // different versions of Android so we'll just look for them directly into + // libc.so. Note that this won't work if we're trying to interpose + // functions that are in other libraries, but hopefully we'll never have + // to do that. + void* handle = dlopen("libc.so", RTLD_LAZY); + + if (handle) { + real_symbol = dlsym_wrapper(handle, aName); + } } #endif