test/hotspot/jtreg/vmTestbase/nsk/jvmti/SetJNIFunctionTable/setjniftab001/setjniftab001.cpp
changeset 52642 9cfc8b0c45fd
parent 52495 52be2c714a2f
child 52949 c78a17d24618
equal deleted inserted replaced
52641:09a3f379b927 52642:9cfc8b0c45fd
    68 static volatile int monent_calls = 0;
    68 static volatile int monent_calls = 0;
    69 
    69 
    70 static void lock() {
    70 static void lock() {
    71     jvmtiError err;
    71     jvmtiError err;
    72 
    72 
    73     if ((err = jvmti->RawMonitorEnter(countLock)) !=
    73     err = jvmti->RawMonitorEnter(countLock);
    74             JVMTI_ERROR_NONE) {
    74     if (err != JVMTI_ERROR_NONE) {
    75         printf("(%s,%d): TEST FAILURE: RawMonitorEnter returns unexpected error: %s\n",
    75         printf("(%s,%d): TEST FAILURE: RawMonitorEnter returns unexpected error: %s\n",
    76             __FILE__, __LINE__, TranslateError(err));
    76             __FILE__, __LINE__, TranslateError(err));
    77         exit(STATUS_FAILED);
    77         exit(STATUS_FAILED);
    78     }
    78     }
    79 }
    79 }
    80 
    80 
    81 static void unlock() {
    81 static void unlock() {
    82     jvmtiError err;
    82     jvmtiError err;
    83 
    83 
    84     if ((err = jvmti->RawMonitorExit(countLock))
    84     err = jvmti->RawMonitorExit(countLock);
    85             != JVMTI_ERROR_NONE) {
    85     if (err != JVMTI_ERROR_NONE) {
    86         printf("(%s,%d): TEST FAILURE: RawMonitorExit returns unexpected error: %s\n",
    86         printf("(%s,%d): TEST FAILURE: RawMonitorExit returns unexpected error: %s\n",
    87             __FILE__, __LINE__, TranslateError(err));
    87             __FILE__, __LINE__, TranslateError(err));
    88         exit(STATUS_FAILED);
    88         exit(STATUS_FAILED);
    89     }
    89     }
    90 }
    90 }
   114 void doRedirect(JNIEnv *env) {
   114 void doRedirect(JNIEnv *env) {
   115     jvmtiError err;
   115     jvmtiError err;
   116 
   116 
   117     if (verbose)
   117     if (verbose)
   118         printf("\ndoRedirect: obtaining the JNI function table ...\n");
   118         printf("\ndoRedirect: obtaining the JNI function table ...\n");
   119     if ((err = jvmti->GetJNIFunctionTable(&orig_jni_functions)) !=
   119     err = jvmti->GetJNIFunctionTable(&orig_jni_functions);
   120             JVMTI_ERROR_NONE) {
   120     if (err != JVMTI_ERROR_NONE) {
   121         result = STATUS_FAILED;
   121         result = STATUS_FAILED;
   122         printf("(%s,%d): TEST FAILED: failed to get original JNI function table: %s\n",
   122         printf("(%s,%d): TEST FAILED: failed to get original JNI function table: %s\n",
   123             __FILE__, __LINE__, TranslateError(err));
   123             __FILE__, __LINE__, TranslateError(err));
   124         env->FatalError("failed to get original JNI function table");
   124         env->FatalError("failed to get original JNI function table");
   125     }
   125     }
   126     if ((err = jvmti->GetJNIFunctionTable(&redir_jni_functions)) !=
   126     err = jvmti->GetJNIFunctionTable(&redir_jni_functions);
   127             JVMTI_ERROR_NONE) {
   127     if (err != JVMTI_ERROR_NONE) {
   128         result = STATUS_FAILED;
   128         result = STATUS_FAILED;
   129         printf("(%s,%d): TEST FAILED: failed to get redirected JNI function table: %s\n",
   129         printf("(%s,%d): TEST FAILED: failed to get redirected JNI function table: %s\n",
   130             __FILE__, __LINE__, TranslateError(err));
   130             __FILE__, __LINE__, TranslateError(err));
   131         env->FatalError("failed to get redirected JNI function table");
   131         env->FatalError("failed to get redirected JNI function table");
   132     }
   132     }
   135 
   135 
   136     if (verbose)
   136     if (verbose)
   137         printf("\ndoRedirect: overwriting the function MonitorEnter ...\n");
   137         printf("\ndoRedirect: overwriting the function MonitorEnter ...\n");
   138     redir_jni_functions->MonitorEnter = MyMonitorEnter;
   138     redir_jni_functions->MonitorEnter = MyMonitorEnter;
   139 
   139 
   140     if ((err = jvmti->SetJNIFunctionTable(redir_jni_functions)) !=
   140     err = jvmti->SetJNIFunctionTable(redir_jni_functions);
   141             JVMTI_ERROR_NONE) {
   141     if (err != JVMTI_ERROR_NONE) {
   142         result = STATUS_FAILED;
   142         result = STATUS_FAILED;
   143         printf("(%s,%d): TEST FAILED: failed to set new JNI function table: %s\n",
   143         printf("(%s,%d): TEST FAILED: failed to set new JNI function table: %s\n",
   144             __FILE__, __LINE__, TranslateError(err));
   144             __FILE__, __LINE__, TranslateError(err));
   145         env->FatalError("failed to set new JNI function table");
   145         env->FatalError("failed to set new JNI function table");
   146     }
   146     }
   152 void doRestore(JNIEnv *env) {
   152 void doRestore(JNIEnv *env) {
   153     jvmtiError err;
   153     jvmtiError err;
   154 
   154 
   155     if (verbose)
   155     if (verbose)
   156         printf("\ndoRestore: restoring the original JNI function table ...\n");
   156         printf("\ndoRestore: restoring the original JNI function table ...\n");
   157     if ((err = jvmti->SetJNIFunctionTable(orig_jni_functions)) !=
   157     err = jvmti->SetJNIFunctionTable(orig_jni_functions);
   158             JVMTI_ERROR_NONE) {
   158     if (err != JVMTI_ERROR_NONE) {
   159         result = STATUS_FAILED;
   159         result = STATUS_FAILED;
   160         printf("(%s,%d): TEST FAILED: failed to restore original JNI function table: %s\n",
   160         printf("(%s,%d): TEST FAILED: failed to restore original JNI function table: %s\n",
   161             __FILE__, __LINE__, TranslateError(err));
   161             __FILE__, __LINE__, TranslateError(err));
   162         env->FatalError("failed to restore original JNI function table");
   162         env->FatalError("failed to restore original JNI function table");
   163     }
   163     }
   166 }
   166 }
   167 
   167 
   168 void doExec(JNIEnv *env, int thrNum) {
   168 void doExec(JNIEnv *env, int thrNum) {
   169     jint res;
   169     jint res;
   170 
   170 
   171     if ((res = env->MonitorEnter(clsObj)) != 0) {
   171     res = env->MonitorEnter(clsObj);
       
   172     if (res != 0) {
   172         result = STATUS_FAILED;
   173         result = STATUS_FAILED;
   173         printf("(%s,%d): TEST FAILURE: MonitorEnter() returns %d for thread #%d\n",
   174         printf("(%s,%d): TEST FAILURE: MonitorEnter() returns %d for thread #%d\n",
   174             __FILE__, __LINE__, res, thrNum);
   175             __FILE__, __LINE__, res, thrNum);
   175     }
   176     }
   176     if (env->ExceptionOccurred()) {
   177     if (env->ExceptionOccurred()) {
   181         env->ExceptionClear();
   182         env->ExceptionClear();
   182     }
   183     }
   183     if (verbose)
   184     if (verbose)
   184         printf("\ndoExec: thread #%d entered the monitor\n",
   185         printf("\ndoExec: thread #%d entered the monitor\n",
   185             thrNum);
   186             thrNum);
   186     if ((res = env->MonitorExit(clsObj)) != 0) {
   187     res = env->MonitorExit(clsObj);
       
   188     if (res != 0) {
   187         result = STATUS_FAILED;
   189         result = STATUS_FAILED;
   188         printf("(%s,%d): TEST FAILURE: MonitorExit() returns %d for thread #%d\n",
   190         printf("(%s,%d): TEST FAILURE: MonitorExit() returns %d for thread #%d\n",
   189             __FILE__, __LINE__, res, thrNum);
   191             __FILE__, __LINE__, res, thrNum);
   190     }
   192     }
   191     if (verbose)
   193     if (verbose)
   219     int indx = *((int *) context);
   221     int indx = *((int *) context);
   220 
   222 
   221     if (verbose)
   223     if (verbose)
   222         printf("\nwaitingThread: thread #%d started\n\tattaching the thread to the VM ...\n",
   224         printf("\nwaitingThread: thread #%d started\n\tattaching the thread to the VM ...\n",
   223             indx);
   225             indx);
   224     if ((res = vm->AttachCurrentThread((void **) &env, (void *) 0)) != 0) {
   226     res = vm->AttachCurrentThread((void **) &env, (void *) 0);
       
   227     if (res != 0) {
   225         printf("(%s,%d): TEST FAILURE: waitingThread: AttachCurrentThread() returns: %d\n",
   228         printf("(%s,%d): TEST FAILURE: waitingThread: AttachCurrentThread() returns: %d\n",
   226             __FILE__, __LINE__, res);
   229             __FILE__, __LINE__, res);
   227         return STATUS_FAILED;
   230         return STATUS_FAILED;
   228     }
   231     }
   229 
   232 
   230     thrStarted[indx-1] = 1;
   233     thrStarted[indx-1] = 1;
   231 
   234 
   232     doExec(env, indx);
   235     doExec(env, indx);
   233 
   236 
   234     if ((res = vm->DetachCurrentThread()) != 0) {
   237     res = vm->DetachCurrentThread();
       
   238     if (res != 0) {
   235         printf("(%s,%d): TEST FAILURE: waitingThread: DetachCurrentThread() returns: %d\n",
   239         printf("(%s,%d): TEST FAILURE: waitingThread: DetachCurrentThread() returns: %d\n",
   236             __FILE__, __LINE__, res);
   240             __FILE__, __LINE__, res);
   237         return STATUS_FAILED;
   241         return STATUS_FAILED;
   238     }
   242     }
   239     if (verbose)
   243     if (verbose)
   250     _objCls = env->GetObjectClass(obj);
   254     _objCls = env->GetObjectClass(obj);
   251 
   255 
   252     if (verbose)
   256     if (verbose)
   253        printf("\ngetObjectFromField: obtaining field ID for name=\"%s\" signature=\"%s\"...\n",
   257        printf("\ngetObjectFromField: obtaining field ID for name=\"%s\" signature=\"%s\"...\n",
   254            javaField, classSig);
   258            javaField, classSig);
   255     if ((fid = env->GetFieldID(_objCls, javaField, classSig)) == 0) {
   259     fid = env->GetFieldID(_objCls, javaField, classSig);
       
   260     if (fid == 0) {
   256         result = STATUS_FAILED;
   261         result = STATUS_FAILED;
   257         printf("(%s,%d): TEST FAILURE: failed to get ID for the field \"%s\"\n",
   262         printf("(%s,%d): TEST FAILURE: failed to get ID for the field \"%s\"\n",
   258             __FILE__, __LINE__, javaField);
   263             __FILE__, __LINE__, javaField);
   259         env->FatalError("failed to get ID for the java field");
   264         env->FatalError("failed to get ID for the java field");
   260     }
   265     }
   324         printf("(%s,%d): TEST FAILURE: JVMTI client was not properly loaded\n",
   329         printf("(%s,%d): TEST FAILURE: JVMTI client was not properly loaded\n",
   325             __FILE__, __LINE__);
   330             __FILE__, __LINE__);
   326         return STATUS_FAILED;
   331         return STATUS_FAILED;
   327     }
   332     }
   328 
   333 
   329     if ((clsObj = env->NewGlobalRef(getObjectFromField(env, obj))) == NULL) {
   334     clsObj = env->NewGlobalRef(getObjectFromField(env, obj));
       
   335     if (clsObj == NULL) {
   330         printf("(%s,%d): TEST FAILURE: cannot create a new global reference of class \"%s\"\n",
   336         printf("(%s,%d): TEST FAILURE: cannot create a new global reference of class \"%s\"\n",
   331             __FILE__, __LINE__, classSig);
   337             __FILE__, __LINE__, classSig);
   332         env->FatalError("failed to create a new global reference");
   338         env->FatalError("failed to create a new global reference");
   333     }
   339     }
   334 
   340 
   349        Note: the JNI spec says that the main thread can be detached from the VM
   355        Note: the JNI spec says that the main thread can be detached from the VM
   350        only since JDK 1.2 */
   356        only since JDK 1.2 */
   351     if (verbose)
   357     if (verbose)
   352         printf("\nb) Checking the assertion inside main thread detached and attached again ...\n\ndetaching the main thread ...\n");
   358         printf("\nb) Checking the assertion inside main thread detached and attached again ...\n\ndetaching the main thread ...\n");
   353 
   359 
   354     if ((res = vm->DetachCurrentThread()) != 0) {
   360     res = vm->DetachCurrentThread();
       
   361     if (res != 0) {
   355         printf(
   362         printf(
   356             "(%s,%d): Warning: DetachCurrentThread() returns: %d\n"
   363             "(%s,%d): Warning: DetachCurrentThread() returns: %d\n"
   357             "\tcheck with the detached main thread skipped\n",
   364             "\tcheck with the detached main thread skipped\n",
   358             __FILE__, __LINE__, res);
   365             __FILE__, __LINE__, res);
   359     } else {
   366     } else {
   360         if (verbose)
   367         if (verbose)
   361             printf("\nattaching the main thread again ...\n");
   368             printf("\nattaching the main thread again ...\n");
   362         if ((res = vm->AttachCurrentThread((void **) &nextEnv, (void *) 0)) != 0) {
   369         res = vm->AttachCurrentThread((void **) &nextEnv, (void *) 0);
       
   370         if (res != 0) {
   363             printf("(%s,%d): TEST FAILURE: waitingThread: AttachCurrentThread() returns: %d\n",
   371             printf("(%s,%d): TEST FAILURE: waitingThread: AttachCurrentThread() returns: %d\n",
   364                 __FILE__, __LINE__, res);
   372                 __FILE__, __LINE__, res);
   365             return STATUS_FAILED;
   373             return STATUS_FAILED;
   366         }
   374         }
   367 
   375 
   417         return JNI_ERR;
   425         return JNI_ERR;
   418     }
   426     }
   419 
   427 
   420     vm = jvm;
   428     vm = jvm;
   421 
   429 
   422     if ((err = jvmti->CreateRawMonitor("_counter_lock",
   430     err = jvmti->CreateRawMonitor("_counter_lock", &countLock);
   423             &countLock)) != JVMTI_ERROR_NONE) {
   431     if (err != JVMTI_ERROR_NONE) {
   424         printf("(%s,%d): TEST FAILURE: CreateRawMonitor() returns unexpected error: %s\n",
   432         printf("(%s,%d): TEST FAILURE: CreateRawMonitor() returns unexpected error: %s\n",
   425             __FILE__, __LINE__, TranslateError(err));
   433             __FILE__, __LINE__, TranslateError(err));
   426         return JNI_ERR;
   434         return JNI_ERR;
   427     }
   435     }
   428 
   436