Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 19 | #include <cutils/log.h> |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 20 | #include <dlfcn.h> |
| 21 | #include <stdio.h> |
| 22 | #include "utils/Mutex.h" |
| 23 | |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | static Mutex native_bridge_lock("native bridge lock"); |
| 28 | |
| 29 | // The symbol name exposed by native-bridge with the type of NativeBridgeCallbacks. |
| 30 | static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf"; |
| 31 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 32 | // The filename of the library we are supposed to load. |
| 33 | static const char* native_bridge_library_filename = nullptr; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 34 | |
| 35 | // Whether a native bridge is available (loaded and ready). |
| 36 | static bool available = false; |
| 37 | // Whether we have already initialized (or tried to). |
| 38 | static bool initialized = false; |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 39 | // Whether we had an error at some point. |
| 40 | static bool had_error = false; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 41 | |
| 42 | static NativeBridgeCallbacks* callbacks = nullptr; |
| 43 | static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr; |
| 44 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 45 | // 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._-]. |
| 47 | static 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]. |
| 59 | bool 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 | |
| 86 | void SetupNativeBridge(const char* nb_library_filename, |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 87 | const NativeBridgeRuntimeCallbacks* runtime_cbs) { |
| 88 | Mutex::Autolock auto_lock(native_bridge_lock); |
| 89 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 90 | 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 Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 98 | runtime_callbacks = runtime_cbs; |
| 99 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 100 | if (nb_library_filename == nullptr) { |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 101 | available = false; |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 102 | 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 Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | static 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 Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 131 | 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 Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 140 | 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 Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 149 | // If we fail initialization, this counts as an error. |
| 150 | had_error = true; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 151 | dlclose(handle); |
| 152 | } |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 153 | } else { |
| 154 | // Being unable to open the library counts as an error. |
| 155 | had_error = true; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | initialized = true; |
| 159 | |
| 160 | return available; |
| 161 | } |
| 162 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 163 | bool NativeBridgeError() { |
| 164 | return had_error; |
| 165 | } |
| 166 | |
| 167 | bool NativeBridgeAvailable() { |
| 168 | return NativeBridgeInitialize(); |
| 169 | } |
| 170 | |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 171 | void* NativeBridgeLoadLibrary(const char* libpath, int flag) { |
| 172 | if (NativeBridgeInitialize()) { |
| 173 | return callbacks->loadLibrary(libpath, flag); |
| 174 | } |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
| 178 | void* 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 | |
| 186 | bool NativeBridgeIsSupported(const char* libpath) { |
| 187 | if (NativeBridgeInitialize()) { |
| 188 | return callbacks->isSupported(libpath); |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | }; // namespace android |