1 /*
   2  * Copyright (c) 2007, 2018, Oracle 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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 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 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include <stdio.h>
  25 #include <string.h>
  26 #include <inttypes.h>
  27 #include "jvmti.h"
  28 #include "agent_common.h"
  29 #include "JVMTITools.h"
  30 
  31 #ifdef __cplusplus
  32 extern "C" {
  33 #endif
  34 
  35 #ifndef JNI_ENV_ARG
  36 
  37 #ifdef __cplusplus
  38 #define JNI_ENV_ARG(x, y) y
  39 #define JNI_ENV_PTR(x) x
  40 #else
  41 #define JNI_ENV_ARG(x,y) x, y
  42 #define JNI_ENV_PTR(x) (*x)
  43 #endif
  44 
  45 #endif
  46 
  47 #define PASSED 0
  48 #define STATUS_FAILED 2
  49 
  50 #define RETURN_FAILED errCode = STATUS_FAILED; fflush(0); return
  51 
  52 static jvmtiEnv *jvmti = NULL;
  53 static jvmtiCapabilities caps;
  54 static jvmtiEventCallbacks callbacks;
  55 static jint errCode = PASSED;
  56 static jboolean printdump = JNI_TRUE;
  57 
  58 static jmethodID midRun             = NULL;
  59 static jmethodID midCountDownObject = NULL;
  60 static jmethodID midCheckPoint      = NULL;
  61 
  62 static jint framesExpected = 0;
  63 static jint framesCount = 0;
  64 static jint methodExitEventCount = 0;
  65 
  66 static const char *cls_exp = "Lnsk/jvmti/unit/ForceEarlyReturn/earlyretobj$earlyretThread;";
  67 
  68 static jobject val_exp = NULL;
  69 static const char  *sig_exp = "(I)Lnsk/jvmti/unit/ForceEarlyReturn/earlyretobj$RetObj;";
  70 static const char *name_exp = "countDownObject";
  71 
  72 static const char *argName = "nestingCount";
  73 
  74 void check(jvmtiEnv *jvmti_env, jthread thr, jmethodID mid,
  75            jlocation loc, jint frame_no) {
  76     jvmtiError err;
  77     jclass cls;
  78     jlocation loc_exp = (frame_no == 0) ? 0x15 : 0xd;
  79     char *sigClass, *name, *sig, *generic;
  80     jvmtiLocalVariableEntry *table = NULL;
  81     jint entryCount = 0;
  82     jint argValue;
  83     jint j;
  84 
  85     err = jvmti_env->GetMethodDeclaringClass(mid, &cls);
  86     if (err != JVMTI_ERROR_NONE) {
  87         printf("(GetMethodDeclaringClass#%d) unexpected error: %s (%d)\n",
  88                frame_no, TranslateError(err), err);
  89         RETURN_FAILED;
  90     }
  91 
  92     err = jvmti_env->GetClassSignature(cls, &sigClass, &generic);
  93     if (err != JVMTI_ERROR_NONE) {
  94         printf("(GetClassSignature#%d) unexpected error: %s (%d)\n",
  95                frame_no, TranslateError(err), err);
  96         RETURN_FAILED;
  97     }
  98 
  99     err = jvmti_env->GetMethodName(mid, &name, &sig, &generic);
 100     if (err != JVMTI_ERROR_NONE) {
 101         printf("(GetMethodName#%d) unexpected error: %s (%d)\n",
 102                frame_no, TranslateError(err), err);
 103         RETURN_FAILED;
 104     }
 105 
 106     /* Get Local Variable Table to be able to get the argument value
 107      * from current method frame and compare it with the expected value
 108      */
 109     err = jvmti_env->GetLocalVariableTable(mid, &entryCount, &table);
 110     if (err != JVMTI_ERROR_NONE) {
 111         printf("(GetLocalVariableTable#%d) unexpected error: %s (%d)\n",
 112                frame_no, TranslateError(err), err);
 113         RETURN_FAILED;
 114     }
 115     if (table != NULL) {
 116         for (j = 0; j < entryCount; j++) {
 117             if (strcmp(table[j].name, argName) == 0) {
 118                 err = jvmti_env->GetLocalInt(thr, 0,
 119                     table[j].slot, &argValue);
 120                 if (err != JVMTI_ERROR_NONE) {
 121                     printf("(GetLocalInt#%d) unexpected error: %s (%d)\n",
 122                            frame_no, TranslateError(err), err);
 123                     RETURN_FAILED;
 124                 }
 125             }
 126         }
 127     }
 128 
 129     if (printdump == JNI_TRUE) {
 130         printf("\n>>> step %d: \"%s.%s%s\"\n", frame_no, sigClass, name, sig);
 131         printf(">>>   location: %#x%08x", (jint)(loc >> 32), (jint)loc);
 132         printf(", arg value: %d\n", argValue);
 133     }
 134 
 135     if (sigClass == NULL || strcmp(sigClass, cls_exp) != 0) {
 136         printf("(step %d) Wrong class sig: \"%s\",\n", frame_no, sigClass);
 137         printf(" expected: \"%s\"\n", cls_exp);
 138         RETURN_FAILED;
 139     }
 140     if (name == NULL || strcmp(name, name_exp) != 0) {
 141         printf("(step %d) wrong method name: \"%s\",", frame_no, name);
 142         printf(" expected: \"%s\"\n", name_exp);
 143         RETURN_FAILED;
 144     }
 145     if (sig == NULL || strcmp(sig, sig_exp) != 0) {
 146         printf("(step %d) wrong method sig: \"%s\",", frame_no, sig);
 147         printf(" expected: \"%s\"\n", sig_exp);
 148         RETURN_FAILED;
 149     }
 150     if (loc != loc_exp) {
 151         printf("(step %d) wrong location: %#x%08x,",
 152                frame_no, (jint)(loc >> 32), (jint)loc);
 153         printf(" expected: %#x\n", (jint)loc_exp);
 154         RETURN_FAILED;
 155     }
 156     if (argValue != frame_no) {
 157         printf("(step %d) wrong argument value: %d,", frame_no, argValue);
 158         printf(" expected: %d\n", frame_no);
 159         RETURN_FAILED;
 160     }
 161 
 162     if (sigClass != NULL) {
 163         jvmti_env->Deallocate((unsigned char*)sigClass);
 164     }
 165     if (name != NULL) {
 166         jvmti_env->Deallocate((unsigned char*)name);
 167     }
 168     if (sig != NULL) {
 169         jvmti_env->Deallocate((unsigned char*)sig);
 170     }
 171     if (table != NULL) {
 172         for (j = 0; j < entryCount; j++) {
 173             jvmti_env->Deallocate((unsigned char*)(table[j].name));
 174             jvmti_env->Deallocate((unsigned char*)(table[j].signature));
 175         }
 176         jvmti_env->Deallocate((unsigned char*)table);
 177     }
 178     if (methodExitEventCount != (framesCount + 1)) {
 179         printf("(step %d) wrong methodExitEventCount: %d,",
 180                frame_no, methodExitEventCount);
 181         printf(" expected: %d\n", framesCount + 1);
 182         RETURN_FAILED;
 183     }
 184     fflush(0);
 185 }
 186 
 187 void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env,
 188         jthread thread, jmethodID method, jlocation location) {
 189     jvmtiError err;
 190 
 191     if (midCheckPoint != method) {
 192         printf("bp: don't know where we get called from");
 193         RETURN_FAILED;
 194     }
 195 
 196     if (printdump == JNI_TRUE) {
 197         printf(">>> breakpoint in checkPoint\n");
 198     }
 199 
 200     err = jvmti_env->ClearBreakpoint(midCheckPoint, 0);
 201     if (err != JVMTI_ERROR_NONE) {
 202         printf("(ClearBreakpoint) unexpected error: %s (%d)\n",
 203                TranslateError(err), err);
 204         RETURN_FAILED;
 205     }
 206 
 207     err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
 208         JVMTI_EVENT_SINGLE_STEP, thread);
 209     if (err != JVMTI_ERROR_NONE) {
 210         printf("Cannot enable single step events: %s (%d)\n",
 211                TranslateError(err), err);
 212         RETURN_FAILED;
 213     }
 214 
 215     err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
 216         JVMTI_EVENT_METHOD_EXIT, thread);
 217     if (err != JVMTI_ERROR_NONE) {
 218         printf("Cannot enable method exit events: %s (%d)\n",
 219                TranslateError(err), err);
 220         RETURN_FAILED;
 221     }
 222 
 223     err = jvmti_env->ForceEarlyReturnVoid(thread);
 224     if (err != JVMTI_ERROR_NONE) {
 225         printf("(ForceEarlyReturnVoid) unexpected error: %s (%d)\n",
 226                TranslateError(err), err);
 227         RETURN_FAILED;
 228     }
 229     fflush(0);
 230 }
 231 
 232 void JNICALL SingleStep(jvmtiEnv *jvmti_env, JNIEnv *env,
 233         jthread thread, jmethodID method, jlocation location) {
 234     jvmtiError err;
 235 
 236     if (method == midRun) {
 237         if (printdump == JNI_TRUE) {
 238             printf(">>> returned early %d frames till method \"run()\"\n",
 239                    framesCount);
 240         }
 241 
 242         err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
 243             JVMTI_EVENT_SINGLE_STEP, thread);
 244         if (err != JVMTI_ERROR_NONE) {
 245             printf("Cannot disable single step events: %s (%d)\n",
 246                    TranslateError(err), err);
 247             RETURN_FAILED;
 248         }
 249         err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
 250             JVMTI_EVENT_METHOD_EXIT, thread);
 251         if (err != JVMTI_ERROR_NONE) {
 252             printf("Cannot disable method exit events: %s (%d)\n",
 253                    TranslateError(err), err);
 254             RETURN_FAILED;
 255         }
 256     } else {
 257         check(jvmti_env, thread, method, location, framesCount);
 258         framesCount++;
 259         err = jvmti_env->ForceEarlyReturnObject(thread,
 260                                                    val_exp);
 261         if (err != JVMTI_ERROR_NONE) {
 262             printf("(ForceEarlyReturnObject) unexpected error: %s (%d)\n",
 263                     TranslateError(err), err);
 264             RETURN_FAILED;
 265         }
 266     }
 267     fflush(0);
 268 }
 269 
 270 void JNICALL MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread,
 271         jmethodID method, jboolean was_popped_by_exception, jvalue value) {
 272     jobject ret_val = value.l;
 273 
 274     methodExitEventCount++;
 275     printf("MethodExit event: methodExitEventCount=%d\n", methodExitEventCount);
 276     if (method == midRun || method == midCheckPoint) {
 277         return;
 278     }
 279     if (method == midCountDownObject) {
 280         if (!JNI_ENV_PTR(env)->IsSameObject(JNI_ENV_ARG(env, ret_val), val_exp)) {
 281             printf("Wrong ForceEarlyReturnObject return value: 0x%p\n", ret_val);
 282             printf("expected: 0x%p\n", val_exp);
 283             errCode = STATUS_FAILED;
 284         }
 285         if (was_popped_by_exception) {
 286             printf("Method was_popped_by_exception unexpectedly\n");
 287             errCode = STATUS_FAILED;
 288         }
 289     }
 290     fflush(0);
 291 }
 292 
 293 #ifdef STATIC_BUILD
 294 JNIEXPORT jint JNICALL Agent_OnLoad_earlyretobj(JavaVM *jvm, char *options, void *reserved) {
 295     return Agent_Initialize(jvm, options, reserved);
 296 }
 297 JNIEXPORT jint JNICALL Agent_OnAttach_earlyretobj(JavaVM *jvm, char *options, void *reserved) {
 298     return Agent_Initialize(jvm, options, reserved);
 299 }
 300 JNIEXPORT jint JNI_OnLoad_earlyretobj(JavaVM *jvm, char *options, void *reserved) {
 301     return JNI_VERSION_1_8;
 302 }
 303 #endif
 304 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 305     jvmtiError err;
 306     jint res;
 307 
 308     if (options != NULL && strcmp(options, "printdump") == 0) {
 309         printf("Printdump is turned on!\n");
 310 
 311         printdump = JNI_TRUE;
 312     }
 313 
 314     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
 315         JVMTI_VERSION_1_1);
 316     if (res != JNI_OK || jvmti == NULL) {
 317         printf("Wrong error code from a valid call to GetEnv!\n");
 318         return JNI_ERR;
 319     }
 320 
 321     err = jvmti->GetPotentialCapabilities(&caps);
 322     if (err != JVMTI_ERROR_NONE) {
 323         printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
 324                TranslateError(err), err);
 325         return JNI_ERR;
 326     }
 327 
 328     err = jvmti->AddCapabilities(&caps);
 329     if (err != JVMTI_ERROR_NONE) {
 330         printf("(AddCapabilities) unexpected error: %s (%d)\n",
 331                TranslateError(err), err);
 332         return JNI_ERR;
 333     }
 334 
 335     err = jvmti->GetCapabilities(&caps);
 336     if (err != JVMTI_ERROR_NONE) {
 337         printf("(GetCapabilities) unexpected error: %s (%d)\n",
 338                TranslateError(err), err);
 339         return JNI_ERR;
 340     }
 341 
 342     if (!caps.can_force_early_return) {
 343         printf("Warning: ForceEarlyReturn is not implemented\n");
 344     }
 345 
 346     if (caps.can_generate_breakpoint_events &&
 347         caps.can_generate_method_exit_events &&
 348         caps.can_generate_single_step_events)
 349     {
 350         callbacks.Breakpoint = &Breakpoint;
 351         callbacks.SingleStep = &SingleStep;
 352         callbacks.MethodExit = &MethodExit;
 353         err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
 354         if (err != JVMTI_ERROR_NONE) {
 355             printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
 356                    TranslateError(err), err);
 357             return JNI_ERR;
 358         }
 359     } else {
 360         printf("Warning: Breakpoint or SingleStep event are not implemented\n");
 361     }
 362 
 363     return JNI_OK;
 364 }
 365 
 366 JNIEXPORT void JNICALL
 367 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretobj_getReady(
 368     JNIEnv *env, jclass c, jclass cls, jint depth, jobject ret_obj) {
 369     jvmtiError err;
 370 
 371     if (jvmti == NULL) {
 372         printf("JVMTI client was not properly loaded!\n");
 373         RETURN_FAILED;
 374     }
 375 
 376     if (!caps.can_force_early_return ||
 377         !caps.can_generate_breakpoint_events ||
 378         !caps.can_generate_method_exit_events ||
 379         !caps.can_generate_single_step_events) {
 380         return;
 381     }
 382 
 383     midRun = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls),
 384          "run", "()V");
 385     if (midRun == NULL) {
 386         printf("Cannot find Method ID for method run\n");
 387         RETURN_FAILED;
 388     }
 389 
 390     midCheckPoint = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls),
 391          "checkPoint", "()V");
 392     if (midCheckPoint == NULL) {
 393         printf("Cannot find Method ID for method checkPoint\n");
 394         RETURN_FAILED;
 395     }
 396 
 397     midCountDownObject = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls),
 398          "countDownObject", sig_exp);
 399     if (midCountDownObject == NULL) {
 400         printf("Cannot find Method ID for method countDownObject\n");
 401         RETURN_FAILED;
 402     }
 403 
 404     err = jvmti->SetBreakpoint(midCheckPoint, 0);
 405     if (err != JVMTI_ERROR_NONE) {
 406         printf("(SetBreakpoint) unexpected error: %s (%d)\n",
 407                TranslateError(err), err);
 408         RETURN_FAILED;
 409     }
 410 
 411     err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
 412         JVMTI_EVENT_BREAKPOINT, NULL);
 413     if (err != JVMTI_ERROR_NONE) {
 414         printf("Failed to enable BREAKPOINT event: %s (%d)\n",
 415                TranslateError(err), err);
 416         RETURN_FAILED;
 417     } else {
 418         val_exp = JNI_ENV_PTR(env)->NewGlobalRef(JNI_ENV_ARG(env, ret_obj));
 419         framesExpected = depth;
 420     }
 421 }
 422 
 423 JNIEXPORT jint JNICALL
 424 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretobj_check(JNIEnv *env, jclass cls) {
 425     if (framesCount != framesExpected) {
 426         printf("Wrong number of returned early frames: %d, expected: %d\n",
 427             framesCount, framesExpected);
 428         errCode = STATUS_FAILED;
 429     }
 430     fflush(0);
 431     return errCode;
 432 }
 433 
 434 JNIEXPORT void JNICALL
 435 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretobj_printObject(
 436          JNIEnv *env, jclass cls, jobject obj) {
 437 
 438     printf("\nReturned jobject: %#" PRIxPTR "\n", (uintptr_t)obj);
 439     fflush(0);
 440     return;
 441 }
 442 
 443 #ifdef __cplusplus
 444 }
 445 #endif