Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8278519: serviceability/jvmti/FieldAccessWatch/FieldAccessWatch.java …
…failed "assert(handle != __null) failed: JNI handle should not be null"

Backport-of: 63e43030ed1260d14df950342c39a377231a3f40
  • Loading branch information
TheRealMDoerr committed Apr 27, 2022
1 parent 6f109a4 commit 740e6b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, 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
Expand Down Expand Up @@ -57,7 +57,7 @@ public static void main(String[] args) throws Exception {
throw ex;
}

if (!initWatchers(MyList.class, MyList.class.getDeclaredField("items"))) {
if (!initWatchers(MyList.class, MyList.class.getDeclaredField("items"), Thread.currentThread())) {
throw new RuntimeException("Watchers initializations error");
}

Expand Down Expand Up @@ -131,7 +131,7 @@ private static void test(String descr, TestAction action) throws Exception {
log(descr + ": OK");
}

private static native boolean initWatchers(Class cls, Field field);
private static native boolean initWatchers(Class cls, Field field, Thread testThread);
private static native boolean startTest(TestResult results);
private static native void stopTest();

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, 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
Expand Down Expand Up @@ -37,6 +37,7 @@ static jvmtiEnv *jvmti = NULL;
// valid while a test is executed
static jobject testResultObject = NULL;
static jclass testResultClass = NULL;
static jthread testThread = NULL;


static void reportError(const char *msg, int err) {
Expand All @@ -46,6 +47,7 @@ static void reportError(const char *msg, int err) {

// logs the notification and updates currentTestResult
static void handleNotification(JNIEnv *jni_env,
jthread thread,
jmethodID method,
jfieldID field,
jclass field_klass,
Expand All @@ -64,6 +66,10 @@ static void handleNotification(JNIEnv *jni_env,
return;
}

if (!(*jni_env)->IsSameObject(jni_env, thread, testThread)) {
return; // skip events from unexpected threads
}

err = (*jvmti)->GetFieldName(jvmti, field_klass, field, &name, NULL, NULL);
if (err != JVMTI_ERROR_NONE) {
reportError("GetFieldName failed", err);
Expand Down Expand Up @@ -179,7 +185,7 @@ onFieldAccess(jvmtiEnv *jvmti_env,
jobject object,
jfieldID field)
{
handleNotification(jni_env, method, field, field_klass, 0, location);
handleNotification(jni_env, thread, method, field, field_klass, 0, location);
}


Expand All @@ -195,7 +201,7 @@ onFieldModification(jvmtiEnv *jvmti_env,
char signature_type,
jvalue new_value)
{
handleNotification(jni_env, method, field, field_klass, 1, location);
handleNotification(jni_env, thread, method, field, field_klass, 1, location);

if (signature_type == 'L') {
jobject newObject = new_value.l;
Expand Down Expand Up @@ -251,7 +257,7 @@ Agent_OnLoad(JavaVM *jvm, char *options, void *reserved)


JNIEXPORT jboolean JNICALL
Java_FieldAccessWatch_initWatchers(JNIEnv *env, jclass thisClass, jclass cls, jobject field)
Java_FieldAccessWatch_initWatchers(JNIEnv *env, jclass thisClass, jclass cls, jobject field, jthread thread)
{
jfieldID fieldId;
jvmtiError err;
Expand All @@ -275,6 +281,8 @@ Java_FieldAccessWatch_initWatchers(JNIEnv *env, jclass thisClass, jclass cls, jo
return JNI_FALSE;
}

testThread = (jthread)(*env)->NewGlobalRef(env, thread);

return JNI_TRUE;
}

Expand Down

1 comment on commit 740e6b1

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.