annotate test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetJNIFunctionTable/getjniftab001/getjniftab001.cpp @ 53610:9cfc8b0c45fd

8214149: Move out assignments when not using NSK*VERIFY macros Summary: Move out the assignments from ifs Reviewed-by: sspitsyn, cjplummer
author jcbeyler
date Wed, 21 Nov 2018 10:46:45 -0800
parents 543a3fb81c4c
children c78a17d24618
rev   line source
iignatyev@50844 1 /*
iignatyev@50844 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
iignatyev@50844 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
iignatyev@50844 4 *
iignatyev@50844 5 * This code is free software; you can redistribute it and/or modify it
iignatyev@50844 6 * under the terms of the GNU General Public License version 2 only, as
iignatyev@50844 7 * published by the Free Software Foundation.
iignatyev@50844 8 *
iignatyev@50844 9 * This code is distributed in the hope that it will be useful, but WITHOUT
iignatyev@50844 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
iignatyev@50844 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
iignatyev@50844 12 * version 2 for more details (a copy is included in the LICENSE file that
iignatyev@50844 13 * accompanied this code).
iignatyev@50844 14 *
iignatyev@50844 15 * You should have received a copy of the GNU General Public License version
iignatyev@50844 16 * 2 along with this work; if not, write to the Free Software Foundation,
iignatyev@50844 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
iignatyev@50844 18 *
iignatyev@50844 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
iignatyev@50844 20 * or visit www.oracle.com if you need additional information or have any
iignatyev@50844 21 * questions.
iignatyev@50844 22 */
iignatyev@50844 23
iignatyev@50844 24 #include <stdio.h>
iignatyev@50844 25 #include <stdarg.h>
iignatyev@50844 26 #include <stdlib.h>
iignatyev@50844 27 #include <string.h>
iignatyev@50844 28
iignatyev@50844 29 #include <jvmti.h>
iignatyev@50844 30 #include "agent_common.h"
iignatyev@50844 31
iignatyev@50844 32 #include "JVMTITools.h"
iignatyev@50844 33 #include "native_thread.h"
iignatyev@50844 34
iignatyev@50844 35 extern "C" {
iignatyev@50844 36
iignatyev@50844 37 #define PASSED 0
iignatyev@50844 38 #define STATUS_FAILED 2
iignatyev@50844 39
iignatyev@50844 40 static JavaVM *vm;
iignatyev@50844 41 static jvmtiEnv *jvmti = NULL;
iignatyev@50844 42
iignatyev@50844 43 static int verbose = 0;
iignatyev@50844 44
iignatyev@50844 45 static jint result = PASSED;
iignatyev@50844 46
iignatyev@50844 47 /* the original JNI function table */
iignatyev@50844 48 static jniNativeInterface *orig_jni_functions = NULL;
iignatyev@50844 49
iignatyev@50844 50 /* the redirected JNI function table */
iignatyev@50844 51 static jniNativeInterface *redir_jni_functions = NULL;
iignatyev@50844 52
iignatyev@50844 53 /* number of the redirected JNI function calls */
iignatyev@50844 54 static volatile int redir_calls = 0;
iignatyev@50844 55
iignatyev@50844 56 /** redirected JNI functions **/
iignatyev@50844 57 jint JNICALL MyGetVersion(JNIEnv *env) {
iignatyev@50844 58 redir_calls++;
iignatyev@50844 59
iignatyev@50844 60 if (verbose)
iignatyev@50844 61 printf("\nMyGetVersion: the function called successfully: number of calls=%d\n",
iignatyev@50844 62 redir_calls);
iignatyev@50844 63
iignatyev@52294 64 return orig_jni_functions->GetVersion(env);
iignatyev@50844 65 }
iignatyev@50844 66 /*****************************/
iignatyev@50844 67
iignatyev@50844 68 static void doRedirect(JNIEnv *env) {
iignatyev@50844 69 jvmtiError err;
iignatyev@50844 70
iignatyev@50844 71 if (verbose)
iignatyev@50844 72 printf("\ndoRedirect: obtaining the JNI function table ...\n");
jcbeyler@53610 73 err = jvmti->GetJNIFunctionTable(&orig_jni_functions);
jcbeyler@53610 74 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 75 result = STATUS_FAILED;
iignatyev@50844 76 printf("(%s,%d): TEST FAILED: failed to get original JNI function table: %s\n",
iignatyev@50844 77 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52440 78 env->FatalError("failed to get original JNI function table");
iignatyev@50844 79 }
jcbeyler@53610 80 err = jvmti->GetJNIFunctionTable(&redir_jni_functions);
jcbeyler@53610 81 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 82 result = STATUS_FAILED;
iignatyev@50844 83 printf("(%s,%d): TEST FAILED: failed to get redirected JNI function table: %s\n",
iignatyev@50844 84 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52440 85 env->FatalError("failed to get redirected JNI function table");
iignatyev@50844 86 }
iignatyev@50844 87 if (verbose)
iignatyev@50844 88 printf("doRedirect: the JNI function table obtained successfully\n");
iignatyev@50844 89
iignatyev@50844 90 if (verbose)
iignatyev@50844 91 printf("\ndoRedirect: overwriting the function GetVersion() ...\n");
iignatyev@50844 92 redir_jni_functions->GetVersion = MyGetVersion;
iignatyev@50844 93
jcbeyler@53610 94 err = jvmti->SetJNIFunctionTable(redir_jni_functions);
jcbeyler@53610 95 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 96 result = STATUS_FAILED;
iignatyev@50844 97 printf("(%s,%d): TEST FAILED: failed to get new JNI function table: %s\n",
iignatyev@50844 98 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52440 99 env->FatalError("failed to get new JNI function table");
iignatyev@50844 100 }
iignatyev@50844 101
iignatyev@50844 102 if (verbose)
iignatyev@50844 103 printf("\ndoRedirect: the function is overwritten successfully\n");
iignatyev@50844 104 }
iignatyev@50844 105
iignatyev@50844 106 static void doRestore(JNIEnv *env) {
iignatyev@50844 107 jvmtiError err;
iignatyev@50844 108
iignatyev@50844 109 if (verbose)
iignatyev@50844 110 printf("\ndoRestore: restoring the original JNI function table ...\n");
jcbeyler@53610 111 err = jvmti->SetJNIFunctionTable(orig_jni_functions);
jcbeyler@53610 112 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 113 result = STATUS_FAILED;
iignatyev@50844 114 printf("(%s,%d): TEST FAILED: failed to restore original JNI function table: %s\n",
iignatyev@50844 115 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52440 116 env->FatalError("failed to restore original JNI function table");
iignatyev@50844 117 }
iignatyev@50844 118 if (verbose)
iignatyev@50844 119 printf("doRestore: the original JNI function table is restored successfully\n");
iignatyev@50844 120 }
iignatyev@50844 121
iignatyev@50844 122 static void checkRedir(JNIEnv *env, int exCalls) {
iignatyev@50844 123 jniNativeInterface *tested_jni_functions = NULL;
iignatyev@50844 124 jvmtiError err;
iignatyev@50844 125 jint res;
iignatyev@50844 126
iignatyev@50844 127 redir_calls = 0;
iignatyev@50844 128
jcbeyler@53610 129 err = jvmti->GetJNIFunctionTable(&tested_jni_functions);
jcbeyler@53610 130 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 131 result = STATUS_FAILED;
iignatyev@50844 132 printf("(%s,%d): TEST FAILED: failed to get modified JNI function table: %s\n",
iignatyev@50844 133 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52440 134 env->FatalError("failed to get modified JNI function table");
iignatyev@50844 135 }
iignatyev@50844 136 if (verbose)
iignatyev@50844 137 printf("checkRedir: the JNI function table obtained successfully\n\tcalling GetVersion() ...\n");
iignatyev@52294 138 res = tested_jni_functions->GetVersion(env);
iignatyev@50844 139
iignatyev@50844 140 if (redir_calls == exCalls) {
iignatyev@50844 141 if (verbose)
iignatyev@50844 142 printf("\nCHECK PASSED: the %s JNI function table is returned by GetJNIFunctionTable():\n\t%d interception of GetVersion() calls as expected\n",
iignatyev@50844 143 (exCalls==0)?"original":"modified",
iignatyev@50844 144 redir_calls);
iignatyev@50844 145 }
iignatyev@50844 146 else {
iignatyev@50844 147 result = STATUS_FAILED;
iignatyev@50844 148 printf("\nTEST FAILED: the %s JNI function table is returned by GetJNIFunctionTable() instead of the %s one:\n\t%d interception of GetVersion() calls instead of %d as expected\n",
iignatyev@50844 149 (exCalls==0)?"modified":"original",
iignatyev@50844 150 (exCalls==0)?"original":"modified",
iignatyev@50844 151 redir_calls, exCalls);
iignatyev@50844 152 }
iignatyev@50844 153 }
iignatyev@50844 154
iignatyev@50844 155 JNIEXPORT jint JNICALL
iignatyev@50844 156 Java_nsk_jvmti_GetJNIFunctionTable_getjniftab001_check(JNIEnv *env, jobject obj) {
iignatyev@50844 157 int exitCode = PASSED;
iignatyev@50844 158
iignatyev@50844 159 if (jvmti == NULL) {
iignatyev@50844 160 printf("(%s,%d): TEST FAILURE: JVMTI client was not properly loaded\n",
iignatyev@50844 161 __FILE__, __LINE__);
iignatyev@50844 162 return STATUS_FAILED;
iignatyev@50844 163 }
iignatyev@50844 164
iignatyev@50844 165 /* 1: check the assertion with the modified function table */
iignatyev@50844 166 doRedirect(env);
iignatyev@50844 167 if (verbose)
iignatyev@50844 168 printf("\na) Checking the assertion with the modified function table ...\n");
iignatyev@50844 169 checkRedir(env, 1);
iignatyev@50844 170
iignatyev@50844 171 /* 2: check the assertion with the original function table */
iignatyev@50844 172 doRestore(env);
iignatyev@50844 173 if (verbose)
iignatyev@50844 174 printf("\nb) Checking the assertion with the original function table ...\n");
iignatyev@50844 175 checkRedir(env, 0);
iignatyev@50844 176
iignatyev@50844 177 return result;
iignatyev@50844 178 }
iignatyev@50844 179
iignatyev@50844 180 #ifdef STATIC_BUILD
iignatyev@50844 181 JNIEXPORT jint JNICALL Agent_OnLoad_getjniftab001(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 182 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 183 }
iignatyev@50844 184 JNIEXPORT jint JNICALL Agent_OnAttach_getjniftab001(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 185 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 186 }
iignatyev@50844 187 JNIEXPORT jint JNI_OnLoad_getjniftab001(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 188 return JNI_VERSION_1_8;
iignatyev@50844 189 }
iignatyev@50844 190 #endif
iignatyev@50844 191 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 192 jint res;
iignatyev@50844 193
iignatyev@50844 194 if (options != NULL && strcmp(options, "-verbose") == 0)
iignatyev@50844 195 verbose = 1;
iignatyev@50844 196
iignatyev@50844 197 if (verbose)
iignatyev@50844 198 printf("verbose mode on\n");
iignatyev@50844 199
jcbeyler@52440 200 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
iignatyev@50844 201 if (res != JNI_OK || jvmti == NULL) {
iignatyev@50844 202 printf("(%s,%d): Failed to call GetEnv\n", __FILE__, __LINE__);
iignatyev@50844 203 return JNI_ERR;
iignatyev@50844 204 }
iignatyev@50844 205
iignatyev@50844 206 return JNI_OK;
iignatyev@50844 207 }
iignatyev@50844 208
iignatyev@50844 209 }