The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Native method resolution. |
| 19 | * |
| 20 | * Currently the "Dalvik native" methods are only used for internal methods. |
| 21 | * Someday we may want to export the interface as a faster but riskier |
| 22 | * alternative to JNI. |
| 23 | */ |
| 24 | #include "Dalvik.h" |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <dlfcn.h> |
| 28 | |
| 29 | static void freeSharedLibEntry(void* ptr); |
| 30 | static void* lookupSharedLibMethod(const Method* method); |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * Initialize the native code loader. |
| 35 | */ |
Carl Shapiro | 1e1433e | 2011-04-20 16:51:38 -0700 | [diff] [blame] | 36 | bool dvmNativeStartup() |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | { |
| 38 | gDvm.nativeLibs = dvmHashTableCreate(4, freeSharedLibEntry); |
| 39 | if (gDvm.nativeLibs == NULL) |
| 40 | return false; |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Free up our tables. |
| 47 | */ |
Carl Shapiro | 1e1433e | 2011-04-20 16:51:38 -0700 | [diff] [blame] | 48 | void dvmNativeShutdown() |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 49 | { |
| 50 | dvmHashTableFree(gDvm.nativeLibs); |
| 51 | gDvm.nativeLibs = NULL; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /* |
| 56 | * Resolve a native method and invoke it. |
| 57 | * |
| 58 | * This is executed as if it were a native bridge or function. If the |
| 59 | * resolution succeeds, method->insns is replaced, and we don't go through |
Andy McFadden | 1e83b4d | 2010-07-15 17:20:24 -0700 | [diff] [blame] | 60 | * here again unless the method is unregistered. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 61 | * |
| 62 | * Initializes method's class if necessary. |
| 63 | * |
| 64 | * An exception is thrown on resolution failure. |
Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 65 | * |
| 66 | * (This should not be taking "const Method*", because it modifies the |
| 67 | * structure, but the declaration needs to match the DalvikBridgeFunc |
| 68 | * type definition.) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 69 | */ |
| 70 | void dvmResolveNativeMethod(const u4* args, JValue* pResult, |
| 71 | const Method* method, Thread* self) |
| 72 | { |
| 73 | ClassObject* clazz = method->clazz; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * If this is a static method, it could be called before the class |
| 77 | * has been initialized. |
| 78 | */ |
| 79 | if (dvmIsStaticMethod(method)) { |
| 80 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) { |
| 81 | assert(dvmCheckException(dvmThreadSelf())); |
| 82 | return; |
| 83 | } |
| 84 | } else { |
| 85 | assert(dvmIsClassInitialized(clazz) || |
| 86 | dvmIsClassInitializing(clazz)); |
| 87 | } |
| 88 | |
| 89 | /* start with our internal-native methods */ |
Carl Shapiro | d5c36b9 | 2011-04-15 18:38:06 -0700 | [diff] [blame] | 90 | DalvikNativeFunc infunc = dvmLookupInternalNativeMethod(method); |
| 91 | if (infunc != NULL) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 92 | /* resolution always gets the same answer, so no race here */ |
| 93 | IF_LOGVV() { |
| 94 | char* desc = dexProtoCopyMethodDescriptor(&method->prototype); |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 95 | LOGVV("+++ resolved native %s.%s %s, invoking", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 96 | clazz->descriptor, method->name, desc); |
| 97 | free(desc); |
| 98 | } |
| 99 | if (dvmIsSynchronizedMethod(method)) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 100 | LOGE("ERROR: internal-native can't be declared 'synchronized'"); |
| 101 | LOGE("Failing on %s.%s", method->clazz->descriptor, method->name); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | dvmAbort(); // harsh, but this is VM-internal problem |
| 103 | } |
Carl Shapiro | d5c36b9 | 2011-04-15 18:38:06 -0700 | [diff] [blame] | 104 | DalvikBridgeFunc dfunc = (DalvikBridgeFunc) infunc; |
Andy McFadden | 1e83b4d | 2010-07-15 17:20:24 -0700 | [diff] [blame] | 105 | dvmSetNativeFunc((Method*) method, dfunc, NULL); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 106 | dfunc(args, pResult, method, self); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | /* now scan any DLLs we have loaded for JNI signatures */ |
Carl Shapiro | d5c36b9 | 2011-04-15 18:38:06 -0700 | [diff] [blame] | 111 | void* func = lookupSharedLibMethod(method); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 112 | if (func != NULL) { |
Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 113 | /* found it, point it at the JNI bridge and then call it */ |
| 114 | dvmUseJNIBridge((Method*) method, func); |
Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame] | 115 | (*method->nativeFunc)(args, pResult, method, self); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
| 119 | IF_LOGW() { |
| 120 | char* desc = dexProtoCopyMethodDescriptor(&method->prototype); |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 121 | LOGW("No implementation found for native %s.%s %s", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 122 | clazz->descriptor, method->name, desc); |
| 123 | free(desc); |
| 124 | } |
| 125 | |
Dan Bornstein | 70b00ab | 2011-02-23 14:11:27 -0800 | [diff] [blame] | 126 | dvmThrowUnsatisfiedLinkError(method->name); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
| 130 | /* |
| 131 | * =========================================================================== |
| 132 | * Native shared library support |
| 133 | * =========================================================================== |
| 134 | */ |
| 135 | |
| 136 | // TODO? if a ClassLoader is unloaded, we need to unload all DLLs that |
| 137 | // are associated with it. (Or not -- can't determine if native code |
| 138 | // is still using parts of it.) |
| 139 | |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 140 | enum OnLoadState { |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 141 | kOnLoadPending = 0, /* initial state, must be zero */ |
| 142 | kOnLoadFailed, |
| 143 | kOnLoadOkay, |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 144 | }; |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 145 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 146 | /* |
| 147 | * We add one of these to the hash table for every library we load. The |
| 148 | * hash is on the "pathName" field. |
| 149 | */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 150 | struct SharedLib { |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 151 | char* pathName; /* absolute path to library */ |
| 152 | void* handle; /* from dlopen */ |
| 153 | Object* classLoader; /* ClassLoader we are associated with */ |
| 154 | |
| 155 | pthread_mutex_t onLoadLock; /* guards remaining items */ |
| 156 | pthread_cond_t onLoadCond; /* wait for JNI_OnLoad in other thread */ |
| 157 | u4 onLoadThreadId; /* recursive invocation guard */ |
| 158 | OnLoadState onLoadResult; /* result of earlier JNI_OnLoad */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 159 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * (This is a dvmHashTableLookup callback.) |
| 163 | * |
| 164 | * Find an entry that matches the string. |
| 165 | */ |
| 166 | static int hashcmpNameStr(const void* ventry, const void* vname) |
| 167 | { |
| 168 | const SharedLib* pLib = (const SharedLib*) ventry; |
| 169 | const char* name = (const char*) vname; |
| 170 | |
| 171 | return strcmp(pLib->pathName, name); |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * (This is a dvmHashTableLookup callback.) |
| 176 | * |
| 177 | * Find an entry that matches the new entry. |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 178 | * |
| 179 | * We don't compare the class loader here, because you're not allowed to |
| 180 | * have the same shared library associated with more than one CL. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 181 | */ |
| 182 | static int hashcmpSharedLib(const void* ventry, const void* vnewEntry) |
| 183 | { |
| 184 | const SharedLib* pLib = (const SharedLib*) ventry; |
| 185 | const SharedLib* pNewLib = (const SharedLib*) vnewEntry; |
| 186 | |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 187 | LOGD("--- comparing %p '%s' %p '%s'", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 188 | pLib, pLib->pathName, pNewLib, pNewLib->pathName); |
| 189 | return strcmp(pLib->pathName, pNewLib->pathName); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Check to see if an entry with the same pathname already exists. |
| 194 | */ |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 195 | static SharedLib* findSharedLibEntry(const char* pathName) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 196 | { |
| 197 | u4 hash = dvmComputeUtf8Hash(pathName); |
| 198 | void* ent; |
| 199 | |
| 200 | ent = dvmHashTableLookup(gDvm.nativeLibs, hash, (void*)pathName, |
| 201 | hashcmpNameStr, false); |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 202 | return (SharedLib*)ent; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Add the new entry to the table. |
| 207 | * |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 208 | * Returns the table entry, which will not be the same as "pLib" if the |
| 209 | * entry already exists. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 210 | */ |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 211 | static SharedLib* addSharedLibEntry(SharedLib* pLib) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 212 | { |
| 213 | u4 hash = dvmComputeUtf8Hash(pLib->pathName); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * Do the lookup with the "add" flag set. If we add it, we will get |
| 217 | * our own pointer back. If somebody beat us to the punch, we'll get |
| 218 | * their pointer back instead. |
| 219 | */ |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 220 | return (SharedLib*)dvmHashTableLookup(gDvm.nativeLibs, hash, pLib, |
| 221 | hashcmpSharedLib, true); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Free up an entry. (This is a dvmHashTableFree callback.) |
| 226 | */ |
| 227 | static void freeSharedLibEntry(void* ptr) |
| 228 | { |
| 229 | SharedLib* pLib = (SharedLib*) ptr; |
| 230 | |
| 231 | /* |
| 232 | * Calling dlclose() here is somewhat dangerous, because it's possible |
| 233 | * that a thread outside the VM is still accessing the code we loaded. |
| 234 | */ |
| 235 | if (false) |
| 236 | dlclose(pLib->handle); |
| 237 | free(pLib->pathName); |
| 238 | free(pLib); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Convert library name to system-dependent form, e.g. "jpeg" becomes |
| 243 | * "libjpeg.so". |
| 244 | * |
| 245 | * (Should we have this take buffer+len and avoid the alloc? It gets |
| 246 | * called very rarely.) |
| 247 | */ |
| 248 | char* dvmCreateSystemLibraryName(char* libName) |
| 249 | { |
| 250 | char buf[256]; |
| 251 | int len; |
| 252 | |
| 253 | len = snprintf(buf, sizeof(buf), OS_SHARED_LIB_FORMAT_STR, libName); |
| 254 | if (len >= (int) sizeof(buf)) |
| 255 | return NULL; |
| 256 | else |
| 257 | return strdup(buf); |
| 258 | } |
| 259 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 260 | /* |
| 261 | * Check the result of an earlier call to JNI_OnLoad on this library. If |
| 262 | * the call has not yet finished in another thread, wait for it. |
| 263 | */ |
| 264 | static bool checkOnLoadResult(SharedLib* pEntry) |
| 265 | { |
| 266 | Thread* self = dvmThreadSelf(); |
| 267 | if (pEntry->onLoadThreadId == self->threadId) { |
| 268 | /* |
| 269 | * Check this so we don't end up waiting for ourselves. We need |
| 270 | * to return "true" so the caller can continue. |
| 271 | */ |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 272 | LOGI("threadid=%d: recursive native library load attempt (%s)", |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 273 | self->threadId, pEntry->pathName); |
| 274 | return true; |
| 275 | } |
| 276 | |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 277 | LOGV("+++ retrieving %s OnLoad status", pEntry->pathName); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 278 | bool result; |
| 279 | |
| 280 | dvmLockMutex(&pEntry->onLoadLock); |
| 281 | while (pEntry->onLoadResult == kOnLoadPending) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 282 | LOGD("threadid=%d: waiting for %s OnLoad status", |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 283 | self->threadId, pEntry->pathName); |
Carl Shapiro | 5617ad3 | 2010-07-02 10:50:57 -0700 | [diff] [blame] | 284 | ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 285 | pthread_cond_wait(&pEntry->onLoadCond, &pEntry->onLoadLock); |
| 286 | dvmChangeStatus(self, oldStatus); |
| 287 | } |
| 288 | if (pEntry->onLoadResult == kOnLoadOkay) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 289 | LOGV("+++ earlier OnLoad(%s) okay", pEntry->pathName); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 290 | result = true; |
| 291 | } else { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 292 | LOGV("+++ earlier OnLoad(%s) failed", pEntry->pathName); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 293 | result = false; |
| 294 | } |
| 295 | dvmUnlockMutex(&pEntry->onLoadLock); |
| 296 | return result; |
| 297 | } |
| 298 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 299 | typedef int (*OnLoadFunc)(JavaVM*, void*); |
| 300 | |
| 301 | /* |
| 302 | * Load native code from the specified absolute pathname. Per the spec, |
| 303 | * if we've already loaded a library with the specified pathname, we |
| 304 | * return without doing anything. |
| 305 | * |
| 306 | * TODO? for better results we should absolutify the pathname. For fully |
| 307 | * correct results we should stat to get the inode and compare that. The |
| 308 | * existing implementation is fine so long as everybody is using |
| 309 | * System.loadLibrary. |
| 310 | * |
| 311 | * The library will be associated with the specified class loader. The JNI |
| 312 | * spec says we can't load the same library into more than one class loader. |
| 313 | * |
Elliott Hughes | f584b4a | 2010-09-30 15:51:31 -0700 | [diff] [blame] | 314 | * Returns "true" on success. On failure, sets *detail to a |
| 315 | * human-readable description of the error or NULL if no detail is |
| 316 | * available; ownership of the string is transferred to the caller. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 317 | */ |
Elliott Hughes | f584b4a | 2010-09-30 15:51:31 -0700 | [diff] [blame] | 318 | bool dvmLoadNativeCode(const char* pathName, Object* classLoader, |
| 319 | char** detail) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 320 | { |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 321 | SharedLib* pEntry; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 322 | void* handle; |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 323 | bool verbose; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 324 | |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 325 | /* reduce noise by not chattering about system libraries */ |
Dima Zavin | b14f405 | 2010-09-23 22:38:45 -0700 | [diff] [blame] | 326 | verbose = !!strncmp(pathName, "/system", sizeof("/system")-1); |
| 327 | verbose = verbose && !!strncmp(pathName, "/vendor", sizeof("/vendor")-1); |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 328 | |
| 329 | if (verbose) |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 330 | LOGD("Trying to load lib %s %p", pathName, classLoader); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 331 | |
Elliott Hughes | f584b4a | 2010-09-30 15:51:31 -0700 | [diff] [blame] | 332 | *detail = NULL; |
| 333 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 334 | /* |
| 335 | * See if we've already loaded it. If we have, and the class loader |
| 336 | * matches, return successfully without doing anything. |
| 337 | */ |
| 338 | pEntry = findSharedLibEntry(pathName); |
| 339 | if (pEntry != NULL) { |
| 340 | if (pEntry->classLoader != classLoader) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 341 | LOGW("Shared lib '%s' already opened by CL %p; can't open in %p", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 342 | pathName, pEntry->classLoader, classLoader); |
| 343 | return false; |
| 344 | } |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 345 | if (verbose) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 346 | LOGD("Shared lib '%s' already loaded in same CL %p", |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 347 | pathName, classLoader); |
| 348 | } |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 349 | if (!checkOnLoadResult(pEntry)) |
| 350 | return false; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 351 | return true; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Open the shared library. Because we're using a full path, the system |
| 356 | * doesn't have to search through LD_LIBRARY_PATH. (It may do so to |
| 357 | * resolve this library's dependencies though.) |
| 358 | * |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 359 | * Failures here are expected when java.library.path has several entries |
| 360 | * and we have to hunt for the lib. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 361 | * |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 362 | * The current version of the dynamic linker prints detailed information |
| 363 | * about dlopen() failures. Some things to check if the message is |
| 364 | * cryptic: |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 365 | * - make sure the library exists on the device |
| 366 | * - verify that the right path is being opened (the debug log message |
| 367 | * above can help with that) |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 368 | * - check to see if the library is valid (e.g. not zero bytes long) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 369 | * - check config/prelink-linux-arm.map to ensure that the library |
| 370 | * is listed and is not being overrun by the previous entry (if |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 371 | * loading suddenly stops working on a prelinked library, this is |
| 372 | * a good one to check) |
| 373 | * - write a trivial app that calls sleep() then dlopen(), attach |
| 374 | * to it with "strace -p <pid>" while it sleeps, and watch for |
| 375 | * attempts to open nonexistent dependent shared libs |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 376 | * |
| 377 | * This can execute slowly for a large library on a busy system, so we |
| 378 | * want to switch from RUNNING to VMWAIT while it executes. This allows |
| 379 | * the GC to ignore us. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 380 | */ |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 381 | Thread* self = dvmThreadSelf(); |
Carl Shapiro | 5617ad3 | 2010-07-02 10:50:57 -0700 | [diff] [blame] | 382 | ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 383 | handle = dlopen(pathName, RTLD_LAZY); |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 384 | dvmChangeStatus(self, oldStatus); |
| 385 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 386 | if (handle == NULL) { |
Elliott Hughes | f584b4a | 2010-09-30 15:51:31 -0700 | [diff] [blame] | 387 | *detail = strdup(dlerror()); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 391 | /* create a new entry */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 392 | SharedLib* pNewEntry; |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 393 | pNewEntry = (SharedLib*) calloc(1, sizeof(SharedLib)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 394 | pNewEntry->pathName = strdup(pathName); |
| 395 | pNewEntry->handle = handle; |
| 396 | pNewEntry->classLoader = classLoader; |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 397 | dvmInitMutex(&pNewEntry->onLoadLock); |
| 398 | pthread_cond_init(&pNewEntry->onLoadCond, NULL); |
| 399 | pNewEntry->onLoadThreadId = self->threadId; |
| 400 | |
| 401 | /* try to add it to the list */ |
| 402 | SharedLib* pActualEntry = addSharedLibEntry(pNewEntry); |
| 403 | |
| 404 | if (pNewEntry != pActualEntry) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 405 | LOGI("WOW: we lost a race to add a shared lib (%s CL=%p)", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 406 | pathName, classLoader); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 407 | freeSharedLibEntry(pNewEntry); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 408 | return checkOnLoadResult(pActualEntry); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 409 | } else { |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 410 | if (verbose) |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 411 | LOGD("Added shared lib %s %p", pathName, classLoader); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 412 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 413 | bool result = true; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 414 | void* vonLoad; |
| 415 | int version; |
| 416 | |
| 417 | vonLoad = dlsym(handle, "JNI_OnLoad"); |
| 418 | if (vonLoad == NULL) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 419 | LOGD("No JNI_OnLoad found in %s %p, skipping init", |
Andy McFadden | dced794 | 2009-11-17 13:13:34 -0800 | [diff] [blame] | 420 | pathName, classLoader); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 421 | } else { |
| 422 | /* |
| 423 | * Call JNI_OnLoad. We have to override the current class |
| 424 | * loader, which will always be "null" since the stuff at the |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 425 | * top of the stack is around Runtime.loadLibrary(). (See |
| 426 | * the comments in the JNI FindClass function.) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 427 | */ |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 428 | OnLoadFunc func = (OnLoadFunc)vonLoad; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 429 | Object* prevOverride = self->classLoaderOverride; |
| 430 | |
| 431 | self->classLoaderOverride = classLoader; |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 432 | oldStatus = dvmChangeStatus(self, THREAD_NATIVE); |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 433 | LOGV("+++ calling JNI_OnLoad(%s)", pathName); |
Elliott Hughes | d5c80e0 | 2011-04-27 12:23:43 -0700 | [diff] [blame] | 434 | version = (*func)(gDvmJni.jniVm, NULL); |
Andy McFadden | 2aa4361 | 2009-06-17 16:29:30 -0700 | [diff] [blame] | 435 | dvmChangeStatus(self, oldStatus); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 436 | self->classLoaderOverride = prevOverride; |
| 437 | |
| 438 | if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && |
| 439 | version != JNI_VERSION_1_6) |
| 440 | { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 441 | LOGW("JNI_OnLoad returned bad version (%d) in %s %p", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 442 | version, pathName, classLoader); |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 443 | /* |
| 444 | * It's unwise to call dlclose() here, but we can mark it |
| 445 | * as bad and ensure that future load attempts will fail. |
| 446 | * |
| 447 | * We don't know how far JNI_OnLoad got, so there could |
| 448 | * be some partially-initialized stuff accessible through |
| 449 | * newly-registered native method calls. We could try to |
| 450 | * unregister them, but that doesn't seem worthwhile. |
| 451 | */ |
| 452 | result = false; |
| 453 | } else { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 454 | LOGV("+++ finished JNI_OnLoad %s", pathName); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 455 | } |
| 456 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 457 | |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 458 | if (result) |
| 459 | pNewEntry->onLoadResult = kOnLoadOkay; |
| 460 | else |
| 461 | pNewEntry->onLoadResult = kOnLoadFailed; |
| 462 | |
| 463 | pNewEntry->onLoadThreadId = 0; |
| 464 | |
| 465 | /* |
| 466 | * Broadcast a wakeup to anybody sleeping on the condition variable. |
| 467 | */ |
| 468 | dvmLockMutex(&pNewEntry->onLoadLock); |
| 469 | pthread_cond_broadcast(&pNewEntry->onLoadCond); |
| 470 | dvmUnlockMutex(&pNewEntry->onLoadLock); |
| 471 | return result; |
| 472 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | |
| 476 | /* |
Andy McFadden | 1e83b4d | 2010-07-15 17:20:24 -0700 | [diff] [blame] | 477 | * Un-register JNI native methods. |
| 478 | * |
| 479 | * There are two relevant fields in struct Method, "nativeFunc" and |
| 480 | * "insns". The former holds a function pointer to a "bridge" function |
| 481 | * (or, for internal native, the actual implementation). The latter holds |
| 482 | * a pointer to the actual JNI method. |
| 483 | * |
| 484 | * The obvious approach is to reset both fields to their initial state |
| 485 | * (nativeFunc points at dvmResolveNativeMethod, insns holds NULL), but |
| 486 | * that creates some unpleasant race conditions. In particular, if another |
| 487 | * thread is executing inside the call bridge for the method in question, |
| 488 | * and we reset insns to NULL, the VM will crash. (See the comments above |
| 489 | * dvmSetNativeFunc() for additional commentary.) |
| 490 | * |
| 491 | * We can't rely on being able to update two 32-bit fields in one atomic |
| 492 | * operation (e.g. no 64-bit atomic ops on ARMv5TE), so we want to change |
| 493 | * only one field. It turns out we can simply reset nativeFunc to its |
| 494 | * initial state, leaving insns alone, because dvmResolveNativeMethod |
| 495 | * ignores "insns" entirely. |
| 496 | * |
| 497 | * When the method is re-registered, both fields will be updated, but |
| 498 | * dvmSetNativeFunc guarantees that "insns" is updated first. This means |
| 499 | * we shouldn't be in a situation where we have a "live" call bridge and |
| 500 | * a stale implementation pointer. |
| 501 | */ |
| 502 | static void unregisterJNINativeMethods(Method* methods, size_t count) |
| 503 | { |
| 504 | while (count != 0) { |
| 505 | count--; |
| 506 | |
| 507 | Method* meth = &methods[count]; |
| 508 | if (!dvmIsNativeMethod(meth)) |
| 509 | continue; |
| 510 | if (dvmIsAbstractMethod(meth)) /* avoid abstract method stubs */ |
| 511 | continue; |
| 512 | |
| 513 | /* |
| 514 | * Strictly speaking this ought to test the function pointer against |
| 515 | * the various JNI bridge functions to ensure that we only undo |
| 516 | * methods that were registered through JNI. In practice, any |
| 517 | * native method with a non-NULL "insns" is a registered JNI method. |
| 518 | * |
| 519 | * If we inadvertently unregister an internal-native, it'll get |
| 520 | * re-resolved on the next call; unregistering an unregistered |
| 521 | * JNI method is a no-op. So we don't really need to test for |
| 522 | * anything. |
| 523 | */ |
| 524 | |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 525 | LOGD("Unregistering JNI method %s.%s:%s", |
Andy McFadden | 1e83b4d | 2010-07-15 17:20:24 -0700 | [diff] [blame] | 526 | meth->clazz->descriptor, meth->name, meth->shorty); |
| 527 | dvmSetNativeFunc(meth, dvmResolveNativeMethod, NULL); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Un-register all JNI native methods from a class. |
| 533 | */ |
| 534 | void dvmUnregisterJNINativeMethods(ClassObject* clazz) |
| 535 | { |
| 536 | unregisterJNINativeMethods(clazz->directMethods, clazz->directMethodCount); |
| 537 | unregisterJNINativeMethods(clazz->virtualMethods, clazz->virtualMethodCount); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 542 | * =========================================================================== |
| 543 | * Signature-based method lookup |
| 544 | * =========================================================================== |
| 545 | */ |
| 546 | |
| 547 | /* |
| 548 | * Create the pre-mangled form of the class+method string. |
| 549 | * |
| 550 | * Returns a newly-allocated string, and sets "*pLen" to the length. |
| 551 | */ |
| 552 | static char* createJniNameString(const char* classDescriptor, |
| 553 | const char* methodName, int* pLen) |
| 554 | { |
| 555 | char* result; |
| 556 | size_t descriptorLength = strlen(classDescriptor); |
| 557 | |
| 558 | *pLen = 4 + descriptorLength + strlen(methodName); |
| 559 | |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 560 | result = (char*)malloc(*pLen +1); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 561 | if (result == NULL) |
| 562 | return NULL; |
| 563 | |
| 564 | /* |
| 565 | * Add one to classDescriptor to skip the "L", and then replace |
| 566 | * the final ";" with a "/" after the sprintf() call. |
| 567 | */ |
| 568 | sprintf(result, "Java/%s%s", classDescriptor + 1, methodName); |
| 569 | result[5 + (descriptorLength - 2)] = '/'; |
| 570 | |
| 571 | return result; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Returns a newly-allocated, mangled copy of "str". |
| 576 | * |
| 577 | * "str" is a "modified UTF-8" string. We convert it to UTF-16 first to |
| 578 | * make life simpler. |
| 579 | */ |
| 580 | static char* mangleString(const char* str, int len) |
| 581 | { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 582 | //LOGI("mangling '%s' %d", str, len); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 583 | |
| 584 | assert(str[len] == '\0'); |
| 585 | |
Carl Shapiro | d5c36b9 | 2011-04-15 18:38:06 -0700 | [diff] [blame] | 586 | size_t charLen = dvmUtf8Len(str); |
| 587 | u2* utf16 = (u2*) malloc(sizeof(u2) * charLen); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 588 | if (utf16 == NULL) |
Carl Shapiro | d5c36b9 | 2011-04-15 18:38:06 -0700 | [diff] [blame] | 589 | return NULL; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 590 | |
| 591 | dvmConvertUtf8ToUtf16(utf16, str); |
| 592 | |
| 593 | /* |
| 594 | * Compute the length of the mangled string. |
| 595 | */ |
Carl Shapiro | d5c36b9 | 2011-04-15 18:38:06 -0700 | [diff] [blame] | 596 | size_t mangleLen = 0; |
| 597 | for (size_t i = 0; i < charLen; i++) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 598 | u2 ch = utf16[i]; |
| 599 | |
Brian McKenna | dfdaa87 | 2009-07-19 20:49:26 +1000 | [diff] [blame] | 600 | if (ch == '$' || ch > 127) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 601 | mangleLen += 6; |
| 602 | } else { |
| 603 | switch (ch) { |
| 604 | case '_': |
| 605 | case ';': |
| 606 | case '[': |
| 607 | mangleLen += 2; |
| 608 | break; |
| 609 | default: |
| 610 | mangleLen++; |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
Carl Shapiro | d5c36b9 | 2011-04-15 18:38:06 -0700 | [diff] [blame] | 616 | char* mangle = (char*) malloc(mangleLen +1); |
| 617 | if (mangle == NULL) { |
| 618 | free(utf16); |
| 619 | return NULL; |
| 620 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 621 | |
Carl Shapiro | d5c36b9 | 2011-04-15 18:38:06 -0700 | [diff] [blame] | 622 | char* cp = mangle; |
| 623 | for (size_t i = 0; i < charLen; i++) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 624 | u2 ch = utf16[i]; |
| 625 | |
Brian McKenna | dfdaa87 | 2009-07-19 20:49:26 +1000 | [diff] [blame] | 626 | if (ch == '$' || ch > 127) { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 627 | sprintf(cp, "_0%04x", ch); |
| 628 | cp += 6; |
| 629 | } else { |
| 630 | switch (ch) { |
| 631 | case '_': |
| 632 | *cp++ = '_'; |
| 633 | *cp++ = '1'; |
| 634 | break; |
| 635 | case ';': |
| 636 | *cp++ = '_'; |
| 637 | *cp++ = '2'; |
| 638 | break; |
| 639 | case '[': |
| 640 | *cp++ = '_'; |
| 641 | *cp++ = '3'; |
| 642 | break; |
| 643 | case '/': |
| 644 | *cp++ = '_'; |
| 645 | break; |
| 646 | default: |
| 647 | *cp++ = (char) ch; |
| 648 | break; |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | *cp = '\0'; |
| 654 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 655 | free(utf16); |
| 656 | return mangle; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Create the mangled form of the parameter types. |
| 661 | */ |
| 662 | static char* createMangledSignature(const DexProto* proto) |
| 663 | { |
| 664 | DexStringCache sigCache; |
| 665 | const char* interim; |
| 666 | char* result; |
| 667 | |
| 668 | dexStringCacheInit(&sigCache); |
| 669 | interim = dexProtoGetParameterDescriptors(proto, &sigCache); |
| 670 | result = mangleString(interim, strlen(interim)); |
| 671 | dexStringCacheRelease(&sigCache); |
| 672 | |
| 673 | return result; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * (This is a dvmHashForeach callback.) |
| 678 | * |
| 679 | * Search for a matching method in this shared library. |
Andy McFadden | 7031888 | 2009-07-09 17:01:04 -0700 | [diff] [blame] | 680 | * |
| 681 | * TODO: we may want to skip libraries for which JNI_OnLoad failed. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 682 | */ |
| 683 | static int findMethodInLib(void* vlib, void* vmethod) |
| 684 | { |
| 685 | const SharedLib* pLib = (const SharedLib*) vlib; |
| 686 | const Method* meth = (const Method*) vmethod; |
| 687 | char* preMangleCM = NULL; |
| 688 | char* mangleCM = NULL; |
| 689 | char* mangleSig = NULL; |
| 690 | char* mangleCMSig = NULL; |
| 691 | void* func = NULL; |
| 692 | int len; |
| 693 | |
| 694 | if (meth->clazz->classLoader != pLib->classLoader) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 695 | LOGV("+++ not scanning '%s' for '%s' (wrong CL)", |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 696 | pLib->pathName, meth->name); |
| 697 | return 0; |
| 698 | } else |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 699 | LOGV("+++ scanning '%s' for '%s'", pLib->pathName, meth->name); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | * First, we try it without the signature. |
| 703 | */ |
| 704 | preMangleCM = |
| 705 | createJniNameString(meth->clazz->descriptor, meth->name, &len); |
| 706 | if (preMangleCM == NULL) |
| 707 | goto bail; |
| 708 | |
| 709 | mangleCM = mangleString(preMangleCM, len); |
| 710 | if (mangleCM == NULL) |
| 711 | goto bail; |
| 712 | |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 713 | LOGV("+++ calling dlsym(%s)", mangleCM); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 714 | func = dlsym(pLib->handle, mangleCM); |
| 715 | if (func == NULL) { |
| 716 | mangleSig = |
| 717 | createMangledSignature(&meth->prototype); |
| 718 | if (mangleSig == NULL) |
| 719 | goto bail; |
| 720 | |
| 721 | mangleCMSig = (char*) malloc(strlen(mangleCM) + strlen(mangleSig) +3); |
| 722 | if (mangleCMSig == NULL) |
| 723 | goto bail; |
| 724 | |
| 725 | sprintf(mangleCMSig, "%s__%s", mangleCM, mangleSig); |
| 726 | |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 727 | LOGV("+++ calling dlsym(%s)", mangleCMSig); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 728 | func = dlsym(pLib->handle, mangleCMSig); |
| 729 | if (func != NULL) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 730 | LOGV("Found '%s' with dlsym", mangleCMSig); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 731 | } |
| 732 | } else { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 733 | LOGV("Found '%s' with dlsym", mangleCM); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | bail: |
| 737 | free(preMangleCM); |
| 738 | free(mangleCM); |
| 739 | free(mangleSig); |
| 740 | free(mangleCMSig); |
| 741 | return (int) func; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * See if the requested method lives in any of the currently-loaded |
| 746 | * shared libraries. We do this by checking each of them for the expected |
| 747 | * method signature. |
| 748 | */ |
| 749 | static void* lookupSharedLibMethod(const Method* method) |
| 750 | { |
| 751 | if (gDvm.nativeLibs == NULL) { |
Dan Bornstein | 60fc806 | 2011-05-26 10:11:58 -0700 | [diff] [blame^] | 752 | LOGE("Unexpected init state: nativeLibs not ready"); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 753 | dvmAbort(); |
| 754 | } |
| 755 | return (void*) dvmHashForeach(gDvm.nativeLibs, findMethodInLib, |
| 756 | (void*) method); |
| 757 | } |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 758 | |
| 759 | |
| 760 | static void appendValue(char type, const JValue value, char* buf, size_t n, |
| 761 | bool appendComma) |
| 762 | { |
| 763 | size_t len = strlen(buf); |
| 764 | if (len >= n - 32) { // 32 should be longer than anything we could append. |
| 765 | buf[len - 1] = '.'; |
| 766 | buf[len - 2] = '.'; |
| 767 | buf[len - 3] = '.'; |
| 768 | return; |
| 769 | } |
| 770 | char* p = buf + len; |
| 771 | switch (type) { |
| 772 | case 'B': |
| 773 | if (value.b >= 0 && value.b < 10) { |
| 774 | sprintf(p, "%d", value.b); |
| 775 | } else { |
| 776 | sprintf(p, "0x%x (%d)", value.b, value.b); |
| 777 | } |
| 778 | break; |
| 779 | case 'C': |
| 780 | if (value.c < 0x7f && value.c >= ' ') { |
| 781 | sprintf(p, "U+%x ('%c')", value.c, value.c); |
| 782 | } else { |
| 783 | sprintf(p, "U+%x", value.c); |
| 784 | } |
| 785 | break; |
| 786 | case 'D': |
| 787 | sprintf(p, "%g", value.d); |
| 788 | break; |
| 789 | case 'F': |
| 790 | sprintf(p, "%g", value.f); |
| 791 | break; |
| 792 | case 'I': |
| 793 | sprintf(p, "%d", value.i); |
| 794 | break; |
| 795 | case 'L': |
| 796 | sprintf(p, "0x%x", value.i); |
| 797 | break; |
| 798 | case 'J': |
| 799 | sprintf(p, "%lld", value.j); |
| 800 | break; |
| 801 | case 'S': |
| 802 | sprintf(p, "%d", value.s); |
| 803 | break; |
| 804 | case 'V': |
| 805 | strcpy(p, "void"); |
| 806 | break; |
| 807 | case 'Z': |
| 808 | strcpy(p, value.z ? "true" : "false"); |
| 809 | break; |
| 810 | default: |
| 811 | sprintf(p, "unknown type '%c'", type); |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | if (appendComma) { |
| 816 | strcat(p, ", "); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | #define LOGI_NATIVE(...) LOG(LOG_INFO, LOG_TAG "-native", __VA_ARGS__) |
| 821 | |
Elliott Hughes | de66fcb | 2010-07-30 13:34:49 -0700 | [diff] [blame] | 822 | void dvmLogNativeMethodEntry(const Method* method, const u4* args) |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 823 | { |
| 824 | char thisString[32] = { 0 }; |
Elliott Hughes | de66fcb | 2010-07-30 13:34:49 -0700 | [diff] [blame] | 825 | const u4* sp = args; // &args[method->registersSize - method->insSize]; |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 826 | if (!dvmIsStaticMethod(method)) { |
| 827 | sprintf(thisString, "this=0x%08x ", *sp++); |
| 828 | } |
| 829 | |
| 830 | char argsString[128]= { 0 }; |
| 831 | const char* desc = &method->shorty[1]; |
| 832 | while (*desc != '\0') { |
| 833 | char argType = *desc++; |
| 834 | JValue value; |
| 835 | if (argType == 'D' || argType == 'J') { |
| 836 | value.j = dvmGetArgLong(sp, 0); |
| 837 | sp += 2; |
| 838 | } else { |
| 839 | value.i = *sp++; |
| 840 | } |
| 841 | appendValue(argType, value, argsString, sizeof(argsString), |
| 842 | *desc != '\0'); |
| 843 | } |
| 844 | |
Elliott Hughes | 8a8286b | 2010-12-03 10:57:15 -0800 | [diff] [blame] | 845 | char* className = dvmHumanReadableDescriptor(method->clazz->descriptor); |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 846 | char* signature = dexProtoCopyMethodDescriptor(&method->prototype); |
Elliott Hughes | 8a8286b | 2010-12-03 10:57:15 -0800 | [diff] [blame] | 847 | LOGI_NATIVE("-> %s %s%s %s(%s)", className, method->name, signature, |
| 848 | thisString, argsString); |
| 849 | free(className); |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 850 | free(signature); |
| 851 | } |
| 852 | |
| 853 | void dvmLogNativeMethodExit(const Method* method, Thread* self, |
| 854 | const JValue returnValue) |
| 855 | { |
Elliott Hughes | 8a8286b | 2010-12-03 10:57:15 -0800 | [diff] [blame] | 856 | char* className = dvmHumanReadableDescriptor(method->clazz->descriptor); |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 857 | char* signature = dexProtoCopyMethodDescriptor(&method->prototype); |
| 858 | if (dvmCheckException(self)) { |
| 859 | Object* exception = dvmGetException(self); |
Elliott Hughes | 8a8286b | 2010-12-03 10:57:15 -0800 | [diff] [blame] | 860 | char* exceptionClassName = |
| 861 | dvmHumanReadableDescriptor(exception->clazz->descriptor); |
| 862 | LOGI_NATIVE("<- %s %s%s threw %s", className, |
| 863 | method->name, signature, exceptionClassName); |
| 864 | free(exceptionClassName); |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 865 | } else { |
| 866 | char returnValueString[128] = { 0 }; |
| 867 | char returnType = method->shorty[0]; |
| 868 | appendValue(returnType, returnValue, |
Elliott Hughes | 8a8286b | 2010-12-03 10:57:15 -0800 | [diff] [blame] | 869 | returnValueString, sizeof(returnValueString), false); |
| 870 | LOGI_NATIVE("<- %s %s%s returned %s", className, |
| 871 | method->name, signature, returnValueString); |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 872 | } |
Elliott Hughes | 8a8286b | 2010-12-03 10:57:15 -0800 | [diff] [blame] | 873 | free(className); |
Elliott Hughes | 8afa9df | 2010-07-07 14:47:25 -0700 | [diff] [blame] | 874 | free(signature); |
| 875 | } |