8167408: Invalid critical JNI function lookup
authorjcm
Mon, 06 Nov 2017 00:30:36 -0800
changeset 47782 d099408b386e
parent 47780 895da9d2087b
child 47783 a11d9dbcd6c0
8167408: Invalid critical JNI function lookup Summary: made correction to arg_size calculation in NativeLookup::lookup_critical_entry Reviewed-by: dholmes, dlong, mdoerr, vlivanov
make/test/JtregNativeHotspot.gmk
src/hotspot/share/prims/nativeLookup.cpp
test/hotspot/jtreg/compiler/runtime/criticalnatives/lookup/LookUp.java
test/hotspot/jtreg/compiler/runtime/criticalnatives/lookup/libCNLookUp.c
--- a/make/test/JtregNativeHotspot.gmk	Mon Nov 06 00:29:08 2017 +0300
+++ b/make/test/JtregNativeHotspot.gmk	Mon Nov 06 00:30:36 2017 -0800
@@ -63,6 +63,7 @@
     $(TOPDIR)/test/hotspot/jtreg/runtime/RedefineTests \
     $(TOPDIR)/test/hotspot/jtreg/compiler/floatingpoint/ \
     $(TOPDIR)/test/hotspot/jtreg/compiler/calls \
+    $(TOPDIR)/test/hotspot/jtreg/compiler/runtime/criticalnatives/lookup \
     $(TOPDIR)/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorInfo \
     $(TOPDIR)/test/hotspot/jtreg/serviceability/jvmti/GetNamedModule \
     $(TOPDIR)/test/hotspot/jtreg/serviceability/jvmti/IsModifiableModule \
--- a/src/hotspot/share/prims/nativeLookup.cpp	Mon Nov 06 00:29:08 2017 +0300
+++ b/src/hotspot/share/prims/nativeLookup.cpp	Mon Nov 06 00:30:36 2017 -0800
@@ -293,10 +293,12 @@
   char* critical_name = critical_jni_name(method);
 
   // Compute argument size
-  int args_size = 1                             // JNIEnv
-                + (method->is_static() ? 1 : 0) // class for static methods
-                + method->size_of_parameters(); // actual parameters
-
+  int args_size = method->size_of_parameters();
+  for (SignatureStream ss(signature); !ss.at_return_type(); ss.next()) {
+    if (ss.is_array()) {
+      args_size += T_INT_size; // array length parameter
+    }
+  }
 
   // 1) Try JNI short style
   entry = lookup_critical_style(method, critical_name, "",        args_size, true);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/compiler/runtime/criticalnatives/lookup/LookUp.java	Mon Nov 06 00:30:36 2017 -0800
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2017, 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 8167408
+ * @run main/othervm/native -Xcomp compiler.runtime.criticalnatives.lookup.LookUp
+ */
+package compiler.runtime.criticalnatives.lookup;
+public class LookUp {
+    static {
+        System.loadLibrary("CNLookUp");
+    }
+    static native void m1(byte a1, long a2, char a3, int a4,  float a5, double a6, byte[] result);
+    static native void m2(int a1, int[] a2, long a3, long[] a4, float a5,float[] a6, double a7, double[] a8, byte result[]);
+    public static void main(String args[]) throws Exception {
+        test();
+    }
+    private static void test() throws Exception {
+        int[] l1 = { 1111, 2222, 3333 };
+        long[] l2 = { 4444L, 5555L, 6666L };
+        float[] l3 = { 7777.0F, 8888.0F, 9999.0F };
+        double[] l4 = { 4545.0D, 5656.0D, 6767.0D };
+        byte[] result = { -1 };
+        m1((byte)0xA, 4444444455555555L, 'A', 12345678, 343434.0F, 6666666677777777.0D, result);
+        check(result[0]);
+        result[0] = -1;
+        m2(12345678, l1, 4444444455555555L, l2, 343434.0F, l3, 6666666677777777.0D, l4, result);
+        check(result[0]);
+    }
+    private static void check(byte result) throws Exception {
+        if (result != 2) {
+            if (result == 1) {
+              throw new Exception("critical native arguments mismatch");
+            }
+            throw new Exception("critical native lookup failed");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/compiler/runtime/criticalnatives/lookup/libCNLookUp.c	Mon Nov 06 00:30:36 2017 -0800
@@ -0,0 +1,35 @@
+#include "jni.h"
+JNIEXPORT void JNICALL JavaCritical_compiler_runtime_criticalnatives_lookup_LookUp_m1
+  (jbyte a1, jlong a2, jchar a3, jint a4, jfloat a5, jdouble a6, jint result_length, jbyte* result) {
+  jint l1 = (jint) a5;
+  jlong l2 = (jlong) a6;
+
+  if (a1 != 0xA || a2 != 4444444455555555LL || a3 != 0x41 || a4 != 12345678 || l1 != 343434 || l2 != 6666666677777777LL ||
+      result_length != 1 || result[0] != -1) {
+    result[0] = 1;
+  } else {
+    result[0] = 2;
+  }
+}
+
+JNIEXPORT void JNICALL JavaCritical_compiler_runtime_criticalnatives_lookup_LookUp_m2
+  (jint a1, jint a2_length, jint* a2, jlong a3, jint a4_length, jlong* a4, jfloat a5, jint a6_length, jfloat* a6, jdouble a7,
+   jint a8_length, jdouble* a8, jint result_length, jbyte* result) {
+  jint l1 = (jint) a5;
+  jlong l2 = (jlong) a7;
+
+  if (a1 != 12345678 || a2_length != 3 || a2[0] != 1111 || a3 != 4444444455555555LL || a4_length != 3 || a4[0] != 4444 ||
+      l1 != 343434 ||  a6_length != 3 ||  7777 != (jint)a6[0] || l2 != 6666666677777777LL || a8_length != 3 || 4545 != (jlong)a8[0] ||
+      result_length != 1 || result[0] != -1) {
+    result[0] = 1;
+  } else {
+    result[0] = 2;
+  }
+}
+
+JNIEXPORT void JNICALL Java_compiler_runtime_criticalnatives_lookup_LookUp_m1
+  (JNIEnv * env, jclass jclazz, jbyte a3, jlong a4, jchar a5, jint a6, jfloat a7, jdouble a8, jbyteArray result) {}
+
+JNIEXPORT void JNICALL Java_compiler_runtime_criticalnatives_lookup_LookUp_m2
+  (JNIEnv * env, jclass jclazz, jint a3, jintArray a4, jlong a5, jlongArray a6, jfloat a7, jfloatArray a8, jdouble a9, jdoubleArray a10, jbyteArray result) {}
+