blob: 1c82dc428ff5ce8d843abe5cd97bab2c7e46f25d [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
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
17#include "nativeloader/native_loader.h"
18#define LOG_TAG "nativeloader"
19
20#include <dlfcn.h>
21#include <errno.h>
22#include <string.h>
23
24#include <log/log.h>
25
26namespace android {
27
28namespace {
29
30void* GetLibHandle() {
31 static void* handle = dlopen("libnativeloader.so", RTLD_NOW);
32 LOG_FATAL_IF(handle == nullptr, "Failed to load libnativeloader.so: %s", dlerror());
33 return handle;
34}
35
36template <typename FuncPtr>
37FuncPtr GetFuncPtr(const char* function_name) {
Martin Stjernholm8a9b51e2024-01-30 21:33:09 +000038 FuncPtr f = reinterpret_cast<FuncPtr>(dlsym(GetLibHandle(), function_name));
Orion Hodson9b16e342019-10-09 13:29:16 +010039 LOG_FATAL_IF(f == nullptr, "Failed to get address of %s: %s", function_name, dlerror());
40 return f;
41}
42
Chih-Hung Hsieh310432e2020-03-04 10:58:29 -080043#define GET_FUNC_PTR(name) GetFuncPtr<decltype(&(name))>(#name)
Orion Hodson9b16e342019-10-09 13:29:16 +010044
45} // namespace
46
Orion Hodson9b16e342019-10-09 13:29:16 +010047jstring CreateClassLoaderNamespace(JNIEnv* env, int32_t target_sdk_version, jobject class_loader,
48 bool is_shared, jstring dex_path, jstring library_path,
Jiyong Park14626a72020-07-02 23:17:58 +090049 jstring permitted_path, jstring uses_library_list) {
Orion Hodson9b16e342019-10-09 13:29:16 +010050 static auto f = GET_FUNC_PTR(CreateClassLoaderNamespace);
51 return f(env, target_sdk_version, class_loader, is_shared, dex_path, library_path,
Jiyong Park14626a72020-07-02 23:17:58 +090052 permitted_path, uses_library_list);
Orion Hodson9b16e342019-10-09 13:29:16 +010053}
54
55void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
56 jobject class_loader, const char* caller_location, jstring library_path,
57 bool* needs_native_bridge, char** error_msg) {
58 static auto f = GET_FUNC_PTR(OpenNativeLibrary);
59 return f(env, target_sdk_version, path, class_loader, caller_location, library_path,
60 needs_native_bridge, error_msg);
61}
62
63bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
64 static auto f = GET_FUNC_PTR(CloseNativeLibrary);
65 return f(handle, needs_native_bridge, error_msg);
66}
67
68void NativeLoaderFreeErrorMessage(char* msg) {
69 static auto f = GET_FUNC_PTR(NativeLoaderFreeErrorMessage);
70 return f(msg);
71}
72
73struct android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
74 static auto f = GET_FUNC_PTR(FindNamespaceByClassLoader);
75 return f(env, class_loader);
76}
77
78struct NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env,
79 jobject class_loader) {
80 static auto f = GET_FUNC_PTR(FindNativeLoaderNamespaceByClassLoader);
81 return f(env, class_loader);
82}
83
84void* OpenNativeLibraryInNamespace(struct NativeLoaderNamespace* ns, const char* path,
85 bool* needs_native_bridge, char** error_msg) {
86 static auto f = GET_FUNC_PTR(OpenNativeLibraryInNamespace);
87 return f(ns, path, needs_native_bridge, error_msg);
88}
89
Orion Hodson9b16e342019-10-09 13:29:16 +010090#undef GET_FUNC_PTR
91
92} // namespace android