annotate test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI03/ji03t003/ji03t003.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 1ebe04845112
children 837f1b8442be
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
iignatyev@50844 34 extern "C" {
iignatyev@50844 35
iignatyev@50844 36 #define PASSED 0
iignatyev@50844 37 #define STATUS_FAILED 2
iignatyev@50844 38
iignatyev@50844 39 static jvmtiEnv *jvmti = NULL;
iignatyev@50844 40 static jint result = PASSED;
iignatyev@50844 41 static int verbose = 0;
iignatyev@50844 42
iignatyev@50844 43 static const char *javaField = "exc";
iignatyev@50844 44 static const char *excClassSig =
iignatyev@50844 45 "Lnsk/jvmti/scenarios/jni_interception/JI03/ji03t003Exc;";
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 int throw_calls = 0;
iignatyev@50844 55 int thrownew_calls = 0;
iignatyev@50844 56 int excoccur_calls = 0;
iignatyev@50844 57
iignatyev@50844 58 /** redirected JNI functions **/
iignatyev@50844 59 jint JNICALL MyThrow(JNIEnv *env, jthrowable thrw) {
iignatyev@50844 60 jint res;
iignatyev@50844 61
iignatyev@50844 62 throw_calls++;
iignatyev@50844 63 if (verbose)
iignatyev@50844 64 printf("\nMyThrow: the function called successfully: number of calls=%d\n",
iignatyev@50844 65 throw_calls);
iignatyev@50844 66
iignatyev@52294 67 res = orig_jni_functions->Throw(env, thrw);
iignatyev@50844 68
iignatyev@50844 69 if (verbose)
iignatyev@50844 70 printf("MyThrow: returning res=%d\n",res);
iignatyev@50844 71 return res;
iignatyev@50844 72 }
iignatyev@50844 73
iignatyev@50844 74 jint JNICALL MyThrowNew(JNIEnv *env, jclass cls, const char *msg) {
iignatyev@50844 75 jint res;
iignatyev@50844 76
iignatyev@50844 77 thrownew_calls++;
iignatyev@50844 78 if (verbose)
iignatyev@50844 79 printf("\nMyThrowNew: the function called successfully: number of calls=%d\n",
iignatyev@50844 80 thrownew_calls);
iignatyev@50844 81
iignatyev@52294 82 res = orig_jni_functions->ThrowNew(env, cls, msg);
iignatyev@50844 83
iignatyev@50844 84 if (verbose)
iignatyev@50844 85 printf("MyThrowNew: returning res=%d\n",res);
iignatyev@50844 86 return res;
iignatyev@50844 87 }
iignatyev@50844 88
iignatyev@50844 89 jthrowable JNICALL MyExceptionOccurred(JNIEnv *env) {
iignatyev@50844 90 if (isThreadExpected(jvmti, NULL)) {
iignatyev@50844 91 excoccur_calls++;
iignatyev@50844 92 if (verbose)
iignatyev@50844 93 printf("\nMyExceptionOccurred: the function called successfully: number of calls=%d\n",
iignatyev@50844 94 excoccur_calls);
iignatyev@50844 95 }
iignatyev@50844 96
iignatyev@52294 97 return orig_jni_functions->ExceptionOccurred(env);
iignatyev@50844 98 }
iignatyev@50844 99 /*****************************/
iignatyev@50844 100
iignatyev@50844 101 void doRedirect(JNIEnv *env) {
iignatyev@50844 102 jvmtiError err;
iignatyev@50844 103
iignatyev@50844 104 if (verbose)
iignatyev@50844 105 printf("\ndoRedirect: obtaining the JNI function table ...\n");
jcbeyler@53610 106 err = jvmti->GetJNIFunctionTable(&orig_jni_functions);
jcbeyler@53610 107 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 108 result = STATUS_FAILED;
iignatyev@50844 109 printf("(%s,%d): TEST FAILED: failed to get original JNI function table: %s\n",
iignatyev@50844 110 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52515 111 env->FatalError("failed to get original JNI function table");
iignatyev@50844 112 }
jcbeyler@53610 113 err = jvmti->GetJNIFunctionTable(&redir_jni_functions);
jcbeyler@53610 114 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 115 result = STATUS_FAILED;
iignatyev@50844 116 printf("(%s,%d): TEST FAILED: failed to get redirected JNI function table: %s\n",
iignatyev@50844 117 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52515 118 env->FatalError("failed to get redirected JNI function table");
iignatyev@50844 119 }
iignatyev@50844 120 if (verbose)
iignatyev@50844 121 printf("doRedirect: the JNI function table obtained successfully\n");
iignatyev@50844 122
iignatyev@50844 123 if (verbose)
iignatyev@50844 124 printf("\ndoRedirect: overwriting the functions Throw,ThrowNew,ExceptionOccured ...\n");
iignatyev@50844 125 redir_jni_functions->Throw = MyThrow;
iignatyev@50844 126 redir_jni_functions->ThrowNew = MyThrowNew;
iignatyev@50844 127 redir_jni_functions->ExceptionOccurred = MyExceptionOccurred;
iignatyev@50844 128
jcbeyler@53610 129 err = jvmti->SetJNIFunctionTable(redir_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 set new JNI function table: %s\n",
iignatyev@50844 133 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52515 134 env->FatalError("failed to set new JNI function table");
iignatyev@50844 135 }
iignatyev@50844 136
iignatyev@50844 137 if (verbose)
iignatyev@50844 138 printf("\ndoRedirect: the functions are overwritten successfully\n");
iignatyev@50844 139 }
iignatyev@50844 140
iignatyev@50844 141 void doRestore(JNIEnv *env) {
iignatyev@50844 142 jvmtiError err;
iignatyev@50844 143
iignatyev@50844 144 if (verbose)
iignatyev@50844 145 printf("\ndoRestore: restoring the original JNI function table ...\n");
jcbeyler@53610 146 err = jvmti->SetJNIFunctionTable(orig_jni_functions);
jcbeyler@53610 147 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 148 result = STATUS_FAILED;
iignatyev@50844 149 printf("(%s,%d): TEST FAILED: failed to restore original JNI function table: %s\n",
iignatyev@50844 150 __FILE__, __LINE__, TranslateError(err));
jcbeyler@52515 151 env->FatalError("failed to restore original JNI function table");
iignatyev@50844 152 }
iignatyev@50844 153 if (verbose)
iignatyev@50844 154 printf("doRestore: the original JNI function table is restored successfully\n");
iignatyev@50844 155 }
iignatyev@50844 156
iignatyev@50844 157 void doExc(JNIEnv *env, jthrowable thrw, jclass thrCls, const char *msg) {
iignatyev@50844 158 jint res;
iignatyev@50844 159
jcbeyler@53610 160 res = env->ThrowNew(thrCls, msg);
jcbeyler@53610 161 if (res != 0) {
iignatyev@50844 162 result = STATUS_FAILED;
iignatyev@50844 163 printf("(%s,%d): TEST FAILED: failed to throw new exception\n",
iignatyev@50844 164 __FILE__, __LINE__);
jcbeyler@52515 165 env->FatalError("failed to failed to throw new exception");
iignatyev@50844 166 }
iignatyev@50844 167
jcbeyler@52515 168 if (env->ExceptionOccurred()) {
iignatyev@50844 169 if (verbose)
iignatyev@50844 170 printf("\nCHECK PASSED: exception %s thrown by ThrowNew()\n\tis detected by ExceptionOccurred() successfully\n",
iignatyev@50844 171 msg);
jcbeyler@52515 172 env->ExceptionClear();
iignatyev@50844 173 }
iignatyev@50844 174 else {
iignatyev@50844 175 result = STATUS_FAILED;
iignatyev@50844 176 printf("(%s,%d): TEST FAILED: exception %s thrown by ThrowNew()\n\tis not detected by ExceptionOccurred()\n",
iignatyev@50844 177 __FILE__, __LINE__, msg);
iignatyev@50844 178 }
iignatyev@50844 179
jcbeyler@53610 180 res = env->Throw(thrw);
jcbeyler@53610 181 if (res != 0) {
iignatyev@50844 182 result = STATUS_FAILED;
iignatyev@50844 183 printf("(%s,%d): TEST FAILED: failed to throw exception\n",
iignatyev@50844 184 __FILE__, __LINE__);
jcbeyler@52515 185 env->FatalError("failed to failed to throw new exception");
iignatyev@50844 186 }
iignatyev@50844 187
jcbeyler@52515 188 if (env->ExceptionOccurred()) {
iignatyev@50844 189 if (verbose)
iignatyev@50844 190 printf("(%s,%d): CHECK PASSED: exception %s thrown by Throw()\n\tis detected by ExceptionOccurred() successfully\n",
iignatyev@50844 191 __FILE__, __LINE__, msg);
jcbeyler@52515 192 env->ExceptionClear();
iignatyev@50844 193 }
iignatyev@50844 194 else {
iignatyev@50844 195 result = STATUS_FAILED;
iignatyev@50844 196 printf("(%s,%d): TEST FAILED: exception %s thrown by Throw()\n\tis not detected by ExceptionOccurred()\n",
iignatyev@50844 197 __FILE__, __LINE__, msg);
iignatyev@50844 198 }
iignatyev@50844 199 }
iignatyev@50844 200
iignatyev@50844 201 void checkCall(int step, int exThrCalls, int exThrNewCalls, int exExcOccCalls) {
iignatyev@50844 202 if (throw_calls == exThrCalls) {
iignatyev@50844 203 if (verbose)
iignatyev@50844 204 printf("\nCHECK PASSED: the %s JNI function Throw() has been %s:\n\t%d intercepted call(s) as expected\n",
iignatyev@50844 205 (step==1)?"tested":"original",
iignatyev@50844 206 (step==1)?"redirected":"restored",
iignatyev@50844 207 throw_calls);
iignatyev@50844 208 }
iignatyev@50844 209 else {
iignatyev@50844 210 result = STATUS_FAILED;
iignatyev@50844 211 printf("\nTEST FAILED: the %s JNI function Throw() has not been %s:\n\t%d intercepted call(s) instead of %d as expected\n",
iignatyev@50844 212 (step==1)?"tested":"original",
iignatyev@50844 213 (step==1)?"redirected":"restored",
iignatyev@50844 214 throw_calls, exThrCalls);
iignatyev@50844 215 }
iignatyev@50844 216 throw_calls = 0; /* zeroing an interception counter */
iignatyev@50844 217
iignatyev@50844 218 if (thrownew_calls == exThrNewCalls) {
iignatyev@50844 219 if (verbose)
iignatyev@50844 220 printf("\nCHECK PASSED: the %s JNI function ThrowNew() has been %s:\n\t%d intercepted call(s) as expected\n",
iignatyev@50844 221 (step==1)?"tested":"original",
iignatyev@50844 222 (step==1)?"redirected":"restored",
iignatyev@50844 223 thrownew_calls);
iignatyev@50844 224 }
iignatyev@50844 225 else {
iignatyev@50844 226 result = STATUS_FAILED;
iignatyev@50844 227 printf("\nTEST FAILED: the %s JNI function ThrowNew() has not been %s:\n\t%d intercepted call(s) instead of %d as expected\n",
iignatyev@50844 228 (step==1)?"tested":"original",
iignatyev@50844 229 (step==1)?"redirected":"restored",
iignatyev@50844 230 thrownew_calls, exThrNewCalls);
iignatyev@50844 231 }
iignatyev@50844 232 thrownew_calls = 0; /* zeroing an interception counter */
iignatyev@50844 233
iignatyev@50844 234 if (excoccur_calls == exExcOccCalls) {
iignatyev@50844 235 if (verbose)
iignatyev@50844 236 printf("\nCHECK PASSED: the %s JNI function ExceptionOccurred() has been %s:\n\t%d intercepted call(s) as expected\n",
iignatyev@50844 237 (step==1)?"tested":"original",
iignatyev@50844 238 (step==1)?"redirected":"restored",
iignatyev@50844 239 excoccur_calls);
iignatyev@50844 240 }
iignatyev@50844 241 else {
iignatyev@50844 242 result = STATUS_FAILED;
iignatyev@50844 243 printf("\nTEST FAILED: the %s JNI function ExceptionOccurred() has not been %s:\n\t%d intercepted call(s) instead of %d as expected\n",
iignatyev@50844 244 (step==1)?"tested":"original",
iignatyev@50844 245 (step==1)?"redirected":"restored",
iignatyev@50844 246 excoccur_calls, exExcOccCalls);
iignatyev@50844 247 }
iignatyev@50844 248 excoccur_calls = 0; /* zeroing an interception counter */
iignatyev@50844 249 }
iignatyev@50844 250
iignatyev@50844 251 JNIEXPORT jint JNICALL
iignatyev@50844 252 Java_nsk_jvmti_scenarios_jni_1interception_JI03_ji03t003_check(JNIEnv *env, jobject obj) {
iignatyev@50844 253 jfieldID fid;
iignatyev@52294 254 jthrowable thrwObj;
iignatyev@50844 255 jclass thrw;
iignatyev@50844 256 jclass objCls;
iignatyev@50844 257
iignatyev@50844 258 if (jvmti == NULL) {
iignatyev@50844 259 printf("(%s,%d): TEST FAILURE: JVMTI client was not properly loaded\n",
iignatyev@50844 260 __FILE__, __LINE__);
iignatyev@50844 261 return STATUS_FAILED;
iignatyev@50844 262 }
iignatyev@50844 263
jcbeyler@52515 264 objCls = env->GetObjectClass(obj);
iignatyev@50844 265
iignatyev@50844 266 if (verbose)
iignatyev@50844 267 printf("\ncheck: obtaining field ID for \"name=%s signature=%s\"...\n",
iignatyev@50844 268 javaField, excClassSig);
jcbeyler@53610 269 fid = env->GetFieldID(objCls, javaField, excClassSig);
jcbeyler@53610 270 if (fid == 0) {
iignatyev@50844 271 result = STATUS_FAILED;
iignatyev@50844 272 printf("(%s,%d): TEST FAILED: failed to get ID for the field \"%s\"\n",
iignatyev@50844 273 __FILE__, __LINE__, javaField);
iignatyev@50844 274 return result;
iignatyev@50844 275 }
iignatyev@50844 276
iignatyev@50844 277 if (verbose)
iignatyev@50844 278 printf("check: obtaining the value of the field \"%s\"...\n",
iignatyev@50844 279 javaField);
jcbeyler@52515 280 thrwObj = (jthrowable) env->GetObjectField(obj, fid);
iignatyev@50844 281
iignatyev@50844 282 if (verbose)
iignatyev@50844 283 printf("check: obtaining the class of the object for \"%s\"...\n",
iignatyev@50844 284 javaField);
jcbeyler@52515 285 thrw = env->GetObjectClass(thrwObj);
iignatyev@50844 286
iignatyev@50844 287 /* 1: check the JNI function table interception */
iignatyev@50844 288 if (verbose)
iignatyev@50844 289 printf("\na) Checking the JNI function table interception ...\n");
iignatyev@50844 290 doRedirect(env);
iignatyev@50844 291 doExc(env, thrwObj, thrw, "redirected");
iignatyev@50844 292 checkCall(1, 1, 1, 2);
iignatyev@50844 293
iignatyev@50844 294 /* 2: check the restored JNI function table */
iignatyev@50844 295 if (verbose)
iignatyev@50844 296 printf("\nb) Checking the restored JNI function table ...\n");
iignatyev@50844 297 doRestore(env);
iignatyev@50844 298 doExc(env, thrwObj, thrw, "restored");
iignatyev@50844 299 checkCall(2, 0, 0, 0);
iignatyev@50844 300
jcbeyler@52515 301 env->DeleteLocalRef(thrw);
jcbeyler@52515 302 env->DeleteLocalRef(thrwObj);
iignatyev@50844 303
iignatyev@50844 304 return result;
iignatyev@50844 305 }
iignatyev@50844 306
iignatyev@50844 307 #ifdef STATIC_BUILD
iignatyev@50844 308 JNIEXPORT jint JNICALL Agent_OnLoad_ji03t003(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 309 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 310 }
iignatyev@50844 311 JNIEXPORT jint JNICALL Agent_OnAttach_ji03t003(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 312 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 313 }
iignatyev@50844 314 JNIEXPORT jint JNI_OnLoad_ji03t003(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 315 return JNI_VERSION_1_8;
iignatyev@50844 316 }
iignatyev@50844 317 #endif
iignatyev@50844 318 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 319 jint res;
iignatyev@50844 320
iignatyev@50844 321 if (options != NULL && strcmp(options, "-verbose") == 0)
iignatyev@50844 322 verbose = 1;
iignatyev@50844 323
iignatyev@50844 324 if (verbose)
iignatyev@50844 325 printf("verbose mode on\n");
iignatyev@50844 326
jcbeyler@52515 327 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
iignatyev@50844 328 if (res != JNI_OK || jvmti == NULL) {
iignatyev@50844 329 printf("(%s,%d): Failed to call GetEnv\n", __FILE__, __LINE__);
iignatyev@50844 330 return JNI_ERR;
iignatyev@50844 331 }
iignatyev@50844 332
iignatyev@50844 333 return JNI_OK;
iignatyev@50844 334 }
iignatyev@50844 335
iignatyev@50844 336 }