changeset 8474:3cd78165832b

8072588: JVM crashes in JNI if toString is declared as an interface method Summary: Check for a valid itable index instead of checking if the holder is an interface Reviewed-by: dsimms, dholmes
author aeriksso
date Tue, 02 Jun 2015 10:41:18 +0200
parents 0d3c3c622f96
children a42faf811ff3 918d124e7e39
files make/test/JtregNative.gmk src/share/vm/prims/jni.cpp test/runtime/jni/ToStringInInterfaceTest/ImplementationOfWithToString.java test/runtime/jni/ToStringInInterfaceTest/InterfaceWithToString.java test/runtime/jni/ToStringInInterfaceTest/ToStringTest.java test/runtime/jni/ToStringInInterfaceTest/libToStringTest.c
diffstat 6 files changed, 224 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/make/test/JtregNative.gmk	Mon Jun 01 14:02:59 2015 -0700
+++ b/make/test/JtregNative.gmk	Tue Jun 02 10:41:18 2015 +0200
@@ -44,6 +44,7 @@
     $(HOTSPOT_TOPDIR)/test/native_sanity \
     $(HOTSPOT_TOPDIR)/test/runtime/jni/8025979 \
     $(HOTSPOT_TOPDIR)/test/runtime/jni/8033445 \
+    $(HOTSPOT_TOPDIR)/test/runtime/jni/ToStringInInterfaceTest \
     #
 
 BUILD_HOTSPOT_JTREG_OUTPUT_DIR := $(BUILD_OUTPUT)/support/test/hotspot/jtreg/native
--- a/src/share/vm/prims/jni.cpp	Mon Jun 01 14:02:59 2015 -0700
+++ b/src/share/vm/prims/jni.cpp	Tue Jun 02 10:41:18 2015 +0200
@@ -1126,39 +1126,32 @@
     Method* m = Method::resolve_jmethod_id(method_id);
     number_of_parameters = m->size_of_parameters();
     Klass* holder = m->method_holder();
-    if (!(holder)->is_interface()) {
+    if (call_type != JNI_VIRTUAL) {
+        selected_method = m;
+    } else if (!m->has_itable_index()) {
       // non-interface call -- for that little speed boost, don't handlize
       debug_only(No_Safepoint_Verifier nosafepoint;)
-      if (call_type == JNI_VIRTUAL) {
-        // jni_GetMethodID makes sure class is linked and initialized
-        // so m should have a valid vtable index.
-        assert(!m->has_itable_index(), "");
-        int vtbl_index = m->vtable_index();
-        if (vtbl_index != Method::nonvirtual_vtable_index) {
-          Klass* k = h_recv->klass();
-          // k might be an arrayKlassOop but all vtables start at
-          // the same place. The cast is to avoid virtual call and assertion.
-          InstanceKlass *ik = (InstanceKlass*)k;
-          selected_method = ik->method_at_vtable(vtbl_index);
-        } else {
-          // final method
-          selected_method = m;
-        }
+      // jni_GetMethodID makes sure class is linked and initialized
+      // so m should have a valid vtable index.
+      assert(m->valid_vtable_index(), "no valid vtable index");
+      int vtbl_index = m->vtable_index();
+      if (vtbl_index != Method::nonvirtual_vtable_index) {
+        Klass* k = h_recv->klass();
+        // k might be an arrayKlassOop but all vtables start at
+        // the same place. The cast is to avoid virtual call and assertion.
+        InstanceKlass *ik = (InstanceKlass*)k;
+        selected_method = ik->method_at_vtable(vtbl_index);
       } else {
-        // JNI_NONVIRTUAL call
+        // final method
         selected_method = m;
       }
     } else {
       // interface call
       KlassHandle h_holder(THREAD, holder);
 
-      if (call_type == JNI_VIRTUAL) {
-        int itbl_index = m->itable_index();
-        Klass* k = h_recv->klass();
-        selected_method = InstanceKlass::cast(k)->method_at_itable(h_holder(), itbl_index, CHECK);
-      } else {
-        selected_method = m;
-      }
+      int itbl_index = m->itable_index();
+      Klass* k = h_recv->klass();
+      selected_method = InstanceKlass::cast(k)->method_at_itable(h_holder(), itbl_index, CHECK);
     }
   }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/runtime/jni/ToStringInInterfaceTest/ImplementationOfWithToString.java	Tue Jun 02 10:41:18 2015 +0200
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * Implementation of InterfaceWithToString.
+ */
+public class ImplementationOfWithToString implements InterfaceWithToString {
+
+  /**
+   * @see InterfaceWithToString#someMethod()
+   * {@inheritDoc}
+   */
+  @Override
+  public void someMethod() {
+    // May do something here.
+  }
+
+  /**
+   * @see java.lang.Object#toString()
+   * {@inheritDoc}
+   */
+  @Override
+  public String toString() {
+    return "toString() from " + getClass().getName();
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/runtime/jni/ToStringInInterfaceTest/InterfaceWithToString.java	Tue Jun 02 10:41:18 2015 +0200
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * Interface with toString declared.
+ */
+public interface InterfaceWithToString {
+
+  void someMethod();
+
+  /**
+   * Same as Object.toString().
+   *
+   * @return some custom string.
+   */
+  String toString();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/runtime/jni/ToStringInInterfaceTest/ToStringTest.java	Tue Jun 02 10:41:18 2015 +0200
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8072588
+ * @build InterfaceWithToString
+ * @build ImplementationOfWithToString
+ * @run main/native ToStringTest
+ */
+public final class ToStringTest {
+
+    static {
+        System.loadLibrary("ToStringTest");
+    }
+
+    native static void nTest();
+
+    public static void main(String[] args) throws Exception {
+        nTest();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/runtime/jni/ToStringInInterfaceTest/libToStringTest.c	Tue Jun 02 10:41:18 2015 +0200
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * Native test for ToStringInInterfaceTest.
+ */
+
+#include "jni.h"
+
+#define checkException(env) if ((*env)->ExceptionCheck(env)) { return; }
+
+jstring callStringMethod(JNIEnv* env, jobject jobj, jmethodID id, ...)
+{
+    jstring value;
+
+    va_list ap;
+    va_start(ap, id);
+    value = (jstring)(*env)->CallObjectMethodV(env, jobj, id, ap);
+    va_end(ap);
+    return value;
+}
+
+JNIEXPORT void JNICALL Java_ToStringTest_nTest(JNIEnv* env, jclass jclazz)
+{
+    jclass classOfInterfaceWithToString;
+    jclass classOfImplementationOfWithToString;
+    jmethodID constructorOfImplementationOfWithToString;
+    jobject instanceOfImplementationOfWithToString;
+    jmethodID toStringOfInterfaceWithToString;
+    jmethodID toStringOfImplementationOfWithToString;
+    jstring jstr;
+    const char *chars;
+
+    classOfInterfaceWithToString = (*env)->FindClass(env, "InterfaceWithToString");
+    checkException(env);
+    classOfImplementationOfWithToString = (*env)->FindClass(env, "ImplementationOfWithToString");
+    checkException(env);
+
+    constructorOfImplementationOfWithToString = (*env)->GetMethodID(env, classOfImplementationOfWithToString, "<init>", "()V");
+    checkException(env);
+
+    instanceOfImplementationOfWithToString = (*env)->NewObject(env, classOfImplementationOfWithToString, constructorOfImplementationOfWithToString);
+    checkException(env);
+
+    toStringOfInterfaceWithToString = (*env)->GetMethodID(env, classOfInterfaceWithToString, "toString", "()Ljava/lang/String;");
+    checkException(env);
+
+    toStringOfImplementationOfWithToString = (*env)->GetMethodID(env, classOfImplementationOfWithToString, "toString", "()Ljava/lang/String;");
+    checkException(env);
+
+    jstr = callStringMethod(env, instanceOfImplementationOfWithToString, toStringOfImplementationOfWithToString);
+    checkException(env);
+
+    chars = (*env)->GetStringUTFChars(env, jstr, NULL);
+    (*env)->ReleaseStringUTFChars(env, jstr, chars);
+
+    jstr = callStringMethod(env, instanceOfImplementationOfWithToString, toStringOfInterfaceWithToString);
+    checkException(env);
+
+    chars = (*env)->GetStringUTFChars(env, jstr, NULL);
+    (*env)->ReleaseStringUTFChars(env, jstr, chars);
+}