native_loader_lazy.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2019 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "nativeloader/native_loader.h"
  17. #define LOG_TAG "nativeloader"
  18. #include <dlfcn.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <log/log.h>
  22. namespace android {
  23. namespace {
  24. void* GetLibHandle() {
  25. static void* handle = dlopen("libnativeloader.so", RTLD_NOW);
  26. LOG_FATAL_IF(handle == nullptr, "Failed to load libnativeloader.so: %s", dlerror());
  27. return handle;
  28. }
  29. template <typename FuncPtr>
  30. FuncPtr GetFuncPtr(const char* function_name) {
  31. auto f = reinterpret_cast<FuncPtr>(dlsym(GetLibHandle(), function_name));
  32. LOG_FATAL_IF(f == nullptr, "Failed to get address of %s: %s", function_name, dlerror());
  33. return f;
  34. }
  35. #define GET_FUNC_PTR(name) GetFuncPtr<decltype(&name)>(#name)
  36. } // namespace
  37. void InitializeNativeLoader() {
  38. static auto f = GET_FUNC_PTR(InitializeNativeLoader);
  39. return f();
  40. }
  41. jstring CreateClassLoaderNamespace(JNIEnv* env, int32_t target_sdk_version, jobject class_loader,
  42. bool is_shared, jstring dex_path, jstring library_path,
  43. jstring permitted_path) {
  44. static auto f = GET_FUNC_PTR(CreateClassLoaderNamespace);
  45. return f(env, target_sdk_version, class_loader, is_shared, dex_path, library_path,
  46. permitted_path);
  47. }
  48. void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
  49. jobject class_loader, const char* caller_location, jstring library_path,
  50. bool* needs_native_bridge, char** error_msg) {
  51. static auto f = GET_FUNC_PTR(OpenNativeLibrary);
  52. return f(env, target_sdk_version, path, class_loader, caller_location, library_path,
  53. needs_native_bridge, error_msg);
  54. }
  55. bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
  56. static auto f = GET_FUNC_PTR(CloseNativeLibrary);
  57. return f(handle, needs_native_bridge, error_msg);
  58. }
  59. void NativeLoaderFreeErrorMessage(char* msg) {
  60. static auto f = GET_FUNC_PTR(NativeLoaderFreeErrorMessage);
  61. return f(msg);
  62. }
  63. struct android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
  64. static auto f = GET_FUNC_PTR(FindNamespaceByClassLoader);
  65. return f(env, class_loader);
  66. }
  67. struct NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env,
  68. jobject class_loader) {
  69. static auto f = GET_FUNC_PTR(FindNativeLoaderNamespaceByClassLoader);
  70. return f(env, class_loader);
  71. }
  72. void* OpenNativeLibraryInNamespace(struct NativeLoaderNamespace* ns, const char* path,
  73. bool* needs_native_bridge, char** error_msg) {
  74. static auto f = GET_FUNC_PTR(OpenNativeLibraryInNamespace);
  75. return f(ns, path, needs_native_bridge, error_msg);
  76. }
  77. void ResetNativeLoader() {
  78. static auto f = GET_FUNC_PTR(ResetNativeLoader);
  79. return f();
  80. }
  81. #undef GET_FUNC_PTR
  82. } // namespace android