Skip to content

Commit 3924a28

Browse files
author
Alex Menkov
committed
8371083: FollowReferences reports non-class objects as JVMTI_HEAP_REFERENCE_SYSTEM_CLASS
Reviewed-by: lmesnik, sspitsyn
1 parent 58b601a commit 3924a28

File tree

3 files changed

+209
-3
lines changed

3 files changed

+209
-3
lines changed

src/hotspot/share/prims/jvmtiTagMap.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,39 @@ class SimpleRootsClosure : public OopClosure {
21902190
virtual void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
21912191
};
21922192

2193+
// A supporting closure used to process ClassLoaderData roots.
2194+
class CLDRootsClosure: public OopClosure {
2195+
private:
2196+
bool _continue;
2197+
public:
2198+
CLDRootsClosure(): _continue(true) {}
2199+
2200+
inline bool stopped() {
2201+
return !_continue;
2202+
}
2203+
2204+
void do_oop(oop* obj_p) {
2205+
if (stopped()) {
2206+
return;
2207+
}
2208+
2209+
oop o = NativeAccess<AS_NO_KEEPALIVE>::oop_load(obj_p);
2210+
// ignore null
2211+
if (o == nullptr) {
2212+
return;
2213+
}
2214+
2215+
jvmtiHeapReferenceKind kind = JVMTI_HEAP_REFERENCE_OTHER;
2216+
if (o->klass() == vmClasses::Class_klass()) {
2217+
kind = JVMTI_HEAP_REFERENCE_SYSTEM_CLASS;
2218+
}
2219+
2220+
// invoke the callback
2221+
_continue = CallbackInvoker::report_simple_root(kind, o);
2222+
}
2223+
virtual void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
2224+
};
2225+
21932226
// A supporting closure used to process JNI locals
21942227
class JNILocalRootsClosure : public OopClosure {
21952228
private:
@@ -2776,10 +2809,10 @@ inline bool VM_HeapWalkOperation::collect_simple_roots() {
27762809
}
27772810

27782811
// Preloaded classes and loader from the system dictionary
2779-
blk.set_kind(JVMTI_HEAP_REFERENCE_SYSTEM_CLASS);
2780-
CLDToOopClosure cld_closure(&blk, ClassLoaderData::_claim_none);
2812+
CLDRootsClosure cld_roots_closure;
2813+
CLDToOopClosure cld_closure(&cld_roots_closure, ClassLoaderData::_claim_none);
27812814
ClassLoaderDataGraph::always_strong_cld_do(&cld_closure);
2782-
if (blk.stopped()) {
2815+
if (cld_roots_closure.stopped()) {
27832816
return false;
27842817
}
27852818

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2025, 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+
/*
25+
* @test
26+
* @bug 8371083
27+
* @summary Verify FollowReferences does not report non-classes roots as JVMTI_HEAP_REFERENCE_SYSTEM_CLASS
28+
* @requires vm.jvmti
29+
* @run main/othervm/native -agentlib:KindSystemClass
30+
* KindSystemClass
31+
*/
32+
33+
public class KindSystemClass {
34+
35+
static native int tagSysClasses();
36+
static native Object[] getObjectsWithTags();
37+
38+
public static void main(String[] args) throws Exception {
39+
System.loadLibrary("KindSystemClass");
40+
41+
int tagged = tagSysClasses();
42+
System.out.println("Tagged " + tagged + " classes");
43+
44+
Object[] objs = getObjectsWithTags();
45+
System.out.println("Tagged objects (total " + objs.length + "):");
46+
int nonClassesCnt = 0;
47+
for (int i = 0; i < objs.length; i++) {
48+
Object obj = objs[i];
49+
String s;
50+
if (obj instanceof Class cls) {
51+
s = "OK: " + cls;
52+
} else {
53+
nonClassesCnt++;
54+
s = "ERROR, not a class: " + obj;
55+
}
56+
System.out.println("[" + i + "] " + s);
57+
}
58+
if (nonClassesCnt != 0) {
59+
throw new RuntimeException("Found " + nonClassesCnt + " non-classes");
60+
}
61+
}
62+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2025, 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 <jvmti.h>
25+
#include "jvmti_common.hpp"
26+
27+
static jvmtiEnv *jvmti = nullptr;
28+
static int class_counter = 0;
29+
static int other_counter = 0;
30+
31+
static jint JNICALL
32+
heap_reference_callback(jvmtiHeapReferenceKind reference_kind,
33+
const jvmtiHeapReferenceInfo* reference_info,
34+
jlong class_tag,
35+
jlong referrer_class_tag,
36+
jlong size,
37+
jlong* tag_ptr,
38+
jlong* referrer_tag_ptr,
39+
jint length,
40+
void* user_data) {
41+
switch (reference_kind) {
42+
case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS:
43+
*tag_ptr = ++class_counter;
44+
break;
45+
case JVMTI_HEAP_REFERENCE_OTHER:
46+
++other_counter;
47+
break;
48+
default:
49+
break;
50+
}
51+
return JVMTI_VISIT_OBJECTS;
52+
}
53+
54+
extern "C" JNIEXPORT jint JNICALL
55+
Java_KindSystemClass_tagSysClasses(JNIEnv* jni, jclass clazz) {
56+
jvmtiHeapCallbacks callbacks = {};
57+
callbacks.heap_reference_callback = heap_reference_callback;
58+
59+
jvmtiError err = jvmti->FollowReferences(0 /* filter nothing */,
60+
nullptr /* no class filter */,
61+
nullptr /* no initial object, follow roots */,
62+
&callbacks,
63+
nullptr);
64+
check_jvmti_error(err, "FollowReferences failed");
65+
66+
LOG("JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: %d, JVMTI_HEAP_REFERENCE_OTHER: %d\n", class_counter, other_counter);
67+
68+
return class_counter;
69+
}
70+
71+
extern "C" JNIEXPORT jobjectArray JNICALL
72+
Java_KindSystemClass_getObjectsWithTags(JNIEnv* jni, jclass clazz) {
73+
// request tagged objects with tags 1..class_counter
74+
jlong* tags = nullptr;
75+
jvmtiError err = jvmti->Allocate(class_counter * sizeof(jlong), (unsigned char**)&tags);
76+
check_jvmti_error(err, "Allocate failed");
77+
78+
for (int i = 0; i < class_counter; i++) {
79+
tags[i] = i + 1;
80+
}
81+
82+
jint count = 0;
83+
jobject* objects = nullptr;
84+
85+
err = jvmti->GetObjectsWithTags(class_counter, tags,
86+
&count, &objects, nullptr);
87+
check_jvmti_error(err, "GetObjectsWithTags failed");
88+
89+
jclass object_klass = jni->FindClass("java/lang/Object");
90+
jobjectArray array = jni->NewObjectArray(count, object_klass, nullptr);
91+
92+
for (jint i = 0; i < count; i++) {
93+
jni->SetObjectArrayElement(array, i, objects[i]);
94+
}
95+
96+
deallocate(jvmti, jni, objects);
97+
98+
return array;
99+
}
100+
101+
extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
102+
if (vm->GetEnv(reinterpret_cast<void**>(&jvmti), JVMTI_VERSION) != JNI_OK || !jvmti) {
103+
LOG("Could not initialize JVMTI\n");
104+
abort();
105+
}
106+
jvmtiCapabilities capabilities;
107+
memset(&capabilities, 0, sizeof(capabilities));
108+
capabilities.can_tag_objects = 1;
109+
check_jvmti_error(jvmti->AddCapabilities(&capabilities), "adding capabilities");
110+
return JVMTI_ERROR_NONE;
111+
}

0 commit comments

Comments
 (0)