Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/jcl/common/jdk_internal_misc_ScopedMemoryAccess.cpp
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2020, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#include "jni.h"
24
#include "jcl.h"
25
#include "jclglob.h"
26
#include "jclprots.h"
27
#include "jcl_internal.h"
28
#include "rommeth.h"
29
#include "omrlinkedlist.h"
30
31
extern "C" {
32
33
#if JAVA_SPEC_VERSION >= 16
34
void JNICALL
35
Java_jdk_internal_misc_ScopedMemoryAccess_registerNatives(JNIEnv *env, jclass clazz)
36
{
37
}
38
39
static UDATA
40
closeScope0FrameWalkFunction(J9VMThread *vmThread, J9StackWalkState *walkState)
41
{
42
if (JNI_FALSE == *(jboolean *)walkState->userData2) {
43
/* scope has been found */
44
return J9_STACKWALK_STOP_ITERATING;
45
}
46
return J9_STACKWALK_KEEP_ITERATING;
47
}
48
49
static void
50
closeScope0OSlotWalkFunction(J9VMThread *vmThread, J9StackWalkState *walkState, j9object_t *slot, const void *stackLocation)
51
{
52
J9Method *method = walkState->method;
53
if (NULL != method) {
54
J9ROMMethod *romMethod = J9_ROM_METHOD_FROM_RAM_METHOD(method);
55
if (NULL != romMethod && J9ROMMETHOD_HAS_EXTENDED_MODIFIERS(romMethod)) {
56
U_32 extraModifiers = getExtendedModifiersDataFromROMMethod(romMethod);
57
if (J9ROMMETHOD_HAS_SCOPED_ANNOTATION(extraModifiers)) {
58
if (*slot == J9_JNI_UNWRAP_REFERENCE(walkState->userData1)) {
59
*(jboolean *)walkState->userData2 = JNI_FALSE;
60
}
61
}
62
}
63
}
64
}
65
66
/**
67
* For each thread, walk Java stack and look for the scope instance. Methods that can take and access a scope
68
* instance are marked with the "@Scoped" extended modifier. If the scope instance is found in a method, that
69
* method is accessing the memory segment associated with the scope and thus closing the scope will fail.
70
*/
71
jboolean JNICALL
72
Java_jdk_internal_misc_ScopedMemoryAccess_closeScope0(JNIEnv *env, jobject instance, jobject scope, jobject exception)
73
{
74
J9VMThread *currentThread = (J9VMThread *)env;
75
J9JavaVM *vm = currentThread->javaVM;
76
const J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
77
jboolean scopeNotFound = JNI_TRUE;
78
79
vmFuncs->internalEnterVMFromJNI(currentThread);
80
vmFuncs->acquireExclusiveVMAccess(currentThread);
81
82
if (NULL == scope) {
83
vmFuncs->setCurrentExceptionUTF(currentThread, J9VMCONSTANTPOOL_JAVALANGNULLPOINTEREXCEPTION, NULL);
84
} else {
85
J9VMThread *walkThread = J9_LINKED_LIST_START_DO(vm->mainThread);
86
while (NULL != walkThread) {
87
J9StackWalkState walkState;
88
walkState.walkThread = walkThread;
89
walkState.flags = J9_STACKWALK_ITERATE_FRAMES | J9_STACKWALK_ITERATE_O_SLOTS;
90
walkState.skipCount = 0;
91
walkState.userData1 = (void *)scope;
92
walkState.userData2 = (void *)&scopeNotFound;
93
walkState.frameWalkFunction = closeScope0FrameWalkFunction;
94
walkState.objectSlotWalkFunction = closeScope0OSlotWalkFunction;
95
96
vm->walkStackFrames(walkThread, &walkState);
97
if (JNI_FALSE == *(jboolean *)walkState.userData2) {
98
/* scope found */
99
break;
100
}
101
102
walkThread = J9_LINKED_LIST_NEXT_DO(vm->mainThread, walkThread);
103
}
104
}
105
106
vmFuncs->releaseExclusiveVMAccess(currentThread);
107
vmFuncs->internalExitVMToJNI(currentThread);
108
return scopeNotFound;
109
}
110
#endif /* JAVA_SPEC_VERSION >= 16 */
111
112
} /* extern "C" */
113
114