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 "jvmti.h"
  27 #include "agent_common.h"
  28 #include "JVMTITools.h"
  29 
  30 #ifdef __cplusplus
  31 extern "C" {
  32 #endif
  33 
  34 #ifndef JNI_ENV_ARG
  35 
  36 #ifdef __cplusplus
  37 #define JNI_ENV_ARG(x, y) y
  38 #define JNI_ENV_PTR(x) x
  39 #else
  40 #define JNI_ENV_ARG(x,y) x, y
  41 #define JNI_ENV_PTR(x) (*x)
  42 #endif
  43 
  44 #endif
  45 
  46 #define PASSED 0
  47 #define STATUS_FAILED 2
  48 
  49 #define RETURN_FAILED errCode = STATUS_FAILED; fflush(0); return
  50 
  51 static jvmtiEnv *jvmti = NULL;
  52 static jvmtiCapabilities caps;
  53 static jvmtiEventCallbacks callbacks;
  54 static jint errCode = PASSED;
  55 static jboolean printdump = JNI_TRUE;
  56 static jmethodID midCheckPoint = NULL;
  57 static jmethodID midRun = NULL;
  58 
  59 static jint framesExpected = 0;
  60 static jint framesCount = 0;
  61 
  62 static const char *cls_exp = "Lnsk/jvmti/unit/ForceEarlyReturn/earlyretvoid$earlyretThread;";
  63 static const char *name_exp = "countDown";
  64 static const char *sig_exp = "(I)V";
  65 static const char *argName = "nestingCount";
  66 
  67 void check(jvmtiEnv *jvmti_env, jthread thr, jmethodID mid,
  68            jlocation loc, jint frame_no) {
  69     jvmtiError err;
  70     jclass cls;
  71     jlocation loc_exp = (frame_no == 0) ? 25 : 11;
  72     char *sigClass, *name, *sig, *generic;
  73     jvmtiLocalVariableEntry *table = NULL;
  74     jint entryCount = 0;
  75     jint argValue;
  76     jint j;
  77 
  78     err = jvmti_env->GetMethodDeclaringClass(mid, &cls);
  79     if (err != JVMTI_ERROR_NONE) {
  80         printf("(GetMethodDeclaringClass#%d) unexpected error: %s (%d)\n",
  81                frame_no, TranslateError(err), err);
  82         RETURN_FAILED;
  83     }
  84 
  85     err = jvmti_env->GetClassSignature(cls, &sigClass, &generic);
  86     if (err != JVMTI_ERROR_NONE) {
  87         printf("(GetClassSignature#%d) unexpected error: %s (%d)\n",
  88                frame_no, TranslateError(err), err);
  89         RETURN_FAILED;
  90     }
  91 
  92     err = jvmti_env->GetMethodName(mid, &name, &sig, &generic);
  93     if (err != JVMTI_ERROR_NONE) {
  94         printf("(GetMethodName#%d) unexpected error: %s (%d)\n",
  95                frame_no, TranslateError(err), err);
  96         RETURN_FAILED;
  97     }
  98 
  99     /* Get Local Variable Table to be able to get the argument value
 100      * from current method frame and compare it with the expected value
 101      */
 102     err = jvmti_env->GetLocalVariableTable(mid, &entryCount, &table);
 103     if (err != JVMTI_ERROR_NONE) {
 104         printf("(GetLocalVariableTable#%d) unexpected error: %s (%d)\n",
 105                frame_no, TranslateError(err), err);
 106         RETURN_FAILED;
 107     }
 108     if (table != NULL) {
 109         for (j = 0; j < entryCount; j++) {
 110             if (strcmp(table[j].name, argName) == 0) {
 111                 err = jvmti_env->GetLocalInt(thr, 0,
 112                     table[j].slot, &argValue);
 113                 if (err != JVMTI_ERROR_NONE) {
 114                     printf("(GetLocalInt#%d) unexpected error: %s (%d)\n",
 115                            frame_no, TranslateError(err), err);
 116                     RETURN_FAILED;
 117                 }
 118             }
 119         }
 120     }
 121 
 122     if (printdump == JNI_TRUE) {
 123         printf(">>> step %d: \"%s.%s%s\"\n", frame_no, sigClass, name, sig);
 124         printf(">>>   location: %#x%08x", (jint)(loc >> 32), (jint)loc);
 125         printf(", arg value: %d\n", argValue);
 126     }
 127 
 128     if (sigClass == NULL || strcmp(sigClass, cls_exp) != 0) {
 129         printf("(step %d) wrong class sig: \"%s\",\n", frame_no, sigClass);
 130         printf(" expected: \"%s\"\n", cls_exp);
 131         RETURN_FAILED;
 132     }
 133     if (name == NULL || strcmp(name, name_exp) != 0) {
 134         printf("(step %d) wrong method name: \"%s\",", frame_no, name);
 135         printf(" expected: \"%s\"\n", name_exp);
 136         RETURN_FAILED;
 137     }
 138     if (sig == NULL || strcmp(sig, sig_exp) != 0) {
 139         printf("(step %d) wrong method sig: \"%s\",", frame_no, sig);
 140         printf(" expected: \"%s\"\n", sig_exp);
 141         RETURN_FAILED;
 142     }
 143     if (loc != loc_exp) {
 144         printf("(step %d) wrong location: %#x%08x,",
 145                frame_no, (jint)(loc >> 32), (jint)loc);
 146         printf(" expected: %#x\n", (jint)loc_exp);
 147         RETURN_FAILED;
 148     }
 149     if (argValue != frame_no) {
 150         printf("(step %d) wrong argument value: %d,", frame_no, argValue);
 151         printf(" expected: %d\n", frame_no);
 152         RETURN_FAILED;
 153     }
 154 
 155     if (sigClass != NULL) {
 156         jvmti_env->Deallocate((unsigned char*)sigClass);
 157     }
 158     if (name != NULL) {
 159         jvmti_env->Deallocate((unsigned char*)name);
 160     }
 161     if (sig != NULL) {
 162         jvmti_env->Deallocate((unsigned char*)sig);
 163     }
 164     if (table != NULL) {
 165         for (j = 0; j < entryCount; j++) {
 166             jvmti_env->Deallocate((unsigned char*)(table[j].name));
 167             jvmti_env->Deallocate((unsigned char*)(table[j].signature));
 168         }
 169         jvmti_env->Deallocate((unsigned char*)table);
 170     }
 171     fflush(0);
 172 }
 173 
 174 void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env,
 175         jthread thread, jmethodID method, jlocation location) {
 176     jvmtiError err;
 177 
 178     if (midCheckPoint != method) {
 179         printf("bp: don't know where we get called from");
 180         RETURN_FAILED;
 181     }
 182 
 183     if (printdump == JNI_TRUE) {
 184         printf(">>> breakpoint in checkPoint\n");
 185     }
 186 
 187     err = jvmti_env->ClearBreakpoint(midCheckPoint, 0);
 188     if (err != JVMTI_ERROR_NONE) {
 189         printf("(ClearBreakpoint) unexpected error: %s (%d)\n",
 190                TranslateError(err), err);
 191         RETURN_FAILED;
 192     }
 193 
 194     err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
 195         JVMTI_EVENT_SINGLE_STEP, thread);
 196     if (err != JVMTI_ERROR_NONE) {
 197         printf("Cannot enable single step: %s (%d)\n",
 198                TranslateError(err), err);
 199         RETURN_FAILED;
 200     }
 201 
 202     err = jvmti_env->ForceEarlyReturnVoid(thread);
 203     if (err != JVMTI_ERROR_NONE) {
 204         printf("(ForceEarlyReturn) unexpected error: %s (%d)\n",
 205                TranslateError(err), err);
 206         RETURN_FAILED;
 207     }
 208     fflush(0);
 209 }
 210 
 211 void JNICALL SingleStep(jvmtiEnv *jvmti_env, JNIEnv *env,
 212         jthread thread, jmethodID method, jlocation location) {
 213     jvmtiError err;
 214 
 215     if (method == midRun) {
 216         if (printdump == JNI_TRUE) {
 217             printf(">>> returned early %d frames till method \"run()\"\n",
 218                    framesCount);
 219         }
 220 
 221         err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
 222             JVMTI_EVENT_SINGLE_STEP, thread);
 223         if (err != JVMTI_ERROR_NONE) {
 224             printf("Cannot disable single step: %s (%d)\n",
 225                    TranslateError(err), err);
 226             RETURN_FAILED;
 227         }
 228     } else {
 229         check(jvmti_env, thread, method, location, framesCount);
 230         framesCount++;
 231 
 232         err = jvmti_env->ForceEarlyReturnVoid(thread);
 233         if (err != JVMTI_ERROR_NONE) {
 234             printf("(ForceEarlyReturn) unexpected error: %s (%d)\n",
 235                    TranslateError(err), err);
 236             RETURN_FAILED;
 237         }
 238     }
 239     fflush(0);
 240 }
 241 
 242 #ifdef STATIC_BUILD
 243 JNIEXPORT jint JNICALL Agent_OnLoad_earlyretvoid(JavaVM *jvm, char *options, void *reserved) {
 244     return Agent_Initialize(jvm, options, reserved);
 245 }
 246 JNIEXPORT jint JNICALL Agent_OnAttach_earlyretvoid(JavaVM *jvm, char *options, void *reserved) {
 247     return Agent_Initialize(jvm, options, reserved);
 248 }
 249 JNIEXPORT jint JNI_OnLoad_earlyretvoid(JavaVM *jvm, char *options, void *reserved) {
 250     return JNI_VERSION_1_8;
 251 }
 252 #endif
 253 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 254     jvmtiError err;
 255     jint res;
 256 
 257     if (options != NULL && strcmp(options, "printdump") == 0) {
 258         printf("Printdump is turned on!\n");
 259 
 260         printdump = JNI_TRUE;
 261     }
 262 
 263     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
 264         JVMTI_VERSION_1_1);
 265     if (res != JNI_OK || jvmti == NULL) {
 266         printf("Wrong error code from a valid call to GetEnv!\n");
 267         return JNI_ERR;
 268     }
 269 
 270     err = jvmti->GetPotentialCapabilities(&caps);
 271     if (err != JVMTI_ERROR_NONE) {
 272         printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
 273                TranslateError(err), err);
 274         return JNI_ERR;
 275     }
 276 
 277     err = jvmti->AddCapabilities(&caps);
 278     if (err != JVMTI_ERROR_NONE) {
 279         printf("(AddCapabilities) unexpected error: %s (%d)\n",
 280                TranslateError(err), err);
 281         return JNI_ERR;
 282     }
 283 
 284     err = jvmti->GetCapabilities(&caps);
 285     if (err != JVMTI_ERROR_NONE) {
 286         printf("(GetCapabilities) unexpected error: %s (%d)\n",
 287                TranslateError(err), err);
 288         return JNI_ERR;
 289     }
 290 
 291     if (!caps.can_force_early_return) {
 292         printf("Warning: ForceEarlyReturn is not implemented\n");
 293     }
 294 
 295     if (caps.can_generate_breakpoint_events &&
 296             caps.can_generate_single_step_events) {
 297         callbacks.Breakpoint = &Breakpoint;
 298         callbacks.SingleStep = &SingleStep;
 299         err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
 300         if (err != JVMTI_ERROR_NONE) {
 301             printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
 302                    TranslateError(err), err);
 303             return JNI_ERR;
 304         }
 305     } else {
 306         printf("Warning: Breakpoint or SingleStep event are not implemented\n");
 307     }
 308 
 309     return JNI_OK;
 310 }
 311 
 312 JNIEXPORT void JNICALL
 313 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretvoid_getReady(
 314     JNIEnv *env, jclass c, jclass cls, jint depth) {
 315     jvmtiError err;
 316 
 317     if (jvmti == NULL) {
 318         printf("JVMTI client was not properly loaded!\n");
 319         RETURN_FAILED;
 320     }
 321 
 322     if (!caps.can_force_early_return ||
 323             !caps.can_generate_breakpoint_events ||
 324             !caps.can_generate_single_step_events) {
 325         return;
 326     }
 327 
 328     midRun = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls),
 329          "run", "()V");
 330     if (midRun == NULL) {
 331         printf("Cannot find Method ID for method run\n");
 332         RETURN_FAILED;
 333     }
 334 
 335     midCheckPoint = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cls),
 336          "checkPoint", "()V");
 337     if (midCheckPoint == NULL) {
 338         printf("Cannot find Method ID for method checkPoint\n");
 339         RETURN_FAILED;
 340     }
 341 
 342     err = jvmti->SetBreakpoint(midCheckPoint, 0);
 343     if (err != JVMTI_ERROR_NONE) {
 344         printf("(SetBreakpoint) unexpected error: %s (%d)\n",
 345                TranslateError(err), err);
 346         RETURN_FAILED;
 347     }
 348 
 349     err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
 350         JVMTI_EVENT_BREAKPOINT, NULL);
 351     if (err != JVMTI_ERROR_NONE) {
 352         printf("Failed to enable BREAKPOINT event: %s (%d)\n",
 353                TranslateError(err), err);
 354         RETURN_FAILED;
 355     } else {
 356         framesExpected = depth;
 357     }
 358     fflush(0);
 359 }
 360 
 361 JNIEXPORT jint JNICALL
 362 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretvoid_check(JNIEnv *env, jclass cls) {
 363     if (framesCount != framesExpected) {
 364         printf("Wrong number of returned early frames: %d, expected: %d\n",
 365             framesCount, framesExpected);
 366         errCode = STATUS_FAILED;
 367     }
 368     fflush(0);
 369     return errCode;
 370 }
 371 
 372 #ifdef __cplusplus
 373 }
 374 #endif