2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Support for -Xcheck:jni (the "careful" version of the JNI interfaces).
20 * We want to verify types, make sure class and field IDs are valid, and
21 * ensure that JNI's semantic expectations are being met. JNI seems to
22 * be relatively lax when it comes to requirements for permission checks,
23 * e.g. access to private methods is generally allowed from anywhere.
27 #include "JniInternal.h"
33 * Abort if we are configured to bail out on JNI warnings.
35 static void abortMaybe() {
36 if (!gDvmJni.warnOnly) {
37 dvmDumpThread(dvmThreadSelf(), false);
43 * ===========================================================================
44 * JNI call bridge wrapper
45 * ===========================================================================
49 * Check the result of a native method call that returns an object reference.
51 * The primary goal here is to verify that native code is returning the
52 * correct type of object. If it's declared to return a String but actually
53 * returns a byte array, things will fail in strange ways later on.
55 * This can be a fairly expensive operation, since we have to look up the
56 * return type class by name in method->clazz' class loader. We take a
57 * shortcut here and allow the call to succeed if the descriptor strings
58 * match. This will allow some false-positives when a class is redefined
59 * by a class loader, but that's rare enough that it doesn't seem worth
62 * At this point, pResult->l has already been converted to an object pointer.
64 static void checkCallResultCommon(const u4* args, const JValue* pResult,
65 const Method* method, Thread* self)
67 assert(pResult->l != NULL);
68 const Object* resultObj = (const Object*) pResult->l;
70 if (resultObj == kInvalidIndirectRefObject) {
71 LOGW("JNI WARNING: invalid reference returned from native code");
72 const Method* method = dvmGetCurrentJNIMethod();
73 char* desc = dexProtoCopyMethodDescriptor(&method->prototype);
74 LOGW(" in %s.%s:%s", method->clazz->descriptor, method->name, desc);
80 ClassObject* objClazz = resultObj->clazz;
83 * Make sure that pResult->l is an instance of the type this
84 * method was expected to return.
86 const char* declType = dexProtoGetReturnType(&method->prototype);
87 const char* objType = objClazz->descriptor;
88 if (strcmp(declType, objType) == 0) {
89 /* names match; ignore class loader issues and allow it */
90 ALOGV("Check %s.%s: %s io %s (FAST-OK)",
91 method->clazz->descriptor, method->name, objType, declType);
94 * Names didn't match. We need to resolve declType in the context
95 * of method->clazz->classLoader, and compare the class objects
98 * Since we're returning an instance of declType, it's safe to
99 * assume that it has been loaded and initialized (or, for the case
100 * of an array, generated). However, the current class loader may
101 * not be listed as an initiating loader, so we can't just look for
102 * it in the loaded-classes list.
104 ClassObject* declClazz = dvmFindClassNoInit(declType, method->clazz->classLoader);
105 if (declClazz == NULL) {
106 LOGW("JNI WARNING: method declared to return '%s' returned '%s'",
108 LOGW(" failed in %s.%s ('%s' not found)",
109 method->clazz->descriptor, method->name, declType);
113 if (!dvmInstanceof(objClazz, declClazz)) {
114 LOGW("JNI WARNING: method declared to return '%s' returned '%s'",
116 LOGW(" failed in %s.%s",
117 method->clazz->descriptor, method->name);
121 ALOGV("Check %s.%s: %s io %s (SLOW-OK)",
122 method->clazz->descriptor, method->name, objType, declType);
128 * Determine if we need to check the return type coming out of the call.
130 * (We don't simply do this at the top of checkCallResultCommon() because
131 * this is on the critical path for native method calls.)
133 static inline bool callNeedsCheck(const u4* args, JValue* pResult,
134 const Method* method, Thread* self)
136 return (method->shorty[0] == 'L' && !dvmCheckException(self) && pResult->l != NULL);
140 * Check a call into native code.
142 void dvmCheckCallJNIMethod(const u4* args, JValue* pResult,
143 const Method* method, Thread* self)
145 dvmCallJNIMethod(args, pResult, method, self);
146 if (callNeedsCheck(args, pResult, method, self)) {
147 checkCallResultCommon(args, pResult, method, self);
152 * ===========================================================================
153 * JNI function helpers
154 * ===========================================================================
157 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
158 return ((JNIEnvExt*) env)->baseFuncTable;
161 static inline const JNIInvokeInterface* baseVm(JavaVM* vm) {
162 return ((JavaVMExt*) vm)->baseFuncTable;
165 class ScopedJniThreadState {
167 explicit ScopedJniThreadState(JNIEnv* env) {
168 dvmChangeStatus(NULL, THREAD_RUNNING);
171 ~ScopedJniThreadState() {
172 dvmChangeStatus(NULL, THREAD_NATIVE);
176 // Disallow copy and assignment.
177 ScopedJniThreadState(const ScopedJniThreadState&);
178 void operator=(const ScopedJniThreadState&);
182 * Flags passed into ScopedCheck.
184 #define kFlag_Default 0x0000
186 #define kFlag_CritBad 0x0000 /* calling while in critical is bad */
187 #define kFlag_CritOkay 0x0001 /* ...okay */
188 #define kFlag_CritGet 0x0002 /* this is a critical "get" */
189 #define kFlag_CritRelease 0x0003 /* this is a critical "release" */
190 #define kFlag_CritMask 0x0003 /* bit mask to get "crit" value */
192 #define kFlag_ExcepBad 0x0000 /* raised exceptions are bad */
193 #define kFlag_ExcepOkay 0x0004 /* ...okay */
195 #define kFlag_Release 0x0010 /* are we in a non-critical release function? */
196 #define kFlag_NullableUtf 0x0020 /* are our UTF parameters nullable? */
198 #define kFlag_Invocation 0x8000 /* Part of the invocation interface (JavaVM*) */
200 static const char* indirectRefKindName(IndirectRef iref)
202 return indirectRefKindToString(indirectRefKind(iref));
207 // For JNIEnv* functions.
208 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName) {
209 init(env, flags, functionName, true);
213 // For JavaVM* functions.
214 explicit ScopedCheck(bool hasMethod, const char* functionName) {
215 init(NULL, kFlag_Invocation, functionName, hasMethod);
219 * In some circumstances the VM will screen class names, but it doesn't
220 * for class lookup. When things get bounced through a class loader, they
221 * can actually get normalized a couple of times; as a result, passing in
222 * a class name like "java.lang.Thread" instead of "java/lang/Thread" will
223 * work in some circumstances.
225 * This is incorrect and could cause strange behavior or compatibility
226 * problems, so we want to screen that out here.
228 * We expect "fully-qualified" class names, like "java/lang/Thread" or
229 * "[Ljava/lang/Object;".
231 void checkClassName(const char* className) {
232 if (!dexIsValidClassName(className, false)) {
233 LOGW("JNI WARNING: illegal class name '%s' (%s)", className, mFunctionName);
234 LOGW(" (should be formed like 'dalvik/system/DexFile')");
235 LOGW(" or '[Ldalvik/system/DexFile;' or '[[B')");
240 void checkFieldTypeForGet(jfieldID fid, const char* expectedSignature, bool isStatic) {
242 LOGW("JNI WARNING: null jfieldID");
247 bool printWarn = false;
248 Field* field = (Field*) fid;
249 const char* actualSignature = field->signature;
250 if (*expectedSignature == 'L') {
251 // 'actualSignature' has the exact type.
252 // We just know we're expecting some kind of reference.
253 if (*actualSignature != 'L' && *actualSignature != '[') {
256 } else if (*actualSignature != *expectedSignature) {
260 if (!printWarn && isStatic && !dvmIsStaticField(field)) {
262 LOGW("JNI WARNING: accessing non-static field %s as static", field->name);
264 LOGW("JNI WARNING: accessing static field %s as non-static", field->name);
270 LOGW("JNI WARNING: %s for field '%s' of expected type %s, got %s",
271 mFunctionName, field->name, expectedSignature, actualSignature);
278 * Verify that the field is of the appropriate type. If the field has an
279 * object type, "jobj" is the object we're trying to assign into it.
281 * Works for both static and instance fields.
283 void checkFieldTypeForSet(jobject jobj, jfieldID fieldID, PrimitiveType prim, bool isStatic) {
284 if (fieldID == NULL) {
285 LOGW("JNI WARNING: null jfieldID");
290 bool printWarn = false;
291 Field* field = (Field*) fieldID;
292 if ((field->signature[0] == 'L' || field->signature[0] == '[') && jobj != NULL) {
293 ScopedJniThreadState ts(mEnv);
294 Object* obj = dvmDecodeIndirectRef(mEnv, jobj);
296 * If jobj is a weak global ref whose referent has been cleared,
297 * obj will be NULL. Otherwise, obj should always be non-NULL
300 if (obj != NULL && !dvmIsHeapAddress(obj)) {
301 LOGW("JNI WARNING: field operation on invalid %s reference (%p)",
302 indirectRefKindName(jobj), jobj);
305 ClassObject* fieldClass = dvmFindLoadedClass(field->signature);
306 ClassObject* objClass = obj->clazz;
308 assert(fieldClass != NULL);
309 assert(objClass != NULL);
311 if (!dvmInstanceof(objClass, fieldClass)) {
312 LOGW("JNI WARNING: set field '%s' expected type %s, got %s",
313 field->name, field->signature, objClass->descriptor);
317 } else if (dexGetPrimitiveTypeFromDescriptorChar(field->signature[0]) != prim) {
318 LOGW("JNI WARNING: %s for field '%s' expected type %s, got %s",
319 mFunctionName, field->name, field->signature, primitiveTypeToName(prim));
321 } else if (isStatic && !dvmIsStaticField(field)) {
323 LOGW("JNI WARNING: accessing non-static field %s as static", field->name);
325 LOGW("JNI WARNING: accessing static field %s as non-static", field->name);
337 * Verify that this instance field ID is valid for this object.
339 * Assumes "jobj" has already been validated.
341 void checkInstanceFieldID(jobject jobj, jfieldID fieldID) {
342 ScopedJniThreadState ts(mEnv);
344 Object* obj = dvmDecodeIndirectRef(mEnv, jobj);
345 if (!dvmIsHeapAddress(obj)) {
346 LOGW("JNI ERROR: field operation on invalid reference (%p)", jobj);
351 * Check this class and all of its superclasses for a matching field.
352 * Don't need to scan interfaces.
354 ClassObject* clazz = obj->clazz;
355 while (clazz != NULL) {
356 if ((InstField*) fieldID >= clazz->ifields &&
357 (InstField*) fieldID < clazz->ifields + clazz->ifieldCount) {
361 clazz = clazz->super;
364 LOGW("JNI WARNING: instance fieldID %p not valid for class %s",
365 fieldID, obj->clazz->descriptor);
371 * Verify that the pointer value is non-NULL.
373 void checkNonNull(const void* ptr) {
375 LOGW("JNI WARNING: invalid null pointer (%s)", mFunctionName);
381 * Verify that the method's return type matches the type of call.
382 * 'expectedType' will be "L" for all objects, including arrays.
384 void checkSig(jmethodID methodID, const char* expectedType, bool isStatic) {
385 const Method* method = (const Method*) methodID;
386 bool printWarn = false;
388 if (*expectedType != method->shorty[0]) {
389 LOGW("JNI WARNING: expected return type '%s'", expectedType);
391 } else if (isStatic && !dvmIsStaticMethod(method)) {
393 LOGW("JNI WARNING: calling non-static method with static call");
395 LOGW("JNI WARNING: calling static method with non-static call");
401 char* desc = dexProtoCopyMethodDescriptor(&method->prototype);
402 LOGW(" calling %s.%s %s", method->clazz->descriptor, method->name, desc);
410 * Verify that this static field ID is valid for this class.
412 * Assumes "jclazz" has already been validated.
414 void checkStaticFieldID(jclass jclazz, jfieldID fieldID) {
415 ScopedJniThreadState ts(mEnv);
416 ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(mEnv, jclazz);
417 StaticField* base = &clazz->sfields[0];
418 int fieldCount = clazz->sfieldCount;
419 if ((StaticField*) fieldID < base || (StaticField*) fieldID >= base + fieldCount) {
420 LOGW("JNI WARNING: static fieldID %p not valid for class %s",
421 fieldID, clazz->descriptor);
422 LOGW(" base=%p count=%d", base, fieldCount);
429 * Verify that "methodID" is appropriate for "clazz".
431 * A mismatch isn't dangerous, because the jmethodID defines the class. In
432 * fact, jclazz is unused in the implementation. It's best if we don't
433 * allow bad code in the system though.
435 * Instances of "jclazz" must be instances of the method's declaring class.
437 void checkStaticMethod(jclass jclazz, jmethodID methodID) {
438 ScopedJniThreadState ts(mEnv);
440 ClassObject* clazz = (ClassObject*) dvmDecodeIndirectRef(mEnv, jclazz);
441 const Method* method = (const Method*) methodID;
443 if (!dvmInstanceof(clazz, method->clazz)) {
444 LOGW("JNI WARNING: can't call static %s.%s on class %s",
445 method->clazz->descriptor, method->name, clazz->descriptor);
452 * Verify that "methodID" is appropriate for "jobj".
454 * Make sure the object is an instance of the method's declaring class.
455 * (Note the methodID might point to a declaration in an interface; this
456 * will be handled automatically by the instanceof check.)
458 void checkVirtualMethod(jobject jobj, jmethodID methodID) {
459 ScopedJniThreadState ts(mEnv);
461 Object* obj = dvmDecodeIndirectRef(mEnv, jobj);
462 const Method* method = (const Method*) methodID;
464 if (!dvmInstanceof(obj->clazz, method->clazz)) {
465 LOGW("JNI WARNING: can't call %s.%s on instance of %s",
466 method->clazz->descriptor, method->name, obj->clazz->descriptor);
473 * The format string is a sequence of the following characters,
474 * and must be followed by arguments of the corresponding types
477 * Java primitive types:
485 * Z - jboolean (shown as true and false)
488 * Java reference types:
495 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
499 * r - jint (for release mode arguments)
500 * u - const char* (modified UTF-8)
501 * z - jsize (for lengths; use i if negative values are okay)
504 * . - no argument; just print "..." (used for varargs JNI calls)
506 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
508 void check(bool entry, const char* fmt0, ...) {
511 bool shouldTrace = false;
512 const Method* method = NULL;
513 if ((gDvm.jniTrace || gDvmJni.logThirdPartyJni) && mHasMethod) {
514 // We need to guard some of the invocation interface's calls: a bad caller might
515 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
516 if ((mFlags & kFlag_Invocation) == 0 || dvmThreadSelf() != NULL) {
517 method = dvmGetCurrentJNIMethod();
520 if (method != NULL) {
521 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
522 // when a native method that matches the Xjnitrace argument calls a JNI function
523 // such as NewByteArray.
524 if (gDvm.jniTrace && strstr(method->clazz->descriptor, gDvm.jniTrace) != NULL) {
527 // If -Xjniopts:logThirdPartyJni is on, we want to log any JNI function calls
528 // made by a third-party native method.
529 if (gDvmJni.logThirdPartyJni) {
530 shouldTrace |= method->shouldTrace;
537 for (const char* fmt = fmt0; *fmt;) {
539 if (ch == 'B') { // jbyte
540 jbyte b = va_arg(ap, int);
541 if (b >= 0 && b < 10) {
542 StringAppendF(&msg, "%d", b);
544 StringAppendF(&msg, "%#x (%d)", b, b);
546 } else if (ch == 'C') { // jchar
547 jchar c = va_arg(ap, int);
548 if (c < 0x7f && c >= ' ') {
549 StringAppendF(&msg, "U+%x ('%c')", c, c);
551 StringAppendF(&msg, "U+%x", c);
553 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
554 StringAppendF(&msg, "%g", va_arg(ap, double));
555 } else if (ch == 'I' || ch == 'S') { // jint, jshort
556 StringAppendF(&msg, "%d", va_arg(ap, int));
557 } else if (ch == 'J') { // jlong
558 StringAppendF(&msg, "%lld", va_arg(ap, jlong));
559 } else if (ch == 'Z') { // jboolean
560 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
561 } else if (ch == 'V') { // void
563 } else if (ch == 'v') { // JavaVM*
564 JavaVM* vm = va_arg(ap, JavaVM*);
565 StringAppendF(&msg, "(JavaVM*)%p", vm);
566 } else if (ch == 'E') { // JNIEnv*
567 JNIEnv* env = va_arg(ap, JNIEnv*);
568 StringAppendF(&msg, "(JNIEnv*)%p", env);
569 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
570 // For logging purposes, these are identical.
571 jobject o = va_arg(ap, jobject);
575 StringAppendF(&msg, "%p", o);
577 } else if (ch == 'b') { // jboolean (JNI-style)
578 jboolean b = va_arg(ap, int);
579 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
580 } else if (ch == 'c') { // jclass
581 jclass jc = va_arg(ap, jclass);
582 Object* c = dvmDecodeIndirectRef(mEnv, jc);
585 } else if (c == kInvalidIndirectRefObject || !dvmIsHeapAddress(c)) {
586 StringAppendF(&msg, "%p(INVALID)", jc);
588 std::string className(dvmHumanReadableType(c));
589 StringAppendF(&msg, "%s", className.c_str());
591 StringAppendF(&msg, " (%p)", jc);
594 } else if (ch == 'f') { // jfieldID
595 jfieldID fid = va_arg(ap, jfieldID);
596 std::string name(dvmHumanReadableField((Field*) fid));
597 StringAppendF(&msg, "%s", name.c_str());
599 StringAppendF(&msg, " (%p)", fid);
601 } else if (ch == 'z') { // non-negative jsize
602 // You might expect jsize to be size_t, but it's not; it's the same as jint.
603 // We only treat this specially so we can do the non-negative check.
604 // TODO: maybe this wasn't worth it?
605 jint i = va_arg(ap, jint);
606 StringAppendF(&msg, "%d", i);
607 } else if (ch == 'm') { // jmethodID
608 jmethodID mid = va_arg(ap, jmethodID);
609 std::string name(dvmHumanReadableMethod((Method*) mid, true));
610 StringAppendF(&msg, "%s", name.c_str());
612 StringAppendF(&msg, " (%p)", mid);
614 } else if (ch == 'p') { // void* ("pointer")
615 void* p = va_arg(ap, void*);
619 StringAppendF(&msg, "(void*) %p", p);
621 } else if (ch == 'r') { // jint (release mode)
622 jint releaseMode = va_arg(ap, jint);
623 if (releaseMode == 0) {
625 } else if (releaseMode == JNI_ABORT) {
627 } else if (releaseMode == JNI_COMMIT) {
630 StringAppendF(&msg, "invalid release mode %d", releaseMode);
632 } else if (ch == 'u') { // const char* (modified UTF-8)
633 const char* utf = va_arg(ap, const char*);
637 StringAppendF(&msg, "\"%s\"", utf);
639 } else if (ch == '.') {
642 LOGE("unknown trace format specifier %c", ch);
646 StringAppendF(&msg, ", ");
653 std::string methodName(dvmHumanReadableMethod(method, false));
654 LOGI("JNI: %s -> %s(%s)", methodName.c_str(), mFunctionName, msg.c_str());
655 mIndent = methodName.size() + 1;
657 LOGI("JNI: -> %s(%s)", mFunctionName, msg.c_str());
661 LOGI("JNI: %*s<- %s returned %s", mIndent, "", mFunctionName, msg.c_str());
665 // We always do the thorough checks on entry, and never on exit...
668 for (const char* fmt = fmt0; *fmt; ++fmt) {
671 checkArray(va_arg(ap, jarray));
672 } else if (ch == 'c') {
673 checkClass(va_arg(ap, jclass));
674 } else if (ch == 'L') {
675 checkObject(va_arg(ap, jobject));
676 } else if (ch == 'r') {
677 checkReleaseMode(va_arg(ap, jint));
678 } else if (ch == 's') {
679 checkString(va_arg(ap, jstring));
680 } else if (ch == 'u') {
681 if ((mFlags & kFlag_Release) != 0) {
682 checkNonNull(va_arg(ap, const char*));
684 bool nullable = ((mFlags & kFlag_NullableUtf) != 0);
685 checkUtfString(va_arg(ap, const char*), nullable);
687 } else if (ch == 'z') {
688 checkLengthPositive(va_arg(ap, jsize));
689 } else if (strchr("BCISZbfmpEv", ch) != NULL) {
690 va_arg(ap, int); // Skip this argument.
691 } else if (ch == 'D' || ch == 'F') {
692 va_arg(ap, double); // Skip this argument.
693 } else if (ch == 'J') {
694 va_arg(ap, long); // Skip this argument.
695 } else if (ch == '.') {
697 LOGE("unknown check format specifier %c", ch);
707 const char* mFunctionName;
712 void init(JNIEnv* env, int flags, const char* functionName, bool hasMethod) {
716 // Use +6 to drop the leading "Check_"...
717 mFunctionName = functionName + 6;
719 // Set "hasMethod" to true if we have a valid thread with a method pointer.
720 // We won't have one before attaching a thread, after detaching a thread, or
721 // after destroying the VM.
722 mHasMethod = hasMethod;
726 * Verify that "array" is non-NULL and points to an Array object.
728 * Since we're dealing with objects, switch to "running" mode.
730 void checkArray(jarray jarr) {
732 LOGW("JNI WARNING: received null array");
738 ScopedJniThreadState ts(mEnv);
739 bool printWarn = false;
741 Object* obj = dvmDecodeIndirectRef(mEnv, jarr);
742 if (!dvmIsHeapAddress(obj)) {
743 LOGW("JNI WARNING: jarray is an invalid %s reference (%p)",
744 indirectRefKindName(jarr), jarr);
746 } else if (obj->clazz->descriptor[0] != '[') {
747 LOGW("JNI WARNING: jarray arg has wrong type (expected array, got %s)",
748 obj->clazz->descriptor);
758 void checkClass(jclass c) {
759 checkInstance(c, gDvm.classJavaLangClass, "jclass");
762 void checkLengthPositive(jsize length) {
764 LOGW("JNI WARNING: negative jsize (%s)", mFunctionName);
770 * Verify that "jobj" is a valid object, and that it's an object that JNI
771 * is allowed to know about. We allow NULL references.
773 * Switches to "running" mode before performing checks.
775 void checkObject(jobject jobj) {
780 ScopedJniThreadState ts(mEnv);
782 bool printWarn = false;
783 if (dvmGetJNIRefType(mEnv, jobj) == JNIInvalidRefType) {
784 LOGW("JNI WARNING: %p is not a valid JNI reference", jobj);
787 Object* obj = dvmDecodeIndirectRef(mEnv, jobj);
788 if (obj == kInvalidIndirectRefObject) {
789 LOGW("JNI WARNING: native code passing in invalid reference %p", jobj);
791 } else if (obj != NULL && !dvmIsHeapAddress(obj)) {
792 // TODO: when we remove workAroundAppJniBugs, this should be impossible.
793 LOGW("JNI WARNING: native code passing in reference to invalid object %p %p",
806 * Verify that the "mode" argument passed to a primitive array Release
807 * function is one of the valid values.
809 void checkReleaseMode(jint mode) {
810 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
811 LOGW("JNI WARNING: bad value for mode (%d) (%s)", mode, mFunctionName);
816 void checkString(jstring s) {
817 checkInstance(s, gDvm.classJavaLangString, "jstring");
820 void checkThread(int flags) {
821 // Get the *correct* JNIEnv by going through our TLS pointer.
822 JNIEnvExt* threadEnv = dvmGetJNIEnvForThread();
825 * Verify that the current thread is (a) attached and (b) associated with
826 * this particular instance of JNIEnv.
828 bool printWarn = false;
829 if (threadEnv == NULL) {
830 LOGE("JNI ERROR: non-VM thread making JNI calls");
831 // don't set printWarn -- it'll try to call showLocation()
833 } else if ((JNIEnvExt*) mEnv != threadEnv) {
834 if (dvmThreadSelf()->threadId != threadEnv->envThreadId) {
835 LOGE("JNI: threadEnv != thread->env?");
839 LOGW("JNI WARNING: threadid=%d using env from threadid=%d",
840 threadEnv->envThreadId, ((JNIEnvExt*) mEnv)->envThreadId);
843 // If we're keeping broken code limping along, we need to suppress the abort...
844 if (gDvmJni.workAroundAppJniBugs) {
848 /* this is a bad idea -- need to throw as we exit, or abort func */
849 //dvmThrowRuntimeException("invalid use of JNI env ptr");
850 } else if (((JNIEnvExt*) mEnv)->self != dvmThreadSelf()) {
851 /* correct JNIEnv*; make sure the "self" pointer is correct */
852 LOGE("JNI ERROR: env->self != thread-self (%p vs. %p)",
853 ((JNIEnvExt*) mEnv)->self, dvmThreadSelf());
858 * Verify that, if this thread previously made a critical "get" call, we
859 * do the corresponding "release" call before we try anything else.
861 switch (flags & kFlag_CritMask) {
862 case kFlag_CritOkay: // okay to call this method
864 case kFlag_CritBad: // not okay to call
865 if (threadEnv->critical) {
866 LOGW("JNI WARNING: threadid=%d using JNI after critical get",
867 threadEnv->envThreadId);
871 case kFlag_CritGet: // this is a "get" call
872 /* don't check here; we allow nested gets */
873 threadEnv->critical++;
875 case kFlag_CritRelease: // this is a "release" call
876 threadEnv->critical--;
877 if (threadEnv->critical < 0) {
878 LOGW("JNI WARNING: threadid=%d called too many crit releases",
879 threadEnv->envThreadId);
888 * Verify that, if an exception has been raised, the native code doesn't
889 * make any JNI calls other than the Exception* methods.
891 bool printException = false;
892 if ((flags & kFlag_ExcepOkay) == 0 && dvmCheckException(dvmThreadSelf())) {
893 LOGW("JNI WARNING: JNI method called with exception pending");
895 printException = true;
901 if (printException) {
902 LOGW("Pending exception is:");
903 dvmLogExceptionStackTrace();
911 * Verify that "bytes" points to valid "modified UTF-8" data.
913 void checkUtfString(const char* bytes, bool nullable) {
916 LOGW("JNI WARNING: non-nullable const char* was NULL");
923 const char* errorKind = NULL;
924 u1 utf8 = checkUtfBytes(bytes, &errorKind);
925 if (errorKind != NULL) {
926 LOGW("JNI WARNING: input is not valid Modified UTF-8: illegal %s byte %#x", errorKind, utf8);
927 LOGW(" string: '%s'", bytes);
934 * Verify that "jobj" is a valid non-NULL object reference, and points to
935 * an instance of expectedClass.
937 * Because we're looking at an object on the GC heap, we have to switch
938 * to "running" mode before doing the checks.
940 void checkInstance(jobject jobj, ClassObject* expectedClass, const char* argName) {
942 LOGW("JNI WARNING: received null %s", argName);
948 ScopedJniThreadState ts(mEnv);
949 bool printWarn = false;
951 Object* obj = dvmDecodeIndirectRef(mEnv, jobj);
952 if (!dvmIsHeapAddress(obj)) {
953 LOGW("JNI WARNING: %s is an invalid %s reference (%p)",
954 argName, indirectRefKindName(jobj), jobj);
956 } else if (obj->clazz != expectedClass) {
957 LOGW("JNI WARNING: %s arg has wrong type (expected %s, got %s)",
958 argName, expectedClass->descriptor, obj->clazz->descriptor);
968 static u1 checkUtfBytes(const char* bytes, const char** errorKind) {
969 while (*bytes != '\0') {
970 u1 utf8 = *(bytes++);
971 // Switch on the high four bits.
981 // Bit pattern 0xxx. No need for any extra bytes.
989 * Bit pattern 10xx or 1111, which are illegal start bytes.
990 * Note: 1111 is valid for normal UTF-8, but not the
991 * modified UTF-8 used here.
993 *errorKind = "start";
996 // Bit pattern 1110, so there are two additional bytes.
998 if ((utf8 & 0xc0) != 0x80) {
999 *errorKind = "continuation";
1002 // Fall through to take care of the final byte.
1005 // Bit pattern 110x, so there is one additional byte.
1007 if ((utf8 & 0xc0) != 0x80) {
1008 *errorKind = "continuation";
1018 * Returns a human-readable name for the given primitive type.
1020 static const char* primitiveTypeToName(PrimitiveType primType) {
1022 case PRIM_VOID: return "void";
1023 case PRIM_BOOLEAN: return "boolean";
1024 case PRIM_BYTE: return "byte";
1025 case PRIM_SHORT: return "short";
1026 case PRIM_CHAR: return "char";
1027 case PRIM_INT: return "int";
1028 case PRIM_LONG: return "long";
1029 case PRIM_FLOAT: return "float";
1030 case PRIM_DOUBLE: return "double";
1031 case PRIM_NOT: return "Object/array";
1032 default: return "???";
1036 void showLocation() {
1037 const Method* method = dvmGetCurrentJNIMethod();
1038 char* desc = dexProtoCopyMethodDescriptor(&method->prototype);
1039 LOGW(" in %s.%s:%s (%s)", method->clazz->descriptor, method->name, desc, mFunctionName);
1043 // Disallow copy and assignment.
1044 ScopedCheck(const ScopedCheck&);
1045 void operator=(const ScopedCheck&);
1049 * ===========================================================================
1051 * ===========================================================================
1054 #define kGuardLen 512 /* must be multiple of 2 */
1055 #define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
1056 #define kGuardMagic 0xffd5aa96
1058 /* this gets tucked in at the start of the buffer; struct size must be even */
1059 struct GuardedCopy {
1063 const void* originalPtr;
1065 /* find the GuardedCopy given the pointer into the "live" data */
1066 static inline const GuardedCopy* fromData(const void* dataBuf) {
1067 return reinterpret_cast<const GuardedCopy*>(actualBuffer(dataBuf));
1071 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
1072 * filling in the area around it with guard data.
1074 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
1076 static void* create(const void* buf, size_t len, bool modOkay) {
1077 size_t newLen = actualLength(len);
1078 u1* newBuf = debugAlloc(newLen);
1080 /* fill it in with a pattern */
1081 u2* pat = (u2*) newBuf;
1082 for (size_t i = 0; i < newLen / 2; i++) {
1083 *pat++ = kGuardPattern;
1086 /* copy the data in; note "len" could be zero */
1087 memcpy(newBuf + kGuardLen / 2, buf, len);
1089 /* if modification is not expected, grab a checksum */
1092 adler = adler32(0L, Z_NULL, 0);
1093 adler = adler32(adler, (const Bytef*)buf, len);
1094 *(uLong*)newBuf = adler;
1097 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
1098 pExtra->magic = kGuardMagic;
1099 pExtra->adler = adler;
1100 pExtra->originalPtr = buf;
1101 pExtra->originalLen = len;
1103 return newBuf + kGuardLen / 2;
1107 * Free up the guard buffer, scrub it, and return the original pointer.
1109 static void* destroy(void* dataBuf) {
1110 const GuardedCopy* pExtra = GuardedCopy::fromData(dataBuf);
1111 void* originalPtr = (void*) pExtra->originalPtr;
1112 size_t len = pExtra->originalLen;
1113 debugFree(dataBuf, len);
1118 * Verify the guard area and, if "modOkay" is false, that the data itself
1119 * has not been altered.
1121 * The caller has already checked that "dataBuf" is non-NULL.
1123 static bool check(const void* dataBuf, bool modOkay) {
1124 static const u4 kMagicCmp = kGuardMagic;
1125 const u1* fullBuf = actualBuffer(dataBuf);
1126 const GuardedCopy* pExtra = GuardedCopy::fromData(dataBuf);
1129 * Before we do anything with "pExtra", check the magic number. We
1130 * do the check with memcmp rather than "==" in case the pointer is
1131 * unaligned. If it points to completely bogus memory we're going
1132 * to crash, but there's no easy way around that.
1134 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
1136 memcpy(buf, &pExtra->magic, 4);
1137 LOGE("JNI: guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
1138 buf[3], buf[2], buf[1], buf[0], dataBuf); /* assume little endian */
1142 size_t len = pExtra->originalLen;
1144 /* check bottom half of guard; skip over optional checksum storage */
1145 const u2* pat = (u2*) fullBuf;
1146 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
1147 if (pat[i] != kGuardPattern) {
1148 LOGE("JNI: guard pattern(1) disturbed at %p + %d", fullBuf, i*2);
1153 int offset = kGuardLen / 2 + len;
1154 if (offset & 0x01) {
1155 /* odd byte; expected value depends on endian-ness of host */
1156 const u2 patSample = kGuardPattern;
1157 if (fullBuf[offset] != ((const u1*) &patSample)[1]) {
1158 LOGE("JNI: guard pattern disturbed in odd byte after %p (+%d) 0x%02x 0x%02x",
1159 fullBuf, offset, fullBuf[offset], ((const u1*) &patSample)[1]);
1165 /* check top half of guard */
1166 pat = (u2*) (fullBuf + offset);
1167 for (size_t i = 0; i < kGuardLen / 4; i++) {
1168 if (pat[i] != kGuardPattern) {
1169 LOGE("JNI: guard pattern(2) disturbed at %p + %d", fullBuf, offset + i*2);
1175 * If modification is not expected, verify checksum. Strictly speaking
1176 * this is wrong: if we told the client that we made a copy, there's no
1177 * reason they can't alter the buffer.
1180 uLong adler = adler32(0L, Z_NULL, 0);
1181 adler = adler32(adler, (const Bytef*)dataBuf, len);
1182 if (pExtra->adler != adler) {
1183 LOGE("JNI: buffer modified (0x%08lx vs 0x%08lx) at addr %p",
1184 pExtra->adler, adler, dataBuf);
1193 static u1* debugAlloc(size_t len) {
1194 void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1195 if (result == MAP_FAILED) {
1196 LOGE("GuardedCopy::create mmap(%d) failed: %s", len, strerror(errno));
1199 return reinterpret_cast<u1*>(result);
1202 static void debugFree(void* dataBuf, size_t len) {
1203 u1* fullBuf = actualBuffer(dataBuf);
1204 size_t totalByteCount = actualLength(len);
1205 // TODO: we could mprotect instead, and keep the allocation around for a while.
1206 // This would be even more expensive, but it might catch more errors.
1207 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
1208 // LOGW("mprotect(PROT_NONE) failed: %s", strerror(errno));
1210 if (munmap(fullBuf, totalByteCount) != 0) {
1211 LOGW("munmap failed: %s", strerror(errno));
1216 static const u1* actualBuffer(const void* dataBuf) {
1217 return reinterpret_cast<const u1*>(dataBuf) - kGuardLen / 2;
1220 static u1* actualBuffer(void* dataBuf) {
1221 return reinterpret_cast<u1*>(dataBuf) - kGuardLen / 2;
1224 // Underlying length of a user allocation of 'length' bytes.
1225 static size_t actualLength(size_t length) {
1226 return (length + kGuardLen + 1) & ~0x01;
1231 * Return the width, in bytes, of a primitive type.
1233 static int dvmPrimitiveTypeWidth(PrimitiveType primType) {
1235 case PRIM_BOOLEAN: return 1;
1236 case PRIM_BYTE: return 1;
1237 case PRIM_SHORT: return 2;
1238 case PRIM_CHAR: return 2;
1239 case PRIM_INT: return 4;
1240 case PRIM_LONG: return 8;
1241 case PRIM_FLOAT: return 4;
1242 case PRIM_DOUBLE: return 8;
1252 * Create a guarded copy of a primitive array. Modifications to the copied
1253 * data are allowed. Returns a pointer to the copied data.
1255 static void* createGuardedPACopy(JNIEnv* env, const jarray jarr, jboolean* isCopy) {
1256 ScopedJniThreadState ts(env);
1258 ArrayObject* arrObj = (ArrayObject*) dvmDecodeIndirectRef(env, jarr);
1259 PrimitiveType primType = arrObj->clazz->elementClass->primitiveType;
1260 int len = arrObj->length * dvmPrimitiveTypeWidth(primType);
1261 void* result = GuardedCopy::create(arrObj->contents, len, true);
1262 if (isCopy != NULL) {
1269 * Perform the array "release" operation, which may or may not copy data
1270 * back into the VM, and may or may not release the underlying storage.
1272 static void* releaseGuardedPACopy(JNIEnv* env, jarray jarr, void* dataBuf, int mode) {
1273 ScopedJniThreadState ts(env);
1274 ArrayObject* arrObj = (ArrayObject*) dvmDecodeIndirectRef(env, jarr);
1276 if (!GuardedCopy::check(dataBuf, true)) {
1277 LOGE("JNI: failed guarded copy check in releaseGuardedPACopy");
1282 if (mode != JNI_ABORT) {
1283 size_t len = GuardedCopy::fromData(dataBuf)->originalLen;
1284 memcpy(arrObj->contents, dataBuf, len);
1288 if (mode != JNI_COMMIT) {
1289 result = (u1*) GuardedCopy::destroy(dataBuf);
1291 result = (u1*) (void*) GuardedCopy::fromData(dataBuf)->originalPtr;
1294 /* pointer is to the array contents; back up to the array object */
1295 result -= OFFSETOF_MEMBER(ArrayObject, contents);
1301 * ===========================================================================
1303 * ===========================================================================
1306 #define CHECK_JNI_ENTRY(flags, types, args...) \
1307 ScopedCheck sc(env, flags, __FUNCTION__); \
1308 sc.check(true, types, ##args)
1310 #define CHECK_JNI_EXIT(type, exp) ({ \
1311 typeof (exp) _rc = (exp); \
1312 sc.check(false, type, _rc); \
1314 #define CHECK_JNI_EXIT_VOID() \
1315 sc.check(false, "V")
1317 static jint Check_GetVersion(JNIEnv* env) {
1318 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1319 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1322 static jclass Check_DefineClass(JNIEnv* env, const char* name, jobject loader,
1323 const jbyte* buf, jsize bufLen)
1325 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
1326 sc.checkClassName(name);
1327 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1330 static jclass Check_FindClass(JNIEnv* env, const char* name) {
1331 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
1332 sc.checkClassName(name);
1333 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1336 static jclass Check_GetSuperclass(JNIEnv* env, jclass clazz) {
1337 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, clazz);
1338 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, clazz));
1341 static jboolean Check_IsAssignableFrom(JNIEnv* env, jclass clazz1, jclass clazz2) {
1342 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, clazz1, clazz2);
1343 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, clazz1, clazz2));
1346 static jmethodID Check_FromReflectedMethod(JNIEnv* env, jobject method) {
1347 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1348 // TODO: check that 'field' is a java.lang.reflect.Method.
1349 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1352 static jfieldID Check_FromReflectedField(JNIEnv* env, jobject field) {
1353 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1354 // TODO: check that 'field' is a java.lang.reflect.Field.
1355 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1358 static jobject Check_ToReflectedMethod(JNIEnv* env, jclass cls,
1359 jmethodID methodID, jboolean isStatic)
1361 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, methodID, isStatic);
1362 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, methodID, isStatic));
1365 static jobject Check_ToReflectedField(JNIEnv* env, jclass cls,
1366 jfieldID fieldID, jboolean isStatic)
1368 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fieldID, isStatic);
1369 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fieldID, isStatic));
1372 static jint Check_Throw(JNIEnv* env, jthrowable obj) {
1373 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1374 // TODO: check that 'obj' is a java.lang.Throwable.
1375 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1378 static jint Check_ThrowNew(JNIEnv* env, jclass clazz, const char* message) {
1379 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, clazz, message);
1380 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, clazz, message));
1383 static jthrowable Check_ExceptionOccurred(JNIEnv* env) {
1384 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1385 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1388 static void Check_ExceptionDescribe(JNIEnv* env) {
1389 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1390 baseEnv(env)->ExceptionDescribe(env);
1391 CHECK_JNI_EXIT_VOID();
1394 static void Check_ExceptionClear(JNIEnv* env) {
1395 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1396 baseEnv(env)->ExceptionClear(env);
1397 CHECK_JNI_EXIT_VOID();
1400 static void Check_FatalError(JNIEnv* env, const char* msg) {
1401 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg);
1402 baseEnv(env)->FatalError(env, msg);
1403 CHECK_JNI_EXIT_VOID();
1406 static jint Check_PushLocalFrame(JNIEnv* env, jint capacity) {
1407 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1408 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1411 static jobject Check_PopLocalFrame(JNIEnv* env, jobject res) {
1412 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1413 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1416 static jobject Check_NewGlobalRef(JNIEnv* env, jobject obj) {
1417 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1418 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1421 static void Check_DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1422 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1423 if (globalRef != NULL && dvmGetJNIRefType(env, globalRef) != JNIGlobalRefType) {
1424 LOGW("JNI WARNING: DeleteGlobalRef on non-global %p (type=%d)",
1425 globalRef, dvmGetJNIRefType(env, globalRef));
1428 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1429 CHECK_JNI_EXIT_VOID();
1433 static jobject Check_NewLocalRef(JNIEnv* env, jobject ref) {
1434 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1435 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1438 static void Check_DeleteLocalRef(JNIEnv* env, jobject localRef) {
1439 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
1440 if (localRef != NULL && dvmGetJNIRefType(env, localRef) != JNILocalRefType) {
1441 LOGW("JNI WARNING: DeleteLocalRef on non-local %p (type=%d)",
1442 localRef, dvmGetJNIRefType(env, localRef));
1445 baseEnv(env)->DeleteLocalRef(env, localRef);
1446 CHECK_JNI_EXIT_VOID();
1450 static jint Check_EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1451 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1452 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1455 static jboolean Check_IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1456 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1457 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1460 static jobject Check_AllocObject(JNIEnv* env, jclass clazz) {
1461 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, clazz);
1462 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, clazz));
1465 static jobject Check_NewObject(JNIEnv* env, jclass clazz, jmethodID methodID, ...) {
1466 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, methodID);
1468 va_start(args, methodID);
1469 jobject result = baseEnv(env)->NewObjectV(env, clazz, methodID, args);
1471 return CHECK_JNI_EXIT("L", result);
1474 static jobject Check_NewObjectV(JNIEnv* env, jclass clazz, jmethodID methodID, va_list args) {
1475 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, methodID);
1476 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, clazz, methodID, args));
1479 static jobject Check_NewObjectA(JNIEnv* env, jclass clazz, jmethodID methodID, jvalue* args) {
1480 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, methodID);
1481 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, clazz, methodID, args));
1484 static jclass Check_GetObjectClass(JNIEnv* env, jobject obj) {
1485 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1486 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1489 static jboolean Check_IsInstanceOf(JNIEnv* env, jobject obj, jclass clazz) {
1490 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, clazz);
1491 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, clazz));
1494 static jmethodID Check_GetMethodID(JNIEnv* env, jclass clazz, const char* name, const char* sig) {
1495 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, clazz, name, sig);
1496 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, clazz, name, sig));
1499 static jfieldID Check_GetFieldID(JNIEnv* env, jclass clazz, const char* name, const char* sig) {
1500 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, clazz, name, sig);
1501 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, clazz, name, sig));
1504 static jmethodID Check_GetStaticMethodID(JNIEnv* env, jclass clazz,
1505 const char* name, const char* sig)
1507 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, clazz, name, sig);
1508 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, clazz, name, sig));
1511 static jfieldID Check_GetStaticFieldID(JNIEnv* env, jclass clazz,
1512 const char* name, const char* sig)
1514 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, clazz, name, sig);
1515 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, clazz, name, sig));
1518 #define FIELD_ACCESSORS(_ctype, _jname, _ftype, _type) \
1519 static _ctype Check_GetStatic##_jname##Field(JNIEnv* env, jclass clazz, jfieldID fieldID) { \
1520 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, clazz, fieldID); \
1521 sc.checkStaticFieldID(clazz, fieldID); \
1522 sc.checkFieldTypeForGet(fieldID, _type, true); \
1523 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, clazz, fieldID)); \
1525 static _ctype Check_Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fieldID) { \
1526 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fieldID); \
1527 sc.checkInstanceFieldID(obj, fieldID); \
1528 sc.checkFieldTypeForGet(fieldID, _type, false); \
1529 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fieldID)); \
1531 static void Check_SetStatic##_jname##Field(JNIEnv* env, jclass clazz, jfieldID fieldID, _ctype value) { \
1532 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, clazz, fieldID, value); \
1533 sc.checkStaticFieldID(clazz, fieldID); \
1534 /* "value" arg only used when type == ref */ \
1535 sc.checkFieldTypeForSet((jobject)(u4)value, fieldID, _ftype, true); \
1536 baseEnv(env)->SetStatic##_jname##Field(env, clazz, fieldID, value); \
1537 CHECK_JNI_EXIT_VOID(); \
1539 static void Check_Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fieldID, _ctype value) { \
1540 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fieldID, value); \
1541 sc.checkInstanceFieldID(obj, fieldID); \
1542 /* "value" arg only used when type == ref */ \
1543 sc.checkFieldTypeForSet((jobject)(u4) value, fieldID, _ftype, false); \
1544 baseEnv(env)->Set##_jname##Field(env, obj, fieldID, value); \
1545 CHECK_JNI_EXIT_VOID(); \
1548 FIELD_ACCESSORS(jobject, Object, PRIM_NOT, "L");
1549 FIELD_ACCESSORS(jboolean, Boolean, PRIM_BOOLEAN, "Z");
1550 FIELD_ACCESSORS(jbyte, Byte, PRIM_BYTE, "B");
1551 FIELD_ACCESSORS(jchar, Char, PRIM_CHAR, "C");
1552 FIELD_ACCESSORS(jshort, Short, PRIM_SHORT, "S");
1553 FIELD_ACCESSORS(jint, Int, PRIM_INT, "I");
1554 FIELD_ACCESSORS(jlong, Long, PRIM_LONG, "J");
1555 FIELD_ACCESSORS(jfloat, Float, PRIM_FLOAT, "F");
1556 FIELD_ACCESSORS(jdouble, Double, PRIM_DOUBLE, "D");
1558 #define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1560 static _ctype Check_Call##_jname##Method(JNIEnv* env, jobject obj, \
1561 jmethodID methodID, ...) \
1563 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, methodID); /* TODO: args! */ \
1564 sc.checkSig(methodID, _retsig, false); \
1565 sc.checkVirtualMethod(obj, methodID); \
1568 va_start(args, methodID); \
1569 _retasgn baseEnv(env)->Call##_jname##MethodV(env, obj, methodID, args); \
1573 static _ctype Check_Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1574 jmethodID methodID, va_list args) \
1576 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, methodID); /* TODO: args! */ \
1577 sc.checkSig(methodID, _retsig, false); \
1578 sc.checkVirtualMethod(obj, methodID); \
1580 _retasgn baseEnv(env)->Call##_jname##MethodV(env, obj, methodID, args); \
1583 static _ctype Check_Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1584 jmethodID methodID, jvalue* args) \
1586 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, methodID); /* TODO: args! */ \
1587 sc.checkSig(methodID, _retsig, false); \
1588 sc.checkVirtualMethod(obj, methodID); \
1590 _retasgn baseEnv(env)->Call##_jname##MethodA(env, obj, methodID, args); \
1593 /* Non-virtual... */ \
1594 static _ctype Check_CallNonvirtual##_jname##Method(JNIEnv* env, \
1595 jobject obj, jclass clazz, jmethodID methodID, ...) \
1597 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, clazz, methodID); /* TODO: args! */ \
1598 sc.checkSig(methodID, _retsig, false); \
1599 sc.checkVirtualMethod(obj, methodID); \
1602 va_start(args, methodID); \
1603 _retasgn baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, clazz, methodID, args); \
1607 static _ctype Check_CallNonvirtual##_jname##MethodV(JNIEnv* env, \
1608 jobject obj, jclass clazz, jmethodID methodID, va_list args) \
1610 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, clazz, methodID); /* TODO: args! */ \
1611 sc.checkSig(methodID, _retsig, false); \
1612 sc.checkVirtualMethod(obj, methodID); \
1614 _retasgn baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, clazz, methodID, args); \
1617 static _ctype Check_CallNonvirtual##_jname##MethodA(JNIEnv* env, \
1618 jobject obj, jclass clazz, jmethodID methodID, jvalue* args) \
1620 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, clazz, methodID); /* TODO: args! */ \
1621 sc.checkSig(methodID, _retsig, false); \
1622 sc.checkVirtualMethod(obj, methodID); \
1624 _retasgn baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, clazz, methodID, args); \
1628 static _ctype Check_CallStatic##_jname##Method(JNIEnv* env, \
1629 jclass clazz, jmethodID methodID, ...) \
1631 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, methodID); /* TODO: args! */ \
1632 sc.checkSig(methodID, _retsig, true); \
1633 sc.checkStaticMethod(clazz, methodID); \
1636 va_start(args, methodID); \
1637 _retasgn baseEnv(env)->CallStatic##_jname##MethodV(env, clazz, methodID, args); \
1641 static _ctype Check_CallStatic##_jname##MethodV(JNIEnv* env, \
1642 jclass clazz, jmethodID methodID, va_list args) \
1644 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, methodID); /* TODO: args! */ \
1645 sc.checkSig(methodID, _retsig, true); \
1646 sc.checkStaticMethod(clazz, methodID); \
1648 _retasgn baseEnv(env)->CallStatic##_jname##MethodV(env, clazz, methodID, args); \
1651 static _ctype Check_CallStatic##_jname##MethodA(JNIEnv* env, \
1652 jclass clazz, jmethodID methodID, jvalue* args) \
1654 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, clazz, methodID); /* TODO: args! */ \
1655 sc.checkSig(methodID, _retsig, true); \
1656 sc.checkStaticMethod(clazz, methodID); \
1658 _retasgn baseEnv(env)->CallStatic##_jname##MethodA(env, clazz, methodID, args); \
1662 #define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1663 #define VOID_RETURN CHECK_JNI_EXIT_VOID()
1665 CALL(jobject, Object, Object* result, result=(Object*), NON_VOID_RETURN("L", jobject), "L");
1666 CALL(jboolean, Boolean, jboolean result, result=, NON_VOID_RETURN("Z", jboolean), "Z");
1667 CALL(jbyte, Byte, jbyte result, result=, NON_VOID_RETURN("B", jbyte), "B");
1668 CALL(jchar, Char, jchar result, result=, NON_VOID_RETURN("C", jchar), "C");
1669 CALL(jshort, Short, jshort result, result=, NON_VOID_RETURN("S", jshort), "S");
1670 CALL(jint, Int, jint result, result=, NON_VOID_RETURN("I", jint), "I");
1671 CALL(jlong, Long, jlong result, result=, NON_VOID_RETURN("J", jlong), "J");
1672 CALL(jfloat, Float, jfloat result, result=, NON_VOID_RETURN("F", jfloat), "F");
1673 CALL(jdouble, Double, jdouble result, result=, NON_VOID_RETURN("D", jdouble), "D");
1674 CALL(void, Void, , , VOID_RETURN, "V");
1676 static jstring Check_NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1677 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1678 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1681 static jsize Check_GetStringLength(JNIEnv* env, jstring string) {
1682 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1683 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1686 static const jchar* Check_GetStringChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1687 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1688 const jchar* result = baseEnv(env)->GetStringChars(env, string, isCopy);
1689 if (gDvmJni.forceCopy && result != NULL) {
1690 ScopedJniThreadState ts(env);
1691 StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, string);
1692 int byteCount = strObj->length() * 2;
1693 result = (const jchar*) GuardedCopy::create(result, byteCount, false);
1694 if (isCopy != NULL) {
1698 return CHECK_JNI_EXIT("p", result);
1701 static void Check_ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1702 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
1703 sc.checkNonNull(chars);
1704 if (gDvmJni.forceCopy) {
1705 if (!GuardedCopy::check(chars, false)) {
1706 LOGE("JNI: failed guarded copy check in ReleaseStringChars");
1710 chars = (const jchar*) GuardedCopy::destroy((jchar*)chars);
1712 baseEnv(env)->ReleaseStringChars(env, string, chars);
1713 CHECK_JNI_EXIT_VOID();
1716 static jstring Check_NewStringUTF(JNIEnv* env, const char* bytes) {
1717 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
1718 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1721 static jsize Check_GetStringUTFLength(JNIEnv* env, jstring string) {
1722 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1723 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1726 static const char* Check_GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1727 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1728 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
1729 if (gDvmJni.forceCopy && result != NULL) {
1730 result = (const char*) GuardedCopy::create(result, strlen(result) + 1, false);
1731 if (isCopy != NULL) {
1735 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
1738 static void Check_ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
1739 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
1740 if (gDvmJni.forceCopy) {
1741 if (!GuardedCopy::check(utf, false)) {
1742 LOGE("JNI: failed guarded copy check in ReleaseStringUTFChars");
1746 utf = (const char*) GuardedCopy::destroy((char*)utf);
1748 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1749 CHECK_JNI_EXIT_VOID();
1752 static jsize Check_GetArrayLength(JNIEnv* env, jarray array) {
1753 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1754 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1757 static jobjectArray Check_NewObjectArray(JNIEnv* env, jsize length,
1758 jclass elementClass, jobject initialElement)
1760 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1761 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1764 static jobject Check_GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1765 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1766 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1769 static void Check_SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value)
1771 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1772 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1773 CHECK_JNI_EXIT_VOID();
1776 #define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1777 static _artype Check_New##_jname##Array(JNIEnv* env, jsize length) { \
1778 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1779 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1781 NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1782 NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1783 NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1784 NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1785 NEW_PRIMITIVE_ARRAY(jintArray, Int);
1786 NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1787 NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1788 NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1792 * Hack to allow forcecopy to work with jniGetNonMovableArrayElements.
1793 * The code deliberately uses an invalid sequence of operations, so we
1794 * need to pass it through unmodified. Review that code before making
1797 #define kNoCopyMagic 0xd5aab57f
1799 #define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1800 static _ctype* Check_Get##_jname##ArrayElements(JNIEnv* env, \
1801 _ctype##Array array, jboolean* isCopy) \
1803 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
1805 if (gDvmJni.forceCopy && isCopy != NULL) { \
1806 /* capture this before the base call tramples on it */ \
1807 noCopy = *(u4*) isCopy; \
1809 _ctype* result = baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy); \
1810 if (gDvmJni.forceCopy && result != NULL) { \
1811 if (noCopy == kNoCopyMagic) { \
1812 ALOGV("FC: not copying %p %x", array, noCopy); \
1814 result = (_ctype*) createGuardedPACopy(env, array, isCopy); \
1817 return CHECK_JNI_EXIT("p", result); \
1820 #define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1821 static void Check_Release##_jname##ArrayElements(JNIEnv* env, \
1822 _ctype##Array array, _ctype* elems, jint mode) \
1824 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
1825 sc.checkNonNull(elems); \
1826 if (gDvmJni.forceCopy) { \
1827 if ((uintptr_t)elems == kNoCopyMagic) { \
1828 ALOGV("FC: not freeing %p", array); \
1829 elems = NULL; /* base JNI call doesn't currently need */ \
1831 elems = (_ctype*) releaseGuardedPACopy(env, array, elems, mode); \
1834 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1835 CHECK_JNI_EXIT_VOID(); \
1838 #define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1839 static void Check_Get##_jname##ArrayRegion(JNIEnv* env, \
1840 _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1841 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1842 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1843 CHECK_JNI_EXIT_VOID(); \
1846 #define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1847 static void Check_Set##_jname##ArrayRegion(JNIEnv* env, \
1848 _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1849 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1850 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1851 CHECK_JNI_EXIT_VOID(); \
1854 #define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1855 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1856 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1857 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1858 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1860 /* TODO: verify primitive array type matches call type */
1861 PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1862 PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1863 PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1864 PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1865 PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1866 PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1867 PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1868 PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1870 static jint Check_RegisterNatives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods,
1873 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, clazz, methods, nMethods);
1874 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, clazz, methods, nMethods));
1877 static jint Check_UnregisterNatives(JNIEnv* env, jclass clazz) {
1878 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, clazz);
1879 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, clazz));
1882 static jint Check_MonitorEnter(JNIEnv* env, jobject obj) {
1883 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1884 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1887 static jint Check_MonitorExit(JNIEnv* env, jobject obj) {
1888 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
1889 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1892 static jint Check_GetJavaVM(JNIEnv *env, JavaVM **vm) {
1893 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1894 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1897 static void Check_GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1898 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1899 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1900 CHECK_JNI_EXIT_VOID();
1903 static void Check_GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1904 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1905 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1906 CHECK_JNI_EXIT_VOID();
1909 static void* Check_GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1910 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1911 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
1912 if (gDvmJni.forceCopy && result != NULL) {
1913 result = createGuardedPACopy(env, array, isCopy);
1915 return CHECK_JNI_EXIT("p", result);
1918 static void Check_ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode)
1920 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
1921 sc.checkNonNull(carray);
1922 if (gDvmJni.forceCopy) {
1923 carray = releaseGuardedPACopy(env, array, carray, mode);
1925 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1926 CHECK_JNI_EXIT_VOID();
1929 static const jchar* Check_GetStringCritical(JNIEnv* env, jstring string, jboolean* isCopy) {
1930 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, string, isCopy);
1931 const jchar* result = baseEnv(env)->GetStringCritical(env, string, isCopy);
1932 if (gDvmJni.forceCopy && result != NULL) {
1933 ScopedJniThreadState ts(env);
1934 StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, string);
1935 int byteCount = strObj->length() * 2;
1936 result = (const jchar*) GuardedCopy::create(result, byteCount, false);
1937 if (isCopy != NULL) {
1941 return CHECK_JNI_EXIT("p", result);
1944 static void Check_ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1945 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
1946 sc.checkNonNull(carray);
1947 if (gDvmJni.forceCopy) {
1948 if (!GuardedCopy::check(carray, false)) {
1949 LOGE("JNI: failed guarded copy check in ReleaseStringCritical");
1953 carray = (const jchar*) GuardedCopy::destroy((jchar*)carray);
1955 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1956 CHECK_JNI_EXIT_VOID();
1959 static jweak Check_NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1960 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1961 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1964 static void Check_DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
1965 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
1966 baseEnv(env)->DeleteWeakGlobalRef(env, obj);
1967 CHECK_JNI_EXIT_VOID();
1970 static jboolean Check_ExceptionCheck(JNIEnv* env) {
1971 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1972 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1975 static jobjectRefType Check_GetObjectRefType(JNIEnv* env, jobject obj) {
1976 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1977 // TODO: proper decoding of jobjectRefType!
1978 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1981 static jobject Check_NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1982 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1983 if (address == NULL || capacity < 0) {
1984 LOGW("JNI WARNING: invalid values for address (%p) or capacity (%ld)",
1985 address, (long) capacity);
1989 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1992 static void* Check_GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1993 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1994 // TODO: check that 'buf' is a java.nio.Buffer.
1995 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1998 static jlong Check_GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1999 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
2000 // TODO: check that 'buf' is a java.nio.Buffer.
2001 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
2006 * ===========================================================================
2007 * JNI invocation functions
2008 * ===========================================================================
2011 static jint Check_DestroyJavaVM(JavaVM* vm) {
2012 ScopedCheck sc(false, __FUNCTION__);
2013 sc.check(true, "v", vm);
2014 return CHECK_JNI_EXIT("I", baseVm(vm)->DestroyJavaVM(vm));
2017 static jint Check_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2018 ScopedCheck sc(false, __FUNCTION__);
2019 sc.check(true, "vpp", vm, p_env, thr_args);
2020 return CHECK_JNI_EXIT("I", baseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
2023 static jint Check_AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2024 ScopedCheck sc(false, __FUNCTION__);
2025 sc.check(true, "vpp", vm, p_env, thr_args);
2026 return CHECK_JNI_EXIT("I", baseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
2029 static jint Check_DetachCurrentThread(JavaVM* vm) {
2030 ScopedCheck sc(true, __FUNCTION__);
2031 sc.check(true, "v", vm);
2032 return CHECK_JNI_EXIT("I", baseVm(vm)->DetachCurrentThread(vm));
2035 static jint Check_GetEnv(JavaVM* vm, void** env, jint version) {
2036 ScopedCheck sc(true, __FUNCTION__);
2037 sc.check(true, "v", vm);
2038 return CHECK_JNI_EXIT("I", baseVm(vm)->GetEnv(vm, env, version));
2043 * ===========================================================================
2045 * ===========================================================================
2048 static const struct JNINativeInterface gCheckNativeInterface = {
2059 Check_FromReflectedMethod,
2060 Check_FromReflectedField,
2061 Check_ToReflectedMethod,
2063 Check_GetSuperclass,
2064 Check_IsAssignableFrom,
2066 Check_ToReflectedField,
2070 Check_ExceptionOccurred,
2071 Check_ExceptionDescribe,
2072 Check_ExceptionClear,
2075 Check_PushLocalFrame,
2076 Check_PopLocalFrame,
2079 Check_DeleteGlobalRef,
2080 Check_DeleteLocalRef,
2083 Check_EnsureLocalCapacity,
2090 Check_GetObjectClass,
2095 Check_CallObjectMethod,
2096 Check_CallObjectMethodV,
2097 Check_CallObjectMethodA,
2098 Check_CallBooleanMethod,
2099 Check_CallBooleanMethodV,
2100 Check_CallBooleanMethodA,
2101 Check_CallByteMethod,
2102 Check_CallByteMethodV,
2103 Check_CallByteMethodA,
2104 Check_CallCharMethod,
2105 Check_CallCharMethodV,
2106 Check_CallCharMethodA,
2107 Check_CallShortMethod,
2108 Check_CallShortMethodV,
2109 Check_CallShortMethodA,
2110 Check_CallIntMethod,
2111 Check_CallIntMethodV,
2112 Check_CallIntMethodA,
2113 Check_CallLongMethod,
2114 Check_CallLongMethodV,
2115 Check_CallLongMethodA,
2116 Check_CallFloatMethod,
2117 Check_CallFloatMethodV,
2118 Check_CallFloatMethodA,
2119 Check_CallDoubleMethod,
2120 Check_CallDoubleMethodV,
2121 Check_CallDoubleMethodA,
2122 Check_CallVoidMethod,
2123 Check_CallVoidMethodV,
2124 Check_CallVoidMethodA,
2126 Check_CallNonvirtualObjectMethod,
2127 Check_CallNonvirtualObjectMethodV,
2128 Check_CallNonvirtualObjectMethodA,
2129 Check_CallNonvirtualBooleanMethod,
2130 Check_CallNonvirtualBooleanMethodV,
2131 Check_CallNonvirtualBooleanMethodA,
2132 Check_CallNonvirtualByteMethod,
2133 Check_CallNonvirtualByteMethodV,
2134 Check_CallNonvirtualByteMethodA,
2135 Check_CallNonvirtualCharMethod,
2136 Check_CallNonvirtualCharMethodV,
2137 Check_CallNonvirtualCharMethodA,
2138 Check_CallNonvirtualShortMethod,
2139 Check_CallNonvirtualShortMethodV,
2140 Check_CallNonvirtualShortMethodA,
2141 Check_CallNonvirtualIntMethod,
2142 Check_CallNonvirtualIntMethodV,
2143 Check_CallNonvirtualIntMethodA,
2144 Check_CallNonvirtualLongMethod,
2145 Check_CallNonvirtualLongMethodV,
2146 Check_CallNonvirtualLongMethodA,
2147 Check_CallNonvirtualFloatMethod,
2148 Check_CallNonvirtualFloatMethodV,
2149 Check_CallNonvirtualFloatMethodA,
2150 Check_CallNonvirtualDoubleMethod,
2151 Check_CallNonvirtualDoubleMethodV,
2152 Check_CallNonvirtualDoubleMethodA,
2153 Check_CallNonvirtualVoidMethod,
2154 Check_CallNonvirtualVoidMethodV,
2155 Check_CallNonvirtualVoidMethodA,
2159 Check_GetObjectField,
2160 Check_GetBooleanField,
2163 Check_GetShortField,
2166 Check_GetFloatField,
2167 Check_GetDoubleField,
2168 Check_SetObjectField,
2169 Check_SetBooleanField,
2172 Check_SetShortField,
2175 Check_SetFloatField,
2176 Check_SetDoubleField,
2178 Check_GetStaticMethodID,
2180 Check_CallStaticObjectMethod,
2181 Check_CallStaticObjectMethodV,
2182 Check_CallStaticObjectMethodA,
2183 Check_CallStaticBooleanMethod,
2184 Check_CallStaticBooleanMethodV,
2185 Check_CallStaticBooleanMethodA,
2186 Check_CallStaticByteMethod,
2187 Check_CallStaticByteMethodV,
2188 Check_CallStaticByteMethodA,
2189 Check_CallStaticCharMethod,
2190 Check_CallStaticCharMethodV,
2191 Check_CallStaticCharMethodA,
2192 Check_CallStaticShortMethod,
2193 Check_CallStaticShortMethodV,
2194 Check_CallStaticShortMethodA,
2195 Check_CallStaticIntMethod,
2196 Check_CallStaticIntMethodV,
2197 Check_CallStaticIntMethodA,
2198 Check_CallStaticLongMethod,
2199 Check_CallStaticLongMethodV,
2200 Check_CallStaticLongMethodA,
2201 Check_CallStaticFloatMethod,
2202 Check_CallStaticFloatMethodV,
2203 Check_CallStaticFloatMethodA,
2204 Check_CallStaticDoubleMethod,
2205 Check_CallStaticDoubleMethodV,
2206 Check_CallStaticDoubleMethodA,
2207 Check_CallStaticVoidMethod,
2208 Check_CallStaticVoidMethodV,
2209 Check_CallStaticVoidMethodA,
2211 Check_GetStaticFieldID,
2213 Check_GetStaticObjectField,
2214 Check_GetStaticBooleanField,
2215 Check_GetStaticByteField,
2216 Check_GetStaticCharField,
2217 Check_GetStaticShortField,
2218 Check_GetStaticIntField,
2219 Check_GetStaticLongField,
2220 Check_GetStaticFloatField,
2221 Check_GetStaticDoubleField,
2223 Check_SetStaticObjectField,
2224 Check_SetStaticBooleanField,
2225 Check_SetStaticByteField,
2226 Check_SetStaticCharField,
2227 Check_SetStaticShortField,
2228 Check_SetStaticIntField,
2229 Check_SetStaticLongField,
2230 Check_SetStaticFloatField,
2231 Check_SetStaticDoubleField,
2235 Check_GetStringLength,
2236 Check_GetStringChars,
2237 Check_ReleaseStringChars,
2240 Check_GetStringUTFLength,
2241 Check_GetStringUTFChars,
2242 Check_ReleaseStringUTFChars,
2244 Check_GetArrayLength,
2245 Check_NewObjectArray,
2246 Check_GetObjectArrayElement,
2247 Check_SetObjectArrayElement,
2249 Check_NewBooleanArray,
2252 Check_NewShortArray,
2255 Check_NewFloatArray,
2256 Check_NewDoubleArray,
2258 Check_GetBooleanArrayElements,
2259 Check_GetByteArrayElements,
2260 Check_GetCharArrayElements,
2261 Check_GetShortArrayElements,
2262 Check_GetIntArrayElements,
2263 Check_GetLongArrayElements,
2264 Check_GetFloatArrayElements,
2265 Check_GetDoubleArrayElements,
2267 Check_ReleaseBooleanArrayElements,
2268 Check_ReleaseByteArrayElements,
2269 Check_ReleaseCharArrayElements,
2270 Check_ReleaseShortArrayElements,
2271 Check_ReleaseIntArrayElements,
2272 Check_ReleaseLongArrayElements,
2273 Check_ReleaseFloatArrayElements,
2274 Check_ReleaseDoubleArrayElements,
2276 Check_GetBooleanArrayRegion,
2277 Check_GetByteArrayRegion,
2278 Check_GetCharArrayRegion,
2279 Check_GetShortArrayRegion,
2280 Check_GetIntArrayRegion,
2281 Check_GetLongArrayRegion,
2282 Check_GetFloatArrayRegion,
2283 Check_GetDoubleArrayRegion,
2284 Check_SetBooleanArrayRegion,
2285 Check_SetByteArrayRegion,
2286 Check_SetCharArrayRegion,
2287 Check_SetShortArrayRegion,
2288 Check_SetIntArrayRegion,
2289 Check_SetLongArrayRegion,
2290 Check_SetFloatArrayRegion,
2291 Check_SetDoubleArrayRegion,
2293 Check_RegisterNatives,
2294 Check_UnregisterNatives,
2301 Check_GetStringRegion,
2302 Check_GetStringUTFRegion,
2304 Check_GetPrimitiveArrayCritical,
2305 Check_ReleasePrimitiveArrayCritical,
2307 Check_GetStringCritical,
2308 Check_ReleaseStringCritical,
2310 Check_NewWeakGlobalRef,
2311 Check_DeleteWeakGlobalRef,
2313 Check_ExceptionCheck,
2315 Check_NewDirectByteBuffer,
2316 Check_GetDirectBufferAddress,
2317 Check_GetDirectBufferCapacity,
2319 Check_GetObjectRefType
2322 static const struct JNIInvokeInterface gCheckInvokeInterface = {
2327 Check_DestroyJavaVM,
2328 Check_AttachCurrentThread,
2329 Check_DetachCurrentThread,
2333 Check_AttachCurrentThreadAsDaemon,
2337 * Replace the normal table with the checked table.
2339 void dvmUseCheckedJniEnv(JNIEnvExt* pEnv) {
2340 assert(pEnv->funcTable != &gCheckNativeInterface);
2341 pEnv->baseFuncTable = pEnv->funcTable;
2342 pEnv->funcTable = &gCheckNativeInterface;
2346 * Replace the normal table with the checked table.
2348 void dvmUseCheckedJniVm(JavaVMExt* pVm) {
2349 assert(pVm->funcTable != &gCheckInvokeInterface);
2350 pVm->baseFuncTable = pVm->funcTable;
2351 pVm->funcTable = &gCheckInvokeInterface;