Skip to content

Commit b12e7f1

Browse files
author
Alex Menkov
committed
8279358: vmTestbase/nsk/jvmti/scenarios/jni_interception/JI03/ji03t003/TestDescription.java fails with usage tracker
Reviewed-by: cjplummer, lmesnik
1 parent 1aa87e0 commit b12e7f1

File tree

10 files changed

+169
-78
lines changed

10 files changed

+169
-78
lines changed

test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/redefclass031/TestDescription.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -44,6 +44,6 @@
4444
* @library /vmTestbase
4545
* /test/lib
4646
* @build nsk.jvmti.RedefineClasses.redefclass031r
47-
* @run main/othervm/native -agentlib:redefclass031 nsk.jvmti.RedefineClasses.redefclass031
47+
* @run main/othervm/native -agentlib:redefclass031 nsk.jvmti.RedefineClasses.redefclass031 -v
4848
*/
4949

test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/redefclass031/redefclass031.cpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,12 +39,26 @@ static jvmtiEventCallbacks callbacks;
3939
static int watch_ev = 0; /* ignore JVMTI events by default */
4040
static int gen_ev = 0; /* number of generated events */
4141
static int result = PASSED; /* total result of the test */
42+
static jthread test_thread = NULL;
4243

4344
static jrawMonitorID watch_ev_monitor;
4445

45-
static void set_watch_ev(int value) {
46+
static void set_watch_ev(JNIEnv *env, int value) {
4647
jvmti->RawMonitorEnter(watch_ev_monitor);
4748

49+
if (value) {
50+
jvmtiError err = jvmti->GetCurrentThread(&test_thread);
51+
if (err != JVMTI_ERROR_NONE) {
52+
printf("Failed to get current thread: %s (%d)\n", TranslateError(err), err);
53+
result = STATUS_FAILED;
54+
} else {
55+
test_thread = env->NewGlobalRef(test_thread);
56+
}
57+
} else if (test_thread != NULL) {
58+
env->DeleteGlobalRef(test_thread);
59+
test_thread = NULL;
60+
}
61+
4862
watch_ev = value;
4963

5064
jvmti->RawMonitorExit(watch_ev_monitor);
@@ -54,9 +68,31 @@ void JNICALL
5468
NativeMethodBind(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr, jmethodID methodID, void * pAddress, void ** pNewAddress) {
5569
jvmti->RawMonitorEnter(watch_ev_monitor);
5670

57-
if (watch_ev && isThreadExpected(jvmti_env, thr)) {
58-
printf("#### JVMTI_EVENT_NATIVE_METHOD_BIND occured ####\n");
59-
gen_ev++;
71+
if (watch_ev) {
72+
// we are interested only in events on the test thread and VMThread.
73+
// In case of VMThread we most likely get crash (VMThread is not a Java Thread),
74+
// but lets check GetThreadInfo - it returns error for non-Java threads.
75+
if (env->IsSameObject(test_thread, thr)) {
76+
printf("#### JVMTI_EVENT_NATIVE_METHOD_BIND occured on test thread ####\n");
77+
gen_ev++;
78+
} else {
79+
jvmtiThreadInfo inf;
80+
jvmtiError err = jvmti_env->GetThreadInfo(thr, &inf);
81+
if (err != JVMTI_ERROR_NONE) {
82+
printf("#### JVMTI_EVENT_NATIVE_METHOD_BIND: Failed to get thread info: %s (%d) ####\n",
83+
TranslateError(err), err);
84+
result = STATUS_FAILED;
85+
} else {
86+
printf("got JVMTI_EVENT_NATIVE_METHOD_BIND event on thread '%s', ignoring", inf.name);
87+
jvmti_env->Deallocate((unsigned char *)inf.name);
88+
if (inf.thread_group != NULL) {
89+
env->DeleteLocalRef(inf.thread_group);
90+
}
91+
if (inf.context_class_loader != NULL) {
92+
env->DeleteLocalRef(inf.context_class_loader);
93+
}
94+
}
95+
}
6096
}
6197

6298
jvmti->RawMonitorExit(watch_ev_monitor);
@@ -153,7 +189,7 @@ Java_nsk_jvmti_RedefineClasses_redefclass031_makeRedefinition(JNIEnv *env,
153189
classDef.class_byte_count = env->GetArrayLength(classBytes);
154190
classDef.class_bytes = (unsigned char *) env->GetByteArrayElements(classBytes, NULL);
155191

156-
set_watch_ev(1); /* watch JVMTI events */
192+
set_watch_ev(env, 1); /* watch JVMTI events */
157193

158194
if (vrb == 1)
159195
printf(">>>>>>>> Invoke RedefineClasses():\n\tnew class byte count=%d\n",
@@ -168,7 +204,7 @@ Java_nsk_jvmti_RedefineClasses_redefclass031_makeRedefinition(JNIEnv *env,
168204
else if (vrb == 1)
169205
printf("Check #1 PASSED: RedefineClasses() is successfully done\n");
170206

171-
set_watch_ev(0); /* again ignore JVMTI events */
207+
set_watch_ev(env, 0); /* again ignore JVMTI events */
172208

173209
if (gen_ev) {
174210
printf("TEST FAILED: %d unexpected JVMTI events were generated by the function RedefineClasses()\n",

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI01/ji01t001/TestDescription.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,6 +42,6 @@
4242
*
4343
* @library /vmTestbase
4444
* /test/lib
45-
* @run main/othervm/native -agentlib:ji01t001 nsk.jvmti.scenarios.jni_interception.JI01.ji01t001
45+
* @run main/othervm/native -agentlib:ji01t001=-verbose nsk.jvmti.scenarios.jni_interception.JI01.ji01t001
4646
*/
4747

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI01/ji01t001/ji01t001.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -45,6 +45,9 @@ static jrawMonitorID eventLock;
4545
static jvmtiEventCallbacks callbacks;
4646
static jint result = NSK_STATUS_PASSED;
4747

48+
// test thread
49+
static jthread testThread = NULL;
50+
4851
/* the original JNI function table */
4952
static jniNativeInterface *orig_jni_functions = NULL;
5053

@@ -54,10 +57,27 @@ static jniNativeInterface *redir_jni_functions = NULL;
5457
/* number of the redirected JNI function calls */
5558
static volatile int fnd_calls = 0;
5659

60+
void setTestThread(JNIEnv *env) {
61+
jthread curThread = NULL;
62+
NSK_JVMTI_VERIFY(jvmti->GetCurrentThread(&curThread));
63+
testThread = env->NewGlobalRef(curThread);
64+
}
65+
66+
void resetTestThread(JNIEnv *env) {
67+
env->DeleteGlobalRef(testThread);
68+
testThread = NULL;
69+
}
70+
71+
bool isOnTestThread(JNIEnv *env) {
72+
jthread curThread = NULL;
73+
NSK_JVMTI_VERIFY(jvmti->GetCurrentThread(&curThread));
74+
return env->IsSameObject(testThread, curThread);
75+
}
76+
5777
/* ====================================================================== */
5878
/** redirected JNI functions **/
5979
jclass JNICALL MyFindClass(JNIEnv *env, const char *name) {
60-
if (isThreadExpected(jvmti, NULL)) {
80+
if (isOnTestThread(env) && strcmp(name, classSig) == 0) {
6181
fnd_calls++;
6282

6383
NSK_DISPLAY1("MyFindClass: the function was called successfully: number of calls so far = %d\n", fnd_calls);
@@ -192,6 +212,9 @@ static void checkCall(JNIEnv *env
192212
{
193213
jclass cls;
194214

215+
setTestThread(env);
216+
fnd_calls = 0;
217+
195218
NSK_TRACE(
196219
(cls = env->FindClass(classSig))
197220
);
@@ -200,23 +223,18 @@ static void checkCall(JNIEnv *env
200223
env->ExceptionClear()
201224
);
202225

203-
// The check should pass if the actual number of invocations is not less that the expected number (fnd_calls >= exFndCalls).
226+
resetTestThread(env);
227+
228+
// The check should pass if the actual number of invocations is the same as the expected number (fnd_calls == exFndCalls).
204229
// If the invocation is not expected (exFndCalls == 0), fnd_calls should be also == 0.
205-
if ((exFndCalls > 0 && fnd_calls >= exFndCalls) || (fnd_calls == exFndCalls)) {
230+
if (fnd_calls == exFndCalls) {
206231
NSK_DISPLAY5("CHECK PASSED: %s: the %s JNI function FindClass() has been %s during %s phase\n\t%d intercepted call(s) as expected\n"
207232
, callBackFunc
208233
, (step == 1) ? "tested" : "original"
209234
, (step == 1) ? "redirected" : "restored"
210235
, msg
211236
, fnd_calls
212237
);
213-
214-
if (fnd_calls != exFndCalls) {
215-
NSK_COMPLAIN2("WARNING: the number of occured calls (%d) exceeds the expected number of calls (%d).\n"
216-
, fnd_calls
217-
, exFndCalls
218-
);
219-
}
220238
} else {
221239
result = NSK_STATUS_FAILED;
222240

@@ -244,12 +262,10 @@ VMInit(jvmtiEnv *jvmti, JNIEnv *env, jthread thread) {
244262
);
245263

246264
// check JNI function table interception
247-
fnd_calls = 0;
248265
NSK_TRACE(doRedirect(jvmti, phase));
249266
NSK_TRACE(checkCall(env, 1, "VMInit", TranslatePhase(phase), 1));
250267

251268
// check restored JNI function table
252-
fnd_calls = 0;
253269
NSK_TRACE(doRestore(jvmti));
254270
NSK_TRACE(checkCall(env, 2, "VMInit", TranslatePhase(phase), 0));
255271

@@ -268,12 +284,10 @@ VMDeath(jvmtiEnv *jvmti, JNIEnv *env) {
268284
);
269285

270286
// check JNI function table interception
271-
fnd_calls = 0;
272287
NSK_TRACE(doRedirect(jvmti, phase));
273288
NSK_TRACE(checkCall(env, 1, "VMDeath", TranslatePhase(phase), 1));
274289

275290
// check restored JNI function table
276-
fnd_calls = 0;
277291
NSK_TRACE(doRestore(jvmti));
278292
NSK_TRACE(checkCall(env, 2, "VMDeath", TranslatePhase(phase), 0));
279293

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI03/ji03t003/TestDescription.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -41,6 +41,6 @@
4141
*
4242
* @library /vmTestbase
4343
* /test/lib
44-
* @run main/othervm/native -agentlib:ji03t003 nsk.jvmti.scenarios.jni_interception.JI03.ji03t003
44+
* @run main/othervm/native -agentlib:ji03t003=-verbose nsk.jvmti.scenarios.jni_interception.JI03.ji03t003 -verbose
4545
*/
4646

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/jni_interception/JI03/ji03t003/ji03t003.cpp

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,8 @@ extern "C" {
3939
static jvmtiEnv *jvmti = NULL;
4040
static jint result = PASSED;
4141
static int verbose = 0;
42+
// test thread
43+
static jthread testThread = NULL;
4244

4345
static const char *javaField = "exc";
4446
static const char *excClassSig =
@@ -55,14 +57,37 @@ int throw_calls = 0;
5557
int thrownew_calls = 0;
5658
int excoccur_calls = 0;
5759

60+
void setTestThread(JNIEnv *env) {
61+
jthread curThread = NULL;
62+
NSK_JVMTI_VERIFY(jvmti->GetCurrentThread(&curThread));
63+
testThread = env->NewGlobalRef(curThread);
64+
}
65+
66+
void resetTestThread(JNIEnv *env) {
67+
env->DeleteGlobalRef(testThread);
68+
testThread = NULL;
69+
}
70+
71+
bool isOnTestThread(JNIEnv *env) {
72+
jthread curThread = NULL;
73+
NSK_JVMTI_VERIFY(jvmti->GetCurrentThread(&curThread));
74+
return env->IsSameObject(testThread, curThread);
75+
}
76+
5877
/** redirected JNI functions **/
5978
jint JNICALL MyThrow(JNIEnv *env, jthrowable thrw) {
6079
jint res;
6180

62-
throw_calls++;
63-
if (verbose)
64-
printf("\nMyThrow: the function called successfully: number of calls=%d\n",
65-
throw_calls);
81+
if (isOnTestThread(env)) {
82+
throw_calls++;
83+
if (verbose) {
84+
printf("\nMyThrow: the function called successfully: number of calls=%d\n", throw_calls);
85+
}
86+
} else {
87+
if (verbose) {
88+
printf("\nMyThrow: the function called on non-test thread, ignoring\n");
89+
}
90+
}
6691

6792
res = orig_jni_functions->Throw(env, thrw);
6893

@@ -74,10 +99,16 @@ jint JNICALL MyThrow(JNIEnv *env, jthrowable thrw) {
7499
jint JNICALL MyThrowNew(JNIEnv *env, jclass cls, const char *msg) {
75100
jint res;
76101

77-
thrownew_calls++;
78-
if (verbose)
79-
printf("\nMyThrowNew: the function called successfully: number of calls=%d\n",
80-
thrownew_calls);
102+
if (isOnTestThread(env)) {
103+
thrownew_calls++;
104+
if (verbose) {
105+
printf("\nMyThrowNew: the function called successfully: number of calls=%d\n", thrownew_calls);
106+
}
107+
} else {
108+
if (verbose) {
109+
printf("\nMyThrowNew: the function called on non-test thread, ignoring\n");
110+
}
111+
}
81112

82113
res = orig_jni_functions->ThrowNew(env, cls, msg);
83114

@@ -87,11 +118,15 @@ jint JNICALL MyThrowNew(JNIEnv *env, jclass cls, const char *msg) {
87118
}
88119

89120
jthrowable JNICALL MyExceptionOccurred(JNIEnv *env) {
90-
if (isThreadExpected(jvmti, NULL)) {
121+
if (isOnTestThread(env)) {
91122
excoccur_calls++;
92-
if (verbose)
93-
printf("\nMyExceptionOccurred: the function called successfully: number of calls=%d\n",
94-
excoccur_calls);
123+
if (verbose) {
124+
printf("\nMyExceptionOccurred: the function called successfully: number of calls=%d\n", excoccur_calls);
125+
}
126+
} else {
127+
if (verbose) {
128+
printf("\nMyExceptionOccurred: the function called on non-test thread, ignoring\n");
129+
}
95130
}
96131

97132
return orig_jni_functions->ExceptionOccurred(env);
@@ -284,6 +319,8 @@ Java_nsk_jvmti_scenarios_jni_1interception_JI03_ji03t003_check(JNIEnv *env, jobj
284319
javaField);
285320
thrw = env->GetObjectClass(thrwObj);
286321

322+
setTestThread(env);
323+
287324
/* 1: check the JNI function table interception */
288325
if (verbose)
289326
printf("\na) Checking the JNI function table interception ...\n");
@@ -301,6 +338,8 @@ Java_nsk_jvmti_scenarios_jni_1interception_JI03_ji03t003_check(JNIEnv *env, jobj
301338
env->DeleteLocalRef(thrw);
302339
env->DeleteLocalRef(thrwObj);
303340

341+
resetTestThread(env);
342+
304343
return result;
305344
}
306345

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/multienv/MA10/ma10t001/TestDescription.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,8 +39,8 @@
3939
* @library /vmTestbase
4040
* /test/lib
4141
* @run main/othervm/native
42-
* -agentlib:ma10t001=-waittime=5
43-
* -agentlib:ma10t001a=-waittime=5
42+
* -agentlib:ma10t001=-waittime=5,-verbose
43+
* -agentlib:ma10t001a=-waittime=5,-verbose
4444
* nsk.jvmti.scenarios.multienv.MA10.ma10t001
4545
*/
4646

0 commit comments

Comments
 (0)