changeset 55967:fe9f33555993

8222935: Fix ExceptionCheckingJniEnv system Summary: Added missing tests and fixed the code Reviewed-by: cjplummer, sspitsyn, amenkov
author jcbeyler
date Wed, 24 Apr 2019 19:51:38 -0700
parents 3a34f5173c36
children 9afd2868c18b
files test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001.java test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001/TestDescription.java test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001/exceptionjni001.cpp test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001/libexceptionjni001.cpp test/hotspot/jtreg/vmTestbase/nsk/share/jni/ExceptionCheckingJniEnv.cpp
diffstat 5 files changed, 304 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001.java	Wed Apr 24 19:51:38 2019 -0700
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Google 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.
+ */
+
+package nsk.share.ExceptionCheckingJniEnv;
+
+import java.io.PrintStream;
+
+public class exceptionjni001 {
+    static {
+        try {
+            System.loadLibrary("exceptionjni001");
+        } catch (UnsatisfiedLinkError ule) {
+            System.err.println("Could not load exceptionjni001 library");
+            System.err.println("java.library.path:"
+                + System.getProperty("java.library.path"));
+            throw ule;
+        }
+    }
+
+    // A field the JNI code can try to get.
+    int anInteger;
+
+    // Check the exception checking code.
+    native static boolean check();
+
+    public static void main(String args[]) {
+        if (!check()) {
+          throw new RuntimeException("Problem with ExceptionCheckingJniEnv");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001/TestDescription.java	Wed Apr 24 19:51:38 2019 -0700
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Google 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
+ *
+ * @summary test the ExceptionCheckingJniEnv system.
+ * DESCRIPTION
+ *     The test exercise the ExceptionCheckingJniEnv system.
+ *
+ * @library /vmTestbase
+ *          /test/lib
+ * @run driver jdk.test.lib.FileInstaller . .
+ * @run main/othervm/native -agentlib:exceptionjni001 nsk.share.ExceptionCheckingJniEnv.exceptionjni001
+ */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001/exceptionjni001.cpp	Wed Apr 24 19:51:38 2019 -0700
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Google 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.
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jvmti.h"
+#include "ExceptionCheckingJniEnv.hpp"
+
+// A few static global variables required due to the callback nature of JNI
+// methods.
+static bool is_error_called;
+static const char* const null_return_expected_message_start =
+    "JNI method GetFieldID : Return is NULL from exceptionjni001.cpp : ";
+static const char* const null_file_expected_message_start =
+    "JNI method GetFieldID : Return is NULL from Unknown File : ";
+
+// Used by the ErrorCheckerMessage and the tests to determine test success.
+static long expected_line_number;
+static bool error_message_ok;
+static const char* expected_message_start;
+
+static bool CheckMessage(JNIEnv* env, const char* message, const char* expected_message,
+                         long expected_line) {
+  if (strstr(message, expected_message) != message) {
+    fprintf(stderr, "Message does not start as expected:\n\t%s\n\t%s\n",
+            message, expected_message);
+    return false;
+  }
+
+  size_t len = strlen(expected_message);
+
+  char* end_ptr = NULL;
+  long actual_line = strtol(message + len, &end_ptr, 0);
+
+  if (end_ptr == NULL || *end_ptr != '\0') {
+    fprintf(stderr, "end_ptr == NULL or *end_ptr terminating from %s\n", message);
+    return false;
+  }
+
+  if (actual_line != expected_line) {
+    fprintf(stderr, "Actual line does not match expected:\n");
+    fprintf(stderr, "\tActual: %ld\n\tExpected: %ld\n\tfrom: %s (%s)\n",
+            actual_line, expected_line, message, message + len);
+    return false;
+  }
+
+  // Clear the exception if everything lines up.
+  env->ExceptionClear();
+  return true;
+}
+
+static void ErrorCheckerMessage(JNIEnv* env, const char* error_message) {
+  is_error_called = true;
+  error_message_ok = CheckMessage(env, error_message, expected_message_start,
+                                  expected_line_number);
+}
+
+static bool checkSuccess(JNIEnv* env, jclass cls) {
+  ExceptionCheckingJniEnvPtr jni(env, ErrorCheckerMessage);
+  is_error_called = false;
+
+  jni->GetFieldID(cls, "anInteger", "I", TRACE_JNI_CALL);
+  return !is_error_called;
+}
+
+static bool checkFailureMessageReturnNull(JNIEnv* env, jclass cls) {
+  ExceptionCheckingJniEnvPtr jni(env, ErrorCheckerMessage);
+
+  expected_message_start = null_return_expected_message_start;
+  expected_line_number = __LINE__ + 1;
+  jni->GetFieldID(cls, "whatever", "does not matter", TRACE_JNI_CALL);
+
+  return is_error_called && error_message_ok;
+}
+
+static bool checkFailureMessageEmptyFile(JNIEnv* env, jclass cls) {
+  ExceptionCheckingJniEnvPtr jni(env, ErrorCheckerMessage);
+
+  expected_message_start = null_file_expected_message_start;
+  expected_line_number = __LINE__ + 1;
+  jni->GetFieldID(cls, "whatever", "does not matter", __LINE__, NULL);
+
+  return is_error_called && error_message_ok;
+}
+
+static bool checkFailureMessageNilLine(JNIEnv* env, jclass cls) {
+  ExceptionCheckingJniEnvPtr jni(env, ErrorCheckerMessage);
+
+  expected_message_start = null_return_expected_message_start;
+  expected_line_number = 0;
+  jni->GetFieldID(cls, "whatever", "does not matter", 0, __FILE__);
+
+  return is_error_called && error_message_ok;
+}
+
+static bool checkFailureMessageNegativeLine(JNIEnv* env, jclass cls) {
+  ExceptionCheckingJniEnvPtr jni(env, ErrorCheckerMessage);
+
+  expected_message_start = null_return_expected_message_start;
+  expected_line_number = -1;
+  jni->GetFieldID(cls, "whatever", "does not matter", -1, __FILE__);
+
+  return is_error_called && error_message_ok;
+}
+
+static bool checkFailureMessageMinLine(JNIEnv* env, jclass cls) {
+  ExceptionCheckingJniEnvPtr jni(env, ErrorCheckerMessage);
+
+  expected_message_start = null_return_expected_message_start;
+  expected_line_number = INT32_MIN;
+  jni->GetFieldID(cls, "whatever", "does not matter", INT32_MIN, __FILE__);
+
+  return is_error_called && error_message_ok;
+}
+
+static bool checkFailureMessageMaxLine(JNIEnv* env, jclass cls) {
+  ExceptionCheckingJniEnvPtr jni(env, ErrorCheckerMessage);
+
+  expected_message_start = null_return_expected_message_start;
+  expected_line_number = INT32_MAX;
+  jni->GetFieldID(cls, "whatever", "does not matter", INT32_MAX, __FILE__);
+
+  return is_error_called && error_message_ok;
+}
+
+static bool CheckExceptionJni(JNIEnv* env, jclass cls) {
+  typedef bool (*TestExceptionJniWrapper)(JNIEnv* env, jclass cls);
+
+  TestExceptionJniWrapper tests[] = {
+    checkSuccess,
+    checkFailureMessageReturnNull,
+    checkFailureMessageEmptyFile,
+    checkFailureMessageNilLine,
+    checkFailureMessageNegativeLine,
+    checkFailureMessageMinLine,
+    checkFailureMessageMaxLine,
+  };
+
+  size_t max_tests = sizeof(tests) / sizeof(tests[0]);
+  for (size_t i = 0; i < max_tests; i++) {
+    is_error_called = false;
+    error_message_ok = false;
+    if (!tests[i](env, cls)) {
+      return false;
+    }
+  }
+  return true;
+}
+
+extern "C" {
+
+jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
+  return JNI_OK;
+}
+
+JNIEXPORT jboolean JNICALL
+Java_nsk_share_ExceptionCheckingJniEnv_exceptionjni001_check(JNIEnv *env, jclass cls) {
+  return CheckExceptionJni(env, cls);
+}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001/libexceptionjni001.cpp	Wed Apr 24 19:51:38 2019 -0700
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Google 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.
+ */
+
+#include "nsk_tools.cpp"
+#include "agent_common.cpp"
+#include "exceptionjni001.cpp"
+#include "ExceptionCheckingJniEnv.cpp"
--- a/test/hotspot/jtreg/vmTestbase/nsk/share/jni/ExceptionCheckingJniEnv.cpp	Thu Apr 25 02:23:27 2019 +0200
+++ b/test/hotspot/jtreg/vmTestbase/nsk/share/jni/ExceptionCheckingJniEnv.cpp	Wed Apr 24 19:51:38 2019 -0700
@@ -180,7 +180,8 @@
       strcat(full_message, strs[i]);
     }
 
-    DecimalToAscii(full_message, _line);
+    // Add line number to end of the string.
+    DecimalToAscii(full_message + strlen(full_message), _line);
 
     if (strlen(full_message) >= len) {
       _env->GetJNIEnv()->FatalError("Final length of message is not what was expected");