11/*
2- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2018, 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
@@ -37,6 +37,7 @@ static jvmtiEnv *jvmti = NULL;
3737// valid while a test is executed
3838static jobject testResultObject = NULL ;
3939static jclass testResultClass = NULL ;
40+ static jthread testThread = NULL ;
4041
4142
4243static void reportError (const char * msg , int err ) {
@@ -46,6 +47,7 @@ static void reportError(const char *msg, int err) {
4647
4748// logs the notification and updates currentTestResult
4849static void handleNotification (JNIEnv * jni_env ,
50+ jthread thread ,
4951 jmethodID method ,
5052 jfieldID field ,
5153 jclass field_klass ,
@@ -64,6 +66,10 @@ static void handleNotification(JNIEnv *jni_env,
6466 return ;
6567 }
6668
69+ if (!(* jni_env )-> IsSameObject (jni_env , thread , testThread )) {
70+ return ; // skip events from unexpected threads
71+ }
72+
6773 err = (* jvmti )-> GetFieldName (jvmti , field_klass , field , & name , NULL , NULL );
6874 if (err != JVMTI_ERROR_NONE ) {
6975 reportError ("GetFieldName failed" , err );
@@ -179,7 +185,7 @@ onFieldAccess(jvmtiEnv *jvmti_env,
179185 jobject object ,
180186 jfieldID field )
181187{
182- handleNotification (jni_env , method , field , field_klass , 0 , location );
188+ handleNotification (jni_env , thread , method , field , field_klass , 0 , location );
183189}
184190
185191
@@ -195,7 +201,7 @@ onFieldModification(jvmtiEnv *jvmti_env,
195201 char signature_type ,
196202 jvalue new_value )
197203{
198- handleNotification (jni_env , method , field , field_klass , 1 , location );
204+ handleNotification (jni_env , thread , method , field , field_klass , 1 , location );
199205
200206 if (signature_type == 'L' ) {
201207 jobject newObject = new_value .l ;
@@ -251,7 +257,7 @@ Agent_OnLoad(JavaVM *jvm, char *options, void *reserved)
251257
252258
253259JNIEXPORT jboolean JNICALL
254- Java_FieldAccessWatch_initWatchers (JNIEnv * env , jclass thisClass , jclass cls , jobject field )
260+ Java_FieldAccessWatch_initWatchers (JNIEnv * env , jclass thisClass , jclass cls , jobject field , jthread thread )
255261{
256262 jfieldID fieldId ;
257263 jvmtiError err ;
@@ -275,6 +281,8 @@ Java_FieldAccessWatch_initWatchers(JNIEnv *env, jclass thisClass, jclass cls, jo
275281 return JNI_FALSE ;
276282 }
277283
284+ testThread = (jthread )(* env )-> NewGlobalRef (env , thread );
285+
278286 return JNI_TRUE ;
279287}
280288
0 commit comments