library_loader_hooks.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/android/library_loader/library_loader_hooks.h"
  5. #include <string>
  6. #include "base/android/jni_string.h"
  7. #include "base/android/library_loader/anchor_functions_buildflags.h"
  8. #include "base/android/library_loader/library_prefetcher.h"
  9. #include "base/android/orderfile/orderfile_buildflags.h"
  10. #include "base/android/sys_utils.h"
  11. #include "base/at_exit.h"
  12. #include "base/base_jni_headers/LibraryLoader_jni.h"
  13. #include "base/base_switches.h"
  14. #include "base/metrics/histogram.h"
  15. #include "base/metrics/histogram_functions.h"
  16. #include "base/metrics/histogram_macros.h"
  17. #if BUILDFLAG(ORDERFILE_INSTRUMENTATION)
  18. #include "base/android/orderfile/orderfile_instrumentation.h"
  19. #endif
  20. namespace base {
  21. namespace android {
  22. namespace {
  23. base::AtExitManager* g_at_exit_manager = nullptr;
  24. LibraryLoadedHook* g_registration_callback = nullptr;
  25. NativeInitializationHook* g_native_initialization_hook = nullptr;
  26. NonMainDexJniRegistrationHook* g_jni_registration_hook = nullptr;
  27. LibraryProcessType g_library_process_type = PROCESS_UNINITIALIZED;
  28. } // namespace
  29. LibraryProcessType GetLibraryProcessType() {
  30. return g_library_process_type;
  31. }
  32. bool IsUsingOrderfileOptimization() {
  33. #if BUILDFLAG(SUPPORTS_CODE_ORDERING)
  34. return SysUtils::IsLowEndDeviceFromJni();
  35. #else // !SUPPORTS_CODE_ORDERING
  36. return false;
  37. #endif
  38. }
  39. void SetNativeInitializationHook(
  40. NativeInitializationHook native_initialization_hook) {
  41. g_native_initialization_hook = native_initialization_hook;
  42. }
  43. void SetNonMainDexJniRegistrationHook(
  44. NonMainDexJniRegistrationHook jni_registration_hook) {
  45. DCHECK(!g_jni_registration_hook);
  46. g_jni_registration_hook = jni_registration_hook;
  47. }
  48. void SetLibraryLoadedHook(LibraryLoadedHook* func) {
  49. g_registration_callback = func;
  50. }
  51. static jboolean JNI_LibraryLoader_LibraryLoaded(
  52. JNIEnv* env,
  53. jint library_process_type) {
  54. DCHECK_EQ(g_library_process_type, PROCESS_UNINITIALIZED);
  55. g_library_process_type =
  56. static_cast<LibraryProcessType>(library_process_type);
  57. #if BUILDFLAG(ORDERFILE_INSTRUMENTATION)
  58. orderfile::StartDelayedDump();
  59. #endif
  60. #if BUILDFLAG(SUPPORTS_CODE_ORDERING)
  61. if (CommandLine::ForCurrentProcess()->HasSwitch(
  62. "log-native-library-residency")) {
  63. NativeLibraryPrefetcher::MadviseForResidencyCollection();
  64. } else if (IsUsingOrderfileOptimization()) {
  65. NativeLibraryPrefetcher::MadviseForOrderfile();
  66. }
  67. #endif
  68. if (g_native_initialization_hook &&
  69. !g_native_initialization_hook(
  70. static_cast<LibraryProcessType>(library_process_type)))
  71. return false;
  72. if (g_registration_callback &&
  73. !g_registration_callback(
  74. env, nullptr,
  75. static_cast<LibraryProcessType>(library_process_type))) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. static void JNI_LibraryLoader_RegisterNonMainDexJni(JNIEnv* env) {
  81. if (g_jni_registration_hook) {
  82. g_jni_registration_hook();
  83. }
  84. }
  85. void LibraryLoaderExitHook() {
  86. if (g_at_exit_manager) {
  87. delete g_at_exit_manager;
  88. g_at_exit_manager = nullptr;
  89. }
  90. }
  91. void InitAtExitManager() {
  92. g_at_exit_manager = new base::AtExitManager();
  93. }
  94. } // namespace android
  95. } // namespace base