Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/jcl/common/jniidcacheinit.c
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2019 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
#include "jclglob.h"
23
#include <string.h>
24
#include <stdlib.h>
25
#include "jcl.h"
26
#include "j2sever.h"
27
#include "j9lib.h"
28
#include "rommeth.h"
29
#include "j9protos.h"
30
#include "j9consts.h"
31
#include "jclglob.h"
32
#include "jniidcacheinit.h"
33
#include "j9.h"
34
#include "jclprots.h"
35
#if defined(J9ZOS390)
36
#include "atoe.h"
37
#include <_Ccsid.h>
38
#endif
39
40
#include "ut_j9jcl.h"
41
42
/**
43
* Initializes java/lang/String class ID in JniIDCache struct
44
*
45
* @param[in] env The JNI env
46
*
47
* @return TRUE or FALSE based on whether successfully initializing IDs or not
48
*/
49
BOOLEAN
50
initializeJavaLangStringIDCache(JNIEnv* env)
51
{
52
jstring jStringClass;
53
jobject globalStringRef;
54
BOOLEAN isGlobalRefRedundant=FALSE;
55
BOOLEAN isClassIDAlreadyCached=TRUE;
56
J9JavaVM *javaVM = ((J9VMThread *) env)->javaVM;
57
58
/* Initializing java/lang/String class ID */
59
/* Check if java/lang/String class ID hasn't been cached yet */
60
omrthread_monitor_enter(javaVM->jclCacheMutex);
61
62
if (JCL_CACHE_GET(env,CLS_java_lang_String) == NULL) {
63
isClassIDAlreadyCached=FALSE;
64
}
65
66
omrthread_monitor_exit(javaVM->jclCacheMutex);
67
68
if (!isClassIDAlreadyCached) {
69
70
/* Load java/lang/String class */
71
jStringClass = (*env)->FindClass(env, "java/lang/String");
72
if ((*env)->ExceptionCheck(env)) {
73
return FALSE;
74
}
75
76
globalStringRef = (*env)->NewGlobalRef(env, jStringClass);
77
if (globalStringRef == NULL) {
78
/* out of memory exception thrown */
79
javaVM->internalVMFunctions->throwNativeOOMError(env, 0, 0);
80
return FALSE;
81
}
82
83
/* Allow the local reference jStringClass to be garbage collected immediately */
84
(*env)->DeleteLocalRef(env,jStringClass);
85
86
87
omrthread_monitor_enter(javaVM->jclCacheMutex);
88
89
if (JCL_CACHE_GET(env,CLS_java_lang_String) == NULL) {
90
/* Store java/lang/String class ID in JniIDCache struct */
91
JCL_CACHE_SET(env, CLS_java_lang_String, globalStringRef);
92
}
93
else {
94
isGlobalRefRedundant=TRUE;
95
}
96
97
omrthread_monitor_exit(javaVM->jclCacheMutex);
98
99
if (isGlobalRefRedundant == TRUE) {
100
(*env)->DeleteGlobalRef(env,globalStringRef);
101
}
102
}
103
return TRUE;
104
}
105
106
107
/**
108
* Initializes sun/reflect/ConstantPool (Java 8) or jdk/internal/reflect/ConstantPool (Java 11+) class ID
109
* and constantPoolOop field ID in JniIDCache struct
110
*
111
* @param[in] env The JNI env
112
*
113
* @return TRUE or FALSE based on whether successfully initializing IDs or not
114
*/
115
BOOLEAN
116
initializeSunReflectConstantPoolIDCache(JNIEnv* env)
117
{
118
J9JavaVM *javaVM = ((J9VMThread *) env)->javaVM;
119
BOOLEAN isClassIDAlreadyCached = TRUE;
120
BOOLEAN result = TRUE;
121
122
omrthread_monitor_enter(javaVM->jclCacheMutex);
123
if (NULL == JCL_CACHE_GET(env, CLS_sun_reflect_ConstantPool)) {
124
isClassIDAlreadyCached = FALSE;
125
}
126
omrthread_monitor_exit(javaVM->jclCacheMutex);
127
128
if (!isClassIDAlreadyCached) {
129
jclass sunReflectConstantPool = NULL;
130
131
/* Load sun/reflect/ConstantPool (Java 8) or jdk/internal/reflect/ConstantPool (Java 11+) */
132
if (J2SE_VERSION(javaVM) >= J2SE_V11) {
133
sunReflectConstantPool = (*env)->FindClass(env, "jdk/internal/reflect/ConstantPool");
134
} else {
135
sunReflectConstantPool = (*env)->FindClass(env, "sun/reflect/ConstantPool");
136
}
137
if (NULL == sunReflectConstantPool) {
138
/* exception thrown */
139
result = FALSE;
140
} else {
141
jobject globalClassRef = (*env)->NewGlobalRef(env, sunReflectConstantPool);
142
/* Allow the local reference sunReflectConstantPool to be garbage collected immediately */
143
(*env)->DeleteLocalRef(env, sunReflectConstantPool);
144
if (NULL == globalClassRef) {
145
/* out of memory exception thrown */
146
javaVM->internalVMFunctions->throwNativeOOMError(env, 0, 0);
147
result = FALSE;
148
} else {
149
/* Initializing constantPoolOop field ID */
150
jfieldID fieldID = (*env)->GetFieldID(env, globalClassRef, "constantPoolOop", "Ljava/lang/Object;");
151
if (NULL == fieldID) {
152
result = FALSE;
153
} else {
154
BOOLEAN isGlobalRefRedundant = FALSE;
155
156
omrthread_monitor_enter(javaVM->jclCacheMutex);
157
if (NULL == JCL_CACHE_GET(env,CLS_sun_reflect_ConstantPool)) {
158
/* Set FID_sun_reflect_ConstantPool_constantPoolOop first and CLS_sun_reflect_ConstantPool afterwards,
159
* read CLS_sun_reflect_ConstantPool first in java_lang_Access.c:Java_java_lang_Access_getConstantPool(),
160
* if NULL is returned, initializeSunReflectConstantPoolIDCache() is invoked to
161
* set FID_sun_reflect_ConstantPool_constantPoolOop, next read it and de-reference.
162
* Otherwise, a NULL FID_sun_reflect_ConstantPool_constantPoolOop could be returned and cause gpf later.
163
*/
164
JCL_CACHE_SET(env, FID_sun_reflect_ConstantPool_constantPoolOop, fieldID);
165
issueWriteBarrier();
166
JCL_CACHE_SET(env, CLS_sun_reflect_ConstantPool, globalClassRef);
167
} else {
168
isGlobalRefRedundant = TRUE;
169
}
170
omrthread_monitor_exit(javaVM->jclCacheMutex);
171
172
if (isGlobalRefRedundant) {
173
(*env)->DeleteGlobalRef(env, globalClassRef);
174
}
175
}
176
}
177
}
178
}
179
return result;
180
}
181
182