-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8365937: post_method_exit might incorrectly set was_popped_by_exception and value in the middle of stack unwinding #26886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lmesnik
wants to merge
29
commits into
openjdk:master
Choose a base branch
from
lmesnik:8365937
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+331
−18
Open
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
47a32e8
8365192: post_meth_exit should be in vm state when calling get_jvmti_…
lmesnik 7cce73e
simplified after feedback
lmesnik 977c9eb
fixed name
lmesnik 31046ba
wong phase
lmesnik 255c0ba
added _
lmesnik 62c2bcd
Merge branch 'master' of https://github.com/openjdk/jdk into 8365192
lmesnik 2ec6068
Merge branch 'master' of https://github.com/openjdk/jdk into 8365192
lmesnik 7c2ac4e
Test added.
lmesnik d9d21af
The oop preservation and exception handling has been fixed.
lmesnik 05e797c
test fixed.
lmesnik 2e96eb8
added assertion and removed comment.
lmesnik 32b0858
moved JRT_BLOCK
lmesnik 0008f72
NULL replaced
lmesnik 320b93e
Update test/hotspot/jtreg/serviceability/jvmti/events/MethodExit/Exce…
lmesnik 6960b1a
added comments to the test
lmesnik 67d87db
Update src/hotspot/share/prims/jvmtiExport.cpp
lmesnik 106e5a1
Update test/hotspot/jtreg/serviceability/jvmti/events/MethodExit/Exce…
lmesnik ce13275
test renamed.
lmesnik d56e353
fixed comment.
lmesnik a659dc2
updated to fix 8365937
lmesnik bc08fd0
bugid fixed:
lmesnik b37325e
fixed ident
lmesnik e8343e0
more comments in the test
lmesnik d9319d9
assertion added.
lmesnik c046e8a
Merge branch 'master' of https://github.com/openjdk/jdk into 8365937
lmesnik c5415aa
comment fixed
lmesnik 4e05639
Apply suggestions from code review
lmesnik bf2c90f
Update test/hotspot/jtreg/serviceability/jvmti/events/MethodExit/Pend…
lmesnik 0960797
Apply suggestions from code review
lmesnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...eability/jvmti/events/MethodExit/PendingException/TestMethodExitWithPendingException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2025, 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. | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @summary Test verifies that MethodExit event is correctly posted | ||
* if method is called while there is a pending exception on this thread. | ||
* | ||
* @bug 8365937 | ||
* @run main/othervm/native -agentlib:TestMethodExitWithPendingException TestMethodExitWithPendingException | ||
*/ | ||
public class TestMethodExitWithPendingException { | ||
|
||
private static native void enable(); | ||
private static native void disableAndCheck(); | ||
|
||
static String exceptionExit() { | ||
throw new RuntimeException("MyRuntimeException"); | ||
} | ||
|
||
|
||
// Called from ExceptionExit MethodExit callback via JNI. | ||
// So MyRuntimeException is thrown already and hasn't been caught yet | ||
// when this method is called. | ||
static String upCall() { | ||
return "MyNewString"; | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
System.loadLibrary("TestMethodExitWithPendingException"); | ||
try { | ||
enable(); | ||
exceptionExit(); | ||
} catch (RuntimeException e){ | ||
disableAndCheck(); | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...bility/jvmti/events/MethodExit/PendingException/libTestMethodExitWithPendingException.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (c) 2025, 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 "jvmti.h" | ||
#include "jni.h" | ||
#include "jvmti_common.hpp" | ||
|
||
jvmtiEnv* jvmti_env; | ||
|
||
bool method_exit_posted = false; | ||
// This method exit callback actually works only for 2 methods: | ||
// 1) for ExceptionExit it verifies that method exit | ||
// has been popped by exception and calls 'upCall' method using JNI. | ||
// 2) for upCall method it verifies that event has correct | ||
// return value and was not popped by exception. | ||
// The event callback just exits for all other methods. | ||
static void JNICALL | ||
cbMethodExit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread, jmethodID method, | ||
jboolean was_popped_by_exception, jvalue return_value) { | ||
const char * mname = get_method_name(jvmti, jni, method); | ||
lmesnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (strcmp("upCall", mname) == 0) { | ||
if (was_popped_by_exception) { | ||
fatal(jni, "The method's was_popped_by_exception value is incorrect."); | ||
} | ||
jstring upcall_result = (jstring) return_value.l; | ||
const char *str = jni->GetStringUTFChars(upcall_result, nullptr); | ||
if (str == nullptr) { | ||
fatal(jni ,"Failed to convert Java string to C string."); | ||
lmesnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if (strcmp("MyNewString", str) != 0) { | ||
fatal(jni, "The upCall result value is incorrect."); | ||
} | ||
method_exit_posted = true; | ||
} | ||
if (strcmp("exceptionExit", mname) != 0) { | ||
return; | ||
} | ||
if (!was_popped_by_exception) { | ||
fatal(jni, "Should have was_popped_by_esxception = true."); | ||
} | ||
jclass main_class = jni->FindClass("TestMethodExitWithPendingException"); | ||
if (main_class == nullptr) { | ||
fatal(jni,"Can't find TestMethodExitWithPendingException class."); | ||
lmesnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
jmethodID upcall_method = jni->GetStaticMethodID(main_class, | ||
"upCall", "()Ljava/lang/String;"); | ||
if (upcall_method == nullptr) { | ||
fatal(jni,"Can't find upCall method."); | ||
lmesnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// Call 'upCall' method while current thread has exception | ||
// that has been thrown but hasn't been caught yet. | ||
jstring upcall_result = (jstring) jni->CallStaticObjectMethod(main_class, upcall_method); | ||
const char *str = jni->GetStringUTFChars(upcall_result, nullptr); | ||
if (str == nullptr) { | ||
fatal(jni ,"Failed to convert Java string to C string."); | ||
lmesnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
if (strcmp("MyNewString", str) != 0) { | ||
fatal(jni, "The upCall result value is incorrect."); | ||
} | ||
jni->ReleaseStringUTFChars(upcall_result, str); | ||
} | ||
|
||
JNIEXPORT jint JNICALL | ||
Agent_OnLoad(JavaVM *vm, char *options, void *reserved) { | ||
jvmtiEnv *jvmti = nullptr; | ||
jint res = vm->GetEnv((void **) &jvmti, JVMTI_VERSION_21); | ||
if (res != JNI_OK) { | ||
return JNI_ERR; | ||
} | ||
jvmtiError err = JVMTI_ERROR_NONE; | ||
jvmtiCapabilities capabilities; | ||
(void) memset(&capabilities, 0, sizeof (capabilities)); | ||
capabilities.can_generate_method_exit_events = true; | ||
err = jvmti->AddCapabilities(&capabilities); | ||
check_jvmti_error(err, "AddCapabilities"); | ||
jvmtiEventCallbacks callbacks; | ||
(void) memset(&callbacks, 0, sizeof (callbacks)); | ||
callbacks.MethodExit = &cbMethodExit; | ||
err = jvmti->SetEventCallbacks(&callbacks, (int) sizeof (jvmtiEventCallbacks)); | ||
check_jvmti_error(err, "SetEventCallbacks"); | ||
jvmti_env = jvmti; | ||
return JNI_OK; | ||
} | ||
|
||
|
||
extern "C" { | ||
|
||
JNIEXPORT void JNICALL | ||
Java_TestMethodExitWithPendingException_enable(JNIEnv *jni, jclass clazz) { | ||
jthread thread = get_current_thread(jvmti_env, jni); | ||
jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, thread); | ||
} | ||
|
||
|
||
JNIEXPORT void JNICALL | ||
Java_TestMethodExitWithPendingException_disableAndCheck(JNIEnv *jni, jclass clazz) { | ||
jthread thread = get_current_thread(jvmti_env, jni); | ||
jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_METHOD_EXIT, thread); | ||
if (!method_exit_posted) { | ||
fatal(jni, "Failed to post method exit event."); | ||
} | ||
} | ||
|
||
} // extern "C" |
50 changes: 50 additions & 0 deletions
50
...jtreg/serviceability/jvmti/events/MethodExit/PoppedByException/TestPoppedByException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2025, 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. | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @run main/othervm/native -agentlib:TestPoppedByException TestPoppedByException | ||
*/ | ||
public class TestPoppedByException { | ||
|
||
private static native void enable(); | ||
private static native void disableAndCheck(); | ||
|
||
static String exceptionExit() { | ||
throw new RuntimeException("MyRuntimeException"); | ||
} | ||
|
||
static String exceptionExitOuter() { | ||
return exceptionExit(); | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
System.loadLibrary("TestPoppedByException"); | ||
try { | ||
enable(); | ||
exceptionExitOuter(); | ||
} catch (RuntimeException e){ | ||
disableAndCheck(); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...reg/serviceability/jvmti/events/MethodExit/PoppedByException/libTestPoppedByException.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2025, 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 "jvmti.h" | ||
#include "jni.h" | ||
#include "jvmti_common.hpp" | ||
|
||
jvmtiEnv* jvmti_env; | ||
bool method_exit_posted = false; | ||
static void JNICALL | ||
cbMethodExit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread, jmethodID method, | ||
jboolean was_popped_by_exception, jvalue return_value) { | ||
const char * mname = get_method_name(jvmti, jni, method); | ||
if (strcmp("exceptionExitOuter", mname) == 0) { | ||
if (!was_popped_by_exception) { | ||
fatal(jni, "The method's was_popped_by_exception value is incorrect."); | ||
} | ||
if (return_value.l != nullptr) { | ||
fatal(jni ,"return_value should be nullptr."); | ||
lmesnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
method_exit_posted = true; | ||
} | ||
} | ||
|
||
JNIEXPORT jint JNICALL | ||
Agent_OnLoad(JavaVM *vm, char *options, void *reserved) { | ||
jvmtiEnv *jvmti = nullptr; | ||
jint res = vm->GetEnv((void **) &jvmti, JVMTI_VERSION_21); | ||
if (res != JNI_OK) { | ||
return JNI_ERR; | ||
} | ||
jvmtiError err = JVMTI_ERROR_NONE; | ||
jvmtiCapabilities capabilities; | ||
(void) memset(&capabilities, 0, sizeof (capabilities)); | ||
capabilities.can_generate_method_exit_events = true; | ||
err = jvmti->AddCapabilities(&capabilities); | ||
check_jvmti_error(err, "AddCapabilities"); | ||
jvmtiEventCallbacks callbacks; | ||
(void) memset(&callbacks, 0, sizeof (callbacks)); | ||
callbacks.MethodExit = &cbMethodExit; | ||
err = jvmti->SetEventCallbacks(&callbacks, (int) sizeof (jvmtiEventCallbacks)); | ||
check_jvmti_error(err, "SetEventCallbacks"); | ||
jvmti_env = jvmti; | ||
return JNI_OK; | ||
} | ||
|
||
|
||
extern "C" { | ||
JNIEXPORT void JNICALL | ||
Java_TestPoppedByException_enable(JNIEnv *jni, jclass clazz) { | ||
jthread thread = get_current_thread(jvmti_env, jni); | ||
jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, thread); | ||
} | ||
|
||
|
||
JNIEXPORT void JNICALL | ||
Java_TestPoppedByException_disableAndCheck(JNIEnv *jni, jclass clazz) { | ||
jthread thread = get_current_thread(jvmti_env, jni); | ||
jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_METHOD_EXIT, thread); | ||
if (!method_exit_posted) { | ||
fatal(jni, "Failed to post method exit event."); | ||
} | ||
printf("The expected method_exit posted.\n"); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.