1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
/*
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <string.h>
#include "jvmti.h"
#include "agent_common.hpp"
#include "jni_tools.hpp"
#include "jvmti_tools.hpp"
extern "C" {
/* ============================================================================= */
static jlong timeout = 0;
#define INFO_NONE 0x00
#define INFO_ALL 0xFF
#define INFO_OBJREF 0x01
#define INFO_STACKREF 0x02
#define INFO_HEAPROOT 0x04
#define INFO_HEAPOBJ 0x08
#define INFO_TAGGED 0x10
static unsigned int info = INFO_NONE;
#define DEBUGEE_CLASS_NAME "nsk/jvmti/IterateOverHeap/iterheap001"
#define ROOT_OBJECT_CLASS_NAME "nsk/jvmti/IterateOverHeap/iterheap001RootTestedClass"
#define ROOT_OBJECT_CLASS_SIG "L" ROOT_OBJECT_CLASS_NAME ";"
#define CHAIN_OBJECT_CLASS_NAME "nsk/jvmti/IterateOverHeap/iterheap001TestedClass"
#define CHAIN_OBJECT_CLASS_SIG "L" CHAIN_OBJECT_CLASS_NAME ";"
#define OBJECT_FIELD_NAME "object"
#define REACHABLE_CHAIN_FIELD_NAME "reachableChain"
#define UNREACHABLE_CHAIN_FIELD_NAME "unreachableChain"
#define TAIL_FIELD_NAME "tail"
#define DEFAULT_CHAIN_LENGTH 4
typedef struct ObjectDescStruct {
jlong tag;
jint found;
int collected;
} ObjectDesc;
static int chainLength = 0;
static int objectsCount = 0;
static ObjectDesc* objectDescList = nullptr;
static volatile int foundUntagged = 0;
static int fakeUserData = 0;
static int userDataError = 0;
/* ============================================================================= */
/** Obtain chain of tested objects and tag them recursively. */
static int getChainObjects(jvmtiEnv* jvmti, JNIEnv* jni, jobject firstObject,
jfieldID firstField, const char firstFieldName[],
jfieldID nextField, const char nextFieldName[],
int count, ObjectDesc objectDescList[],
jlong tag, int reachable) {
int success = NSK_TRUE;
jobject obj = nullptr;
jlong objTag = (!reachable ? -tag : (tag % 2 != 0) ? tag : (jlong)0);
if (count <= 0)
return NSK_TRUE;
count--;
tag++;
if (!NSK_JNI_VERIFY(jni, (obj =
jni->GetObjectField(firstObject, firstField)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
objectDescList[count].tag = objTag;
if (!NSK_JVMTI_VERIFY(
jvmti->SetTag(obj, objTag))) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY2(" tag=%-5ld object=0x%p\n", (long)objTag, (void*)obj);
if (!getChainObjects(jvmti, jni, obj, nextField, nextFieldName,
nextField, nextFieldName,
count, objectDescList, tag, reachable)) {
return NSK_FALSE;
}
NSK_TRACE(jni->DeleteLocalRef(obj));
return success;
}
/** Obtain all tested objects from debugee class and tag them recursively. */
static int getTestedObjects(jvmtiEnv* jvmti, JNIEnv* jni, int chainLength,
int *objectsCount, ObjectDesc* *objectDescList) {
jclass debugeeClass = nullptr;
jclass rootObjectClass = nullptr;
jclass chainObjectClass = nullptr;
jfieldID objectField = nullptr;
jfieldID reachableChainField = nullptr;
jfieldID unreachableChainField = nullptr;
jfieldID tailField = nullptr;
jobject rootObject = nullptr;
jlong chainObjectTag = 100;
*objectsCount = 2 * chainLength;
NSK_DISPLAY1("Allocate memory for objects list: %d objects\n", *objectsCount);
if (!NSK_JVMTI_VERIFY(jvmti->Allocate((*objectsCount * sizeof(ObjectDesc)), (unsigned char**)objectDescList))) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... allocated array: 0x%p\n", (void*)objectDescList);
{
int k;
for (k = 0; k < *objectsCount; k++) {
(*objectDescList)[k].tag = 0;
(*objectDescList)[k].found = 0;
(*objectDescList)[k].collected = 0;
}
}
NSK_DISPLAY1("Find debugee class: %s\n", DEBUGEE_CLASS_NAME);
if (!NSK_JNI_VERIFY(jni, (debugeeClass =
jni->FindClass(DEBUGEE_CLASS_NAME)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... found class: 0x%p\n", (void*)debugeeClass);
NSK_DISPLAY1("Find root object class: %s\n", ROOT_OBJECT_CLASS_NAME);
if (!NSK_JNI_VERIFY(jni, (rootObjectClass =
jni->FindClass(ROOT_OBJECT_CLASS_NAME)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... found class: 0x%p\n", (void*)rootObjectClass);
NSK_DISPLAY1("Find chain object class: %s\n", CHAIN_OBJECT_CLASS_NAME);
if (!NSK_JNI_VERIFY(jni, (chainObjectClass =
jni->FindClass(CHAIN_OBJECT_CLASS_NAME)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... found class: 0x%p\n", (void*)chainObjectClass);
NSK_DISPLAY1("Find static field in debugee class: %s\n", OBJECT_FIELD_NAME);
if (!NSK_JNI_VERIFY(jni, (objectField =
jni->GetStaticFieldID(debugeeClass, OBJECT_FIELD_NAME, ROOT_OBJECT_CLASS_SIG)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... got fieldID: 0x%p\n", (void*)objectField);
NSK_DISPLAY1("Find instance field in root object class: %s\n", REACHABLE_CHAIN_FIELD_NAME);
if (!NSK_JNI_VERIFY(jni, (reachableChainField =
jni->GetFieldID(rootObjectClass, REACHABLE_CHAIN_FIELD_NAME, CHAIN_OBJECT_CLASS_SIG)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... got fieldID: 0x%p\n", (void*)reachableChainField);
NSK_DISPLAY1("Find instance field in root object class: %s\n", UNREACHABLE_CHAIN_FIELD_NAME);
if (!NSK_JNI_VERIFY(jni, (unreachableChainField =
jni->GetFieldID(rootObjectClass, UNREACHABLE_CHAIN_FIELD_NAME, CHAIN_OBJECT_CLASS_SIG)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... got fieldID: 0x%p\n", (void*)unreachableChainField);
NSK_DISPLAY1("Find instance field in chain object class: %s\n", TAIL_FIELD_NAME);
if (!NSK_JNI_VERIFY(jni, (tailField =
jni->GetFieldID(chainObjectClass, TAIL_FIELD_NAME, CHAIN_OBJECT_CLASS_SIG)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... got fieldID: 0x%p\n", (void*)tailField);
NSK_DISPLAY1("Get root object from static field: %s\n", OBJECT_FIELD_NAME);
if (!NSK_JNI_VERIFY(jni, (rootObject =
jni->GetStaticObjectField(debugeeClass, objectField)) != nullptr)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" ... got object: 0x%p\n", (void*)rootObject);
NSK_DISPLAY0("Obtain and tag chain objects:\n");
NSK_DISPLAY1(" reachable objects chain: %d objects\n", chainLength);
if (!getChainObjects(jvmti, jni, rootObject,
reachableChainField, REACHABLE_CHAIN_FIELD_NAME,
tailField, TAIL_FIELD_NAME,
chainLength, *objectDescList,
chainObjectTag, NSK_TRUE)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
NSK_DISPLAY1(" unreachable objects chain: %d objects\n", chainLength);
if (!getChainObjects(jvmti, jni, rootObject,
unreachableChainField, UNREACHABLE_CHAIN_FIELD_NAME,
tailField, TAIL_FIELD_NAME,
chainLength, *objectDescList + chainLength,
chainObjectTag, NSK_FALSE)) {
nsk_jvmti_setFailStatus();
return NSK_FALSE;
}
return NSK_TRUE;
}
/** Check if tagged objects were iterated. */
static int checkTestedObjects(jvmtiEnv* jvmti, JNIEnv* jni,
int chainLength, ObjectDesc objectDescList[]) {
int success = NSK_TRUE;
int expectedUntagged = 0;
int i;
NSK_DISPLAY0("Following tagged/untagged objects were iterated:\n");
NSK_DISPLAY0(" reachable objects:\n");
for (i = 0; i < chainLength; i++) {
NSK_DISPLAY2(" tag=%-5ld iterated=%d times\n",
(long)objectDescList[i].tag, objectDescList[i].found);
if (objectDescList[i].found <= 0
&& objectDescList[i].tag != 0) {
NSK_COMPLAIN2("Reachable tagged object was not iterated:\n"
"# tag: %ld\n"
"# iterated: %d times\n",
(long)objectDescList[i].tag,
objectDescList[i].found);
nsk_jvmti_setFailStatus();
}
if (objectDescList[i].tag == 0) {
expectedUntagged++;
}
}
NSK_DISPLAY0(" unreachable objects:\n");
for (i = 0; i < chainLength; i++) {
NSK_DISPLAY3(" tag=%-5ld iterated=%-3d collected=%d times\n",
(long)objectDescList[i + chainLength].tag,
objectDescList[i + chainLength].found,
objectDescList[i + chainLength].collected);
if (objectDescList[i + chainLength].found <= 0
&& objectDescList[i + chainLength].collected <= 0) {
NSK_COMPLAIN2("Not collected unreachable tagged object was not iterated:\n"
"# tag: %ld\n"
"# iterated: %d times\n",
(long)objectDescList[i + chainLength].tag,
objectDescList[i + chainLength].found);
nsk_jvmti_setFailStatus();
}
}
NSK_DISPLAY0(" untagged objects:\n");
NSK_DISPLAY2(" minimum=%-3d iterated=%d objects\n",
expectedUntagged, foundUntagged);
if (foundUntagged < expectedUntagged) {
NSK_COMPLAIN2("Unexpected number of untagged objects were iterated:\n"
"# iterated untagged objects: %d\n"
"# expected at least: %d\n",
foundUntagged, expectedUntagged);
nsk_jvmti_setFailStatus();
}
return NSK_TRUE;
}
/** Release references to the tested objects and free allocated memory. */
static int releaseTestedObjects(jvmtiEnv* jvmti, JNIEnv* jni, int chainLength,
ObjectDesc* objectDescList) {
if (objectDescList != nullptr) {
NSK_DISPLAY1("Deallocate objects list: 0x%p\n", (void*)objectDescList);
if (!NSK_JVMTI_VERIFY(
jvmti->Deallocate((unsigned char*)objectDescList))) {
nsk_jvmti_setFailStatus();
}
}
return NSK_TRUE;
}
/* ============================================================================= */
/** heapRootCallback for heap iterator. */
jvmtiIterationControl JNICALL
heapObjectCallback(jlong class_tag, jlong size, jlong* tag_ptr, void* user_data) {
if (info & INFO_HEAPOBJ) {
NSK_DISPLAY3(" heapObjectCallback: class_tag=%-3ld size=%-3ld *tag_ptr=%-5ld\n",
(long)class_tag, (long)size,
(long)(tag_ptr == nullptr ? (jlong)0 : *tag_ptr));
} else if ((info & INFO_TAGGED) != 0 &&
tag_ptr != nullptr && *tag_ptr != 0) {
NSK_DISPLAY3(" heapObjectCallback: class_tag=%-3ld size=%-3ld *tag_ptr=%-5ld\n",
(long)class_tag, (long)size,
(long)*tag_ptr);
}
if (class_tag != 0) {
NSK_COMPLAIN3("Unexpected class_tag passed to heapObjectCallback:\n"
"# object tag: %ld\n"
"# class_tag: %ld\n"
"# size: %ld\n",
(long)*tag_ptr,
(long)class_tag,
(long)size);
nsk_jvmti_setFailStatus();
}
if (tag_ptr == nullptr) {
NSK_COMPLAIN3("null tag_ptr is passed to heapObjectCallback:\n"
"# tag_ptr: 0x%p\n"
"# class_tag: %ld\n"
"# size: %ld\n",
(void*)tag_ptr,
(long)class_tag,
(long)size);
nsk_jvmti_setFailStatus();
} else {
if (*tag_ptr == 0) {
foundUntagged++;
} else {
int found = 0;
int i;
for (i = 0; i < objectsCount; i++) {
if (*tag_ptr == objectDescList[i].tag) {
found++;
objectDescList[i].found++;
break;
}
}
if (found <= 0) {
NSK_COMPLAIN3("Unknown tagged object passed to heapObjectCallback:\n"
"# tag: %ld\n"
"# class_tag: %ld\n"
"# size: %ld\n",
(long)*tag_ptr,
(long)class_tag,
(long)size);
nsk_jvmti_setFailStatus();
}
}
}
if (user_data != &fakeUserData && !userDataError) {
NSK_COMPLAIN2("Unexpected user_data is passed to heapObjectCallback:\n"
"# expected: 0x%p\n"
"# actual: 0x%p\n",
user_data,
&fakeUserData);
nsk_jvmti_setFailStatus();
userDataError++;
}
return JVMTI_ITERATION_CONTINUE;
}
/* ============================================================================= */
/** Agent algorithm. */
static void JNICALL
agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
NSK_DISPLAY0("Wait for tested objects created\n");
if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
return;
{
NSK_DISPLAY0(">>> Obtain and tag tested objects from debugee class\n");
{
if (!NSK_VERIFY(getTestedObjects(jvmti, jni, chainLength,
&objectsCount, &objectDescList)))
return;
}
NSK_DISPLAY0(">>> Enable OBJECT_FREE event and let debugee to clean links to unreachable objects\n");
{
jvmtiEvent event = JVMTI_EVENT_OBJECT_FREE;
if (!NSK_VERIFY(nsk_jvmti_enableEvents(JVMTI_ENABLE, 1, &event, nullptr)))
return;
if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
return;
if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
return;
}
NSK_DISPLAY0(">>> Iterate over all object in heap with filter JVMTI_HEAP_OBJECT_EITHER\n");
{
if (!NSK_JVMTI_VERIFY(
jvmti->IterateOverHeap(JVMTI_HEAP_OBJECT_EITHER, heapObjectCallback, &fakeUserData))) {
nsk_jvmti_setFailStatus();
return;
}
}
NSK_DISPLAY0(">>> Disable OBJECT_FREE event and check if tagged/untagged objects were iterated:\n");
{
jvmtiEvent event = JVMTI_EVENT_OBJECT_FREE;
if (!NSK_VERIFY(nsk_jvmti_enableEvents(JVMTI_DISABLE, 1, &event, nullptr)))
return;
if (!checkTestedObjects(jvmti, jni, chainLength, objectDescList)) {
nsk_jvmti_setFailStatus();
}
}
NSK_DISPLAY0(">>> Clean used data\n");
{
if (!NSK_VERIFY(releaseTestedObjects(jvmti, jni, chainLength, objectDescList)))
return;
}
}
NSK_DISPLAY0("Let debugee to finish\n");
if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
return;
}
/* ============================================================================= */
JNIEXPORT void JNICALL
callbackObjectFree(jvmtiEnv* jvmti, jlong tag) {
int i;
if (info & INFO_HEAPOBJ) {
NSK_DISPLAY1(" <ObjectFree>: tag=%-5ld\n", tag);
}
if (tag != 0) {
for (i = 0; i < objectsCount; i++) {
if (tag == objectDescList[i].tag) {
objectDescList[i].collected++;
break;
}
}
}
}
/* ============================================================================= */
/** Agent library initialization. */
#ifdef STATIC_BUILD
JNIEXPORT jint JNICALL Agent_OnLoad_iterheap001(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNICALL Agent_OnAttach_iterheap001(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNI_OnLoad_iterheap001(JavaVM *jvm, char *options, void *reserved) {
return JNI_VERSION_1_8;
}
#endif
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
jvmtiEnv* jvmti = nullptr;
if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
return JNI_ERR;
timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
{
const char* infoOpt = nsk_jvmti_findOptionValue("info");
if (infoOpt != nullptr) {
if (strcmp(infoOpt, "none") == 0)
info = INFO_NONE;
else if (strcmp(infoOpt, "all") == 0)
info = INFO_ALL;
else if (strcmp(infoOpt, "objref") == 0)
info = INFO_OBJREF;
else if (strcmp(infoOpt, "stackref") == 0)
info = INFO_STACKREF;
else if (strcmp(infoOpt, "heaproot") == 0)
info = INFO_HEAPROOT;
else if (strcmp(infoOpt, "heapobj") == 0)
info = INFO_HEAPOBJ;
else if (strcmp(infoOpt, "tagged") == 0)
info = INFO_TAGGED;
else {
NSK_COMPLAIN1("Unknown option value: info=%s\n", infoOpt);
return JNI_ERR;
}
}
}
chainLength = nsk_jvmti_findOptionIntValue("objects", DEFAULT_CHAIN_LENGTH);
if (!NSK_VERIFY(chainLength > 0))
return JNI_ERR;
if (!NSK_VERIFY((jvmti =
nsk_jvmti_createJVMTIEnv(jvm, reserved)) != nullptr))
return JNI_ERR;
{
jvmtiCapabilities caps;
memset(&caps, 0, sizeof(caps));
caps.can_tag_objects = 1;
caps.can_generate_object_free_events = 1;
if (!NSK_JVMTI_VERIFY(
jvmti->AddCapabilities(&caps))) {
return JNI_ERR;
}
}
{
jvmtiEventCallbacks eventCallbacks;
memset(&eventCallbacks, 0, sizeof(eventCallbacks));
eventCallbacks.ObjectFree = callbackObjectFree;
if (!NSK_JVMTI_VERIFY(
jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))
return JNI_ERR;
}
if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, nullptr)))
return JNI_ERR;
return JNI_OK;
}
/* ============================================================================= */
}
|