Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/ExceptionCheckingJniEnv/exceptionjni001/exceptionjni001.cpp
41162 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018, 2019, Google and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324#include <stdint.h>25#include <stdio.h>26#include <stdlib.h>27#include <string.h>28#include "jvmti.h"29#include "ExceptionCheckingJniEnv.hpp"3031// A few static global variables required due to the callback nature of JNI32// methods.33static bool is_error_called;34static const char* const null_return_expected_message_start =35"JNI method GetFieldID : Return is NULL from exceptionjni001.cpp : ";36static const char* const null_file_expected_message_start =37"JNI method GetFieldID : Return is NULL from Unknown File : ";3839// Used by the ErrorCheckerMessage and the tests to determine test success.40static long expected_line_number;41static bool error_message_ok;42static const char* expected_message_start;4344static bool CheckMessage(JNIEnv* env, const char* message, const char* expected_message,45long expected_line) {46if (strstr(message, expected_message) != message) {47fprintf(stderr, "Message does not start as expected:\n\t%s\n\t%s\n",48message, expected_message);49return false;50}5152size_t len = strlen(expected_message);5354char* end_ptr = NULL;55long actual_line = strtol(message + len, &end_ptr, 0);5657if (end_ptr == NULL || *end_ptr != '\0') {58fprintf(stderr, "end_ptr == NULL or *end_ptr terminating from %s\n", message);59return false;60}6162if (actual_line != expected_line) {63fprintf(stderr, "Actual line does not match expected:\n");64fprintf(stderr, "\tActual: %ld\n\tExpected: %ld\n\tfrom: %s (%s)\n",65actual_line, expected_line, message, message + len);66return false;67}6869// Clear the exception if everything lines up.70env->ExceptionClear();71return true;72}7374static void ErrorCheckerMessage(JNIEnv* env, const char* error_message) {75is_error_called = true;76error_message_ok = CheckMessage(env, error_message, expected_message_start,77expected_line_number);78}7980static bool checkSuccess(JNIEnv* env, jclass cls) {81ExceptionCheckingJniEnvPtr ec_jni(env, ErrorCheckerMessage);82is_error_called = false;8384ec_jni->GetFieldID(cls, "anInteger", "I", TRACE_JNI_CALL);85return !is_error_called;86}8788static bool checkFailureMessageReturnNull(JNIEnv* env, jclass cls) {89ExceptionCheckingJniEnvPtr ec_jni(env, ErrorCheckerMessage);9091expected_message_start = null_return_expected_message_start;92expected_line_number = __LINE__ + 1;93ec_jni->GetFieldID(cls, "whatever", "does not matter", TRACE_JNI_CALL);9495return is_error_called && error_message_ok;96}9798static bool checkFailureMessageEmptyFile(JNIEnv* env, jclass cls) {99ExceptionCheckingJniEnvPtr ec_jni(env, ErrorCheckerMessage);100101expected_message_start = null_file_expected_message_start;102expected_line_number = __LINE__ + 1;103ec_jni->GetFieldID(cls, "whatever", "does not matter", __LINE__, NULL);104105return is_error_called && error_message_ok;106}107108static bool checkFailureMessageNilLine(JNIEnv* env, jclass cls) {109ExceptionCheckingJniEnvPtr ec_jni(env, ErrorCheckerMessage);110111expected_message_start = null_return_expected_message_start;112expected_line_number = 0;113ec_jni->GetFieldID(cls, "whatever", "does not matter", 0, __FILE__);114115return is_error_called && error_message_ok;116}117118static bool checkFailureMessageNegativeLine(JNIEnv* env, jclass cls) {119ExceptionCheckingJniEnvPtr ec_jni(env, ErrorCheckerMessage);120121expected_message_start = null_return_expected_message_start;122expected_line_number = -1;123ec_jni->GetFieldID(cls, "whatever", "does not matter", -1, __FILE__);124125return is_error_called && error_message_ok;126}127128static bool checkFailureMessageMinLine(JNIEnv* env, jclass cls) {129ExceptionCheckingJniEnvPtr ec_jni(env, ErrorCheckerMessage);130131expected_message_start = null_return_expected_message_start;132expected_line_number = INT32_MIN;133ec_jni->GetFieldID(cls, "whatever", "does not matter", INT32_MIN, __FILE__);134135return is_error_called && error_message_ok;136}137138static bool checkFailureMessageMaxLine(JNIEnv* env, jclass cls) {139ExceptionCheckingJniEnvPtr ec_jni(env, ErrorCheckerMessage);140141expected_message_start = null_return_expected_message_start;142expected_line_number = INT32_MAX;143ec_jni->GetFieldID(cls, "whatever", "does not matter", INT32_MAX, __FILE__);144145return is_error_called && error_message_ok;146}147148static bool CheckExceptionJni(JNIEnv* env, jclass cls) {149typedef bool (*TestExceptionJniWrapper)(JNIEnv* env, jclass cls);150151TestExceptionJniWrapper tests[] = {152checkSuccess,153checkFailureMessageReturnNull,154checkFailureMessageEmptyFile,155checkFailureMessageNilLine,156checkFailureMessageNegativeLine,157checkFailureMessageMinLine,158checkFailureMessageMaxLine,159};160161size_t max_tests = sizeof(tests) / sizeof(tests[0]);162for (size_t i = 0; i < max_tests; i++) {163is_error_called = false;164error_message_ok = false;165if (!tests[i](env, cls)) {166return false;167}168}169return true;170}171172extern "C" {173174jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {175return JNI_OK;176}177178JNIEXPORT jboolean JNICALL179Java_nsk_share_ExceptionCheckingJniEnv_exceptionjni001_check(JNIEnv *env, jclass cls) {180return CheckExceptionJni(env, cls);181}182183}184185186