Skip to content

Commit

Permalink
Add error handling for JVMTI functions for Loom
Browse files Browse the repository at this point in the history
Return JVMTI_ERROR_UNSUPPORTED_OPERATION from JVMTI functions that do not
accept virtual threads as their current thread or parameters.

Issue: eclipse-openj9#15183
Signed-off-by: Eric Yang <eric.yang@ibm.com>
  • Loading branch information
EricYangIBM committed Jun 24, 2022
1 parent 05eeda5 commit 27f6d32
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
8 changes: 8 additions & 0 deletions runtime/jvmti/jvmtiThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ jvmtiStopThread(jvmtiEnv* env,

ENSURE_JOBJECT_NON_NULL(exception);

ENSURE_JTHREAD_NON_NULL(thread);
#if JAVA_SPEC_VERSION >= 19
ENSURE_JTHREAD_NOT_VIRTUAL(currentThread, thread);
#endif /* JAVA_SPEC_VERSION >= 19 */

rc = getVMThread(currentThread, thread, &targetThread, FALSE, TRUE);
if (rc == JVMTI_ERROR_NONE) {
omrthread_monitor_enter(targetThread->publicFlagsMutex);
Expand Down Expand Up @@ -805,6 +810,9 @@ jvmtiRunAgentThread(jvmtiEnv* env,
ENSURE_PHASE_LIVE(env);

ENSURE_JTHREAD_NON_NULL(thread);
#if JAVA_SPEC_VERSION >= 19
ENSURE_JTHREAD_NOT_VIRTUAL(currentThread, thread);
#endif /* JAVA_SPEC_VERSION >= 19 */
/* Perhaps verify that thread has not already been started? */
ENSURE_NON_NULL(proc);
if ((priority < JVMTI_THREAD_MIN_PRIORITY) || (priority > JVMTI_THREAD_MAX_PRIORITY)) {
Expand Down
17 changes: 16 additions & 1 deletion runtime/jvmti/jvmtiTimers.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2018 IBM Corp. and others
* Copyright (c) 1991, 2022 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -57,6 +57,10 @@ jvmtiError JNICALL
jvmtiGetCurrentThreadCpuTime(jvmtiEnv* env,
jlong* nanos_ptr)
{
#if JAVA_SPEC_VERSION >= 19
J9JavaVM *vm = JAVAVM_FROM_ENV(env);
J9VMThread *currentThread = NULL;
#endif /* JAVA_SPEC_VERSION >= 19 */
jvmtiError rc;
jlong rv_nanos = 0;

Expand All @@ -67,6 +71,14 @@ jvmtiGetCurrentThreadCpuTime(jvmtiEnv* env,

ENSURE_NON_NULL(nanos_ptr);

#if JAVA_SPEC_VERSION >= 19
rc = getCurrentVMThread(vm, &currentThread);
if (JVMTI_ERROR_NONE != rc) {
goto done;
}
ENSURE_JTHREADOBJECT_NOT_VIRTUAL(currentThread, currentThread->threadObject);
#endif /* JAVA_SPEC_VERSION >= 19 */

rv_nanos = (jlong)omrthread_get_self_cpu_time(omrthread_self());
rc = JVMTI_ERROR_NONE;

Expand Down Expand Up @@ -131,6 +143,9 @@ jvmtiGetThreadCpuTime(jvmtiEnv* env,
ENSURE_NON_NULL(nanos_ptr);
rv_nanos = (jlong)omrthread_get_cpu_time(omrthread_self());
} else {
#if JAVA_SPEC_VERSION >= 19
ENSURE_JTHREAD_NOT_VIRTUAL(currentThread, thread);
#endif /* JAVA_SPEC_VERSION >= 19 */
rc = getVMThread(currentThread, thread, &targetThread, TRUE, TRUE);
if (rc == JVMTI_ERROR_NONE) {
if (nanos_ptr == NULL) {
Expand Down
3 changes: 3 additions & 0 deletions runtime/oti/j9.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ static const struct { \
#define J9_IS_HIDDEN_METHOD(method) \
((NULL != (method)) && (J9ROMCLASS_IS_ANON_OR_HIDDEN(J9_CLASS_FROM_METHOD((method))->romClass) || J9_ARE_ANY_BITS_SET(J9_ROM_METHOD_FROM_RAM_METHOD((method))->modifiers, J9AccMethodFrameIteratorSkip)))

#define IS_VIRTUAL_THREAD(vmThread, object) \
isSameOrSuperClassOf(J9VMJAVALANGBASEVIRTUALTHREAD_OR_NULL(((vmThread)->javaVM)), J9OBJECT_CLAZZ((vmThread), (object)))

#if defined(OPENJ9_BUILD)
#define J9_SHARED_CACHE_DEFAULT_BOOT_SHARING(vm) TRUE
#else /* defined(OPENJ9_BUILD) */
Expand Down
22 changes: 21 additions & 1 deletion runtime/oti/jvmtiInternal.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2021 IBM Corp. and others
* Copyright (c) 1991, 2022 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -482,6 +482,22 @@ typedef struct jvmtiGcp_translation {
} \
} while(0)

#if JAVA_SPEC_VERSION >= 19
#define ENSURE_JTHREAD_NOT_VIRTUAL(vmThread, jthrd) \
do { \
if (IS_VIRTUAL_THREAD((vmThread), J9_JNI_UNWRAP_REFERENCE(jthrd))) { \
JVMTI_ERROR(JVMTI_ERROR_UNSUPPORTED_OPERATION); \
} \
} while(0)

#define ENSURE_JTHREADOBJECT_NOT_VIRTUAL(vmThread, jthrdObject) \
do { \
if (IS_VIRTUAL_THREAD((vmThread), (jthrdObject))) { \
JVMTI_ERROR(JVMTI_ERROR_UNSUPPORTED_OPERATION); \
} \
} while(0)
#endif /* JAVA_SPEC_VERSION >= 19 */

#else

#define ENSURE_PHASE_START_OR_LIVE(env)
Expand All @@ -499,6 +515,10 @@ typedef struct jvmtiGcp_translation {
#define ENSURE_JTHREADGROUP_NON_NULL(var)
#define ENSURE_VALID_HEAP_OBJECT_FILTER(var)
#define ENSURE_MONITOR_NON_NULL(var)
#if JAVA_SPEC_VERSION >= 19
#define ENSURE_JTHREAD_NOT_VIRTUAL(vmThread, jthrd)
#define ENSURE_JTHREADOBJECT_NOT_VIRTUAL(vmThread, jthrdObject)
#endif /* JAVA_SPEC_VERSION >= 19 */

#endif

Expand Down
1 change: 1 addition & 0 deletions runtime/oti/vmconstantpool.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-excepti
<classref name="java/lang/IllegalThreadStateException"/>
<classref name="java/lang/Thread"/>
<classref name="java/lang/Thread$FieldHolder" versions="19-"/>
<classref name="java/lang/BaseVirtualThread" versions="19-"/>
<classref name="java/lang/ArithmeticException"/>
<classref name="java/lang/ThreadGroup"/>
<classref name="java/lang/InterruptedException"/>
Expand Down

0 comments on commit 27f6d32

Please sign in to comment.