| 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 | * Dalvik implementation of JNI interfaces. |
| 19 | */ |
| 20 | #include "Dalvik.h" |
| 21 | #include "JniInternal.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <limits.h> |
| 26 | |
| 27 | /* |
| 28 | Native methods and interaction with the GC |
| 29 | |
| 30 | All JNI methods must start by changing their thread status to |
| 31 | THREAD_RUNNING, and finish by changing it back to THREAD_NATIVE before |
| 32 | returning to native code. The switch to "running" triggers a thread |
| 33 | suspension check. |
| 34 | |
| 35 | With a rudimentary GC we should be able to skip the status change for |
| 36 | simple functions, e.g. IsSameObject, GetJavaVM, GetStringLength, maybe |
| 37 | even access to fields with primitive types. Our options are more limited |
| 38 | with a compacting GC, so we should replace JNI_ENTER with JNI_ENTER_NCGC |
| 39 | or somesuch on the "lite" functions if we want to try this optimization. |
| 40 | |
| 41 | For performance reasons we do as little error-checking as possible here. |
| 42 | For example, we don't check to make sure the correct type of Object is |
| 43 | passed in when setting a field, and we don't prevent you from storing |
| 44 | new values in a "final" field. Such things are best handled in the |
| 45 | "check" version. For actions that are common, dangerous, and must be |
| 46 | checked at runtime, such as array bounds checks, we do the tests here. |
| 47 | |
| 48 | |
| 49 | General notes on local/global reference tracking |
| 50 | |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 51 | JNI provides explicit control over natively-held references that the GC |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 52 | needs to know about. These can be local, in which case they're released |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 53 | when the native method returns into the VM, or global, which are held |
| 54 | until explicitly released. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 55 | |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 56 | The references can be created with explicit JNI NewLocalRef / NewGlobalRef |
| 57 | calls. The former is very unusual, the latter is reasonably common |
| 58 | (e.g. for caching references to class objects). |
| 59 | |
| 60 | Local references are most often created as a side-effect of JNI functions. |
| 61 | For example, the AllocObject/NewObject functions must create local |
| 62 | references to the objects returned, because nothing else in the GC root |
| 63 | set has a reference to the new objects. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 64 | |
| 65 | The most common mode of operation is for a method to create zero or |
| 66 | more local references and return. Explicit "local delete" operations |
| 67 | are expected to be exceedingly rare, except when walking through an |
| 68 | object array, and the Push/PopLocalFrame calls are expected to be used |
| 69 | infrequently. For efficient operation, we want to add new local refs |
| 70 | with a simple store/increment operation; to avoid infinite growth in |
| 71 | pathological situations, we need to reclaim the space used by deleted |
| 72 | entries. |
| 73 | |
| 74 | The simplest implementation is an expanding append-only array that compacts |
| 75 | when objects are deleted. In typical situations, e.g. running through |
| 76 | an array of objects, we will be deleting one of the most recently added |
| 77 | entries, so we can minimize the number of elements moved (or avoid having |
| 78 | to move any). |
| 79 | |
| 80 | The spec says, "Local references are only valid in the thread in which |
| 81 | they are created. The native code must not pass local references from |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 82 | one thread to another." |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 83 | |
| 84 | |
| 85 | Global reference tracking |
| 86 | |
| 87 | There should be a small "active" set centered around the most-recently |
| 88 | added items. We can use an append-only, compacting array like we do for |
| 89 | local refs. |
| 90 | |
| 91 | Because it's global, access to it has to be synchronized. |
| 92 | |
| 93 | The JNI spec does not define any sort of limit, so the list must be able |
| 94 | to expand. It may be useful to log significant increases in usage to |
| 95 | help identify resource leaks. |
| 96 | |
| 97 | TODO: we currently use global references on strings and primitive array |
| 98 | data, because they have the property we need (i.e. the pointer we return |
| 99 | is guaranteed valid until we explicitly release it). However, if we have |
| 100 | a compacting GC and don't want to pin all memory held by all global refs, |
| 101 | we actually want to treat these differently. Either we need a way to |
| 102 | tell the GC that specific global references are pinned, or we have to |
| 103 | make a copy of the data and return that instead (something JNI supports). |
| 104 | |
| 105 | |
| 106 | Local reference tracking |
| 107 | |
| 108 | The table of local references can be stored on the interpreted stack or |
| 109 | in a parallel data structure (one per thread). |
| 110 | |
| 111 | *** Approach #1: use the interpreted stack |
| 112 | |
| 113 | The easiest place to tuck it is between the frame ptr and the first saved |
| 114 | register, which is always in0. (See the ASCII art in Stack.h.) We can |
| 115 | shift the "VM-specific goop" and frame ptr down, effectively inserting |
| 116 | the JNI local refs in the space normally occupied by local variables. |
| 117 | |
| 118 | (Three things are accessed from the frame pointer: |
| 119 | (1) framePtr[N] is register vN, used to get at "ins" and "locals". |
| 120 | (2) framePtr - sizeof(StackSaveArea) is the VM frame goop. |
| 121 | (3) framePtr - sizeof(StackSaveArea) - numOuts is where the "outs" go. |
| 122 | The only thing that isn't determined by an offset from the current FP |
| 123 | is the previous frame. However, tucking things below the previous frame |
| 124 | can be problematic because the "outs" of the previous frame overlap with |
| 125 | the "ins" of the current frame. If the "ins" are altered they must be |
| 126 | restored before we return. For a native method call, the easiest and |
| 127 | safest thing to disrupt is #1, because there are no locals and the "ins" |
| 128 | are all copied to the native stack.) |
| 129 | |
| 130 | We can implement Push/PopLocalFrame with the existing stack frame calls, |
| 131 | making sure we copy some goop from the previous frame (notably the method |
| 132 | ptr, so that dvmGetCurrentJNIMethod() doesn't require extra effort). |
| 133 | |
| 134 | We can pre-allocate the storage at the time the stack frame is first |
| 135 | set up, but we have to be careful. When calling from interpreted code |
| 136 | the frame ptr points directly at the arguments we're passing, but we can |
| 137 | offset the args pointer when calling the native bridge. |
| 138 | |
| 139 | To manage the local ref collection, we need to be able to find three |
| 140 | things: (1) the start of the region, (2) the end of the region, and (3) |
| 141 | the next available entry. The last is only required for quick adds. |
| 142 | We currently have two easily-accessible pointers, the current FP and the |
| 143 | previous frame's FP. (The "stack pointer" shown in the ASCII art doesn't |
| 144 | actually exist in the interpreted world.) |
| 145 | |
| 146 | We can't use the current FP to find the first "in", because we want to |
| 147 | insert the variable-sized local refs table between them. It's awkward |
| 148 | to use the previous frame's FP because native methods invoked via |
| 149 | dvmCallMethod() or dvmInvokeMethod() don't have "ins", but native methods |
| 150 | invoked from interpreted code do. We can either track the local refs |
| 151 | table size with a field in the stack frame, or insert unnecessary items |
| 152 | so that all native stack frames have "ins". |
| 153 | |
| 154 | Assuming we can find the region bounds, we still need pointer #3 |
| 155 | for an efficient implementation. This can be stored in an otherwise |
| 156 | unused-for-native field in the frame goop. |
| 157 | |
| 158 | When we run out of room we have to make more space. If we start allocating |
| 159 | locals immediately below in0 and grow downward, we will detect end-of-space |
| 160 | by running into the current frame's FP. We then memmove() the goop down |
| 161 | (memcpy if we guarantee the additional size is larger than the frame). |
| 162 | This is nice because we only have to move sizeof(StackSaveArea) bytes |
| 163 | each time. |
| 164 | |
| 165 | Stack walking should be okay so long as nothing tries to access the |
| 166 | "ins" by an offset from the FP. In theory the "ins" could be read by |
| 167 | the debugger or SIGQUIT handler looking for "this" or other arguments, |
| 168 | but in practice this behavior isn't expected to work for native methods, |
| 169 | so we can simply disallow it. |
| 170 | |
| 171 | A conservative GC can just scan the entire stack from top to bottom to find |
| 172 | all references. An exact GC will need to understand the actual layout. |
| 173 | |
| 174 | *** Approach #2: use a parallel stack |
| 175 | |
| 176 | Each Thread/JNIEnv points to a ReferenceTable struct. The struct |
| 177 | has a system-heap-allocated array of references and a pointer to the |
| 178 | next-available entry ("nextEntry"). |
| 179 | |
| 180 | Each stack frame has a pointer to what it sees as the "top" element in the |
| 181 | array (we can double-up the "currentPc" field). This is set to "nextEntry" |
| 182 | when the frame is pushed on. As local references are added or removed, |
| 183 | "nextEntry" is updated. |
| 184 | |
| 185 | We implement Push/PopLocalFrame with actual stack frames. Before a JNI |
| 186 | frame gets popped, we set "nextEntry" to the "top" pointer of the current |
| 187 | frame, effectively releasing the references. |
| 188 | |
| 189 | The GC will scan all references from the start of the table to the |
| 190 | "nextEntry" pointer. |
| 191 | |
| 192 | *** Comparison |
| 193 | |
| 194 | All approaches will return a failure result when they run out of local |
| 195 | reference space. For #1 that means blowing out the stack, for #2 it's |
| 196 | running out of room in the array. |
| 197 | |
| 198 | Compared to #1, approach #2: |
| 199 | - Needs only one pointer in the stack frame goop. |
| 200 | - Makes pre-allocating storage unnecessary. |
| 201 | - Doesn't contend with interpreted stack depth for space. In most |
| 202 | cases, if something blows out the local ref storage, it's because the |
| 203 | JNI code was misbehaving rather than called from way down. |
| 204 | - Allows the GC to do a linear scan per thread in a buffer that is 100% |
| 205 | references. The GC can be slightly less smart when scanning the stack. |
| 206 | - Will be easier to work with if we combine native and interpeted stacks. |
| 207 | |
| 208 | - Isn't as clean, especially when popping frames, since we have to do |
| 209 | explicit work. Fortunately we only have to do it when popping native |
| 210 | method calls off, so it doesn't add overhead to interpreted code paths. |
| 211 | - Is awkward to expand dynamically. We'll want to pre-allocate the full |
| 212 | amount of space; this is fine, since something on the order of 1KB should |
| 213 | be plenty. The JNI spec allows us to limit this. |
| 214 | - Requires the GC to scan even more memory. With the references embedded |
| 215 | in the stack we get better locality of reference. |
| 216 | |
| 217 | */ |
| 218 | |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 219 | /* fwd */ |
| 220 | static const struct JNINativeInterface gNativeInterface; |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 221 | static jobject addLocalReference(JNIEnv* env, Object* obj); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 222 | static jobject addGlobalReference(Object* obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 223 | |
| 224 | |
| 225 | #ifdef WITH_JNI_STACK_CHECK |
| 226 | # define COMPUTE_STACK_SUM(_self) computeStackSum(_self); |
| 227 | # define CHECK_STACK_SUM(_self) checkStackSum(_self); |
| 228 | static void computeStackSum(Thread* self); |
| 229 | static void checkStackSum(Thread* self); |
| 230 | #else |
| 231 | # define COMPUTE_STACK_SUM(_self) ((void)0) |
| 232 | # define CHECK_STACK_SUM(_self) ((void)0) |
| 233 | #endif |
| 234 | |
| 235 | |
| 236 | /* |
| 237 | * =========================================================================== |
| 238 | * JNI call bridge |
| 239 | * =========================================================================== |
| 240 | */ |
| 241 | |
| 242 | /* |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 243 | * The functions here form a bridge between interpreted code and JNI native |
| 244 | * functions. The basic task is to convert an array of primitives and |
| 245 | * references into C-style function arguments. This is architecture-specific |
| 246 | * and usually requires help from assembly code. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 247 | * |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 248 | * The bridge takes four arguments: the array of parameters, a place to |
| 249 | * store the function result (if any), the method to call, and a pointer |
| 250 | * to the current thread. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 251 | * |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 252 | * These functions aren't called directly from elsewhere in the VM. |
| 253 | * A pointer in the Method struct points to one of these, and when a native |
| 254 | * method is invoked the interpreter jumps to it. |
| 255 | * |
| 256 | * (The "internal native" methods are invoked the same way, but instead |
| 257 | * of calling through a bridge, the target method is called directly.) |
| 258 | * |
| 259 | * The "args" array should not be modified, but we do so anyway for |
| 260 | * performance reasons. We know that it points to the "outs" area on |
| 261 | * the current method's interpreted stack. This area is ignored by the |
| 262 | * precise GC, because there is no register map for a native method (for |
| 263 | * an interpreted method the args would be listed in the argument set). |
| 264 | * We know all of the values exist elsewhere on the interpreted stack, |
| 265 | * because the method call setup copies them right before making the call, |
| 266 | * so we don't have to worry about concealing stuff from the GC. |
| 267 | * |
| 268 | * If we don't want to modify "args", we either have to create a local |
| 269 | * copy and modify it before calling dvmPlatformInvoke, or we have to do |
| 270 | * the local reference replacement within dvmPlatformInvoke. The latter |
| 271 | * has some performance advantages, though if we can inline the local |
| 272 | * reference adds we may win when there's a lot of reference args (unless |
| 273 | * we want to code up some local ref table manipulation in assembly. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 274 | */ |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 275 | |
| 276 | /* |
| 277 | * General form, handles all cases. |
| 278 | */ |
| 279 | void dvmCallJNIMethod_general(const u4* args, JValue* pResult, |
| 280 | const Method* method, Thread* self) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 281 | { |
| 282 | int oldStatus; |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 283 | u4* modArgs = (u4*) args; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 284 | |
| 285 | assert(method->insns != NULL); |
| 286 | |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 287 | //LOGI("JNI calling %p (%s.%s:%s):\n", method->insns, |
| 288 | // method->clazz->descriptor, method->name, method->shorty); |
| 289 | |
| 290 | /* |
| 291 | * Walk the argument list, creating local references for appropriate |
| 292 | * arguments. |
| 293 | */ |
| 294 | JNIEnv* env = self->jniEnv; |
| 295 | jclass staticMethodClass; |
| 296 | int idx = 0; |
| 297 | if (dvmIsStaticMethod(method)) { |
| 298 | /* add the class object we pass in */ |
| 299 | staticMethodClass = addLocalReference(env, (Object*) method->clazz); |
| 300 | if (staticMethodClass == NULL) { |
| 301 | assert(dvmCheckException(self)); |
| 302 | return; |
| 303 | } |
| 304 | } else { |
| 305 | /* add "this" */ |
| 306 | staticMethodClass = NULL; |
| 307 | jobject thisObj = addLocalReference(env, (Object*) modArgs[0]); |
| 308 | if (thisObj == NULL) { |
| 309 | assert(dvmCheckException(self)); |
| 310 | return; |
| 311 | } |
| 312 | modArgs[idx] = (u4) thisObj; |
| 313 | idx = 1; |
| 314 | } |
| 315 | |
| 316 | const char* shorty = &method->shorty[1]; /* skip return type */ |
| 317 | while (*shorty != '\0') { |
| 318 | switch (*shorty++) { |
| 319 | case 'L': |
| 320 | //LOGI(" local %d: 0x%08x\n", idx, modArgs[idx]); |
| 321 | if (modArgs[idx] != 0) { |
| 322 | //if (!dvmIsValidObject((Object*) modArgs[idx])) |
| 323 | // dvmAbort(); |
| 324 | jobject argObj = addLocalReference(env, (Object*) modArgs[idx]); |
| 325 | if (argObj == NULL) { |
| 326 | assert(dvmCheckException(self)); |
| 327 | return; |
| 328 | } |
| 329 | modArgs[idx] = (u4) argObj; |
| 330 | } |
| 331 | break; |
| 332 | case 'D': |
| 333 | case 'J': |
| 334 | idx++; |
| 335 | break; |
| 336 | default: |
| 337 | /* Z B C S I -- do nothing */ |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | idx++; |
| 342 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 343 | |
| 344 | oldStatus = dvmChangeStatus(self, THREAD_NATIVE); |
| 345 | |
| 346 | COMPUTE_STACK_SUM(self); |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 347 | dvmPlatformInvoke(self->jniEnv, staticMethodClass, |
| 348 | method->jniArgInfo, method->insSize, modArgs, method->shorty, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 349 | (void*)method->insns, pResult); |
| 350 | CHECK_STACK_SUM(self); |
| 351 | |
| 352 | dvmChangeStatus(self, oldStatus); |
| 353 | } |
| 354 | |
| 355 | /* |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 356 | * Handler for the unusual case of a synchronized native method. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 357 | * |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 358 | * Lock the object, then call through the general function. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 359 | */ |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 360 | void dvmCallJNIMethod_synchronized(const u4* args, JValue* pResult, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 361 | const Method* method, Thread* self) |
| 362 | { |
| 363 | Object* lockObj; |
| 364 | |
| 365 | assert(dvmIsSynchronizedMethod(method)); |
| 366 | |
| 367 | if (dvmIsStaticMethod(method)) |
| 368 | lockObj = (Object*) method->clazz; |
| 369 | else |
| 370 | lockObj = (Object*) args[0]; |
| 371 | |
| 372 | LOGVV("Calling %s.%s: locking %p (%s)\n", |
| 373 | method->clazz->descriptor, method->name, |
| 374 | lockObj, lockObj->clazz->descriptor); |
| 375 | |
| 376 | dvmLockObject(self, lockObj); |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 377 | dvmCallJNIMethod_general(args, pResult, method, self); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 378 | dvmUnlockObject(self, lockObj); |
| 379 | } |
| 380 | |
| 381 | /* |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 382 | * Virtual method call, no reference arguments. |
| 383 | * |
| 384 | * We need to local-ref the "this" argument, found in args[0]. |
| 385 | */ |
| 386 | void dvmCallJNIMethod_virtualNoRef(const u4* args, JValue* pResult, |
| 387 | const Method* method, Thread* self) |
| 388 | { |
| 389 | u4* modArgs = (u4*) args; |
| 390 | int oldStatus; |
| 391 | |
| 392 | jobject thisObj = addLocalReference(self->jniEnv, (Object*) args[0]); |
| 393 | if (thisObj == NULL) { |
| 394 | assert(dvmCheckException(self)); |
| 395 | return; |
| 396 | } |
| 397 | modArgs[0] = (u4) thisObj; |
| 398 | |
| 399 | oldStatus = dvmChangeStatus(self, THREAD_NATIVE); |
| 400 | |
| 401 | COMPUTE_STACK_SUM(self); |
| 402 | dvmPlatformInvoke(self->jniEnv, NULL, |
| 403 | method->jniArgInfo, method->insSize, modArgs, method->shorty, |
| 404 | (void*)method->insns, pResult); |
| 405 | CHECK_STACK_SUM(self); |
| 406 | |
| 407 | dvmChangeStatus(self, oldStatus); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Static method call, no reference arguments. |
| 412 | * |
| 413 | * We need to local-ref the class reference. |
| 414 | */ |
| 415 | void dvmCallJNIMethod_staticNoRef(const u4* args, JValue* pResult, |
| 416 | const Method* method, Thread* self) |
| 417 | { |
| 418 | jclass staticMethodClass; |
| 419 | int oldStatus; |
| 420 | |
| 421 | staticMethodClass = addLocalReference(self->jniEnv, (Object*)method->clazz); |
| 422 | if (staticMethodClass == NULL) { |
| 423 | assert(dvmCheckException(self)); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | oldStatus = dvmChangeStatus(self, THREAD_NATIVE); |
| 428 | |
| 429 | COMPUTE_STACK_SUM(self); |
| 430 | dvmPlatformInvoke(self->jniEnv, staticMethodClass, |
| 431 | method->jniArgInfo, method->insSize, args, method->shorty, |
| 432 | (void*)method->insns, pResult); |
| 433 | CHECK_STACK_SUM(self); |
| 434 | |
| 435 | dvmChangeStatus(self, oldStatus); |
| 436 | } |
| 437 | |
| 438 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 439 | * Extract the return type enum from the "jniArgInfo" field. |
| 440 | */ |
| 441 | DalvikJniReturnType dvmGetArgInfoReturnType(int jniArgInfo) |
| 442 | { |
| 443 | return (jniArgInfo & DALVIK_JNI_RETURN_MASK) >> DALVIK_JNI_RETURN_SHIFT; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /* |
| 448 | * =========================================================================== |
| 449 | * Utility functions |
| 450 | * =========================================================================== |
| 451 | */ |
| 452 | |
| 453 | /* |
| 454 | * Entry/exit processing for all JNI calls. |
| 455 | * |
| 456 | * If TRUSTED_JNIENV is set, we get to skip the (curiously expensive) |
| 457 | * thread-local storage lookup on our Thread*. If the caller has passed |
| 458 | * the wrong JNIEnv in, we're going to be accessing unsynchronized |
| 459 | * structures from more than one thread, and things are going to fail |
| 460 | * in bizarre ways. This is only sensible if the native code has been |
| 461 | * fully exercised with CheckJNI enabled. |
| 462 | */ |
| 463 | #define TRUSTED_JNIENV |
| 464 | #ifdef TRUSTED_JNIENV |
| 465 | # define JNI_ENTER() \ |
| 466 | Thread* _self = ((JNIEnvExt*)env)->self; \ |
| 467 | CHECK_STACK_SUM(_self); \ |
| 468 | dvmChangeStatus(_self, THREAD_RUNNING) |
| 469 | #else |
| 470 | # define JNI_ENTER() \ |
| 471 | Thread* _self = dvmThreadSelf(); \ |
| 472 | UNUSED_PARAMETER(env); \ |
| 473 | CHECK_STACK_SUM(_self); \ |
| 474 | dvmChangeStatus(_self, THREAD_RUNNING) |
| 475 | #endif |
| 476 | #define JNI_EXIT() \ |
| 477 | dvmChangeStatus(_self, THREAD_NATIVE); \ |
| 478 | COMPUTE_STACK_SUM(_self) |
| 479 | |
| 480 | #define kGlobalRefsTableInitialSize 512 |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 481 | #define kGlobalRefsTableMaxSize 51200 /* arbitrary, must be < 64K */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 482 | #define kGrefWaterInterval 100 |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 483 | #define kTrackGrefUsage true |
| 484 | |
| Andy McFadden | c26bb63 | 2009-08-21 12:01:31 -0700 | [diff] [blame] | 485 | #define kPinTableInitialSize 16 |
| 486 | #define kPinTableMaxSize 1024 |
| 487 | #define kPinComplainThreshold 10 |
| 488 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 489 | /* |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 490 | * Allocate the global references table, and look up some classes for |
| 491 | * the benefit of direct buffer access. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 492 | */ |
| 493 | bool dvmJniStartup(void) |
| 494 | { |
| 495 | if (!dvmInitReferenceTable(&gDvm.jniGlobalRefTable, |
| 496 | kGlobalRefsTableInitialSize, kGlobalRefsTableMaxSize)) |
| 497 | return false; |
| 498 | |
| 499 | dvmInitMutex(&gDvm.jniGlobalRefLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 500 | gDvm.jniGlobalRefLoMark = 0; |
| 501 | gDvm.jniGlobalRefHiMark = kGrefWaterInterval * 2; |
| 502 | |
| Andy McFadden | c26bb63 | 2009-08-21 12:01:31 -0700 | [diff] [blame] | 503 | if (!dvmInitReferenceTable(&gDvm.jniPinRefTable, |
| 504 | kPinTableInitialSize, kPinTableMaxSize)) |
| 505 | return false; |
| 506 | |
| 507 | dvmInitMutex(&gDvm.jniPinRefLock); |
| 508 | |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 509 | /* |
| 510 | * Look up and cache pointers to some direct buffer classes, fields, |
| 511 | * and methods. |
| 512 | */ |
| 513 | Method* meth; |
| 514 | |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 515 | ClassObject* platformAddressClass = |
| 516 | dvmFindSystemClassNoInit("Lorg/apache/harmony/luni/platform/PlatformAddress;"); |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 517 | ClassObject* platformAddressFactoryClass = |
| 518 | dvmFindSystemClassNoInit("Lorg/apache/harmony/luni/platform/PlatformAddressFactory;"); |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 519 | ClassObject* directBufferClass = |
| 520 | dvmFindSystemClassNoInit("Lorg/apache/harmony/nio/internal/DirectBuffer;"); |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 521 | ClassObject* readWriteBufferClass = |
| 522 | dvmFindSystemClassNoInit("Ljava/nio/ReadWriteDirectByteBuffer;"); |
| 523 | ClassObject* bufferClass = |
| 524 | dvmFindSystemClassNoInit("Ljava/nio/Buffer;"); |
| 525 | |
| 526 | if (platformAddressClass == NULL || platformAddressFactoryClass == NULL || |
| 527 | directBufferClass == NULL || readWriteBufferClass == NULL || |
| 528 | bufferClass == NULL) |
| 529 | { |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 530 | LOGE("Unable to find internal direct buffer classes\n"); |
| 531 | return false; |
| 532 | } |
| 533 | /* needs to be a global ref so CheckJNI thinks we're allowed to see it */ |
| 534 | gDvm.classOrgApacheHarmonyNioInternalDirectBuffer = |
| 535 | addGlobalReference((Object*) directBufferClass); |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 536 | gDvm.classJavaNioReadWriteDirectByteBuffer = readWriteBufferClass; |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 537 | |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 538 | /* |
| 539 | * We need a Method* here rather than a vtable offset, because |
| 540 | * DirectBuffer is an interface class. |
| 541 | */ |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 542 | meth = dvmFindVirtualMethodByDescriptor( |
| 543 | gDvm.classOrgApacheHarmonyNioInternalDirectBuffer, |
| 544 | "getEffectiveAddress", |
| 545 | "()Lorg/apache/harmony/luni/platform/PlatformAddress;"); |
| 546 | if (meth == NULL) { |
| 547 | LOGE("Unable to find PlatformAddress.getEffectiveAddress\n"); |
| 548 | return false; |
| 549 | } |
| 550 | gDvm.methOrgApacheHarmonyNioInternalDirectBuffer_getEffectiveAddress = meth; |
| 551 | |
| 552 | meth = dvmFindVirtualMethodByDescriptor(platformAddressClass, |
| 553 | "toLong", "()J"); |
| 554 | if (meth == NULL) { |
| 555 | LOGE("Unable to find PlatformAddress.toLong\n"); |
| 556 | return false; |
| 557 | } |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 558 | gDvm.voffOrgApacheHarmonyLuniPlatformPlatformAddress_toLong = |
| 559 | meth->methodIndex; |
| 560 | |
| 561 | meth = dvmFindDirectMethodByDescriptor(platformAddressFactoryClass, |
| 562 | "on", |
| 563 | "(I)Lorg/apache/harmony/luni/platform/PlatformAddress;"); |
| 564 | if (meth == NULL) { |
| 565 | LOGE("Unable to find PlatformAddressFactory.on\n"); |
| 566 | return false; |
| 567 | } |
| 568 | gDvm.methOrgApacheHarmonyLuniPlatformPlatformAddress_on = meth; |
| 569 | |
| 570 | meth = dvmFindDirectMethodByDescriptor(readWriteBufferClass, |
| 571 | "<init>", |
| 572 | "(Lorg/apache/harmony/luni/platform/PlatformAddress;II)V"); |
| 573 | if (meth == NULL) { |
| 574 | LOGE("Unable to find ReadWriteDirectByteBuffer.<init>\n"); |
| 575 | return false; |
| 576 | } |
| 577 | gDvm.methJavaNioReadWriteDirectByteBuffer_init = meth; |
| 578 | |
| 579 | gDvm.offOrgApacheHarmonyLuniPlatformPlatformAddress_osaddr = |
| 580 | dvmFindFieldOffset(platformAddressClass, "osaddr", "I"); |
| 581 | if (gDvm.offOrgApacheHarmonyLuniPlatformPlatformAddress_osaddr < 0) { |
| 582 | LOGE("Unable to find PlatformAddress.osaddr\n"); |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | gDvm.offJavaNioBuffer_capacity = |
| 587 | dvmFindFieldOffset(bufferClass, "capacity", "I"); |
| 588 | if (gDvm.offJavaNioBuffer_capacity < 0) { |
| 589 | LOGE("Unable to find Buffer.capacity\n"); |
| 590 | return false; |
| 591 | } |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 592 | |
| Andy McFadden | 8e696dc | 2009-07-24 15:28:16 -0700 | [diff] [blame] | 593 | gDvm.offJavaNioBuffer_effectiveDirectAddress = |
| 594 | dvmFindFieldOffset(bufferClass, "effectiveDirectAddress", "I"); |
| 595 | if (gDvm.offJavaNioBuffer_effectiveDirectAddress < 0) { |
| 596 | LOGE("Unable to find Buffer.effectiveDirectAddress\n"); |
| 597 | return false; |
| 598 | } |
| 599 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 600 | return true; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Free the global references table. |
| 605 | */ |
| 606 | void dvmJniShutdown(void) |
| 607 | { |
| 608 | dvmClearReferenceTable(&gDvm.jniGlobalRefTable); |
| 609 | } |
| 610 | |
| 611 | |
| 612 | /* |
| 613 | * Find the JNIEnv associated with the current thread. |
| 614 | * |
| 615 | * Currently stored in the Thread struct. Could also just drop this into |
| 616 | * thread-local storage. |
| 617 | */ |
| 618 | JNIEnvExt* dvmGetJNIEnvForThread(void) |
| 619 | { |
| 620 | Thread* self = dvmThreadSelf(); |
| 621 | if (self == NULL) |
| 622 | return NULL; |
| 623 | return (JNIEnvExt*) dvmGetThreadJNIEnv(self); |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Create a new JNIEnv struct and add it to the VM's list. |
| 628 | * |
| 629 | * "self" will be NULL for the main thread, since the VM hasn't started |
| 630 | * yet; the value will be filled in later. |
| 631 | */ |
| 632 | JNIEnv* dvmCreateJNIEnv(Thread* self) |
| 633 | { |
| 634 | JavaVMExt* vm = (JavaVMExt*) gDvm.vmList; |
| 635 | JNIEnvExt* newEnv; |
| 636 | |
| 637 | //if (self != NULL) |
| 638 | // LOGI("Ent CreateJNIEnv: threadid=%d %p\n", self->threadId, self); |
| 639 | |
| 640 | assert(vm != NULL); |
| 641 | |
| 642 | newEnv = (JNIEnvExt*) calloc(1, sizeof(JNIEnvExt)); |
| 643 | newEnv->funcTable = &gNativeInterface; |
| 644 | newEnv->vm = vm; |
| 645 | newEnv->forceDataCopy = vm->forceDataCopy; |
| 646 | if (self != NULL) { |
| 647 | dvmSetJniEnvThreadId((JNIEnv*) newEnv, self); |
| 648 | assert(newEnv->envThreadId != 0); |
| 649 | } else { |
| 650 | /* make it obvious if we fail to initialize these later */ |
| 651 | newEnv->envThreadId = 0x77777775; |
| 652 | newEnv->self = (Thread*) 0x77777779; |
| 653 | } |
| 654 | if (vm->useChecked) |
| 655 | dvmUseCheckedJniEnv(newEnv); |
| 656 | |
| 657 | dvmLockMutex(&vm->envListLock); |
| 658 | |
| 659 | /* insert at head of list */ |
| 660 | newEnv->next = vm->envList; |
| 661 | assert(newEnv->prev == NULL); |
| 662 | if (vm->envList == NULL) // rare, but possible |
| 663 | vm->envList = newEnv; |
| 664 | else |
| 665 | vm->envList->prev = newEnv; |
| 666 | vm->envList = newEnv; |
| 667 | |
| 668 | dvmUnlockMutex(&vm->envListLock); |
| 669 | |
| 670 | //if (self != NULL) |
| 671 | // LOGI("Xit CreateJNIEnv: threadid=%d %p\n", self->threadId, self); |
| 672 | return (JNIEnv*) newEnv; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Remove a JNIEnv struct from the list and free it. |
| 677 | */ |
| 678 | void dvmDestroyJNIEnv(JNIEnv* env) |
| 679 | { |
| 680 | JNIEnvExt* extEnv = (JNIEnvExt*) env; |
| 681 | JavaVMExt* vm = extEnv->vm; |
| 682 | Thread* self; |
| 683 | |
| 684 | if (env == NULL) |
| 685 | return; |
| 686 | |
| 687 | self = dvmThreadSelf(); |
| 688 | assert(self != NULL); |
| 689 | |
| 690 | //LOGI("Ent DestroyJNIEnv: threadid=%d %p\n", self->threadId, self); |
| 691 | |
| 692 | dvmLockMutex(&vm->envListLock); |
| 693 | |
| 694 | if (extEnv == vm->envList) { |
| 695 | assert(extEnv->prev == NULL); |
| 696 | vm->envList = extEnv->next; |
| 697 | } else { |
| 698 | assert(extEnv->prev != NULL); |
| 699 | extEnv->prev->next = extEnv->next; |
| 700 | } |
| 701 | if (extEnv->next != NULL) |
| 702 | extEnv->next->prev = extEnv->prev; |
| 703 | |
| 704 | dvmUnlockMutex(&extEnv->vm->envListLock); |
| 705 | |
| 706 | free(env); |
| 707 | //LOGI("Xit DestroyJNIEnv: threadid=%d %p\n", self->threadId, self); |
| 708 | } |
| 709 | |
| 710 | |
| 711 | /* |
| 712 | * Retrieve the ReferenceTable struct for the current thread. |
| 713 | * |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 714 | * Going through "env" rather than dvmThreadSelf() is faster but will |
| 715 | * get weird if the JNI code is passing the wrong JNIEnv around. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 716 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 717 | static inline ReferenceTable* getLocalRefTable(JNIEnv* env) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 718 | { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 719 | //return &dvmThreadSelf()->jniLocalRefTable; |
| 720 | return &((JNIEnvExt*)env)->self->jniLocalRefTable; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Add a local reference for an object to the current stack frame. When |
| 725 | * the native function returns, the reference will be discarded. |
| 726 | * |
| 727 | * We need to allow the same reference to be added multiple times. |
| 728 | * |
| 729 | * This will be called on otherwise unreferenced objects. We cannot do |
| 730 | * GC allocations here, and it's best if we don't grab a mutex. |
| 731 | * |
| 732 | * Returns the local reference (currently just the same pointer that was |
| 733 | * passed in), or NULL on failure. |
| 734 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 735 | static jobject addLocalReference(JNIEnv* env, Object* obj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 736 | { |
| 737 | if (obj == NULL) |
| 738 | return NULL; |
| 739 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 740 | ReferenceTable* pRefTable = getLocalRefTable(env); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 741 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 742 | if (!dvmAddToReferenceTable(pRefTable, obj)) { |
| 743 | dvmDumpReferenceTable(pRefTable, "JNI local"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 744 | LOGE("Failed adding to JNI local ref table (has %d entries)\n", |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 745 | (int) dvmReferenceTableEntries(pRefTable)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 746 | dvmDumpThread(dvmThreadSelf(), false); |
| 747 | dvmAbort(); // spec says call FatalError; this is equivalent |
| 748 | } else { |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 749 | LOGVV("LREF add %p (%s.%s) (ent=%d)\n", obj, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 750 | dvmGetCurrentJNIMethod()->clazz->descriptor, |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 751 | dvmGetCurrentJNIMethod()->name, |
| 752 | (int) dvmReferenceTableEntries(pRefTable)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | return obj; |
| 756 | } |
| 757 | |
| 758 | /* |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 759 | * Convert an indirect reference to an Object reference. The indirect |
| 760 | * reference may be local, global, or weak-global. |
| 761 | * |
| 762 | * If "jobj" is NULL or an invalid indirect reference, this returns NULL. |
| 763 | * |
| 764 | * [ this is currently a no-op ] |
| 765 | */ |
| 766 | Object* dvmDecodeIndirectRef(JNIEnv* env, jobject jobj) |
| 767 | { |
| 768 | return (Object*) jobj; |
| 769 | } |
| 770 | |
| 771 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 772 | * Ensure that at least "capacity" references can be held in the local |
| 773 | * refs table of the current thread. |
| 774 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 775 | static bool ensureLocalCapacity(JNIEnv* env, int capacity) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 776 | { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 777 | ReferenceTable* pRefTable = getLocalRefTable(env); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 778 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 779 | return (kJniLocalRefMax - (pRefTable->nextEntry - pRefTable->table) >= capacity); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Explicitly delete a reference from the local list. |
| 784 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 785 | static void deleteLocalReference(JNIEnv* env, jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 786 | { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 787 | if (jobj == NULL) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 788 | return; |
| 789 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 790 | ReferenceTable* pRefTable = getLocalRefTable(env); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 791 | Thread* self = dvmThreadSelf(); |
| 792 | Object** top = SAVEAREA_FROM_FP(self->curFrame)->xtra.localRefTop; |
| 793 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 794 | if (!dvmRemoveFromReferenceTable(pRefTable, top, (Object*) jobj)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 795 | /* |
| 796 | * Attempting to delete a local reference that is not in the |
| 797 | * topmost local reference frame is a no-op. DeleteLocalRef returns |
| 798 | * void and doesn't throw any exceptions, but we should probably |
| 799 | * complain about it so the user will notice that things aren't |
| 800 | * going quite the way they expect. |
| 801 | */ |
| 802 | LOGW("JNI WARNING: DeleteLocalRef(%p) failed to find entry (valid=%d)\n", |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 803 | jobj, dvmIsValidObject((Object*) jobj)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Add a global reference for an object. |
| 809 | * |
| 810 | * We may add the same object more than once. Add/remove calls are paired, |
| 811 | * so it needs to appear on the list multiple times. |
| 812 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 813 | static jobject addGlobalReference(Object* obj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 814 | { |
| 815 | if (obj == NULL) |
| 816 | return NULL; |
| 817 | |
| 818 | //LOGI("adding obj=%p\n", obj); |
| 819 | //dvmDumpThread(dvmThreadSelf(), false); |
| 820 | |
| 821 | if (false && ((Object*)obj)->clazz == gDvm.classJavaLangClass) { |
| 822 | ClassObject* clazz = (ClassObject*) obj; |
| 823 | LOGI("-------\n"); |
| 824 | LOGI("Adding global ref on class %s\n", clazz->descriptor); |
| 825 | dvmDumpThread(dvmThreadSelf(), false); |
| 826 | } |
| 827 | if (false && ((Object*)obj)->clazz == gDvm.classJavaLangString) { |
| 828 | StringObject* strObj = (StringObject*) obj; |
| 829 | char* str = dvmCreateCstrFromString(strObj); |
| 830 | if (strcmp(str, "sync-response") == 0) { |
| 831 | LOGI("-------\n"); |
| 832 | LOGI("Adding global ref on string '%s'\n", str); |
| 833 | dvmDumpThread(dvmThreadSelf(), false); |
| 834 | //dvmAbort(); |
| 835 | } |
| 836 | free(str); |
| 837 | } |
| 838 | if (false && ((Object*)obj)->clazz == gDvm.classArrayByte) { |
| 839 | ArrayObject* arrayObj = (ArrayObject*) obj; |
| 840 | if (arrayObj->length == 8192 && |
| 841 | dvmReferenceTableEntries(&gDvm.jniGlobalRefTable) > 400) |
| 842 | { |
| 843 | LOGI("Adding global ref on byte array %p (len=%d)\n", |
| 844 | arrayObj, arrayObj->length); |
| 845 | dvmDumpThread(dvmThreadSelf(), false); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | dvmLockMutex(&gDvm.jniGlobalRefLock); |
| 850 | |
| 851 | /* |
| 852 | * Expanding the table should happen rarely, so I'm not overly |
| 853 | * concerned about the performance impact of copying the old list |
| 854 | * over. We shouldn't see one-time activity spikes, so freeing |
| 855 | * up storage shouldn't be required. |
| 856 | * |
| 857 | * Throwing an exception on failure is problematic, because JNI code |
| 858 | * may not be expecting an exception, and things sort of cascade. We |
| 859 | * want to have a hard limit to catch leaks during debugging, but this |
| 860 | * otherwise needs to expand until memory is consumed. As a practical |
| 861 | * matter, if we have many thousands of global references, chances are |
| 862 | * we're either leaking global ref table entries or we're going to |
| 863 | * run out of space in the GC heap. |
| 864 | */ |
| 865 | if (!dvmAddToReferenceTable(&gDvm.jniGlobalRefTable, (Object*)obj)) { |
| 866 | dvmDumpReferenceTable(&gDvm.jniGlobalRefTable, "JNI global"); |
| 867 | LOGE("Failed adding to JNI global ref table (%d entries)\n", |
| 868 | (int) dvmReferenceTableEntries(&gDvm.jniGlobalRefTable)); |
| 869 | dvmAbort(); |
| 870 | } |
| 871 | |
| 872 | LOGVV("GREF add %p (%s.%s)\n", obj, |
| 873 | dvmGetCurrentJNIMethod()->clazz->descriptor, |
| 874 | dvmGetCurrentJNIMethod()->name); |
| 875 | |
| 876 | /* GREF usage tracking; should probably be disabled for production env */ |
| 877 | if (kTrackGrefUsage && gDvm.jniGrefLimit != 0) { |
| 878 | int count = dvmReferenceTableEntries(&gDvm.jniGlobalRefTable); |
| 879 | if (count > gDvm.jniGlobalRefHiMark) { |
| 880 | LOGD("GREF has increased to %d\n", count); |
| 881 | gDvm.jniGlobalRefHiMark += kGrefWaterInterval; |
| 882 | gDvm.jniGlobalRefLoMark += kGrefWaterInterval; |
| 883 | |
| 884 | /* watch for "excessive" use; not generally appropriate */ |
| 885 | if (count >= gDvm.jniGrefLimit) { |
| 886 | JavaVMExt* vm = (JavaVMExt*) gDvm.vmList; |
| 887 | if (vm->warnError) { |
| 888 | dvmDumpReferenceTable(&gDvm.jniGlobalRefTable,"JNI global"); |
| 889 | LOGE("Excessive JNI global references (%d)\n", count); |
| 890 | dvmAbort(); |
| 891 | } else { |
| 892 | LOGW("Excessive JNI global references (%d)\n", count); |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | bail: |
| 899 | dvmUnlockMutex(&gDvm.jniGlobalRefLock); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 900 | return (jobject) obj; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /* |
| 904 | * Remove a global reference. In most cases it's the entry most recently |
| 905 | * added, which makes this pretty quick. |
| 906 | * |
| 907 | * Thought: if it's not the most recent entry, just null it out. When we |
| 908 | * fill up, do a compaction pass before we expand the list. |
| 909 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 910 | static void deleteGlobalReference(jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 911 | { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 912 | if (jobj == NULL) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 913 | return; |
| 914 | |
| 915 | dvmLockMutex(&gDvm.jniGlobalRefLock); |
| 916 | |
| 917 | if (!dvmRemoveFromReferenceTable(&gDvm.jniGlobalRefTable, |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 918 | gDvm.jniGlobalRefTable.table, jobj)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 919 | { |
| 920 | LOGW("JNI: DeleteGlobalRef(%p) failed to find entry (valid=%d)\n", |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 921 | jobj, dvmIsValidObject((Object*) jobj)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 922 | goto bail; |
| 923 | } |
| 924 | |
| 925 | if (kTrackGrefUsage && gDvm.jniGrefLimit != 0) { |
| 926 | int count = dvmReferenceTableEntries(&gDvm.jniGlobalRefTable); |
| 927 | if (count < gDvm.jniGlobalRefLoMark) { |
| 928 | LOGD("GREF has decreased to %d\n", count); |
| 929 | gDvm.jniGlobalRefHiMark -= kGrefWaterInterval; |
| 930 | gDvm.jniGlobalRefLoMark -= kGrefWaterInterval; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | bail: |
| 935 | dvmUnlockMutex(&gDvm.jniGlobalRefLock); |
| 936 | } |
| 937 | |
| 938 | /* |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 939 | * Objects don't currently move, so we just need to create a reference |
| 940 | * that will ensure the array object isn't collected. |
| 941 | * |
| Andy McFadden | c26bb63 | 2009-08-21 12:01:31 -0700 | [diff] [blame] | 942 | * We use a separate reference table, which is part of the GC root set. |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 943 | */ |
| 944 | static void pinPrimitiveArray(ArrayObject* arrayObj) |
| 945 | { |
| Andy McFadden | c26bb63 | 2009-08-21 12:01:31 -0700 | [diff] [blame] | 946 | if (arrayObj == NULL) |
| 947 | return; |
| 948 | |
| 949 | dvmLockMutex(&gDvm.jniPinRefLock); |
| 950 | if (!dvmAddToReferenceTable(&gDvm.jniPinRefTable, (Object*)arrayObj)) { |
| 951 | dvmDumpReferenceTable(&gDvm.jniPinRefTable, "JNI pinned array"); |
| 952 | LOGE("Failed adding to JNI pinned array ref table (%d entries)\n", |
| 953 | (int) dvmReferenceTableEntries(&gDvm.jniPinRefTable)); |
| 954 | dvmDumpThread(dvmThreadSelf(), false); |
| 955 | dvmAbort(); |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * If we're watching global ref usage, also keep an eye on these. |
| 960 | * |
| 961 | * The total number of pinned primitive arrays should be pretty small. |
| 962 | * A single array should not be pinned more than once or twice; any |
| 963 | * more than that is a strong indicator that a Release function is |
| 964 | * not being called. |
| 965 | */ |
| 966 | if (kTrackGrefUsage && gDvm.jniGrefLimit != 0) { |
| 967 | int count = 0; |
| 968 | Object** ppObj = gDvm.jniPinRefTable.table; |
| 969 | while (ppObj < gDvm.jniPinRefTable.nextEntry) { |
| 970 | if (*ppObj++ == (Object*) arrayObj) |
| 971 | count++; |
| 972 | } |
| 973 | |
| 974 | if (count > kPinComplainThreshold) { |
| 975 | LOGW("JNI: pin count on array %p (%s) is now %d\n", |
| 976 | arrayObj, arrayObj->obj.clazz->descriptor, count); |
| 977 | /* keep going */ |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | dvmUnlockMutex(&gDvm.jniPinRefLock); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Un-pin the array object. If an object was pinned twice, it must be |
| 986 | * unpinned twice before it's free to move. |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 987 | */ |
| 988 | static void unpinPrimitiveArray(ArrayObject* arrayObj) |
| 989 | { |
| Andy McFadden | c26bb63 | 2009-08-21 12:01:31 -0700 | [diff] [blame] | 990 | if (arrayObj == NULL) |
| 991 | return; |
| 992 | |
| 993 | dvmLockMutex(&gDvm.jniPinRefLock); |
| 994 | if (!dvmRemoveFromReferenceTable(&gDvm.jniPinRefTable, |
| 995 | gDvm.jniPinRefTable.table, (Object*) arrayObj)) |
| 996 | { |
| 997 | LOGW("JNI: unpinPrimitiveArray(%p) failed to find entry (valid=%d)\n", |
| 998 | arrayObj, dvmIsValidObject((Object*) arrayObj)); |
| 999 | goto bail; |
| 1000 | } |
| 1001 | |
| 1002 | bail: |
| 1003 | dvmUnlockMutex(&gDvm.jniPinRefLock); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1007 | * GC helper function to mark all JNI global references. |
| Andy McFadden | c26bb63 | 2009-08-21 12:01:31 -0700 | [diff] [blame] | 1008 | * |
| 1009 | * We're currently handling the "pin" table here too. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1010 | */ |
| 1011 | void dvmGcMarkJniGlobalRefs() |
| 1012 | { |
| 1013 | Object **op; |
| 1014 | |
| 1015 | dvmLockMutex(&gDvm.jniGlobalRefLock); |
| 1016 | |
| 1017 | op = gDvm.jniGlobalRefTable.table; |
| 1018 | while ((uintptr_t)op < (uintptr_t)gDvm.jniGlobalRefTable.nextEntry) { |
| 1019 | dvmMarkObjectNonNull(*(op++)); |
| 1020 | } |
| 1021 | |
| 1022 | dvmUnlockMutex(&gDvm.jniGlobalRefLock); |
| Andy McFadden | c26bb63 | 2009-08-21 12:01:31 -0700 | [diff] [blame] | 1023 | |
| 1024 | |
| 1025 | dvmLockMutex(&gDvm.jniPinRefLock); |
| 1026 | |
| 1027 | op = gDvm.jniPinRefTable.table; |
| 1028 | while ((uintptr_t)op < (uintptr_t)gDvm.jniPinRefTable.nextEntry) { |
| 1029 | dvmMarkObjectNonNull(*(op++)); |
| 1030 | } |
| 1031 | |
| 1032 | dvmUnlockMutex(&gDvm.jniPinRefLock); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 1036 | #if 0 |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1037 | /* |
| 1038 | * Determine if "obj" appears in the argument list for the native method. |
| 1039 | * |
| 1040 | * We use the "shorty" signature to determine which argument slots hold |
| 1041 | * reference types. |
| 1042 | */ |
| 1043 | static bool findInArgList(Thread* self, Object* obj) |
| 1044 | { |
| 1045 | const Method* meth; |
| 1046 | u4* fp; |
| 1047 | int i; |
| 1048 | |
| 1049 | fp = self->curFrame; |
| 1050 | while (1) { |
| 1051 | /* |
| 1052 | * Back up over JNI PushLocalFrame frames. This works because the |
| 1053 | * previous frame on the interpreted stack is either a break frame |
| 1054 | * (if we called here via native code) or an interpreted method (if |
| 1055 | * we called here via the interpreter). In both cases the method |
| 1056 | * pointer won't match. |
| 1057 | */ |
| 1058 | StackSaveArea* saveArea = SAVEAREA_FROM_FP(fp); |
| 1059 | meth = saveArea->method; |
| 1060 | if (meth != SAVEAREA_FROM_FP(saveArea->prevFrame)->method) |
| 1061 | break; |
| 1062 | fp = saveArea->prevFrame; |
| 1063 | } |
| 1064 | |
| 1065 | LOGVV("+++ scanning %d args in %s (%s)\n", |
| 1066 | meth->insSize, meth->name, meth->shorty); |
| 1067 | const char* shorty = meth->shorty +1; /* skip return type char */ |
| 1068 | for (i = 0; i < meth->insSize; i++) { |
| 1069 | if (i == 0 && !dvmIsStaticMethod(meth)) { |
| 1070 | /* first arg is "this" ref, not represented in "shorty" */ |
| 1071 | if (fp[i] == (u4) obj) |
| 1072 | return true; |
| 1073 | } else { |
| 1074 | /* if this is a reference type, see if it matches */ |
| 1075 | switch (*shorty) { |
| 1076 | case 'L': |
| 1077 | if (fp[i] == (u4) obj) |
| 1078 | return true; |
| 1079 | break; |
| 1080 | case 'D': |
| 1081 | case 'J': |
| 1082 | i++; |
| 1083 | break; |
| 1084 | case '\0': |
| 1085 | LOGE("Whoops! ran off the end of %s (%d)\n", |
| 1086 | meth->shorty, meth->insSize); |
| 1087 | break; |
| 1088 | default: |
| 1089 | if (fp[i] == (u4) obj) |
| 1090 | LOGI("NOTE: ref %p match on arg type %c\n", obj, *shorty); |
| 1091 | break; |
| 1092 | } |
| 1093 | shorty++; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * For static methods, we also pass a class pointer in. |
| 1099 | */ |
| 1100 | if (dvmIsStaticMethod(meth)) { |
| 1101 | //LOGI("+++ checking class pointer in %s\n", meth->name); |
| 1102 | if ((void*)obj == (void*)meth->clazz) |
| 1103 | return true; |
| 1104 | } |
| 1105 | return false; |
| 1106 | } |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 1107 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1108 | |
| 1109 | /* |
| 1110 | * Verify that a reference passed in from native code is one that the |
| 1111 | * code is allowed to have. |
| 1112 | * |
| 1113 | * It's okay for native code to pass us a reference that: |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 1114 | * - was passed in as an argument when invoked by native code (and hence |
| 1115 | * is in the JNI local refs table) |
| 1116 | * - was returned to it from JNI (and is now in the local refs table) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1117 | * - is present in the JNI global refs table |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1118 | * |
| 1119 | * Used by -Xcheck:jni and GetObjectRefType. |
| 1120 | * |
| 1121 | * NOTE: in the current VM, global and local references are identical. If |
| 1122 | * something is both global and local, we can't tell them apart, and always |
| 1123 | * return "local". |
| 1124 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1125 | jobjectRefType dvmGetJNIRefType(JNIEnv* env, jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1126 | { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1127 | ReferenceTable* pRefTable = getLocalRefTable(env); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1128 | Thread* self = dvmThreadSelf(); |
| 1129 | //Object** top; |
| 1130 | Object** ptr; |
| 1131 | |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 1132 | #if 0 |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1133 | /* check args */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1134 | if (findInArgList(self, jobj)) { |
| 1135 | //LOGI("--- REF found %p on stack\n", jobj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1136 | return JNILocalRefType; |
| 1137 | } |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 1138 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1139 | |
| 1140 | /* check locals */ |
| 1141 | //top = SAVEAREA_FROM_FP(self->curFrame)->xtra.localRefTop; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1142 | if (dvmFindInReferenceTable(pRefTable, pRefTable->table, jobj) != NULL) { |
| 1143 | //LOGI("--- REF found %p in locals\n", jobj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1144 | return JNILocalRefType; |
| 1145 | } |
| 1146 | |
| 1147 | /* check globals */ |
| 1148 | dvmLockMutex(&gDvm.jniGlobalRefLock); |
| 1149 | if (dvmFindInReferenceTable(&gDvm.jniGlobalRefTable, |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1150 | gDvm.jniGlobalRefTable.table, jobj)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1151 | { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1152 | //LOGI("--- REF found %p in globals\n", jobj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1153 | dvmUnlockMutex(&gDvm.jniGlobalRefLock); |
| 1154 | return JNIGlobalRefType; |
| 1155 | } |
| 1156 | dvmUnlockMutex(&gDvm.jniGlobalRefLock); |
| 1157 | |
| 1158 | /* not found! */ |
| 1159 | return JNIInvalidRefType; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Register a method that uses JNI calling conventions. |
| 1164 | */ |
| 1165 | static bool dvmRegisterJNIMethod(ClassObject* clazz, const char* methodName, |
| 1166 | const char* signature, void* fnPtr) |
| 1167 | { |
| 1168 | Method* method; |
| 1169 | bool result = false; |
| 1170 | |
| 1171 | if (fnPtr == NULL) |
| 1172 | goto bail; |
| 1173 | |
| 1174 | method = dvmFindDirectMethodByDescriptor(clazz, methodName, signature); |
| 1175 | if (method == NULL) |
| 1176 | method = dvmFindVirtualMethodByDescriptor(clazz, methodName, signature); |
| 1177 | if (method == NULL) { |
| 1178 | LOGW("ERROR: Unable to find decl for native %s.%s %s\n", |
| 1179 | clazz->descriptor, methodName, signature); |
| 1180 | goto bail; |
| 1181 | } |
| 1182 | |
| 1183 | if (!dvmIsNativeMethod(method)) { |
| 1184 | LOGW("Unable to register: not native: %s.%s %s\n", |
| 1185 | clazz->descriptor, methodName, signature); |
| 1186 | goto bail; |
| 1187 | } |
| 1188 | |
| 1189 | if (method->nativeFunc != dvmResolveNativeMethod) { |
| 1190 | LOGW("Warning: %s.%s %s was already registered/resolved?\n", |
| 1191 | clazz->descriptor, methodName, signature); |
| 1192 | /* keep going, I guess */ |
| 1193 | } |
| 1194 | |
| Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 1195 | dvmUseJNIBridge(method, fnPtr); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1196 | |
| 1197 | LOGV("JNI-registered %s.%s %s\n", clazz->descriptor, methodName, |
| 1198 | signature); |
| 1199 | result = true; |
| 1200 | |
| 1201 | bail: |
| 1202 | return result; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 1206 | * Returns "true" if CheckJNI is enabled in the VM. |
| 1207 | */ |
| 1208 | static bool dvmIsCheckJNIEnabled(void) |
| 1209 | { |
| 1210 | JavaVMExt* vm = (JavaVMExt*) gDvm.vmList; |
| 1211 | return vm->useChecked; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Point "method->nativeFunc" at the JNI bridge, and overload "method->insns" |
| 1216 | * to point at the actual function. |
| 1217 | */ |
| 1218 | void dvmUseJNIBridge(Method* method, void* func) |
| 1219 | { |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 1220 | enum { |
| 1221 | kJNIGeneral = 0, |
| 1222 | kJNISync = 1, |
| 1223 | kJNIVirtualNoRef = 2, |
| 1224 | kJNIStaticNoRef = 3, |
| 1225 | } kind; |
| 1226 | static const DalvikBridgeFunc stdFunc[] = { |
| 1227 | dvmCallJNIMethod_general, |
| 1228 | dvmCallJNIMethod_synchronized, |
| 1229 | dvmCallJNIMethod_virtualNoRef, |
| 1230 | dvmCallJNIMethod_staticNoRef |
| 1231 | }; |
| 1232 | static const DalvikBridgeFunc checkFunc[] = { |
| 1233 | dvmCheckCallJNIMethod_general, |
| 1234 | dvmCheckCallJNIMethod_synchronized, |
| 1235 | dvmCheckCallJNIMethod_virtualNoRef, |
| 1236 | dvmCheckCallJNIMethod_staticNoRef |
| 1237 | }; |
| 1238 | |
| 1239 | bool hasRefArg = false; |
| 1240 | |
| 1241 | if (dvmIsSynchronizedMethod(method)) { |
| 1242 | /* use version with synchronization; calls into general handler */ |
| 1243 | kind = kJNISync; |
| Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 1244 | } else { |
| Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame^] | 1245 | /* |
| 1246 | * Do a quick scan through the "shorty" signature to see if the method |
| 1247 | * takes any reference arguments. |
| 1248 | */ |
| 1249 | const char* cp = method->shorty; |
| 1250 | while (*++cp != '\0') { /* pre-incr to skip return type */ |
| 1251 | if (*cp == 'L') { |
| 1252 | /* 'L' used for both object and array references */ |
| 1253 | hasRefArg = true; |
| 1254 | break; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | if (hasRefArg) { |
| 1259 | /* use general handler to slurp up reference args */ |
| 1260 | kind = kJNIGeneral; |
| 1261 | } else { |
| 1262 | /* virtual methods have a ref in args[0] (not in signature) */ |
| 1263 | if (dvmIsStaticMethod(method)) |
| 1264 | kind = kJNIStaticNoRef; |
| 1265 | else |
| 1266 | kind = kJNIVirtualNoRef; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | if (dvmIsCheckJNIEnabled()) { |
| 1271 | dvmSetNativeFunc(method, checkFunc[kind], func); |
| 1272 | } else { |
| 1273 | dvmSetNativeFunc(method, stdFunc[kind], func); |
| Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1278 | * Get the method currently being executed by examining the interp stack. |
| 1279 | */ |
| 1280 | const Method* dvmGetCurrentJNIMethod(void) |
| 1281 | { |
| 1282 | assert(dvmThreadSelf() != NULL); |
| 1283 | |
| 1284 | void* fp = dvmThreadSelf()->curFrame; |
| 1285 | const Method* meth = SAVEAREA_FROM_FP(fp)->method; |
| 1286 | |
| 1287 | assert(meth != NULL); |
| 1288 | assert(dvmIsNativeMethod(meth)); |
| 1289 | return meth; |
| 1290 | } |
| 1291 | |
| 1292 | |
| 1293 | /* |
| 1294 | * Track a JNI MonitorEnter in the current thread. |
| 1295 | * |
| 1296 | * The goal is to be able to "implicitly" release all JNI-held monitors |
| 1297 | * when the thread detaches. |
| 1298 | * |
| 1299 | * Monitors may be entered multiple times, so we add a new entry for each |
| 1300 | * enter call. It would be more efficient to keep a counter. At present |
| 1301 | * there's no real motivation to improve this however. |
| 1302 | */ |
| 1303 | static void trackMonitorEnter(Thread* self, Object* obj) |
| 1304 | { |
| 1305 | static const int kInitialSize = 16; |
| 1306 | ReferenceTable* refTable = &self->jniMonitorRefTable; |
| 1307 | |
| 1308 | /* init table on first use */ |
| 1309 | if (refTable->table == NULL) { |
| 1310 | assert(refTable->maxEntries == 0); |
| 1311 | |
| 1312 | if (!dvmInitReferenceTable(refTable, kInitialSize, INT_MAX)) { |
| 1313 | LOGE("Unable to initialize monitor tracking table\n"); |
| 1314 | dvmAbort(); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | if (!dvmAddToReferenceTable(refTable, obj)) { |
| 1319 | /* ran out of memory? could throw exception instead */ |
| 1320 | LOGE("Unable to add entry to monitor tracking table\n"); |
| 1321 | dvmAbort(); |
| 1322 | } else { |
| 1323 | LOGVV("--- added monitor %p\n", obj); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1327 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1328 | /* |
| 1329 | * Track a JNI MonitorExit in the current thread. |
| 1330 | */ |
| 1331 | static void trackMonitorExit(Thread* self, Object* obj) |
| 1332 | { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1333 | ReferenceTable* pRefTable = &self->jniMonitorRefTable; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1334 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1335 | if (!dvmRemoveFromReferenceTable(pRefTable, pRefTable->table, obj)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1336 | LOGE("JNI monitor %p not found in tracking list\n", obj); |
| 1337 | /* keep going? */ |
| 1338 | } else { |
| 1339 | LOGVV("--- removed monitor %p\n", obj); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | /* |
| 1344 | * Release all monitors held by the jniMonitorRefTable list. |
| 1345 | */ |
| 1346 | void dvmReleaseJniMonitors(Thread* self) |
| 1347 | { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1348 | ReferenceTable* pRefTable = &self->jniMonitorRefTable; |
| 1349 | Object** top = pRefTable->table; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1350 | |
| 1351 | if (top == NULL) |
| 1352 | return; |
| 1353 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1354 | Object** ptr = pRefTable->nextEntry; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1355 | while (--ptr >= top) { |
| 1356 | if (!dvmUnlockObject(self, *ptr)) { |
| 1357 | LOGW("Unable to unlock monitor %p at thread detach\n", *ptr); |
| 1358 | } else { |
| 1359 | LOGVV("--- detach-releasing monitor %p\n", *ptr); |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | /* zap it */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1364 | pRefTable->nextEntry = pRefTable->table; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | #ifdef WITH_JNI_STACK_CHECK |
| 1368 | /* |
| 1369 | * Compute a CRC on the entire interpreted stack. |
| 1370 | * |
| 1371 | * Would be nice to compute it on "self" as well, but there are parts of |
| 1372 | * the Thread that can be altered by other threads (e.g. prev/next pointers). |
| 1373 | */ |
| 1374 | static void computeStackSum(Thread* self) |
| 1375 | { |
| 1376 | const u1* low = (const u1*)SAVEAREA_FROM_FP(self->curFrame); |
| 1377 | u4 crc = dvmInitCrc32(); |
| 1378 | self->stackCrc = 0; |
| 1379 | crc = dvmComputeCrc32(crc, low, self->interpStackStart - low); |
| 1380 | self->stackCrc = crc; |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Compute a CRC on the entire interpreted stack, and compare it to what |
| 1385 | * we previously computed. |
| 1386 | * |
| 1387 | * We can execute JNI directly from native code without calling in from |
| 1388 | * interpreted code during VM initialization and immediately after JNI |
| 1389 | * thread attachment. Another opportunity exists during JNI_OnLoad. Rather |
| 1390 | * than catching these cases we just ignore them here, which is marginally |
| 1391 | * less accurate but reduces the amount of code we have to touch with #ifdefs. |
| 1392 | */ |
| 1393 | static void checkStackSum(Thread* self) |
| 1394 | { |
| 1395 | const u1* low = (const u1*)SAVEAREA_FROM_FP(self->curFrame); |
| 1396 | u4 stackCrc, crc; |
| 1397 | |
| 1398 | stackCrc = self->stackCrc; |
| 1399 | self->stackCrc = 0; |
| 1400 | crc = dvmInitCrc32(); |
| 1401 | crc = dvmComputeCrc32(crc, low, self->interpStackStart - low); |
| 1402 | if (crc != stackCrc) { |
| 1403 | const Method* meth = dvmGetCurrentJNIMethod(); |
| 1404 | if (dvmComputeExactFrameDepth(self->curFrame) == 1) { |
| 1405 | LOGD("JNI: bad stack CRC (0x%08x) -- okay during init\n", |
| 1406 | stackCrc); |
| 1407 | } else if (strcmp(meth->name, "nativeLoad") == 0 && |
| 1408 | (strcmp(meth->clazz->descriptor, "Ljava/lang/Runtime;") == 0)) |
| 1409 | { |
| 1410 | LOGD("JNI: bad stack CRC (0x%08x) -- okay during JNI_OnLoad\n", |
| 1411 | stackCrc); |
| 1412 | } else { |
| 1413 | LOGW("JNI: bad stack CRC (%08x vs %08x)\n", crc, stackCrc); |
| 1414 | dvmAbort(); |
| 1415 | } |
| 1416 | } |
| 1417 | self->stackCrc = (u4) -1; /* make logic errors more noticeable */ |
| 1418 | } |
| 1419 | #endif |
| 1420 | |
| 1421 | |
| 1422 | /* |
| 1423 | * =========================================================================== |
| 1424 | * JNI implementation |
| 1425 | * =========================================================================== |
| 1426 | */ |
| 1427 | |
| 1428 | /* |
| 1429 | * Return the version of the native method interface. |
| 1430 | */ |
| 1431 | static jint GetVersion(JNIEnv* env) |
| 1432 | { |
| 1433 | JNI_ENTER(); |
| 1434 | /* |
| 1435 | * There is absolutely no need to toggle the mode for correct behavior. |
| 1436 | * However, it does provide native code with a simple "suspend self |
| 1437 | * if necessary" call. |
| 1438 | */ |
| 1439 | JNI_EXIT(); |
| 1440 | return JNI_VERSION_1_6; |
| 1441 | } |
| 1442 | |
| 1443 | /* |
| 1444 | * Create a new class from a bag of bytes. |
| 1445 | * |
| 1446 | * This is not currently supported within Dalvik. |
| 1447 | */ |
| 1448 | static jclass DefineClass(JNIEnv* env, const char *name, jobject loader, |
| 1449 | const jbyte* buf, jsize bufLen) |
| 1450 | { |
| 1451 | UNUSED_PARAMETER(name); |
| 1452 | UNUSED_PARAMETER(loader); |
| 1453 | UNUSED_PARAMETER(buf); |
| 1454 | UNUSED_PARAMETER(bufLen); |
| 1455 | |
| 1456 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1457 | LOGW("JNI DefineClass is not supported\n"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1458 | JNI_EXIT(); |
| 1459 | return NULL; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
| 1463 | * Find a class by name. |
| 1464 | * |
| 1465 | * We have to use the "no init" version of FindClass here, because we might |
| 1466 | * be getting the class prior to registering native methods that will be |
| 1467 | * used in <clinit>. |
| 1468 | * |
| 1469 | * We need to get the class loader associated with the current native |
| 1470 | * method. If there is no native method, e.g. we're calling this from native |
| 1471 | * code right after creating the VM, the spec says we need to use the class |
| 1472 | * loader returned by "ClassLoader.getBaseClassLoader". There is no such |
| 1473 | * method, but it's likely they meant ClassLoader.getSystemClassLoader. |
| 1474 | * We can't get that until after the VM has initialized though. |
| 1475 | */ |
| 1476 | static jclass FindClass(JNIEnv* env, const char* name) |
| 1477 | { |
| 1478 | JNI_ENTER(); |
| 1479 | |
| 1480 | const Method* thisMethod; |
| 1481 | ClassObject* clazz; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1482 | jclass jclazz = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1483 | Object* loader; |
| 1484 | char* descriptor = NULL; |
| 1485 | |
| 1486 | thisMethod = dvmGetCurrentJNIMethod(); |
| 1487 | assert(thisMethod != NULL); |
| 1488 | |
| 1489 | descriptor = dvmNameToDescriptor(name); |
| 1490 | if (descriptor == NULL) { |
| 1491 | clazz = NULL; |
| 1492 | goto bail; |
| 1493 | } |
| 1494 | |
| 1495 | //Thread* self = dvmThreadSelf(); |
| 1496 | if (_self->classLoaderOverride != NULL) { |
| 1497 | /* hack for JNI_OnLoad */ |
| 1498 | assert(strcmp(thisMethod->name, "nativeLoad") == 0); |
| 1499 | loader = _self->classLoaderOverride; |
| 1500 | } else if (thisMethod == gDvm.methFakeNativeEntry) { |
| 1501 | /* start point of invocation interface */ |
| 1502 | if (!gDvm.initializing) |
| 1503 | loader = dvmGetSystemClassLoader(); |
| 1504 | else |
| 1505 | loader = NULL; |
| 1506 | } else { |
| 1507 | loader = thisMethod->clazz->classLoader; |
| 1508 | } |
| 1509 | |
| 1510 | clazz = dvmFindClassNoInit(descriptor, loader); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1511 | jclazz = addLocalReference(env, (Object*) clazz); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1512 | |
| 1513 | bail: |
| 1514 | free(descriptor); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1515 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1516 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1517 | return jclazz; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * Return the superclass of a class. |
| 1522 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1523 | static jclass GetSuperclass(JNIEnv* env, jclass jclazz) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1524 | { |
| 1525 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1526 | jclass jsuper; |
| 1527 | |
| 1528 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| 1529 | if (clazz == NULL) |
| 1530 | jsuper = NULL; |
| 1531 | else |
| 1532 | jsuper = addLocalReference(env, (Object*)clazz->super); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1533 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1534 | return jsuper; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | * Determine whether an object of clazz1 can be safely cast to clazz2. |
| 1539 | * |
| 1540 | * Like IsInstanceOf, but with a pair of class objects instead of obj+class. |
| 1541 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1542 | static jboolean IsAssignableFrom(JNIEnv* env, jclass jclazz1, jclass jclazz2) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1543 | { |
| 1544 | JNI_ENTER(); |
| 1545 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1546 | ClassObject* clazz1 = (ClassObject*) dvmDecodeIndirectRef(env, jclazz1); |
| 1547 | ClassObject* clazz2 = (ClassObject*) dvmDecodeIndirectRef(env, jclazz2); |
| 1548 | |
| 1549 | jboolean result = dvmInstanceof(clazz1, clazz2); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1550 | |
| 1551 | JNI_EXIT(); |
| 1552 | return result; |
| 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | * Given a java.lang.reflect.Method or .Constructor, return a methodID. |
| 1557 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1558 | static jmethodID FromReflectedMethod(JNIEnv* env, jobject jmethod) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1559 | { |
| 1560 | JNI_ENTER(); |
| 1561 | jmethodID methodID; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1562 | Object* method = dvmDecodeIndirectRef(env, jmethod); |
| 1563 | methodID = (jmethodID) dvmGetMethodFromReflectObj(method); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1564 | JNI_EXIT(); |
| 1565 | return methodID; |
| 1566 | } |
| 1567 | |
| 1568 | /* |
| 1569 | * Given a java.lang.reflect.Field, return a fieldID. |
| 1570 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1571 | static jfieldID FromReflectedField(JNIEnv* env, jobject jfield) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1572 | { |
| 1573 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1574 | jfieldID fieldID; |
| 1575 | Object* field = dvmDecodeIndirectRef(env, jfield); |
| 1576 | fieldID = (jfieldID) dvmGetFieldFromReflectObj(field); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1577 | JNI_EXIT(); |
| 1578 | return fieldID; |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * Convert a methodID to a java.lang.reflect.Method or .Constructor. |
| 1583 | * |
| 1584 | * (The "isStatic" field does not appear in the spec.) |
| 1585 | * |
| 1586 | * Throws OutOfMemory and returns NULL on failure. |
| 1587 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1588 | static jobject ToReflectedMethod(JNIEnv* env, jclass jcls, jmethodID methodID, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1589 | jboolean isStatic) |
| 1590 | { |
| 1591 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1592 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jcls); |
| 1593 | Object* obj = dvmCreateReflectObjForMethod(clazz, (Method*) methodID); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1594 | dvmReleaseTrackedAlloc(obj, NULL); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1595 | jobject jobj = addLocalReference(env, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1596 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1597 | return jobj; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * Convert a fieldID to a java.lang.reflect.Field. |
| 1602 | * |
| 1603 | * (The "isStatic" field does not appear in the spec.) |
| 1604 | * |
| 1605 | * Throws OutOfMemory and returns NULL on failure. |
| 1606 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1607 | static jobject ToReflectedField(JNIEnv* env, jclass jcls, jfieldID fieldID, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1608 | jboolean isStatic) |
| 1609 | { |
| 1610 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1611 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jcls); |
| 1612 | Object* obj = dvmCreateReflectObjForField(jcls, (Field*) fieldID); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1613 | dvmReleaseTrackedAlloc(obj, NULL); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1614 | jobject jobj = addLocalReference(env, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1615 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1616 | return jobj; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1617 | } |
| 1618 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1619 | /* |
| 1620 | * Take this exception and throw it. |
| 1621 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1622 | static jint Throw(JNIEnv* env, jthrowable jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1623 | { |
| 1624 | JNI_ENTER(); |
| 1625 | |
| 1626 | jint retval; |
| 1627 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1628 | if (jobj != NULL) { |
| 1629 | Object* obj = dvmDecodeIndirectRef(env, jobj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1630 | dvmSetException(_self, obj); |
| 1631 | retval = JNI_OK; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1632 | } else { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1633 | retval = JNI_ERR; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1634 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1635 | |
| 1636 | JNI_EXIT(); |
| 1637 | return retval; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1641 | * Constructs an exception object from the specified class with the message |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1642 | * specified by "message", and throws it. |
| 1643 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1644 | static jint ThrowNew(JNIEnv* env, jclass jclazz, const char* message) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1645 | { |
| 1646 | JNI_ENTER(); |
| 1647 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1648 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| 1649 | dvmThrowExceptionByClass(clazz, message); |
| 1650 | // TODO: should return failure if this didn't work (e.g. OOM) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1651 | |
| 1652 | JNI_EXIT(); |
| 1653 | return JNI_OK; |
| 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * If an exception is being thrown, return the exception object. Otherwise, |
| 1658 | * return NULL. |
| 1659 | * |
| 1660 | * TODO: if there is no pending exception, we should be able to skip the |
| 1661 | * enter/exit checks. If we find one, we need to enter and then re-fetch |
| 1662 | * the exception (in case it got moved by a compacting GC). |
| 1663 | */ |
| 1664 | static jthrowable ExceptionOccurred(JNIEnv* env) |
| 1665 | { |
| 1666 | JNI_ENTER(); |
| 1667 | |
| 1668 | Object* exception; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1669 | jobject localException; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1670 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1671 | exception = dvmGetException(_self); |
| 1672 | localException = addLocalReference(env, exception); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1673 | if (localException == NULL && exception != NULL) { |
| 1674 | /* |
| 1675 | * We were unable to add a new local reference, and threw a new |
| 1676 | * exception. We can't return "exception", because it's not a |
| 1677 | * local reference. So we have to return NULL, indicating that |
| 1678 | * there was no exception, even though it's pretty much raining |
| 1679 | * exceptions in here. |
| 1680 | */ |
| 1681 | LOGW("JNI WARNING: addLocal/exception combo\n"); |
| 1682 | } |
| 1683 | |
| 1684 | JNI_EXIT(); |
| 1685 | return localException; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Print an exception and stack trace to stderr. |
| 1690 | */ |
| 1691 | static void ExceptionDescribe(JNIEnv* env) |
| 1692 | { |
| 1693 | JNI_ENTER(); |
| 1694 | |
| 1695 | Object* exception = dvmGetException(_self); |
| 1696 | if (exception != NULL) { |
| 1697 | dvmPrintExceptionStackTrace(); |
| 1698 | } else { |
| 1699 | LOGI("Odd: ExceptionDescribe called, but no exception pending\n"); |
| 1700 | } |
| 1701 | |
| 1702 | JNI_EXIT(); |
| 1703 | } |
| 1704 | |
| 1705 | /* |
| 1706 | * Clear the exception currently being thrown. |
| 1707 | * |
| 1708 | * TODO: we should be able to skip the enter/exit stuff. |
| 1709 | */ |
| 1710 | static void ExceptionClear(JNIEnv* env) |
| 1711 | { |
| 1712 | JNI_ENTER(); |
| 1713 | dvmClearException(_self); |
| 1714 | JNI_EXIT(); |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * Kill the VM. This function does not return. |
| 1719 | */ |
| 1720 | static void FatalError(JNIEnv* env, const char* msg) |
| 1721 | { |
| 1722 | //dvmChangeStatus(NULL, THREAD_RUNNING); |
| 1723 | LOGE("JNI posting fatal error: %s\n", msg); |
| 1724 | dvmAbort(); |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * Push a new JNI frame on the stack, with a new set of locals. |
| 1729 | * |
| 1730 | * The new frame must have the same method pointer. (If for no other |
| 1731 | * reason than FindClass needs it to get the appropriate class loader.) |
| 1732 | */ |
| 1733 | static jint PushLocalFrame(JNIEnv* env, jint capacity) |
| 1734 | { |
| 1735 | JNI_ENTER(); |
| 1736 | int result = JNI_OK; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1737 | if (!ensureLocalCapacity(env, capacity) || |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1738 | !dvmPushLocalFrame(_self /*dvmThreadSelf()*/, dvmGetCurrentJNIMethod())) |
| 1739 | { |
| 1740 | /* yes, OutOfMemoryError, not StackOverflowError */ |
| 1741 | dvmClearException(_self); |
| 1742 | dvmThrowException("Ljava/lang/OutOfMemoryError;", |
| 1743 | "out of stack in JNI PushLocalFrame"); |
| 1744 | result = JNI_ERR; |
| 1745 | } |
| 1746 | JNI_EXIT(); |
| 1747 | return result; |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | * Pop the local frame off. If "result" is not null, add it as a |
| 1752 | * local reference on the now-current frame. |
| 1753 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1754 | static jobject PopLocalFrame(JNIEnv* env, jobject jresult) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1755 | { |
| 1756 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1757 | Object* result = dvmDecodeIndirectRef(env, jresult); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1758 | if (!dvmPopLocalFrame(_self /*dvmThreadSelf()*/)) { |
| 1759 | LOGW("JNI WARNING: too many PopLocalFrame calls\n"); |
| 1760 | dvmClearException(_self); |
| 1761 | dvmThrowException("Ljava/lang/RuntimeException;", |
| 1762 | "too many PopLocalFrame calls"); |
| 1763 | } |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1764 | jresult = addLocalReference(env, result); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1765 | JNI_EXIT(); |
| 1766 | return result; |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * Add a reference to the global list. |
| 1771 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1772 | static jobject NewGlobalRef(JNIEnv* env, jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1773 | { |
| 1774 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1775 | Object* obj = dvmDecodeIndirectRef(env, jobj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1776 | jobject retval = addGlobalReference(obj); |
| 1777 | JNI_EXIT(); |
| 1778 | return retval; |
| 1779 | } |
| 1780 | |
| 1781 | /* |
| 1782 | * Delete a reference from the global list. |
| 1783 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1784 | static void DeleteGlobalRef(JNIEnv* env, jobject jglobalRef) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1785 | { |
| 1786 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1787 | deleteGlobalReference(jglobalRef); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1788 | JNI_EXIT(); |
| 1789 | } |
| 1790 | |
| 1791 | |
| 1792 | /* |
| 1793 | * Add a reference to the local list. |
| 1794 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1795 | static jobject NewLocalRef(JNIEnv* env, jobject jref) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1796 | { |
| 1797 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1798 | Object* obj = dvmDecodeIndirectRef(env, jref); |
| 1799 | jobject retval = addLocalReference(env, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1800 | JNI_EXIT(); |
| 1801 | return retval; |
| 1802 | } |
| 1803 | |
| 1804 | /* |
| 1805 | * Delete a reference from the local list. |
| 1806 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1807 | static void DeleteLocalRef(JNIEnv* env, jobject jlocalRef) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1808 | { |
| 1809 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1810 | deleteLocalReference(env, jlocalRef); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1811 | JNI_EXIT(); |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * Ensure that the local references table can hold at least this many |
| 1816 | * references. |
| 1817 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1818 | static jint EnsureLocalCapacity(JNIEnv* env, jint capacity) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1819 | { |
| 1820 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1821 | bool okay = ensureLocalCapacity(env, capacity); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1822 | if (!okay) { |
| 1823 | dvmThrowException("Ljava/lang/OutOfMemoryError;", |
| 1824 | "can't ensure local reference capacity"); |
| 1825 | } |
| 1826 | JNI_EXIT(); |
| 1827 | if (okay) |
| 1828 | return 0; |
| 1829 | else |
| 1830 | return -1; |
| 1831 | } |
| 1832 | |
| 1833 | |
| 1834 | /* |
| 1835 | * Determine whether two Object references refer to the same underlying object. |
| 1836 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1837 | static jboolean IsSameObject(JNIEnv* env, jobject jref1, jobject jref2) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1838 | { |
| 1839 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1840 | Object* obj1 = dvmDecodeIndirectRef(env, jref1); |
| 1841 | Object* obj2 = dvmDecodeIndirectRef(env, jref2); |
| 1842 | jboolean result = (obj1 == obj2); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1843 | JNI_EXIT(); |
| 1844 | return result; |
| 1845 | } |
| 1846 | |
| 1847 | /* |
| 1848 | * Allocate a new object without invoking any constructors. |
| 1849 | */ |
| 1850 | static jobject AllocObject(JNIEnv* env, jclass jclazz) |
| 1851 | { |
| 1852 | JNI_ENTER(); |
| 1853 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1854 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| 1855 | jobject result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1856 | |
| 1857 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) { |
| 1858 | assert(dvmCheckException(_self)); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1859 | result = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1860 | } else { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1861 | Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 1862 | result = addLocalReference(env, newObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1866 | return result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | /* |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1870 | * Allocate a new object and invoke the supplied constructor. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1871 | */ |
| 1872 | static jobject NewObject(JNIEnv* env, jclass jclazz, jmethodID methodID, ...) |
| 1873 | { |
| 1874 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1875 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| 1876 | jobject result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1877 | |
| 1878 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) { |
| 1879 | assert(dvmCheckException(_self)); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1880 | result = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1881 | } else { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1882 | Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 1883 | result = addLocalReference(env, newObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1884 | if (newObj != NULL) { |
| 1885 | JValue unused; |
| 1886 | va_list args; |
| 1887 | va_start(args, methodID); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1888 | dvmCallMethodV(_self, (Method*) methodID, newObj, &unused, args); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1889 | va_end(args); |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1894 | return result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1895 | } |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1896 | static jobject NewObjectV(JNIEnv* env, jclass jclazz, jmethodID methodID, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1897 | va_list args) |
| 1898 | { |
| 1899 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1900 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| 1901 | jobject result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1902 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1903 | Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 1904 | result = addLocalReference(env, newObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1905 | if (newObj != NULL) { |
| 1906 | JValue unused; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1907 | dvmCallMethodV(_self, (Method*) methodID, newObj, &unused, args); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1911 | return result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1912 | } |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1913 | static jobject NewObjectA(JNIEnv* env, jclass jclazz, jmethodID methodID, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1914 | jvalue* args) |
| 1915 | { |
| 1916 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1917 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| 1918 | jobject result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1919 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1920 | Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 1921 | result = addLocalReference(env, newObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1922 | if (newObj != NULL) { |
| 1923 | JValue unused; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1924 | dvmCallMethodA(_self, (Method*) methodID, newObj, &unused, args); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1928 | return result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | /* |
| 1932 | * Returns the class of an object. |
| 1933 | * |
| 1934 | * JNI spec says: obj must not be NULL. |
| 1935 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1936 | static jclass GetObjectClass(JNIEnv* env, jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1937 | { |
| 1938 | JNI_ENTER(); |
| 1939 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1940 | assert(jobj != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1941 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1942 | Object* obj = dvmDecodeIndirectRef(env, jobj); |
| 1943 | jclass jclazz = addLocalReference(env, (Object*) obj->clazz); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1944 | |
| 1945 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1946 | return jclazz; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | /* |
| 1950 | * Determine whether "obj" is an instance of "clazz". |
| 1951 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1952 | static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass jclazz) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1953 | { |
| 1954 | JNI_ENTER(); |
| 1955 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1956 | assert(jclazz != NULL); |
| 1957 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1958 | jboolean result; |
| 1959 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1960 | if (jobj == NULL) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1961 | result = true; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1962 | } else { |
| 1963 | Object* obj = dvmDecodeIndirectRef(env, jobj); |
| 1964 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| 1965 | result = dvmInstanceof(obj->clazz, clazz); |
| 1966 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1967 | |
| 1968 | JNI_EXIT(); |
| 1969 | return result; |
| 1970 | } |
| 1971 | |
| 1972 | /* |
| 1973 | * Get a method ID for an instance method. |
| 1974 | * |
| 1975 | * JNI defines <init> as an instance method, but Dalvik considers it a |
| 1976 | * "direct" method, so we have to special-case it here. |
| 1977 | * |
| 1978 | * Dalvik also puts all private methods into the "direct" list, so we |
| 1979 | * really need to just search both lists. |
| 1980 | */ |
| 1981 | static jmethodID GetMethodID(JNIEnv* env, jclass jclazz, const char* name, |
| 1982 | const char* sig) |
| 1983 | { |
| 1984 | JNI_ENTER(); |
| 1985 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 1986 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1987 | jmethodID id = NULL; |
| 1988 | |
| 1989 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) { |
| 1990 | assert(dvmCheckException(_self)); |
| 1991 | } else { |
| 1992 | Method* meth; |
| 1993 | |
| 1994 | meth = dvmFindVirtualMethodHierByDescriptor(clazz, name, sig); |
| 1995 | if (meth == NULL) { |
| 1996 | /* search private methods and constructors; non-hierarchical */ |
| 1997 | meth = dvmFindDirectMethodByDescriptor(clazz, name, sig); |
| 1998 | } |
| 1999 | if (meth != NULL && dvmIsStaticMethod(meth)) { |
| 2000 | IF_LOGD() { |
| 2001 | char* desc = dexProtoCopyMethodDescriptor(&meth->prototype); |
| 2002 | LOGD("GetMethodID: not returning static method %s.%s %s\n", |
| 2003 | clazz->descriptor, meth->name, desc); |
| 2004 | free(desc); |
| 2005 | } |
| 2006 | meth = NULL; |
| 2007 | } |
| 2008 | if (meth == NULL) { |
| Andy McFadden | 03bd0d5 | 2009-08-18 15:32:27 -0700 | [diff] [blame] | 2009 | LOGD("GetMethodID: method not found: %s.%s:%s\n", |
| 2010 | clazz->descriptor, name, sig); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2011 | dvmThrowException("Ljava/lang/NoSuchMethodError;", name); |
| 2012 | } |
| 2013 | |
| 2014 | /* |
| 2015 | * The method's class may not be the same as clazz, but if |
| 2016 | * it isn't this must be a virtual method and the class must |
| 2017 | * be a superclass (and, hence, already initialized). |
| 2018 | */ |
| 2019 | if (meth != NULL) { |
| 2020 | assert(dvmIsClassInitialized(meth->clazz) || |
| 2021 | dvmIsClassInitializing(meth->clazz)); |
| 2022 | } |
| 2023 | id = (jmethodID) meth; |
| 2024 | } |
| 2025 | JNI_EXIT(); |
| 2026 | return id; |
| 2027 | } |
| 2028 | |
| 2029 | /* |
| 2030 | * Get a field ID (instance fields). |
| 2031 | */ |
| 2032 | static jfieldID GetFieldID(JNIEnv* env, jclass jclazz, |
| 2033 | const char* name, const char* sig) |
| 2034 | { |
| 2035 | JNI_ENTER(); |
| 2036 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2037 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2038 | jfieldID id; |
| 2039 | |
| 2040 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) { |
| 2041 | assert(dvmCheckException(_self)); |
| 2042 | id = NULL; |
| 2043 | } else { |
| 2044 | id = (jfieldID) dvmFindInstanceFieldHier(clazz, name, sig); |
| Andy McFadden | 03bd0d5 | 2009-08-18 15:32:27 -0700 | [diff] [blame] | 2045 | if (id == NULL) { |
| 2046 | LOGD("GetFieldID: unable to find field %s.%s:%s\n", |
| 2047 | clazz->descriptor, name, sig); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2048 | dvmThrowException("Ljava/lang/NoSuchFieldError;", name); |
| Andy McFadden | 03bd0d5 | 2009-08-18 15:32:27 -0700 | [diff] [blame] | 2049 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2050 | } |
| 2051 | JNI_EXIT(); |
| 2052 | return id; |
| 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * Get the method ID for a static method in a class. |
| 2057 | */ |
| 2058 | static jmethodID GetStaticMethodID(JNIEnv* env, jclass jclazz, |
| 2059 | const char* name, const char* sig) |
| 2060 | { |
| 2061 | JNI_ENTER(); |
| 2062 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2063 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2064 | jmethodID id; |
| 2065 | |
| 2066 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) { |
| 2067 | assert(dvmCheckException(_self)); |
| 2068 | id = NULL; |
| 2069 | } else { |
| 2070 | Method* meth; |
| 2071 | |
| 2072 | meth = dvmFindDirectMethodHierByDescriptor(clazz, name, sig); |
| 2073 | |
| 2074 | /* make sure it's static, not virtual+private */ |
| 2075 | if (meth != NULL && !dvmIsStaticMethod(meth)) { |
| 2076 | IF_LOGD() { |
| 2077 | char* desc = dexProtoCopyMethodDescriptor(&meth->prototype); |
| 2078 | LOGD("GetStaticMethodID: " |
| 2079 | "not returning nonstatic method %s.%s %s\n", |
| 2080 | clazz->descriptor, meth->name, desc); |
| 2081 | free(desc); |
| 2082 | } |
| 2083 | meth = NULL; |
| 2084 | } |
| 2085 | |
| 2086 | id = (jmethodID) meth; |
| 2087 | if (id == NULL) |
| 2088 | dvmThrowException("Ljava/lang/NoSuchMethodError;", name); |
| 2089 | } |
| 2090 | |
| 2091 | JNI_EXIT(); |
| 2092 | return id; |
| 2093 | } |
| 2094 | |
| 2095 | /* |
| 2096 | * Get a field ID (static fields). |
| 2097 | */ |
| 2098 | static jfieldID GetStaticFieldID(JNIEnv* env, jclass jclazz, |
| 2099 | const char* name, const char* sig) |
| 2100 | { |
| 2101 | JNI_ENTER(); |
| 2102 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2103 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2104 | jfieldID id; |
| 2105 | |
| 2106 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) { |
| 2107 | assert(dvmCheckException(_self)); |
| 2108 | id = NULL; |
| 2109 | } else { |
| 2110 | id = (jfieldID) dvmFindStaticField(clazz, name, sig); |
| 2111 | if (id == NULL) |
| 2112 | dvmThrowException("Ljava/lang/NoSuchFieldError;", name); |
| 2113 | } |
| 2114 | JNI_EXIT(); |
| 2115 | return id; |
| 2116 | } |
| 2117 | |
| 2118 | /* |
| 2119 | * Get a static field. |
| 2120 | * |
| 2121 | * If we get an object reference, add it to the local refs list. |
| 2122 | */ |
| 2123 | #define GET_STATIC_TYPE_FIELD(_ctype, _jname, _isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2124 | static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass jclazz, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2125 | jfieldID fieldID) \ |
| 2126 | { \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2127 | UNUSED_PARAMETER(jclazz); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2128 | JNI_ENTER(); \ |
| 2129 | StaticField* sfield = (StaticField*) fieldID; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2130 | _ctype value; \ |
| 2131 | if (_isref) { /* only when _ctype==jobject */ \ |
| 2132 | Object* obj = dvmGetStaticFieldObject(sfield); \ |
| 2133 | value = (_ctype)(u4)addLocalReference(env, obj); \ |
| 2134 | } else { \ |
| 2135 | value = dvmGetStaticField##_jname(sfield); \ |
| 2136 | } \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2137 | JNI_EXIT(); \ |
| 2138 | return value; \ |
| 2139 | } |
| 2140 | GET_STATIC_TYPE_FIELD(jobject, Object, true); |
| 2141 | GET_STATIC_TYPE_FIELD(jboolean, Boolean, false); |
| 2142 | GET_STATIC_TYPE_FIELD(jbyte, Byte, false); |
| 2143 | GET_STATIC_TYPE_FIELD(jchar, Char, false); |
| 2144 | GET_STATIC_TYPE_FIELD(jshort, Short, false); |
| 2145 | GET_STATIC_TYPE_FIELD(jint, Int, false); |
| 2146 | GET_STATIC_TYPE_FIELD(jlong, Long, false); |
| 2147 | GET_STATIC_TYPE_FIELD(jfloat, Float, false); |
| 2148 | GET_STATIC_TYPE_FIELD(jdouble, Double, false); |
| 2149 | |
| 2150 | /* |
| 2151 | * Set a static field. |
| 2152 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2153 | #define SET_STATIC_TYPE_FIELD(_ctype, _jname, _isref) \ |
| 2154 | static void SetStatic##_jname##Field(JNIEnv* env, jclass jclazz, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2155 | jfieldID fieldID, _ctype value) \ |
| 2156 | { \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2157 | UNUSED_PARAMETER(jclazz); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2158 | JNI_ENTER(); \ |
| 2159 | StaticField* sfield = (StaticField*) fieldID; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2160 | if (_isref) { /* only when _ctype==jobject */ \ |
| 2161 | Object* valObj = dvmDecodeIndirectRef(env, (jobject)(u4)value); \ |
| 2162 | dvmSetStaticFieldObject(sfield, valObj); \ |
| 2163 | } else { \ |
| 2164 | dvmSetStaticField##_jname(sfield, value); \ |
| 2165 | } \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2166 | JNI_EXIT(); \ |
| 2167 | } |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2168 | SET_STATIC_TYPE_FIELD(jobject, Object, true); |
| 2169 | SET_STATIC_TYPE_FIELD(jboolean, Boolean, false); |
| 2170 | SET_STATIC_TYPE_FIELD(jbyte, Byte, false); |
| 2171 | SET_STATIC_TYPE_FIELD(jchar, Char, false); |
| 2172 | SET_STATIC_TYPE_FIELD(jshort, Short, false); |
| 2173 | SET_STATIC_TYPE_FIELD(jint, Int, false); |
| 2174 | SET_STATIC_TYPE_FIELD(jlong, Long, false); |
| 2175 | SET_STATIC_TYPE_FIELD(jfloat, Float, false); |
| 2176 | SET_STATIC_TYPE_FIELD(jdouble, Double, false); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2177 | |
| 2178 | /* |
| 2179 | * Get an instance field. |
| 2180 | * |
| 2181 | * If we get an object reference, add it to the local refs list. |
| 2182 | */ |
| 2183 | #define GET_TYPE_FIELD(_ctype, _jname, _isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2184 | static _ctype Get##_jname##Field(JNIEnv* env, jobject jobj, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2185 | jfieldID fieldID) \ |
| 2186 | { \ |
| 2187 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2188 | Object* obj = dvmDecodeIndirectRef(env, jobj); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2189 | InstField* field = (InstField*) fieldID; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2190 | _ctype value; \ |
| 2191 | if (_isref) { /* only when _ctype==jobject */ \ |
| 2192 | Object* valObj = dvmGetFieldObject(obj, field->byteOffset); \ |
| 2193 | value = (_ctype)(u4)addLocalReference(env, valObj); \ |
| 2194 | } else { \ |
| 2195 | value = dvmGetField##_jname(obj, field->byteOffset); \ |
| 2196 | } \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2197 | JNI_EXIT(); \ |
| 2198 | return value; \ |
| 2199 | } |
| 2200 | GET_TYPE_FIELD(jobject, Object, true); |
| 2201 | GET_TYPE_FIELD(jboolean, Boolean, false); |
| 2202 | GET_TYPE_FIELD(jbyte, Byte, false); |
| 2203 | GET_TYPE_FIELD(jchar, Char, false); |
| 2204 | GET_TYPE_FIELD(jshort, Short, false); |
| 2205 | GET_TYPE_FIELD(jint, Int, false); |
| 2206 | GET_TYPE_FIELD(jlong, Long, false); |
| 2207 | GET_TYPE_FIELD(jfloat, Float, false); |
| 2208 | GET_TYPE_FIELD(jdouble, Double, false); |
| 2209 | |
| 2210 | /* |
| 2211 | * Set an instance field. |
| 2212 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2213 | #define SET_TYPE_FIELD(_ctype, _jname, _isref) \ |
| 2214 | static void Set##_jname##Field(JNIEnv* env, jobject jobj, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2215 | jfieldID fieldID, _ctype value) \ |
| 2216 | { \ |
| 2217 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2218 | Object* obj = dvmDecodeIndirectRef(env, jobj); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2219 | InstField* field = (InstField*) fieldID; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2220 | if (_isref) { /* only when _ctype==jobject */ \ |
| 2221 | Object* valObj = dvmDecodeIndirectRef(env, (jobject)(u4)value); \ |
| 2222 | dvmSetFieldObject(obj, field->byteOffset, valObj); \ |
| 2223 | } else { \ |
| 2224 | dvmSetField##_jname(obj, field->byteOffset, value); \ |
| 2225 | } \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2226 | JNI_EXIT(); \ |
| 2227 | } |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2228 | SET_TYPE_FIELD(jobject, Object, true); |
| 2229 | SET_TYPE_FIELD(jboolean, Boolean, false); |
| 2230 | SET_TYPE_FIELD(jbyte, Byte, false); |
| 2231 | SET_TYPE_FIELD(jchar, Char, false); |
| 2232 | SET_TYPE_FIELD(jshort, Short, false); |
| 2233 | SET_TYPE_FIELD(jint, Int, false); |
| 2234 | SET_TYPE_FIELD(jlong, Long, false); |
| 2235 | SET_TYPE_FIELD(jfloat, Float, false); |
| 2236 | SET_TYPE_FIELD(jdouble, Double, false); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2237 | |
| 2238 | /* |
| 2239 | * Make a virtual method call. |
| 2240 | * |
| 2241 | * Three versions (..., va_list, jvalue[]) for each return type. If we're |
| 2242 | * returning an Object, we have to add it to the local references table. |
| 2243 | */ |
| 2244 | #define CALL_VIRTUAL(_ctype, _jname, _retfail, _retok, _isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2245 | static _ctype Call##_jname##Method(JNIEnv* env, jobject jobj, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2246 | jmethodID methodID, ...) \ |
| 2247 | { \ |
| 2248 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2249 | Object* obj = dvmDecodeIndirectRef(env, jobj); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2250 | const Method* meth; \ |
| 2251 | va_list args; \ |
| 2252 | JValue result; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2253 | meth = dvmGetVirtualizedMethod(obj->clazz, (Method*)methodID); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2254 | if (meth == NULL) { \ |
| 2255 | JNI_EXIT(); \ |
| 2256 | return _retfail; \ |
| 2257 | } \ |
| 2258 | va_start(args, methodID); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2259 | dvmCallMethodV(_self, meth, obj, &result, args); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2260 | va_end(args); \ |
| 2261 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2262 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2263 | JNI_EXIT(); \ |
| 2264 | return _retok; \ |
| 2265 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2266 | static _ctype Call##_jname##MethodV(JNIEnv* env, jobject jobj, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2267 | jmethodID methodID, va_list args) \ |
| 2268 | { \ |
| 2269 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2270 | Object* obj = dvmDecodeIndirectRef(env, jobj); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2271 | const Method* meth; \ |
| 2272 | JValue result; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2273 | meth = dvmGetVirtualizedMethod(obj->clazz, (Method*)methodID); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2274 | if (meth == NULL) { \ |
| 2275 | JNI_EXIT(); \ |
| 2276 | return _retfail; \ |
| 2277 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2278 | dvmCallMethodV(_self, meth, obj, &result, args); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2279 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2280 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2281 | JNI_EXIT(); \ |
| 2282 | return _retok; \ |
| 2283 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2284 | static _ctype Call##_jname##MethodA(JNIEnv* env, jobject jobj, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2285 | jmethodID methodID, jvalue* args) \ |
| 2286 | { \ |
| 2287 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2288 | Object* obj = dvmDecodeIndirectRef(env, jobj); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2289 | const Method* meth; \ |
| 2290 | JValue result; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2291 | meth = dvmGetVirtualizedMethod(obj->clazz, (Method*)methodID); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2292 | if (meth == NULL) { \ |
| 2293 | JNI_EXIT(); \ |
| 2294 | return _retfail; \ |
| 2295 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2296 | dvmCallMethodA(_self, meth, obj, &result, args); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2297 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2298 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2299 | JNI_EXIT(); \ |
| 2300 | return _retok; \ |
| 2301 | } |
| 2302 | CALL_VIRTUAL(jobject, Object, NULL, result.l, true); |
| 2303 | CALL_VIRTUAL(jboolean, Boolean, 0, result.z, false); |
| 2304 | CALL_VIRTUAL(jbyte, Byte, 0, result.b, false); |
| 2305 | CALL_VIRTUAL(jchar, Char, 0, result.c, false); |
| 2306 | CALL_VIRTUAL(jshort, Short, 0, result.s, false); |
| 2307 | CALL_VIRTUAL(jint, Int, 0, result.i, false); |
| 2308 | CALL_VIRTUAL(jlong, Long, 0, result.j, false); |
| 2309 | CALL_VIRTUAL(jfloat, Float, 0.0f, result.f, false); |
| 2310 | CALL_VIRTUAL(jdouble, Double, 0.0, result.d, false); |
| 2311 | CALL_VIRTUAL(void, Void, , , false); |
| 2312 | |
| 2313 | /* |
| 2314 | * Make a "non-virtual" method call. We're still calling a virtual method, |
| 2315 | * but this time we're not doing an indirection through the object's vtable. |
| 2316 | * The "clazz" parameter defines which implementation of a method we want. |
| 2317 | * |
| 2318 | * Three versions (..., va_list, jvalue[]) for each return type. |
| 2319 | */ |
| 2320 | #define CALL_NONVIRTUAL(_ctype, _jname, _retfail, _retok, _isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2321 | static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, jobject jobj, \ |
| 2322 | jclass jclazz, jmethodID methodID, ...) \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2323 | { \ |
| 2324 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2325 | Object* obj = dvmDecodeIndirectRef(env, jobj); \ |
| 2326 | ClassObject* clazz = \ |
| 2327 | (ClassObject*) dvmDecodeIndirectRef(env, jclazz); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2328 | const Method* meth; \ |
| 2329 | va_list args; \ |
| 2330 | JValue result; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2331 | meth = dvmGetVirtualizedMethod(clazz, (Method*)methodID); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2332 | if (meth == NULL) { \ |
| 2333 | JNI_EXIT(); \ |
| 2334 | return _retfail; \ |
| 2335 | } \ |
| 2336 | va_start(args, methodID); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2337 | dvmCallMethodV(_self, meth, obj, &result, args); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2338 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2339 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2340 | va_end(args); \ |
| 2341 | JNI_EXIT(); \ |
| 2342 | return _retok; \ |
| 2343 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2344 | static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, jobject jobj,\ |
| 2345 | jclass jclazz, jmethodID methodID, va_list args) \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2346 | { \ |
| 2347 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2348 | Object* obj = dvmDecodeIndirectRef(env, jobj); \ |
| 2349 | ClassObject* clazz = \ |
| 2350 | (ClassObject*) dvmDecodeIndirectRef(env, jclazz); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2351 | const Method* meth; \ |
| 2352 | JValue result; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2353 | meth = dvmGetVirtualizedMethod(clazz, (Method*)methodID); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2354 | if (meth == NULL) { \ |
| 2355 | JNI_EXIT(); \ |
| 2356 | return _retfail; \ |
| 2357 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2358 | dvmCallMethodV(_self, meth, obj, &result, args); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2359 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2360 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2361 | JNI_EXIT(); \ |
| 2362 | return _retok; \ |
| 2363 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2364 | static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, jobject jobj,\ |
| 2365 | jclass jclazz, jmethodID methodID, jvalue* args) \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2366 | { \ |
| 2367 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2368 | Object* obj = dvmDecodeIndirectRef(env, jobj); \ |
| 2369 | ClassObject* clazz = \ |
| 2370 | (ClassObject*) dvmDecodeIndirectRef(env, jclazz); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2371 | const Method* meth; \ |
| 2372 | JValue result; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2373 | meth = dvmGetVirtualizedMethod(clazz, (Method*)methodID); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2374 | if (meth == NULL) { \ |
| 2375 | JNI_EXIT(); \ |
| 2376 | return _retfail; \ |
| 2377 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2378 | dvmCallMethodA(_self, meth, obj, &result, args); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2379 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2380 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2381 | JNI_EXIT(); \ |
| 2382 | return _retok; \ |
| 2383 | } |
| 2384 | CALL_NONVIRTUAL(jobject, Object, NULL, result.l, true); |
| 2385 | CALL_NONVIRTUAL(jboolean, Boolean, 0, result.z, false); |
| 2386 | CALL_NONVIRTUAL(jbyte, Byte, 0, result.b, false); |
| 2387 | CALL_NONVIRTUAL(jchar, Char, 0, result.c, false); |
| 2388 | CALL_NONVIRTUAL(jshort, Short, 0, result.s, false); |
| 2389 | CALL_NONVIRTUAL(jint, Int, 0, result.i, false); |
| 2390 | CALL_NONVIRTUAL(jlong, Long, 0, result.j, false); |
| 2391 | CALL_NONVIRTUAL(jfloat, Float, 0.0f, result.f, false); |
| 2392 | CALL_NONVIRTUAL(jdouble, Double, 0.0, result.d, false); |
| 2393 | CALL_NONVIRTUAL(void, Void, , , false); |
| 2394 | |
| 2395 | |
| 2396 | /* |
| 2397 | * Call a static method. |
| 2398 | */ |
| 2399 | #define CALL_STATIC(_ctype, _jname, _retfail, _retok, _isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2400 | static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass jclazz, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2401 | jmethodID methodID, ...) \ |
| 2402 | { \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2403 | UNUSED_PARAMETER(jclazz); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2404 | JNI_ENTER(); \ |
| 2405 | JValue result; \ |
| 2406 | va_list args; \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2407 | va_start(args, methodID); \ |
| 2408 | dvmCallMethodV(_self, (Method*) methodID, NULL, &result, args); \ |
| 2409 | va_end(args); \ |
| 2410 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2411 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2412 | JNI_EXIT(); \ |
| 2413 | return _retok; \ |
| 2414 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2415 | static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass jclazz, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2416 | jmethodID methodID, va_list args) \ |
| 2417 | { \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2418 | UNUSED_PARAMETER(jclazz); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2419 | JNI_ENTER(); \ |
| 2420 | JValue result; \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2421 | dvmCallMethodV(_self, (Method*) methodID, NULL, &result, args); \ |
| 2422 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2423 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2424 | JNI_EXIT(); \ |
| 2425 | return _retok; \ |
| 2426 | } \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2427 | static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass jclazz, \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2428 | jmethodID methodID, jvalue* args) \ |
| 2429 | { \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2430 | UNUSED_PARAMETER(jclazz); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2431 | JNI_ENTER(); \ |
| 2432 | JValue result; \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2433 | dvmCallMethodA(_self, (Method*) methodID, NULL, &result, args); \ |
| 2434 | if (_isref) \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2435 | result.l = addLocalReference(env, result.l); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2436 | JNI_EXIT(); \ |
| 2437 | return _retok; \ |
| 2438 | } |
| 2439 | CALL_STATIC(jobject, Object, NULL, result.l, true); |
| 2440 | CALL_STATIC(jboolean, Boolean, 0, result.z, false); |
| 2441 | CALL_STATIC(jbyte, Byte, 0, result.b, false); |
| 2442 | CALL_STATIC(jchar, Char, 0, result.c, false); |
| 2443 | CALL_STATIC(jshort, Short, 0, result.s, false); |
| 2444 | CALL_STATIC(jint, Int, 0, result.i, false); |
| 2445 | CALL_STATIC(jlong, Long, 0, result.j, false); |
| 2446 | CALL_STATIC(jfloat, Float, 0.0f, result.f, false); |
| 2447 | CALL_STATIC(jdouble, Double, 0.0, result.d, false); |
| 2448 | CALL_STATIC(void, Void, , , false); |
| 2449 | |
| 2450 | /* |
| 2451 | * Create a new String from Unicode data. |
| 2452 | * |
| 2453 | * If "len" is zero, we will return an empty string even if "unicodeChars" |
| 2454 | * is NULL. (The JNI spec is vague here.) |
| 2455 | */ |
| 2456 | static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) |
| 2457 | { |
| 2458 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2459 | jobject retval; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2460 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2461 | StringObject* jstr = dvmCreateStringFromUnicode(unicodeChars, len); |
| 2462 | if (jstr == NULL) { |
| 2463 | retval = NULL; |
| 2464 | } else { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2465 | dvmReleaseTrackedAlloc((Object*) jstr, NULL); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2466 | retval = addLocalReference(env, (Object*) jstr); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | JNI_EXIT(); |
| 2470 | return jstr; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * Return the length of a String in Unicode character units. |
| 2475 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2476 | static jsize GetStringLength(JNIEnv* env, jstring jstr) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2477 | { |
| 2478 | JNI_ENTER(); |
| 2479 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2480 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| 2481 | jsize len = dvmStringLen(strObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2482 | |
| 2483 | JNI_EXIT(); |
| 2484 | return len; |
| 2485 | } |
| 2486 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2487 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2488 | /* |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2489 | * Get a string's character data. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2490 | * |
| 2491 | * The result is guaranteed to be valid until ReleaseStringChars is |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2492 | * called, which means we have to pin it or return a copy. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2493 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2494 | static const jchar* GetStringChars(JNIEnv* env, jstring jstr, jboolean* isCopy) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2495 | { |
| 2496 | JNI_ENTER(); |
| 2497 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2498 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| 2499 | ArrayObject* strChars = dvmStringCharArray(jstr); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2500 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2501 | pinPrimitiveArray(strChars); |
| 2502 | |
| 2503 | const u2* data = dvmStringChars(strObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2504 | if (isCopy != NULL) |
| 2505 | *isCopy = JNI_FALSE; |
| 2506 | |
| 2507 | JNI_EXIT(); |
| 2508 | return (jchar*)data; |
| 2509 | } |
| 2510 | |
| 2511 | /* |
| 2512 | * Release our grip on some characters from a string. |
| 2513 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2514 | static void ReleaseStringChars(JNIEnv* env, jstring jstr, const jchar* chars) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2515 | { |
| 2516 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2517 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| 2518 | ArrayObject* strChars = dvmStringCharArray(jstr); |
| 2519 | unpinPrimitiveArray(strChars); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2520 | JNI_EXIT(); |
| 2521 | } |
| 2522 | |
| 2523 | /* |
| 2524 | * Create a new java.lang.String object from chars in modified UTF-8 form. |
| 2525 | * |
| 2526 | * The spec doesn't say how to handle a NULL string. Popular desktop VMs |
| 2527 | * accept it and return a NULL pointer in response. |
| 2528 | */ |
| 2529 | static jstring NewStringUTF(JNIEnv* env, const char* bytes) |
| 2530 | { |
| 2531 | JNI_ENTER(); |
| 2532 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2533 | jstring result; |
| 2534 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2535 | if (bytes == NULL) { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2536 | result = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2537 | } else { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2538 | /* note newStr could come back NULL on OOM */ |
| 2539 | StringObject* newStr = dvmCreateStringFromCstr(bytes, ALLOC_DEFAULT); |
| 2540 | result = addLocalReference(env, (Object*) newStr); |
| 2541 | dvmReleaseTrackedAlloc((Object*)newStr, NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2545 | return result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | /* |
| 2549 | * Return the length in bytes of the modified UTF-8 form of the string. |
| 2550 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2551 | static jsize GetStringUTFLength(JNIEnv* env, jstring jstr) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2552 | { |
| 2553 | JNI_ENTER(); |
| 2554 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2555 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| 2556 | jsize len = dvmStringUtf8ByteLen(strObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2557 | |
| 2558 | JNI_EXIT(); |
| 2559 | return len; |
| 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | * Convert "string" to modified UTF-8 and return a pointer. The returned |
| 2564 | * value must be released with ReleaseStringUTFChars. |
| 2565 | * |
| 2566 | * According to the JNI reference, "Returns a pointer to a UTF-8 string, |
| 2567 | * or NULL if the operation fails. Returns NULL if and only if an invocation |
| 2568 | * of this function has thrown an exception." |
| 2569 | * |
| 2570 | * The behavior here currently follows that of other open-source VMs, which |
| 2571 | * quietly return NULL if "string" is NULL. We should consider throwing an |
| 2572 | * NPE. (The CheckJNI code blows up if you try to pass in a NULL string, |
| 2573 | * which should catch this sort of thing during development.) Certain other |
| 2574 | * VMs will crash with a segmentation fault. |
| 2575 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2576 | static const char* GetStringUTFChars(JNIEnv* env, jstring jstr, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2577 | jboolean* isCopy) |
| 2578 | { |
| 2579 | JNI_ENTER(); |
| 2580 | char* newStr; |
| 2581 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2582 | if (jstr == NULL) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2583 | /* this shouldn't happen; throw NPE? */ |
| 2584 | newStr = NULL; |
| 2585 | } else { |
| 2586 | if (isCopy != NULL) |
| 2587 | *isCopy = JNI_TRUE; |
| 2588 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2589 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| 2590 | newStr = dvmCreateCstrFromString(strObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2591 | if (newStr == NULL) { |
| 2592 | /* assume memory failure */ |
| 2593 | dvmThrowException("Ljava/lang/OutOfMemoryError;", |
| 2594 | "native heap string alloc failed"); |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | JNI_EXIT(); |
| 2599 | return newStr; |
| 2600 | } |
| 2601 | |
| 2602 | /* |
| 2603 | * Release a string created by GetStringUTFChars(). |
| 2604 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2605 | static void ReleaseStringUTFChars(JNIEnv* env, jstring jstr, const char* utf) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2606 | { |
| 2607 | JNI_ENTER(); |
| 2608 | free((char*)utf); |
| 2609 | JNI_EXIT(); |
| 2610 | } |
| 2611 | |
| 2612 | /* |
| 2613 | * Return the capacity of the array. |
| 2614 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2615 | static jsize GetArrayLength(JNIEnv* env, jarray jarr) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2616 | { |
| 2617 | JNI_ENTER(); |
| 2618 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2619 | ArrayObject* arrObj = (ArrayObject*) dvmDecodeIndirectRef(env, jarr); |
| 2620 | jsize length = arrObj->length; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2621 | |
| 2622 | JNI_EXIT(); |
| 2623 | return length; |
| 2624 | } |
| 2625 | |
| 2626 | /* |
| 2627 | * Construct a new array that holds objects from class "elementClass". |
| 2628 | */ |
| 2629 | static jobjectArray NewObjectArray(JNIEnv* env, jsize length, |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2630 | jclass jelementClass, jobject jinitialElement) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2631 | { |
| 2632 | JNI_ENTER(); |
| 2633 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2634 | jobjectArray newArray = NULL; |
| 2635 | ClassObject* elemClassObj = |
| 2636 | (ClassObject*) dvmDecodeIndirectRef(env, jelementClass); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2637 | |
| 2638 | if (elemClassObj == NULL) { |
| 2639 | dvmThrowException("Ljava/lang/NullPointerException;", |
| 2640 | "JNI NewObjectArray"); |
| 2641 | goto bail; |
| 2642 | } |
| 2643 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2644 | ArrayObject* newObj = |
| 2645 | dvmAllocObjectArray(elemClassObj, length, ALLOC_DEFAULT); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2646 | if (newObj == NULL) { |
| 2647 | assert(dvmCheckException(_self)); |
| 2648 | goto bail; |
| 2649 | } |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2650 | newArray = addLocalReference(env, (Object*) newObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2651 | dvmReleaseTrackedAlloc((Object*) newObj, NULL); |
| 2652 | |
| 2653 | /* |
| 2654 | * Initialize the array. Trashes "length". |
| 2655 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2656 | if (jinitialElement != NULL) { |
| 2657 | Object* initialElement = dvmDecodeIndirectRef(env, jinitialElement); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2658 | Object** arrayData = (Object**) newObj->contents; |
| 2659 | |
| 2660 | while (length--) |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2661 | *arrayData++ = initialElement; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2662 | } |
| 2663 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2664 | |
| 2665 | bail: |
| 2666 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2667 | return newArray; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | /* |
| 2671 | * Get one element of an Object array. |
| 2672 | * |
| 2673 | * Add the object to the local references table in case the array goes away. |
| 2674 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2675 | static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray jarr, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2676 | jsize index) |
| 2677 | { |
| 2678 | JNI_ENTER(); |
| 2679 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2680 | ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(env, jarr); |
| 2681 | jobject retval = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2682 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2683 | assert(arrayObj != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2684 | |
| 2685 | /* check the array bounds */ |
| 2686 | if (index < 0 || index >= (int) arrayObj->length) { |
| 2687 | dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 2688 | arrayObj->obj.clazz->descriptor); |
| 2689 | goto bail; |
| 2690 | } |
| 2691 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2692 | Object* value = ((Object**) arrayObj->contents)[index]; |
| 2693 | retval = addLocalReference(env, value); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2694 | |
| 2695 | bail: |
| 2696 | JNI_EXIT(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2697 | return retval; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | /* |
| 2701 | * Set one element of an Object array. |
| 2702 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2703 | static void SetObjectArrayElement(JNIEnv* env, jobjectArray jarr, |
| 2704 | jsize index, jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2705 | { |
| 2706 | JNI_ENTER(); |
| 2707 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2708 | ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(env, jarr); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2709 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2710 | assert(arrayObj != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2711 | |
| 2712 | /* check the array bounds */ |
| 2713 | if (index < 0 || index >= (int) arrayObj->length) { |
| 2714 | dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 2715 | arrayObj->obj.clazz->descriptor); |
| 2716 | goto bail; |
| 2717 | } |
| 2718 | |
| 2719 | //LOGV("JNI: set element %d in array %p to %p\n", index, array, value); |
| 2720 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2721 | Object* obj; |
| 2722 | if (jobj == NULL) |
| 2723 | obj = NULL; |
| 2724 | else |
| 2725 | obj = dvmDecodeIndirectRef(env, jobj); |
| 2726 | ((Object**) arrayObj->contents)[index] = obj; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2727 | |
| 2728 | bail: |
| 2729 | JNI_EXIT(); |
| 2730 | } |
| 2731 | |
| 2732 | /* |
| 2733 | * Create a new array of primitive elements. |
| 2734 | */ |
| 2735 | #define NEW_PRIMITIVE_ARRAY(_artype, _jname, _typechar) \ |
| 2736 | static _artype New##_jname##Array(JNIEnv* env, jsize length) \ |
| 2737 | { \ |
| 2738 | JNI_ENTER(); \ |
| 2739 | ArrayObject* arrayObj; \ |
| 2740 | arrayObj = dvmAllocPrimitiveArray(_typechar, length, \ |
| 2741 | ALLOC_DEFAULT); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2742 | jarray jarr = NULL; \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2743 | if (arrayObj != NULL) { \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2744 | jarr = addLocalReference(env, (Object*) arrayObj); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2745 | dvmReleaseTrackedAlloc((Object*) arrayObj, NULL); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2746 | } \ |
| 2747 | JNI_EXIT(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2748 | return (_artype)jarr; \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2749 | } |
| 2750 | NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean, 'Z'); |
| 2751 | NEW_PRIMITIVE_ARRAY(jbyteArray, Byte, 'B'); |
| 2752 | NEW_PRIMITIVE_ARRAY(jcharArray, Char, 'C'); |
| 2753 | NEW_PRIMITIVE_ARRAY(jshortArray, Short, 'S'); |
| 2754 | NEW_PRIMITIVE_ARRAY(jintArray, Int, 'I'); |
| 2755 | NEW_PRIMITIVE_ARRAY(jlongArray, Long, 'J'); |
| 2756 | NEW_PRIMITIVE_ARRAY(jfloatArray, Float, 'F'); |
| 2757 | NEW_PRIMITIVE_ARRAY(jdoubleArray, Double, 'D'); |
| 2758 | |
| 2759 | /* |
| 2760 | * Get a pointer to a C array of primitive elements from an array object |
| 2761 | * of the matching type. |
| 2762 | * |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2763 | * In a compacting GC, we either need to return a copy of the elements or |
| 2764 | * "pin" the memory. Otherwise we run the risk of native code using the |
| 2765 | * buffer as the destination of e.g. a blocking read() call that wakes up |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2766 | * during a GC. |
| 2767 | */ |
| 2768 | #define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \ |
| 2769 | static _ctype* Get##_jname##ArrayElements(JNIEnv* env, \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2770 | _ctype##Array jarr, jboolean* isCopy) \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2771 | { \ |
| 2772 | JNI_ENTER(); \ |
| 2773 | _ctype* data; \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2774 | ArrayObject* arrayObj = \ |
| 2775 | (ArrayObject*) dvmDecodeIndirectRef(env, jarr); \ |
| 2776 | pinPrimitiveArray(arrayObj); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2777 | data = (_ctype*) arrayObj->contents; \ |
| 2778 | if (isCopy != NULL) \ |
| 2779 | *isCopy = JNI_FALSE; \ |
| 2780 | JNI_EXIT(); \ |
| 2781 | return data; \ |
| 2782 | } |
| 2783 | |
| 2784 | /* |
| 2785 | * Release the storage locked down by the "get" function. |
| 2786 | * |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2787 | * The spec says, "'mode' has no effect if 'elems' is not a copy of the |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2788 | * elements in 'array'." They apparently did not anticipate the need to |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2789 | * un-pin memory. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2790 | */ |
| 2791 | #define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \ |
| 2792 | static void Release##_jname##ArrayElements(JNIEnv* env, \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2793 | _ctype##Array jarr, _ctype* elems, jint mode) \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2794 | { \ |
| 2795 | UNUSED_PARAMETER(elems); \ |
| 2796 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2797 | if (mode != JNI_COMMIT) { \ |
| 2798 | ArrayObject* arrayObj = \ |
| 2799 | (ArrayObject*) dvmDecodeIndirectRef(env, jarr); \ |
| 2800 | unpinPrimitiveArray(arrayObj); \ |
| 2801 | } \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2802 | JNI_EXIT(); \ |
| 2803 | } |
| 2804 | |
| 2805 | /* |
| 2806 | * Copy a section of a primitive array to a buffer. |
| 2807 | */ |
| 2808 | #define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \ |
| 2809 | static void Get##_jname##ArrayRegion(JNIEnv* env, \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2810 | _ctype##Array jarr, jsize start, jsize len, _ctype* buf) \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2811 | { \ |
| 2812 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2813 | ArrayObject* arrayObj = \ |
| 2814 | (ArrayObject*) dvmDecodeIndirectRef(env, jarr); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2815 | _ctype* data = (_ctype*) arrayObj->contents; \ |
| 2816 | if (start < 0 || len < 0 || start + len > (int) arrayObj->length) { \ |
| 2817 | dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", \ |
| 2818 | arrayObj->obj.clazz->descriptor); \ |
| 2819 | } else { \ |
| 2820 | memcpy(buf, data + start, len * sizeof(_ctype)); \ |
| 2821 | } \ |
| 2822 | JNI_EXIT(); \ |
| 2823 | } |
| 2824 | |
| 2825 | /* |
| 2826 | * Copy a section of a primitive array to a buffer. |
| 2827 | */ |
| 2828 | #define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \ |
| 2829 | static void Set##_jname##ArrayRegion(JNIEnv* env, \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2830 | _ctype##Array jarr, jsize start, jsize len, const _ctype* buf) \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2831 | { \ |
| 2832 | JNI_ENTER(); \ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2833 | ArrayObject* arrayObj = \ |
| 2834 | (ArrayObject*) dvmDecodeIndirectRef(env, jarr); \ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2835 | _ctype* data = (_ctype*) arrayObj->contents; \ |
| 2836 | if (start < 0 || len < 0 || start + len > (int) arrayObj->length) { \ |
| 2837 | dvmThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", \ |
| 2838 | arrayObj->obj.clazz->descriptor); \ |
| 2839 | } else { \ |
| 2840 | memcpy(data + start, buf, len * sizeof(_ctype)); \ |
| 2841 | } \ |
| 2842 | JNI_EXIT(); \ |
| 2843 | } |
| 2844 | |
| 2845 | /* |
| 2846 | * 4-in-1: |
| 2847 | * Get<Type>ArrayElements |
| 2848 | * Release<Type>ArrayElements |
| 2849 | * Get<Type>ArrayRegion |
| 2850 | * Set<Type>ArrayRegion |
| 2851 | */ |
| 2852 | #define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname) \ |
| 2853 | GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \ |
| 2854 | RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \ |
| 2855 | GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \ |
| 2856 | SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); |
| 2857 | |
| 2858 | PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean); |
| 2859 | PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte); |
| 2860 | PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char); |
| 2861 | PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short); |
| 2862 | PRIMITIVE_ARRAY_FUNCTIONS(jint, Int); |
| 2863 | PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long); |
| 2864 | PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float); |
| 2865 | PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double); |
| 2866 | |
| 2867 | /* |
| 2868 | * Register one or more native functions in one class. |
| 2869 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2870 | static jint RegisterNatives(JNIEnv* env, jclass jclazz, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2871 | const JNINativeMethod* methods, jint nMethods) |
| 2872 | { |
| 2873 | JNI_ENTER(); |
| 2874 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2875 | ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(env, jclazz); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2876 | jint retval; |
| 2877 | int i; |
| 2878 | |
| 2879 | if (gDvm.verboseJni) { |
| 2880 | LOGI("[Registering JNI native methods for class %s]\n", |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2881 | clazz->descriptor); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | for (i = 0; i < nMethods; i++) { |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2885 | if (!dvmRegisterJNIMethod(clazz, methods[i].name, |
| 2886 | methods[i].signature, methods[i].fnPtr)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2887 | { |
| 2888 | retval = JNI_ERR; |
| 2889 | goto bail; |
| 2890 | } |
| 2891 | } |
| 2892 | retval = JNI_OK; |
| 2893 | |
| 2894 | bail: |
| 2895 | JNI_EXIT(); |
| 2896 | return retval; |
| 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * Un-register a native function. |
| 2901 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2902 | static jint UnregisterNatives(JNIEnv* env, jclass jclazz) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2903 | { |
| 2904 | JNI_ENTER(); |
| 2905 | /* |
| 2906 | * The JNI docs refer to this as a way to reload/relink native libraries, |
| 2907 | * and say it "should not be used in normal native code". |
| 2908 | * |
| 2909 | * We can implement it if we decide we need it. |
| 2910 | */ |
| 2911 | JNI_EXIT(); |
| 2912 | return JNI_ERR; |
| 2913 | } |
| 2914 | |
| 2915 | /* |
| 2916 | * Lock the monitor. |
| 2917 | * |
| 2918 | * We have to track all monitor enters and exits, so that we can undo any |
| 2919 | * outstanding synchronization before the thread exits. |
| 2920 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2921 | static jint MonitorEnter(JNIEnv* env, jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2922 | { |
| 2923 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2924 | Object* obj = dvmDecodeIndirectRef(env, jobj); |
| 2925 | dvmLockObject(_self, obj); |
| 2926 | trackMonitorEnter(_self, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2927 | JNI_EXIT(); |
| 2928 | return JNI_OK; |
| 2929 | } |
| 2930 | |
| 2931 | /* |
| 2932 | * Unlock the monitor. |
| 2933 | * |
| 2934 | * Throws an IllegalMonitorStateException if the current thread |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2935 | * doesn't own the monitor. (dvmUnlockObject() takes care of the throw.) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2936 | * |
| 2937 | * According to the 1.6 spec, it's legal to call here with an exception |
| 2938 | * pending. If this fails, we'll stomp the original exception. |
| 2939 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2940 | static jint MonitorExit(JNIEnv* env, jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2941 | { |
| 2942 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2943 | Object* obj = dvmDecodeIndirectRef(env, jobj); |
| 2944 | bool success = dvmUnlockObject(_self, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2945 | if (success) |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2946 | trackMonitorExit(_self, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2947 | JNI_EXIT(); |
| 2948 | return success ? JNI_OK : JNI_ERR; |
| 2949 | } |
| 2950 | |
| 2951 | /* |
| 2952 | * Return the JavaVM interface associated with the current thread. |
| 2953 | */ |
| 2954 | static jint GetJavaVM(JNIEnv* env, JavaVM** vm) |
| 2955 | { |
| 2956 | JNI_ENTER(); |
| 2957 | //*vm = gDvm.vmList; |
| 2958 | *vm = (JavaVM*) ((JNIEnvExt*)env)->vm; |
| 2959 | JNI_EXIT(); |
| 2960 | if (*vm == NULL) |
| 2961 | return JNI_ERR; |
| 2962 | else |
| 2963 | return JNI_OK; |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * Copies "len" Unicode characters, from offset "start". |
| 2968 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2969 | static void GetStringRegion(JNIEnv* env, jstring jstr, jsize start, jsize len, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2970 | jchar* buf) |
| 2971 | { |
| 2972 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2973 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2974 | if (start + len > dvmStringLen(strObj)) |
| 2975 | dvmThrowException("Ljava/lang/StringIndexOutOfBoundsException;", NULL); |
| 2976 | else |
| 2977 | memcpy(buf, dvmStringChars(strObj) + start, len * sizeof(u2)); |
| 2978 | JNI_EXIT(); |
| 2979 | } |
| 2980 | |
| 2981 | /* |
| 2982 | * Translates "len" Unicode characters, from offset "start", into |
| 2983 | * modified UTF-8 encoding. |
| 2984 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2985 | static void GetStringUTFRegion(JNIEnv* env, jstring jstr, jsize start, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2986 | jsize len, char* buf) |
| 2987 | { |
| 2988 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 2989 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 2990 | if (start + len > dvmStringLen(strObj)) |
| 2991 | dvmThrowException("Ljava/lang/StringIndexOutOfBoundsException;", NULL); |
| 2992 | else |
| 2993 | dvmCreateCstrFromStringRegion(strObj, start, len, buf); |
| 2994 | JNI_EXIT(); |
| 2995 | } |
| 2996 | |
| 2997 | /* |
| 2998 | * Get a raw pointer to array data. |
| 2999 | * |
| 3000 | * The caller is expected to call "release" before doing any JNI calls |
| 3001 | * or blocking I/O operations. |
| 3002 | * |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3003 | * We need to pin the memory or block GC. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3004 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3005 | static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray jarr, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3006 | jboolean* isCopy) |
| 3007 | { |
| 3008 | JNI_ENTER(); |
| 3009 | void* data; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3010 | ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(env, jarr); |
| 3011 | pinPrimitiveArray(arrayObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3012 | data = arrayObj->contents; |
| 3013 | if (isCopy != NULL) |
| 3014 | *isCopy = JNI_FALSE; |
| 3015 | JNI_EXIT(); |
| 3016 | return data; |
| 3017 | } |
| 3018 | |
| 3019 | /* |
| 3020 | * Release an array obtained with GetPrimitiveArrayCritical. |
| 3021 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3022 | static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray jarr, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3023 | void* carray, jint mode) |
| 3024 | { |
| 3025 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3026 | if (mode != JNI_COMMIT) { |
| 3027 | ArrayObject* arrayObj = (ArrayObject*) dvmDecodeIndirectRef(env, jarr); |
| 3028 | unpinPrimitiveArray(arrayObj); |
| 3029 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3030 | JNI_EXIT(); |
| 3031 | } |
| 3032 | |
| 3033 | /* |
| 3034 | * Like GetStringChars, but with restricted use. |
| 3035 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3036 | static const jchar* GetStringCritical(JNIEnv* env, jstring jstr, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3037 | jboolean* isCopy) |
| 3038 | { |
| 3039 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3040 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| 3041 | ArrayObject* strChars = dvmStringCharArray(strObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3042 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3043 | pinPrimitiveArray(strChars); |
| 3044 | |
| 3045 | const u2* data = dvmStringChars(strObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3046 | if (isCopy != NULL) |
| 3047 | *isCopy = JNI_FALSE; |
| 3048 | |
| 3049 | JNI_EXIT(); |
| 3050 | return (jchar*)data; |
| 3051 | } |
| 3052 | |
| 3053 | /* |
| 3054 | * Like ReleaseStringChars, but with restricted use. |
| 3055 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3056 | static void ReleaseStringCritical(JNIEnv* env, jstring jstr, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3057 | const jchar* carray) |
| 3058 | { |
| 3059 | JNI_ENTER(); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3060 | StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr); |
| 3061 | ArrayObject* strChars = dvmStringCharArray(jstr); |
| 3062 | unpinPrimitiveArray(strChars); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3063 | JNI_EXIT(); |
| 3064 | } |
| 3065 | |
| 3066 | /* |
| 3067 | * Create a new weak global reference. |
| 3068 | */ |
| 3069 | static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) |
| 3070 | { |
| 3071 | JNI_ENTER(); |
| 3072 | // TODO - implement |
| 3073 | jobject gref = NULL; |
| 3074 | LOGE("JNI ERROR: NewWeakGlobalRef not implemented\n"); |
| 3075 | dvmAbort(); |
| 3076 | JNI_EXIT(); |
| 3077 | return gref; |
| 3078 | } |
| 3079 | |
| 3080 | /* |
| 3081 | * Delete the specified weak global reference. |
| 3082 | */ |
| 3083 | static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) |
| 3084 | { |
| 3085 | JNI_ENTER(); |
| 3086 | // TODO - implement |
| 3087 | LOGE("JNI ERROR: DeleteWeakGlobalRef not implemented\n"); |
| 3088 | dvmAbort(); |
| 3089 | JNI_EXIT(); |
| 3090 | } |
| 3091 | |
| 3092 | /* |
| 3093 | * Quick check for pending exceptions. |
| 3094 | * |
| 3095 | * TODO: we should be able to skip the enter/exit macros here. |
| 3096 | */ |
| 3097 | static jboolean ExceptionCheck(JNIEnv* env) |
| 3098 | { |
| 3099 | JNI_ENTER(); |
| 3100 | bool result = dvmCheckException(_self); |
| 3101 | JNI_EXIT(); |
| 3102 | return result; |
| 3103 | } |
| 3104 | |
| 3105 | /* |
| 3106 | * Returns the type of the object referred to by "obj". It can be local, |
| 3107 | * global, or weak global. |
| 3108 | * |
| 3109 | * In the current implementation, references can be global and local at |
| 3110 | * the same time, so while the return value is accurate it may not tell |
| 3111 | * the whole story. |
| 3112 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3113 | static jobjectRefType GetObjectRefType(JNIEnv* env, jobject jobj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3114 | { |
| 3115 | JNI_ENTER(); |
| 3116 | jobjectRefType type; |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3117 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3118 | if (jobj == NULL) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3119 | type = JNIInvalidRefType; |
| 3120 | else |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3121 | type = dvmGetJNIRefType(env, jobj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3122 | JNI_EXIT(); |
| 3123 | return type; |
| 3124 | } |
| 3125 | |
| 3126 | /* |
| 3127 | * Allocate and return a new java.nio.ByteBuffer for this block of memory. |
| 3128 | * |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3129 | * "address" may not be NULL, and "capacity" must be > 0. (These are only |
| 3130 | * verified when CheckJNI is enabled.) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3131 | */ |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3132 | static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3133 | { |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3134 | JNI_ENTER(); |
| 3135 | |
| 3136 | Thread* self = _self /*dvmThreadSelf()*/; |
| 3137 | Object* platformAddress = NULL; |
| 3138 | JValue callResult; |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3139 | jobject result = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3140 | |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3141 | /* get an instance of PlatformAddress that wraps the provided address */ |
| 3142 | dvmCallMethod(self, |
| 3143 | gDvm.methOrgApacheHarmonyLuniPlatformPlatformAddress_on, |
| 3144 | NULL, &callResult, address); |
| 3145 | if (dvmGetException(self) != NULL || callResult.l == NULL) |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3146 | goto bail; |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3147 | |
| 3148 | /* don't let the GC discard it */ |
| 3149 | platformAddress = (Object*) callResult.l; |
| 3150 | dvmAddTrackedAlloc(platformAddress, self); |
| 3151 | LOGV("tracking %p for address=%p\n", platformAddress, address); |
| 3152 | |
| 3153 | /* create an instance of java.nio.ReadWriteDirectByteBuffer */ |
| 3154 | ClassObject* clazz = gDvm.classJavaNioReadWriteDirectByteBuffer; |
| 3155 | if (!dvmIsClassInitialized(clazz) && !dvmInitClass(clazz)) |
| 3156 | goto bail; |
| 3157 | Object* newObj = dvmAllocObject(clazz, ALLOC_DONT_TRACK); |
| 3158 | if (newObj != NULL) { |
| 3159 | /* call the (PlatformAddress, int, int) constructor */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3160 | result = addLocalReference(env, newObj); |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3161 | dvmCallMethod(self, gDvm.methJavaNioReadWriteDirectByteBuffer_init, |
| 3162 | newObj, &callResult, platformAddress, (jint) capacity, (jint) 0); |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3163 | if (dvmGetException(self) != NULL) { |
| 3164 | deleteLocalReference(env, result); |
| 3165 | result = NULL; |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3166 | goto bail; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3167 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3168 | } |
| 3169 | |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3170 | bail: |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3171 | if (platformAddress != NULL) |
| 3172 | dvmReleaseTrackedAlloc(platformAddress, self); |
| 3173 | JNI_EXIT(); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3174 | return result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | /* |
| 3178 | * Get the starting address of the buffer for the specified java.nio.Buffer. |
| 3179 | * |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3180 | * If this is not a "direct" buffer, we return NULL. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3181 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3182 | static void* GetDirectBufferAddress(JNIEnv* env, jobject jbuf) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3183 | { |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3184 | JNI_ENTER(); |
| 3185 | |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3186 | Object* bufObj = dvmDecodeIndirectRef(env, jbuf); |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3187 | Thread* self = _self /*dvmThreadSelf()*/; |
| Andy McFadden | 8e696dc | 2009-07-24 15:28:16 -0700 | [diff] [blame] | 3188 | void* result; |
| 3189 | |
| 3190 | /* |
| 3191 | * All Buffer objects have an effectiveDirectAddress field. If it's |
| 3192 | * nonzero, we can just return that value. If not, we have to call |
| 3193 | * through DirectBuffer.getEffectiveAddress(), which as a side-effect |
| 3194 | * will set the effectiveDirectAddress field for direct buffers (and |
| 3195 | * things that wrap direct buffers). |
| 3196 | */ |
| 3197 | result = (void*) dvmGetFieldInt(bufObj, |
| 3198 | gDvm.offJavaNioBuffer_effectiveDirectAddress); |
| 3199 | if (result != NULL) { |
| 3200 | //LOGI("fast path for %p\n", buf); |
| 3201 | goto bail; |
| 3202 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3203 | |
| Andy McFadden | 72d61fb | 2009-06-24 16:56:06 -0700 | [diff] [blame] | 3204 | /* |
| 3205 | * Start by determining if the object supports the DirectBuffer |
| 3206 | * interfaces. Note this does not guarantee that it's a direct buffer. |
| 3207 | */ |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3208 | if (!dvmInstanceof(bufObj->clazz, |
| 3209 | gDvm.classOrgApacheHarmonyNioInternalDirectBuffer)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3210 | { |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3211 | goto bail; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3212 | } |
| 3213 | |
| Andy McFadden | 72d61fb | 2009-06-24 16:56:06 -0700 | [diff] [blame] | 3214 | /* |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3215 | * Get a PlatformAddress object with the effective address. |
| Andy McFadden | 5f612b8 | 2009-07-22 15:07:27 -0700 | [diff] [blame] | 3216 | * |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3217 | * If this isn't a direct buffer, the result will be NULL and/or an |
| Andy McFadden | 72d61fb | 2009-06-24 16:56:06 -0700 | [diff] [blame] | 3218 | * exception will have been thrown. |
| 3219 | */ |
| Andy McFadden | 8e696dc | 2009-07-24 15:28:16 -0700 | [diff] [blame] | 3220 | JValue callResult; |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3221 | const Method* meth = dvmGetVirtualizedMethod(bufObj->clazz, |
| 3222 | gDvm.methOrgApacheHarmonyNioInternalDirectBuffer_getEffectiveAddress); |
| 3223 | dvmCallMethodA(self, meth, bufObj, &callResult, NULL); |
| 3224 | if (dvmGetException(self) != NULL) { |
| 3225 | dvmClearException(self); |
| 3226 | callResult.l = NULL; |
| Andy McFadden | 72d61fb | 2009-06-24 16:56:06 -0700 | [diff] [blame] | 3227 | } |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3228 | |
| Andy McFadden | 8e696dc | 2009-07-24 15:28:16 -0700 | [diff] [blame] | 3229 | Object* platformAddr = callResult.l; |
| Andy McFadden | 72d61fb | 2009-06-24 16:56:06 -0700 | [diff] [blame] | 3230 | if (platformAddr == NULL) { |
| Andy McFadden | 8e696dc | 2009-07-24 15:28:16 -0700 | [diff] [blame] | 3231 | LOGV("Got request for address of non-direct buffer\n"); |
| Andy McFadden | 72d61fb | 2009-06-24 16:56:06 -0700 | [diff] [blame] | 3232 | goto bail; |
| 3233 | } |
| 3234 | |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3235 | /* |
| 3236 | * Extract the address from the PlatformAddress object. Instead of |
| 3237 | * calling the toLong() method, just grab the field directly. This |
| 3238 | * is faster but more fragile. |
| 3239 | */ |
| 3240 | result = (void*) dvmGetFieldInt(platformAddr, |
| 3241 | gDvm.offOrgApacheHarmonyLuniPlatformPlatformAddress_osaddr); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3242 | |
| Andy McFadden | 8e696dc | 2009-07-24 15:28:16 -0700 | [diff] [blame] | 3243 | //LOGI("slow path for %p --> %p\n", buf, result); |
| 3244 | |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3245 | bail: |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3246 | JNI_EXIT(); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3247 | return result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | /* |
| 3251 | * Get the capacity of the buffer for the specified java.nio.Buffer. |
| 3252 | * |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3253 | * Returns -1 if the object is not a direct buffer. (We actually skip |
| 3254 | * this check, since it's expensive to determine, and just return the |
| 3255 | * capacity regardless.) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3256 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3257 | static jlong GetDirectBufferCapacity(JNIEnv* env, jobject jbuf) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3258 | { |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3259 | JNI_ENTER(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3260 | |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3261 | /* |
| 3262 | * The capacity is always in the Buffer.capacity field. |
| 3263 | * |
| 3264 | * (The "check" version should verify that this is actually a Buffer, |
| 3265 | * but we're not required to do so here.) |
| 3266 | */ |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3267 | Object* buf = dvmDecodeIndirectRef(env, jbuf); |
| 3268 | jlong result = dvmGetFieldInt(buf, gDvm.offJavaNioBuffer_capacity); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3269 | |
| Andy McFadden | 8e5c784 | 2009-07-23 17:47:18 -0700 | [diff] [blame] | 3270 | JNI_EXIT(); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3271 | return result; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | |
| 3275 | /* |
| 3276 | * =========================================================================== |
| 3277 | * JNI invocation functions |
| 3278 | * =========================================================================== |
| 3279 | */ |
| 3280 | |
| 3281 | /* |
| 3282 | * Handle AttachCurrentThread{AsDaemon}. |
| 3283 | * |
| 3284 | * We need to make sure the VM is actually running. For example, if we start |
| 3285 | * up, issue an Attach, and the VM exits almost immediately, by the time the |
| 3286 | * attaching happens the VM could already be shutting down. |
| 3287 | * |
| 3288 | * It's hard to avoid a race condition here because we don't want to hold |
| 3289 | * a lock across the entire operation. What we can do is temporarily |
| 3290 | * increment the thread count to prevent a VM exit. |
| 3291 | * |
| 3292 | * This could potentially still have problems if a daemon thread calls here |
| 3293 | * while the VM is shutting down. dvmThreadSelf() will work, since it just |
| 3294 | * uses pthread TLS, but dereferencing "vm" could fail. Such is life when |
| 3295 | * you shut down a VM while threads are still running inside it. |
| 3296 | * |
| 3297 | * Remember that some code may call this as a way to find the per-thread |
| 3298 | * JNIEnv pointer. Don't do excess work for that case. |
| 3299 | */ |
| 3300 | static jint attachThread(JavaVM* vm, JNIEnv** p_env, void* thr_args, |
| 3301 | bool isDaemon) |
| 3302 | { |
| 3303 | JavaVMAttachArgs* args = (JavaVMAttachArgs*) thr_args; |
| 3304 | Thread* self; |
| 3305 | bool result = false; |
| 3306 | |
| 3307 | /* |
| 3308 | * Return immediately if we're already one with the VM. |
| 3309 | */ |
| 3310 | self = dvmThreadSelf(); |
| 3311 | if (self != NULL) { |
| 3312 | *p_env = self->jniEnv; |
| 3313 | return JNI_OK; |
| 3314 | } |
| 3315 | |
| 3316 | /* |
| 3317 | * No threads allowed in zygote mode. |
| 3318 | */ |
| 3319 | if (gDvm.zygote) { |
| 3320 | return JNI_ERR; |
| 3321 | } |
| 3322 | |
| 3323 | /* increment the count to keep the VM from bailing while we run */ |
| 3324 | dvmLockThreadList(NULL); |
| 3325 | if (gDvm.nonDaemonThreadCount == 0) { |
| 3326 | // dead or dying |
| 3327 | LOGV("Refusing to attach thread '%s' -- VM is shutting down\n", |
| 3328 | (thr_args == NULL) ? "(unknown)" : args->name); |
| 3329 | dvmUnlockThreadList(); |
| 3330 | return JNI_ERR; |
| 3331 | } |
| 3332 | gDvm.nonDaemonThreadCount++; |
| 3333 | dvmUnlockThreadList(); |
| 3334 | |
| 3335 | /* tweak the JavaVMAttachArgs as needed */ |
| 3336 | JavaVMAttachArgs argsCopy; |
| 3337 | if (args == NULL) { |
| 3338 | /* allow the v1.1 calling convention */ |
| 3339 | argsCopy.version = JNI_VERSION_1_2; |
| 3340 | argsCopy.name = NULL; |
| 3341 | argsCopy.group = dvmGetMainThreadGroup(); |
| 3342 | } else { |
| 3343 | assert(args->version >= JNI_VERSION_1_2); |
| 3344 | |
| 3345 | argsCopy.version = args->version; |
| 3346 | argsCopy.name = args->name; |
| 3347 | if (args->group != NULL) |
| 3348 | argsCopy.group = args->group; |
| 3349 | else |
| 3350 | argsCopy.group = dvmGetMainThreadGroup(); |
| 3351 | } |
| 3352 | |
| 3353 | result = dvmAttachCurrentThread(&argsCopy, isDaemon); |
| 3354 | |
| 3355 | /* restore the count */ |
| 3356 | dvmLockThreadList(NULL); |
| 3357 | gDvm.nonDaemonThreadCount--; |
| 3358 | dvmUnlockThreadList(); |
| 3359 | |
| 3360 | /* |
| 3361 | * Change the status to indicate that we're out in native code. This |
| 3362 | * call is not guarded with state-change macros, so we have to do it |
| 3363 | * by hand. |
| 3364 | */ |
| 3365 | if (result) { |
| 3366 | self = dvmThreadSelf(); |
| 3367 | assert(self != NULL); |
| 3368 | dvmChangeStatus(self, THREAD_NATIVE); |
| 3369 | *p_env = self->jniEnv; |
| 3370 | return JNI_OK; |
| 3371 | } else { |
| 3372 | return JNI_ERR; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | /* |
| 3377 | * Attach the current thread to the VM. If the thread is already attached, |
| 3378 | * this is a no-op. |
| 3379 | */ |
| 3380 | static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) |
| 3381 | { |
| 3382 | return attachThread(vm, p_env, thr_args, false); |
| 3383 | } |
| 3384 | |
| 3385 | /* |
| 3386 | * Like AttachCurrentThread, but set the "daemon" flag. |
| 3387 | */ |
| 3388 | static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, |
| 3389 | void* thr_args) |
| 3390 | { |
| 3391 | return attachThread(vm, p_env, thr_args, true); |
| 3392 | } |
| 3393 | |
| 3394 | /* |
| 3395 | * Dissociate the current thread from the VM. |
| 3396 | */ |
| 3397 | static jint DetachCurrentThread(JavaVM* vm) |
| 3398 | { |
| 3399 | Thread* self = dvmThreadSelf(); |
| 3400 | |
| 3401 | if (self == NULL) /* not attached, can't do anything */ |
| 3402 | return JNI_ERR; |
| 3403 | |
| 3404 | /* switch to "running" to check for suspension */ |
| 3405 | dvmChangeStatus(self, THREAD_RUNNING); |
| 3406 | |
| 3407 | /* detach the thread */ |
| 3408 | dvmDetachCurrentThread(); |
| 3409 | |
| 3410 | /* (no need to change status back -- we have no status) */ |
| 3411 | return JNI_OK; |
| 3412 | } |
| 3413 | |
| 3414 | /* |
| 3415 | * If current thread is attached to VM, return the associated JNIEnv. |
| 3416 | * Otherwise, stuff NULL in and return JNI_EDETACHED. |
| 3417 | * |
| 3418 | * JVMTI overloads this by specifying a magic value for "version", so we |
| 3419 | * do want to check that here. |
| 3420 | */ |
| 3421 | static jint GetEnv(JavaVM* vm, void** env, jint version) |
| 3422 | { |
| 3423 | Thread* self = dvmThreadSelf(); |
| 3424 | |
| 3425 | if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) |
| 3426 | return JNI_EVERSION; |
| 3427 | |
| 3428 | if (self == NULL) { |
| 3429 | *env = NULL; |
| 3430 | } else { |
| 3431 | /* TODO: status change is probably unnecessary */ |
| 3432 | dvmChangeStatus(self, THREAD_RUNNING); |
| 3433 | *env = (void*) dvmGetThreadJNIEnv(self); |
| 3434 | dvmChangeStatus(self, THREAD_NATIVE); |
| 3435 | } |
| 3436 | if (*env == NULL) |
| 3437 | return JNI_EDETACHED; |
| 3438 | else |
| 3439 | return JNI_OK; |
| 3440 | } |
| 3441 | |
| 3442 | /* |
| 3443 | * Destroy the VM. This may be called from any thread. |
| 3444 | * |
| 3445 | * If the current thread is attached, wait until the current thread is |
| 3446 | * the only non-daemon user-level thread. If the current thread is not |
| 3447 | * attached, we attach it and do the processing as usual. (If the attach |
| 3448 | * fails, it's probably because all the non-daemon threads have already |
| 3449 | * exited and the VM doesn't want to let us back in.) |
| 3450 | * |
| 3451 | * TODO: we don't really deal with the situation where more than one thread |
| 3452 | * has called here. One thread wins, the other stays trapped waiting on |
| 3453 | * the condition variable forever. Not sure this situation is interesting |
| 3454 | * in real life. |
| 3455 | */ |
| 3456 | static jint DestroyJavaVM(JavaVM* vm) |
| 3457 | { |
| 3458 | JavaVMExt* ext = (JavaVMExt*) vm; |
| 3459 | Thread* self; |
| 3460 | |
| 3461 | if (ext == NULL) |
| 3462 | return JNI_ERR; |
| 3463 | |
| 3464 | LOGD("DestroyJavaVM waiting for non-daemon threads to exit\n"); |
| 3465 | |
| 3466 | /* |
| 3467 | * Sleep on a condition variable until it's okay to exit. |
| 3468 | */ |
| 3469 | self = dvmThreadSelf(); |
| 3470 | if (self == NULL) { |
| 3471 | JNIEnv* tmpEnv; |
| 3472 | if (AttachCurrentThread(vm, &tmpEnv, NULL) != JNI_OK) { |
| 3473 | LOGV("Unable to reattach main for Destroy; assuming VM is " |
| 3474 | "shutting down (count=%d)\n", |
| 3475 | gDvm.nonDaemonThreadCount); |
| 3476 | goto shutdown; |
| 3477 | } else { |
| 3478 | LOGV("Attached to wait for shutdown in Destroy\n"); |
| 3479 | } |
| 3480 | } |
| 3481 | dvmChangeStatus(self, THREAD_VMWAIT); |
| 3482 | |
| 3483 | dvmLockThreadList(self); |
| 3484 | gDvm.nonDaemonThreadCount--; // remove current thread from count |
| 3485 | |
| 3486 | while (gDvm.nonDaemonThreadCount > 0) |
| 3487 | pthread_cond_wait(&gDvm.vmExitCond, &gDvm.threadListLock); |
| 3488 | |
| 3489 | dvmUnlockThreadList(); |
| 3490 | self = NULL; |
| 3491 | |
| 3492 | shutdown: |
| 3493 | // TODO: call System.exit() to run any registered shutdown hooks |
| 3494 | // (this may not return -- figure out how this should work) |
| 3495 | |
| 3496 | LOGD("DestroyJavaVM shutting VM down\n"); |
| 3497 | dvmShutdown(); |
| 3498 | |
| 3499 | // TODO - free resources associated with JNI-attached daemon threads |
| 3500 | free(ext->envList); |
| 3501 | free(ext); |
| 3502 | |
| 3503 | return JNI_OK; |
| 3504 | } |
| 3505 | |
| 3506 | |
| 3507 | /* |
| 3508 | * =========================================================================== |
| 3509 | * Function tables |
| 3510 | * =========================================================================== |
| 3511 | */ |
| 3512 | |
| 3513 | static const struct JNINativeInterface gNativeInterface = { |
| 3514 | NULL, |
| 3515 | NULL, |
| 3516 | NULL, |
| 3517 | NULL, |
| 3518 | |
| 3519 | GetVersion, |
| 3520 | |
| 3521 | DefineClass, |
| 3522 | FindClass, |
| 3523 | |
| 3524 | FromReflectedMethod, |
| 3525 | FromReflectedField, |
| 3526 | ToReflectedMethod, |
| 3527 | |
| 3528 | GetSuperclass, |
| 3529 | IsAssignableFrom, |
| 3530 | |
| 3531 | ToReflectedField, |
| 3532 | |
| 3533 | Throw, |
| 3534 | ThrowNew, |
| 3535 | ExceptionOccurred, |
| 3536 | ExceptionDescribe, |
| 3537 | ExceptionClear, |
| 3538 | FatalError, |
| 3539 | |
| 3540 | PushLocalFrame, |
| 3541 | PopLocalFrame, |
| 3542 | |
| 3543 | NewGlobalRef, |
| 3544 | DeleteGlobalRef, |
| 3545 | DeleteLocalRef, |
| 3546 | IsSameObject, |
| 3547 | NewLocalRef, |
| 3548 | EnsureLocalCapacity, |
| 3549 | |
| 3550 | AllocObject, |
| 3551 | NewObject, |
| 3552 | NewObjectV, |
| 3553 | NewObjectA, |
| 3554 | |
| 3555 | GetObjectClass, |
| 3556 | IsInstanceOf, |
| 3557 | |
| 3558 | GetMethodID, |
| 3559 | |
| 3560 | CallObjectMethod, |
| 3561 | CallObjectMethodV, |
| 3562 | CallObjectMethodA, |
| 3563 | CallBooleanMethod, |
| 3564 | CallBooleanMethodV, |
| 3565 | CallBooleanMethodA, |
| 3566 | CallByteMethod, |
| 3567 | CallByteMethodV, |
| 3568 | CallByteMethodA, |
| 3569 | CallCharMethod, |
| 3570 | CallCharMethodV, |
| 3571 | CallCharMethodA, |
| 3572 | CallShortMethod, |
| 3573 | CallShortMethodV, |
| 3574 | CallShortMethodA, |
| 3575 | CallIntMethod, |
| 3576 | CallIntMethodV, |
| 3577 | CallIntMethodA, |
| 3578 | CallLongMethod, |
| 3579 | CallLongMethodV, |
| 3580 | CallLongMethodA, |
| 3581 | CallFloatMethod, |
| 3582 | CallFloatMethodV, |
| 3583 | CallFloatMethodA, |
| 3584 | CallDoubleMethod, |
| 3585 | CallDoubleMethodV, |
| 3586 | CallDoubleMethodA, |
| 3587 | CallVoidMethod, |
| 3588 | CallVoidMethodV, |
| 3589 | CallVoidMethodA, |
| 3590 | |
| 3591 | CallNonvirtualObjectMethod, |
| 3592 | CallNonvirtualObjectMethodV, |
| 3593 | CallNonvirtualObjectMethodA, |
| 3594 | CallNonvirtualBooleanMethod, |
| 3595 | CallNonvirtualBooleanMethodV, |
| 3596 | CallNonvirtualBooleanMethodA, |
| 3597 | CallNonvirtualByteMethod, |
| 3598 | CallNonvirtualByteMethodV, |
| 3599 | CallNonvirtualByteMethodA, |
| 3600 | CallNonvirtualCharMethod, |
| 3601 | CallNonvirtualCharMethodV, |
| 3602 | CallNonvirtualCharMethodA, |
| 3603 | CallNonvirtualShortMethod, |
| 3604 | CallNonvirtualShortMethodV, |
| 3605 | CallNonvirtualShortMethodA, |
| 3606 | CallNonvirtualIntMethod, |
| 3607 | CallNonvirtualIntMethodV, |
| 3608 | CallNonvirtualIntMethodA, |
| 3609 | CallNonvirtualLongMethod, |
| 3610 | CallNonvirtualLongMethodV, |
| 3611 | CallNonvirtualLongMethodA, |
| 3612 | CallNonvirtualFloatMethod, |
| 3613 | CallNonvirtualFloatMethodV, |
| 3614 | CallNonvirtualFloatMethodA, |
| 3615 | CallNonvirtualDoubleMethod, |
| 3616 | CallNonvirtualDoubleMethodV, |
| 3617 | CallNonvirtualDoubleMethodA, |
| 3618 | CallNonvirtualVoidMethod, |
| 3619 | CallNonvirtualVoidMethodV, |
| 3620 | CallNonvirtualVoidMethodA, |
| 3621 | |
| 3622 | GetFieldID, |
| 3623 | |
| 3624 | GetObjectField, |
| 3625 | GetBooleanField, |
| 3626 | GetByteField, |
| 3627 | GetCharField, |
| 3628 | GetShortField, |
| 3629 | GetIntField, |
| 3630 | GetLongField, |
| 3631 | GetFloatField, |
| 3632 | GetDoubleField, |
| 3633 | SetObjectField, |
| 3634 | SetBooleanField, |
| 3635 | SetByteField, |
| 3636 | SetCharField, |
| 3637 | SetShortField, |
| 3638 | SetIntField, |
| 3639 | SetLongField, |
| 3640 | SetFloatField, |
| 3641 | SetDoubleField, |
| 3642 | |
| 3643 | GetStaticMethodID, |
| 3644 | |
| 3645 | CallStaticObjectMethod, |
| 3646 | CallStaticObjectMethodV, |
| 3647 | CallStaticObjectMethodA, |
| 3648 | CallStaticBooleanMethod, |
| 3649 | CallStaticBooleanMethodV, |
| 3650 | CallStaticBooleanMethodA, |
| 3651 | CallStaticByteMethod, |
| 3652 | CallStaticByteMethodV, |
| 3653 | CallStaticByteMethodA, |
| 3654 | CallStaticCharMethod, |
| 3655 | CallStaticCharMethodV, |
| 3656 | CallStaticCharMethodA, |
| 3657 | CallStaticShortMethod, |
| 3658 | CallStaticShortMethodV, |
| 3659 | CallStaticShortMethodA, |
| 3660 | CallStaticIntMethod, |
| 3661 | CallStaticIntMethodV, |
| 3662 | CallStaticIntMethodA, |
| 3663 | CallStaticLongMethod, |
| 3664 | CallStaticLongMethodV, |
| 3665 | CallStaticLongMethodA, |
| 3666 | CallStaticFloatMethod, |
| 3667 | CallStaticFloatMethodV, |
| 3668 | CallStaticFloatMethodA, |
| 3669 | CallStaticDoubleMethod, |
| 3670 | CallStaticDoubleMethodV, |
| 3671 | CallStaticDoubleMethodA, |
| 3672 | CallStaticVoidMethod, |
| 3673 | CallStaticVoidMethodV, |
| 3674 | CallStaticVoidMethodA, |
| 3675 | |
| 3676 | GetStaticFieldID, |
| 3677 | |
| 3678 | GetStaticObjectField, |
| 3679 | GetStaticBooleanField, |
| 3680 | GetStaticByteField, |
| 3681 | GetStaticCharField, |
| 3682 | GetStaticShortField, |
| 3683 | GetStaticIntField, |
| 3684 | GetStaticLongField, |
| 3685 | GetStaticFloatField, |
| 3686 | GetStaticDoubleField, |
| 3687 | |
| 3688 | SetStaticObjectField, |
| 3689 | SetStaticBooleanField, |
| 3690 | SetStaticByteField, |
| 3691 | SetStaticCharField, |
| 3692 | SetStaticShortField, |
| 3693 | SetStaticIntField, |
| 3694 | SetStaticLongField, |
| 3695 | SetStaticFloatField, |
| 3696 | SetStaticDoubleField, |
| 3697 | |
| 3698 | NewString, |
| 3699 | |
| 3700 | GetStringLength, |
| 3701 | GetStringChars, |
| 3702 | ReleaseStringChars, |
| 3703 | |
| 3704 | NewStringUTF, |
| 3705 | GetStringUTFLength, |
| 3706 | GetStringUTFChars, |
| 3707 | ReleaseStringUTFChars, |
| 3708 | |
| 3709 | GetArrayLength, |
| 3710 | NewObjectArray, |
| 3711 | GetObjectArrayElement, |
| 3712 | SetObjectArrayElement, |
| 3713 | |
| 3714 | NewBooleanArray, |
| 3715 | NewByteArray, |
| 3716 | NewCharArray, |
| 3717 | NewShortArray, |
| 3718 | NewIntArray, |
| 3719 | NewLongArray, |
| 3720 | NewFloatArray, |
| 3721 | NewDoubleArray, |
| 3722 | |
| 3723 | GetBooleanArrayElements, |
| 3724 | GetByteArrayElements, |
| 3725 | GetCharArrayElements, |
| 3726 | GetShortArrayElements, |
| 3727 | GetIntArrayElements, |
| 3728 | GetLongArrayElements, |
| 3729 | GetFloatArrayElements, |
| 3730 | GetDoubleArrayElements, |
| 3731 | |
| 3732 | ReleaseBooleanArrayElements, |
| 3733 | ReleaseByteArrayElements, |
| 3734 | ReleaseCharArrayElements, |
| 3735 | ReleaseShortArrayElements, |
| 3736 | ReleaseIntArrayElements, |
| 3737 | ReleaseLongArrayElements, |
| 3738 | ReleaseFloatArrayElements, |
| 3739 | ReleaseDoubleArrayElements, |
| 3740 | |
| 3741 | GetBooleanArrayRegion, |
| 3742 | GetByteArrayRegion, |
| 3743 | GetCharArrayRegion, |
| 3744 | GetShortArrayRegion, |
| 3745 | GetIntArrayRegion, |
| 3746 | GetLongArrayRegion, |
| 3747 | GetFloatArrayRegion, |
| 3748 | GetDoubleArrayRegion, |
| 3749 | SetBooleanArrayRegion, |
| 3750 | SetByteArrayRegion, |
| 3751 | SetCharArrayRegion, |
| 3752 | SetShortArrayRegion, |
| 3753 | SetIntArrayRegion, |
| 3754 | SetLongArrayRegion, |
| 3755 | SetFloatArrayRegion, |
| 3756 | SetDoubleArrayRegion, |
| 3757 | |
| 3758 | RegisterNatives, |
| 3759 | UnregisterNatives, |
| 3760 | |
| 3761 | MonitorEnter, |
| 3762 | MonitorExit, |
| 3763 | |
| 3764 | GetJavaVM, |
| 3765 | |
| 3766 | GetStringRegion, |
| 3767 | GetStringUTFRegion, |
| 3768 | |
| 3769 | GetPrimitiveArrayCritical, |
| 3770 | ReleasePrimitiveArrayCritical, |
| 3771 | |
| 3772 | GetStringCritical, |
| 3773 | ReleaseStringCritical, |
| 3774 | |
| 3775 | NewWeakGlobalRef, |
| 3776 | DeleteWeakGlobalRef, |
| 3777 | |
| 3778 | ExceptionCheck, |
| 3779 | |
| 3780 | NewDirectByteBuffer, |
| 3781 | GetDirectBufferAddress, |
| 3782 | GetDirectBufferCapacity, |
| 3783 | |
| 3784 | GetObjectRefType |
| 3785 | }; |
| 3786 | static const struct JNIInvokeInterface gInvokeInterface = { |
| 3787 | NULL, |
| 3788 | NULL, |
| 3789 | NULL, |
| 3790 | |
| 3791 | DestroyJavaVM, |
| 3792 | AttachCurrentThread, |
| 3793 | DetachCurrentThread, |
| 3794 | |
| 3795 | GetEnv, |
| 3796 | |
| 3797 | AttachCurrentThreadAsDaemon, |
| 3798 | }; |
| 3799 | |
| 3800 | |
| 3801 | /* |
| 3802 | * =========================================================================== |
| 3803 | * VM/Env creation |
| 3804 | * =========================================================================== |
| 3805 | */ |
| 3806 | |
| 3807 | /* |
| 3808 | * Enable "checked JNI" after the VM has partially started. This must |
| 3809 | * only be called in "zygote" mode, when we have one thread running. |
| Andy McFadden | 59b6177 | 2009-05-13 16:44:34 -0700 | [diff] [blame] | 3810 | * |
| 3811 | * This doesn't attempt to rewrite the JNI call bridge associated with |
| 3812 | * native methods, so we won't get those checks for any methods that have |
| 3813 | * already been resolved. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3814 | */ |
| 3815 | void dvmLateEnableCheckedJni(void) |
| 3816 | { |
| 3817 | JNIEnvExt* extEnv; |
| 3818 | JavaVMExt* extVm; |
| Andy McFadden | ab00d45 | 2009-08-19 07:21:41 -0700 | [diff] [blame] | 3819 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 3820 | extEnv = dvmGetJNIEnvForThread(); |
| 3821 | if (extEnv == NULL) { |
| 3822 | LOGE("dvmLateEnableCheckedJni: thread has no JNIEnv\n"); |
| 3823 | return; |
| 3824 | } |
| 3825 | extVm = extEnv->vm; |
| 3826 | assert(extVm != NULL); |
| 3827 | |
| 3828 | if (!extVm->useChecked) { |
| 3829 | LOGD("Late-enabling CheckJNI\n"); |
| 3830 | dvmUseCheckedJniVm(extVm); |
| 3831 | extVm->useChecked = true; |
| 3832 | dvmUseCheckedJniEnv(extEnv); |
| 3833 | |
| 3834 | /* currently no way to pick up jniopts features */ |
| 3835 | } else { |
| 3836 | LOGD("Not late-enabling CheckJNI (already on)\n"); |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | /* |
| 3841 | * Not supported. |
| 3842 | */ |
| 3843 | jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) |
| 3844 | { |
| 3845 | return JNI_ERR; |
| 3846 | } |
| 3847 | |
| 3848 | /* |
| 3849 | * Return a buffer full of created VMs. |
| 3850 | * |
| 3851 | * We always have zero or one. |
| 3852 | */ |
| 3853 | jint JNI_GetCreatedJavaVMs(JavaVM** vmBuf, jsize bufLen, jsize* nVMs) |
| 3854 | { |
| 3855 | if (gDvm.vmList != NULL) { |
| 3856 | *nVMs = 1; |
| 3857 | |
| 3858 | if (bufLen > 0) |
| 3859 | *vmBuf++ = gDvm.vmList; |
| 3860 | } else { |
| 3861 | *nVMs = 0; |
| 3862 | } |
| 3863 | |
| 3864 | return JNI_OK; |
| 3865 | } |
| 3866 | |
| 3867 | |
| 3868 | /* |
| 3869 | * Create a new VM instance. |
| 3870 | * |
| 3871 | * The current thread becomes the main VM thread. We return immediately, |
| 3872 | * which effectively means the caller is executing in a native method. |
| 3873 | */ |
| 3874 | jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) |
| 3875 | { |
| 3876 | const JavaVMInitArgs* args = (JavaVMInitArgs*) vm_args; |
| 3877 | JNIEnvExt* pEnv = NULL; |
| 3878 | JavaVMExt* pVM = NULL; |
| 3879 | const char** argv; |
| 3880 | int argc = 0; |
| 3881 | int i, curOpt; |
| 3882 | int result = JNI_ERR; |
| 3883 | bool checkJni = false; |
| 3884 | bool warnError = true; |
| 3885 | bool forceDataCopy = false; |
| 3886 | |
| 3887 | if (args->version < JNI_VERSION_1_2) |
| 3888 | return JNI_EVERSION; |
| 3889 | |
| 3890 | // TODO: don't allow creation of multiple VMs -- one per customer for now |
| 3891 | |
| 3892 | /* zero globals; not strictly necessary the first time a VM is started */ |
| 3893 | memset(&gDvm, 0, sizeof(gDvm)); |
| 3894 | |
| 3895 | /* |
| 3896 | * Set up structures for JNIEnv and VM. |
| 3897 | */ |
| 3898 | //pEnv = (JNIEnvExt*) malloc(sizeof(JNIEnvExt)); |
| 3899 | pVM = (JavaVMExt*) malloc(sizeof(JavaVMExt)); |
| 3900 | |
| 3901 | //memset(pEnv, 0, sizeof(JNIEnvExt)); |
| 3902 | //pEnv->funcTable = &gNativeInterface; |
| 3903 | //pEnv->vm = pVM; |
| 3904 | memset(pVM, 0, sizeof(JavaVMExt)); |
| 3905 | pVM->funcTable = &gInvokeInterface; |
| 3906 | pVM->envList = pEnv; |
| 3907 | dvmInitMutex(&pVM->envListLock); |
| 3908 | |
| 3909 | argv = (const char**) malloc(sizeof(char*) * (args->nOptions)); |
| 3910 | memset(argv, 0, sizeof(char*) * (args->nOptions)); |
| 3911 | |
| 3912 | curOpt = 0; |
| 3913 | |
| 3914 | /* |
| 3915 | * Convert JNI args to argv. |
| 3916 | * |
| 3917 | * We have to pull out vfprintf/exit/abort, because they use the |
| 3918 | * "extraInfo" field to pass function pointer "hooks" in. We also |
| 3919 | * look for the -Xcheck:jni stuff here. |
| 3920 | */ |
| 3921 | for (i = 0; i < args->nOptions; i++) { |
| 3922 | const char* optStr = args->options[i].optionString; |
| 3923 | |
| 3924 | if (optStr == NULL) { |
| 3925 | fprintf(stderr, "ERROR: arg %d string was null\n", i); |
| 3926 | goto bail; |
| 3927 | } else if (strcmp(optStr, "vfprintf") == 0) { |
| 3928 | gDvm.vfprintfHook = args->options[i].extraInfo; |
| 3929 | } else if (strcmp(optStr, "exit") == 0) { |
| 3930 | gDvm.exitHook = args->options[i].extraInfo; |
| 3931 | } else if (strcmp(optStr, "abort") == 0) { |
| 3932 | gDvm.abortHook = args->options[i].extraInfo; |
| 3933 | } else if (strcmp(optStr, "-Xcheck:jni") == 0) { |
| 3934 | checkJni = true; |
| 3935 | } else if (strncmp(optStr, "-Xjniopts:", 10) == 0) { |
| 3936 | const char* jniOpts = optStr + 9; |
| 3937 | while (jniOpts != NULL) { |
| 3938 | jniOpts++; /* skip past ':' or ',' */ |
| 3939 | if (strncmp(jniOpts, "warnonly", 8) == 0) { |
| 3940 | warnError = false; |
| 3941 | } else if (strncmp(jniOpts, "forcecopy", 9) == 0) { |
| 3942 | forceDataCopy = true; |
| 3943 | } else { |
| 3944 | LOGW("unknown jni opt starting at '%s'\n", jniOpts); |
| 3945 | } |
| 3946 | jniOpts = strchr(jniOpts, ','); |
| 3947 | } |
| 3948 | } else { |
| 3949 | /* regular option */ |
| 3950 | argv[curOpt++] = optStr; |
| 3951 | } |
| 3952 | } |
| 3953 | argc = curOpt; |
| 3954 | |
| 3955 | if (checkJni) { |
| 3956 | dvmUseCheckedJniVm(pVM); |
| 3957 | pVM->useChecked = true; |
| 3958 | } |
| 3959 | pVM->warnError = warnError; |
| 3960 | pVM->forceDataCopy = forceDataCopy; |
| 3961 | |
| 3962 | /* set this up before initializing VM, so it can create some JNIEnvs */ |
| 3963 | gDvm.vmList = (JavaVM*) pVM; |
| 3964 | |
| 3965 | /* |
| 3966 | * Create an env for main thread. We need to have something set up |
| 3967 | * here because some of the class initialization we do when starting |
| 3968 | * up the VM will call into native code. |
| 3969 | */ |
| 3970 | pEnv = (JNIEnvExt*) dvmCreateJNIEnv(NULL); |
| 3971 | |
| 3972 | /* initialize VM */ |
| 3973 | gDvm.initializing = true; |
| 3974 | if (dvmStartup(argc, argv, args->ignoreUnrecognized, (JNIEnv*)pEnv) != 0) { |
| 3975 | free(pEnv); |
| 3976 | free(pVM); |
| 3977 | goto bail; |
| 3978 | } |
| 3979 | |
| 3980 | /* |
| 3981 | * Success! Return stuff to caller. |
| 3982 | */ |
| 3983 | dvmChangeStatus(NULL, THREAD_NATIVE); |
| 3984 | *p_env = (JNIEnv*) pEnv; |
| 3985 | *p_vm = (JavaVM*) pVM; |
| 3986 | result = JNI_OK; |
| 3987 | |
| 3988 | bail: |
| 3989 | gDvm.initializing = false; |
| 3990 | if (result == JNI_OK) |
| 3991 | LOGV("JNI_CreateJavaVM succeeded\n"); |
| 3992 | else |
| 3993 | LOGW("JNI_CreateJavaVM failed\n"); |
| 3994 | free(argv); |
| 3995 | return result; |
| 3996 | } |
| 3997 | |