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"

Reviewed-by: cjplummer, sspitsyn
  • Loading branch information
Alex Menkov committed Dec 18, 2021
1 parent 6f0e8da commit 63e4303
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

3 comments on commit 63e4303

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@TheRealMDoerr
Copy link
Contributor

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 63e4303 Apr 25, 2022

Choose a reason for hiding this comment

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

@TheRealMDoerr the backport was successfully created on the branch TheRealMDoerr-backport-63e43030 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 63e43030 from the openjdk/jdk repository.

The commit being backported was authored by Alex Menkov on 18 Dec 2021 and was reviewed by Chris Plummer and Serguei Spitsyn.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev TheRealMDoerr-backport-63e43030:TheRealMDoerr-backport-63e43030
$ git checkout TheRealMDoerr-backport-63e43030
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev TheRealMDoerr-backport-63e43030

Please sign in to comment.