blob: 2205f453b319dc8a3b1decb4d34a7b97b1f37b46 [file] [log] [blame]
Calin Juravle961ae122014-08-11 16:11:59 +01001/*
2 * Copyright (C) 2014 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 "nativebridge/native_bridge.h"
18
Andreas Gampe049249c2014-08-19 22:31:31 -070019#include <cutils/log.h>
Calin Juravle961ae122014-08-11 16:11:59 +010020#include <dlfcn.h>
21#include <stdio.h>
22#include "utils/Mutex.h"
23
24
25namespace android {
26
27static Mutex native_bridge_lock("native bridge lock");
28
29// The symbol name exposed by native-bridge with the type of NativeBridgeCallbacks.
30static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf";
31
Andreas Gampe049249c2014-08-19 22:31:31 -070032// The filename of the library we are supposed to load.
33static const char* native_bridge_library_filename = nullptr;
Calin Juravle961ae122014-08-11 16:11:59 +010034
35// Whether a native bridge is available (loaded and ready).
36static bool available = false;
37// Whether we have already initialized (or tried to).
38static bool initialized = false;
Andreas Gampe049249c2014-08-19 22:31:31 -070039// Whether we had an error at some point.
40static bool had_error = false;
Calin Juravle961ae122014-08-11 16:11:59 +010041
42static NativeBridgeCallbacks* callbacks = nullptr;
43static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr;
44
Andreas Gampe049249c2014-08-19 22:31:31 -070045// Characters allowed in a native bridge filename. The first character must
46// be in [a-zA-Z] (expected 'l' for "libx"). The rest must be in [a-zA-Z0-9._-].
47static bool CharacterAllowed(char c, bool first) {
48 if (first) {
49 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
50 } else {
51 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') ||
52 (c == '.') || (c == '_') || (c == '-');
53 }
54}
55
56// We only allow simple names for the library. It is supposed to be a file in
57// /system/lib or /vendor/lib. Only allow a small range of characters, that is
58// names consisting of [a-zA-Z0-9._-] and starting with [a-zA-Z].
59bool NativeBridgeNameAcceptable(const char* nb_library_filename) {
60 const char* ptr = nb_library_filename;
61 if (*ptr == 0) {
62 // Emptry string. Allowed, means no native bridge.
63 return true;
64 } else {
65 // First character must be [a-zA-Z].
66 if (!CharacterAllowed(*ptr, true)) {
67 // Found an invalid fist character, don't accept.
68 ALOGE("Native bridge library %s has been rejected for first character %c", nb_library_filename, *ptr);
69 return false;
70 } else {
71 // For the rest, be more liberal.
72 ptr++;
73 while (*ptr != 0) {
74 if (!CharacterAllowed(*ptr, false)) {
75 // Found an invalid character, don't accept.
76 ALOGE("Native bridge library %s has been rejected for %c", nb_library_filename, *ptr);
77 return false;
78 }
79 ptr++;
80 }
81 }
82 return true;
83 }
84}
85
86void SetupNativeBridge(const char* nb_library_filename,
Calin Juravle961ae122014-08-11 16:11:59 +010087 const NativeBridgeRuntimeCallbacks* runtime_cbs) {
88 Mutex::Autolock auto_lock(native_bridge_lock);
89
Andreas Gampe049249c2014-08-19 22:31:31 -070090 if (initialized || native_bridge_library_filename != nullptr) {
91 // Setup has been called before. Ignore this call.
92 ALOGW("Called SetupNativeBridge for an already set up native bridge.");
93 // Note: counts as an error, even though the bridge may be functional.
94 had_error = true;
95 return;
96 }
97
Calin Juravle961ae122014-08-11 16:11:59 +010098 runtime_callbacks = runtime_cbs;
99
Andreas Gampe049249c2014-08-19 22:31:31 -0700100 if (nb_library_filename == nullptr) {
Calin Juravle961ae122014-08-11 16:11:59 +0100101 available = false;
Andreas Gampe049249c2014-08-19 22:31:31 -0700102 initialized = true;
103 } else {
104 // Check whether it's an empty string.
105 if (*nb_library_filename == 0) {
106 available = false;
107 initialized = true;
108 } else if (!NativeBridgeNameAcceptable(nb_library_filename)) {
109 available = false;
110 initialized = true;
111 had_error = true;
112 }
113
114 if (!initialized) {
115 // Didn't find a name error or empty string, assign it.
116 native_bridge_library_filename = nb_library_filename;
117 }
Calin Juravle961ae122014-08-11 16:11:59 +0100118 }
119}
120
121static bool NativeBridgeInitialize() {
122 Mutex::Autolock auto_lock(native_bridge_lock);
123
124 if (initialized) {
125 // Somebody did it before.
126 return available;
127 }
128
129 available = false;
130
Andreas Gampe049249c2014-08-19 22:31:31 -0700131 if (native_bridge_library_filename == nullptr) {
132 // Called initialize without setup. dlopen has special semantics for nullptr input.
133 // So just call it a day here. This counts as an error.
134 initialized = true;
135 had_error = true;
136 return false;
137 }
138
139 void* handle = dlopen(native_bridge_library_filename, RTLD_LAZY);
Calin Juravle961ae122014-08-11 16:11:59 +0100140 if (handle != nullptr) {
141 callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
142 kNativeBridgeInterfaceSymbol));
143
144 if (callbacks != nullptr) {
145 available = callbacks->initialize(runtime_callbacks);
146 }
147
148 if (!available) {
Andreas Gampe049249c2014-08-19 22:31:31 -0700149 // If we fail initialization, this counts as an error.
150 had_error = true;
Calin Juravle961ae122014-08-11 16:11:59 +0100151 dlclose(handle);
152 }
Andreas Gampe049249c2014-08-19 22:31:31 -0700153 } else {
154 // Being unable to open the library counts as an error.
155 had_error = true;
Calin Juravle961ae122014-08-11 16:11:59 +0100156 }
157
158 initialized = true;
159
160 return available;
161}
162
Andreas Gampe049249c2014-08-19 22:31:31 -0700163bool NativeBridgeError() {
164 return had_error;
165}
166
167bool NativeBridgeAvailable() {
168 return NativeBridgeInitialize();
169}
170
Calin Juravle961ae122014-08-11 16:11:59 +0100171void* NativeBridgeLoadLibrary(const char* libpath, int flag) {
172 if (NativeBridgeInitialize()) {
173 return callbacks->loadLibrary(libpath, flag);
174 }
175 return nullptr;
176}
177
178void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty,
179 uint32_t len) {
180 if (NativeBridgeInitialize()) {
181 return callbacks->getTrampoline(handle, name, shorty, len);
182 }
183 return nullptr;
184}
185
186bool NativeBridgeIsSupported(const char* libpath) {
187 if (NativeBridgeInitialize()) {
188 return callbacks->isSupported(libpath);
189 }
190 return false;
191}
192
193}; // namespace android