Skip to content

Commit 0ca86da

Browse files
author
Alex Menkov
committedMay 6, 2021
8266002: vmTestbase/nsk/jvmti/ClassPrepare/classprep001 should skip events for unexpected classes
Reviewed-by: cjplummer, sspitsyn
1 parent 52f1db6 commit 0ca86da

File tree

2 files changed

+65
-34
lines changed

2 files changed

+65
-34
lines changed
 

‎test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassPrepare/classprep001.java

+29-6
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, 2021, 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
@@ -40,8 +40,8 @@ public class classprep001 {
4040
}
4141
}
4242

43-
native static void getReady();
44-
native static int check();
43+
native static void getReady(Thread thread);
44+
native static int check(Thread thread);
4545

4646
public static void main(String args[]) {
4747
args = nsk.share.jvmti.JVMTITest.commonInit(args);
@@ -51,12 +51,26 @@ public static void main(String args[]) {
5151
}
5252

5353
public static int run(String args[], PrintStream out) {
54-
getReady();
54+
Thread otherThread = new Thread(() -> {
55+
new TestClass2().run();
56+
});
57+
58+
getReady(Thread.currentThread());
59+
60+
// should generate the events
5561
new TestClass().run();
56-
return check();
62+
63+
// loading classes on other thread should not generate the events
64+
otherThread.start();
65+
try {
66+
otherThread.join();
67+
} catch (InterruptedException e) {
68+
}
69+
70+
return check(Thread.currentThread());
5771
}
5872

59-
static interface TestInterface {
73+
interface TestInterface {
6074
int constant = Integer.parseInt("10");
6175
void run();
6276
}
@@ -71,4 +85,13 @@ public void run() {
7185
count++;
7286
}
7387
}
88+
89+
interface TestInterface2 {
90+
void run();
91+
}
92+
93+
static class TestClass2 implements TestInterface2 {
94+
public void run() {
95+
}
96+
}
7497
}

‎test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassPrepare/classprep001/classprep001.cpp

+36-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, 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
@@ -62,6 +62,12 @@ static class_info classes[] = {
6262
{ "Lnsk/jvmti/ClassPrepare/classprep001$TestInterface;", EXP_STATUS, 2, 1, 0 },
6363
{ "Lnsk/jvmti/ClassPrepare/classprep001$TestClass;", EXP_STATUS, 3, 2, 1 }
6464
};
65+
// These classes are loaded on a different thread.
66+
// We should not get ClassPrepare events for them.
67+
static const class_info unexpectedClasses[] = {
68+
{ "Lnsk/jvmti/ClassPrepare/classprep001$TestInterface2;", 0, 0, 0, 0 },
69+
{ "Lnsk/jvmti/ClassPrepare/classprep001$TestClass2;", 0, 0, 0, 0}
70+
};
6571

6672
void printStatus(jint status) {
6773
int flags = 0;
@@ -87,6 +93,17 @@ void printStatus(jint status) {
8793
printf(" (0x%x)\n", status);
8894
}
8995

96+
const size_t NOT_FOUND = (size_t)(-1);
97+
98+
size_t findClass(const char *classSig, const class_info *arr, int size) {
99+
for (int i = 0; i < size; i++) {
100+
if (strcmp(classSig, arr[i].sig) == 0) {
101+
return i;
102+
}
103+
}
104+
return NOT_FOUND;
105+
}
106+
90107
void JNICALL ClassPrepare(jvmtiEnv *jvmti_env, JNIEnv *env,
91108
jthread thr, jclass cls) {
92109
jvmtiError err;
@@ -188,19 +205,25 @@ void JNICALL ClassPrepare(jvmtiEnv *jvmti_env, JNIEnv *env,
188205
printf("\n");
189206
}
190207

191-
if (eventsCount >= eventsExpected) {
192-
printf("(#%" PRIuPTR ") too many events: %" PRIuPTR ", expected: %" PRIuPTR "\n",
193-
eventsCount, eventsCount + 1, eventsExpected);
194-
result = STATUS_FAILED;
208+
size_t expectedClassIdx = findClass(inf.sig, classes, sizeof(classes)/sizeof(class_info));
209+
// Test classes loading may cause system classes loading - skip them.
210+
if (expectedClassIdx == NOT_FOUND) {
211+
size_t unexpectedClassIdx = findClass(inf.sig, unexpectedClasses,
212+
sizeof(unexpectedClasses)/sizeof(class_info));
213+
if (unexpectedClassIdx != NOT_FOUND) {
214+
printf("# wrong class: \"%s\"\n", inf.sig);
215+
result = STATUS_FAILED;
216+
}
195217
return;
196218
}
197219

198-
if (inf.sig == NULL || strcmp(inf.sig, classes[eventsCount].sig) != 0) {
199-
printf("(#%" PRIuPTR ") wrong class: \"%s\"",
200-
eventsCount, inf.sig);
201-
printf(", expected: \"%s\"\n", classes[eventsCount].sig);
220+
if (eventsCount != expectedClassIdx) {
221+
printf("(#%" PRIuPTR ") unexpected order: %" PRIuPTR ", expected: %" PRIuPTR "\n",
222+
eventsCount, expectedClassIdx, eventsCount);
202223
result = STATUS_FAILED;
224+
return;
203225
}
226+
204227
if (inf.status != classes[eventsCount].status) {
205228
printf("(#%" PRIuPTR ") wrong status: ", eventsCount);
206229
printStatus(inf.status);
@@ -266,24 +289,16 @@ jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
266289
}
267290

268291
JNIEXPORT void JNICALL
269-
Java_nsk_jvmti_ClassPrepare_classprep001_getReady(JNIEnv *env, jclass cls) {
292+
Java_nsk_jvmti_ClassPrepare_classprep001_getReady(JNIEnv *env, jclass cls, jthread thread) {
270293
jvmtiError err;
271-
jthread prep_thread;
272294

273295
if (jvmti == NULL) {
274296
printf("JVMTI client was not properly loaded!\n");
275297
return;
276298
}
277299

278-
err = jvmti->GetCurrentThread(&prep_thread);
279-
if (err != JVMTI_ERROR_NONE) {
280-
printf("Failed to get current thread: %s (%d)\n", TranslateError(err), err);
281-
result = STATUS_FAILED;
282-
return;
283-
}
284-
285300
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
286-
JVMTI_EVENT_CLASS_PREPARE, prep_thread);
301+
JVMTI_EVENT_CLASS_PREPARE, thread);
287302
if (err == JVMTI_ERROR_NONE) {
288303
eventsExpected = sizeof(classes)/sizeof(class_info);
289304
} else {
@@ -294,23 +309,16 @@ Java_nsk_jvmti_ClassPrepare_classprep001_getReady(JNIEnv *env, jclass cls) {
294309
}
295310

296311
JNIEXPORT jint JNICALL
297-
Java_nsk_jvmti_ClassPrepare_classprep001_check(JNIEnv *env, jclass cls) {
312+
Java_nsk_jvmti_ClassPrepare_classprep001_check(JNIEnv *env, jclass cls, jthread thread) {
298313
jvmtiError err;
299-
jthread prep_thread;
300314

301315
if (jvmti == NULL) {
302316
printf("JVMTI client was not properly loaded!\n");
303317
return STATUS_FAILED;
304318
}
305319

306-
err = jvmti->GetCurrentThread(&prep_thread);
307-
if (err != JVMTI_ERROR_NONE) {
308-
printf("Failed to get current thread: %s (%d)\n", TranslateError(err), err);
309-
return STATUS_FAILED;
310-
}
311-
312320
err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,
313-
JVMTI_EVENT_CLASS_PREPARE, prep_thread);
321+
JVMTI_EVENT_CLASS_PREPARE, thread);
314322
if (err != JVMTI_ERROR_NONE) {
315323
printf("Failed to disable JVMTI_EVENT_CLASS_PREPARE: %s (%d)\n",
316324
TranslateError(err), err);

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on May 6, 2021

@openjdk-notifier[bot]
Please sign in to comment.