Commits

Anonymous committed 8c82bcc Merge

Merge "Check JNI versions handed to JNI and JII functions."

Comments (0)

Files changed (3)

     dvmClearReferenceTable(&gDvm.jniPinRefTable);
 }
 
+bool dvmIsBadJniVersion(int version) {
+  // We don't support JNI_VERSION_1_1. These are the only other valid versions.
+  return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
+}
+
 /*
  * Find the JNIEnv associated with the current thread.
  *
         argsCopy.name = NULL;
         argsCopy.group = (jobject) dvmGetMainThreadGroup();
     } else {
+        if (dvmIsBadJniVersion(args->version)) {
+            ALOGE("Bad JNI version passed to %s: %d",
+                  (isDaemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread"),
+                  args->version);
+            return JNI_EVERSION;
+        }
+
         argsCopy.version = args->version;
         argsCopy.name = args->name;
         if (args->group != NULL) {
 static jint GetEnv(JavaVM* vm, void** env, jint version) {
     Thread* self = dvmThreadSelf();
 
-    if (version < JNI_VERSION_1_1 || version > JNI_VERSION_1_6) {
+    if (dvmIsBadJniVersion(version)) {
+        ALOGE("Bad JNI version passed to GetEnv: %d", version);
         return JNI_EVERSION;
     }
 
  */
 jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
     const JavaVMInitArgs* args = (JavaVMInitArgs*) vm_args;
-    if (args->version < JNI_VERSION_1_2) {
+    if (dvmIsBadJniVersion(args->version)) {
+        ALOGE("Bad JNI version passed to CreateJavaVM: %d", args->version);
         return JNI_EVERSION;
     }
 
 bool dvmJniStartup(void);
 void dvmJniShutdown(void);
 
+bool dvmIsBadJniVersion(int version);
+
 /*
  * Our data structures for JNIEnv and JavaVM.
  *
             dvmChangeStatus(self, oldStatus);
             self->classLoaderOverride = prevOverride;
 
-            if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 &&
-                version != JNI_VERSION_1_6)
-            {
-                ALOGW("JNI_OnLoad returned bad version (%d) in %s %p",
-                    version, pathName, classLoader);
+            if (version == JNI_ERR) {
+                *detail = strdup(StringPrintf("JNI_ERR returned from JNI_OnLoad in \"%s\"",
+                                              pathName).c_str());
+            } else if (dvmIsBadJniVersion(version)) {
+                *detail = strdup(StringPrintf("Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
+                                              pathName, version).c_str());
                 /*
                  * It's unwise to call dlclose() here, but we can mark it
                  * as bad and ensure that future load attempts will fail.
                  * unregister them, but that doesn't seem worthwhile.
                  */
                 result = false;
-            } else {
-                if (gDvm.verboseJni) {
-                    ALOGI("[Returned from JNI_OnLoad for \"%s\"]", pathName);
-                }
+            }
+            if (gDvm.verboseJni) {
+                ALOGI("[Returned %s from JNI_OnLoad for \"%s\"]",
+                      (result ? "successfully" : "failure"), pathName);
             }
         }