Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/jcl/common/jcldefine.c
6000 views
1
/*******************************************************************************
2
* Copyright (c) 1998, 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 "j9.h"
24
#include "j9consts.h"
25
#include "jclprots.h"
26
#include "j9protos.h"
27
#include "j9jclnls.h"
28
29
jclass
30
defineClassCommon(JNIEnv *env, jobject classLoaderObject,
31
jstring className, jbyteArray classRep, jint offset, jint length, jobject protectionDomain, UDATA *options, J9Class *hostClass, J9ClassPatchMap *patchMap, BOOLEAN validateName)
32
{
33
#ifdef J9VM_OPT_DYNAMIC_LOAD_SUPPORT
34
35
/* Try a couple of GC passes (1 doesn't sem to be enough), but don't try forever */
36
#define MAX_RETRY_COUNT 2
37
38
J9VMThread *currentThread = (J9VMThread *)env;
39
J9JavaVM *vm = currentThread->javaVM;
40
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
41
J9TranslationBufferSet *dynFuncs = NULL;
42
J9ClassLoader *classLoader = NULL;
43
UDATA retried = FALSE;
44
UDATA utf8Length = 0;
45
char utf8NameStackBuffer[J9VM_PACKAGE_NAME_BUFFER_LENGTH];
46
U_8 *utf8Name = NULL;
47
U_8 *classBytes = NULL;
48
J9Class *clazz = NULL;
49
jclass result = NULL;
50
PORT_ACCESS_FROM_JAVAVM(vm);
51
UDATA isContiguousClassBytes = 0;
52
J9ROMClass *loadedClass = NULL;
53
U_8 *tempClassBytes = NULL;
54
I_32 tempLength = 0;
55
J9TranslationLocalBuffer localBuffer = {J9_CP_INDEX_NONE, LOAD_LOCATION_UNKNOWN, NULL};
56
57
if (vm->dynamicLoadBuffers == NULL) {
58
throwNewInternalError(env, "Dynamic loader is unavailable");
59
goto done;
60
}
61
dynFuncs = vm->dynamicLoadBuffers;
62
63
if (classRep == NULL) {
64
throwNewNullPointerException(env, NULL);
65
goto done;
66
}
67
68
if ((patchMap != NULL) && (patchMap->size != 0)) {
69
localBuffer.patchMap = patchMap;
70
}
71
72
vmFuncs->internalEnterVMFromJNI(currentThread);
73
isContiguousClassBytes = J9ISCONTIGUOUSARRAY(currentThread, *(J9IndexableObject **)classRep);
74
if (!isContiguousClassBytes) {
75
vmFuncs->internalExitVMToJNI(currentThread);
76
/* Make a "flat" copy of classRep */
77
if (length < 0) {
78
throwNewIndexOutOfBoundsException(env, NULL);
79
goto done;
80
}
81
classBytes = j9mem_allocate_memory(length, J9MEM_CATEGORY_CLASSES);
82
if (classBytes == NULL) {
83
vmFuncs->throwNativeOOMError(env, 0, 0);
84
goto done;
85
}
86
(*env)->GetByteArrayRegion(env, classRep, offset, length, (jbyte *)classBytes);
87
if ((*env)->ExceptionCheck(env)) {
88
j9mem_free_memory(classBytes);
89
goto done;
90
}
91
vmFuncs->internalEnterVMFromJNI(currentThread);
92
}
93
94
/* Allocate and initialize a UTF8 copy of the Unicode class-name */
95
if (NULL != className) {
96
j9object_t classNameObject = J9_JNI_UNWRAP_REFERENCE(className);
97
UDATA stringFlags = J9_STR_NULL_TERMINATE_RESULT;
98
99
if (!validateName) {
100
stringFlags |= J9_STR_XLAT;
101
}
102
103
utf8Name = (U_8*)vmFuncs->copyStringToUTF8WithMemAlloc(currentThread, classNameObject, stringFlags, "", 0, utf8NameStackBuffer, J9VM_PACKAGE_NAME_BUFFER_LENGTH, &utf8Length);
104
105
if (NULL == utf8Name) {
106
vmFuncs->setNativeOutOfMemoryError(currentThread, 0, 0);
107
goto done;
108
}
109
110
if (validateName && (CLASSNAME_INVALID == vmFuncs->verifyQualifiedName(currentThread, utf8Name, utf8Length, CLASSNAME_VALID_NON_ARRARY))) {
111
/* We don't yet know if the class being defined is exempt. Setting this option tells
112
* defineClassCommon() to fail if it discovers that the class is not exempt. That failure
113
* is distinguished by returning NULL with no exception pending.
114
*/
115
*options |= J9_FINDCLASS_FLAG_NAME_IS_INVALID;
116
}
117
118
if (J9_ARE_ANY_BITS_SET(*options, J9_FINDCLASS_FLAG_HIDDEN | J9_FINDCLASS_FLAG_UNSAFE)) {
119
/*
120
* Prevent generated LambdaForm classes from MethodHandles to be stored to the shared cache.
121
* When there are a large number of such classes in the shared cache, they trigger a lot of class comparisons.
122
* Performance can be much worse (compared to shared cache turned off).
123
*/
124
#define J9NON_SHARING_CLASS_NAME "java/lang/invoke/LambdaForm$"
125
if ((utf8Length > LITERAL_STRLEN(J9NON_SHARING_CLASS_NAME))
126
&& J9UTF8_LITERAL_EQUALS(utf8Name, LITERAL_STRLEN(J9NON_SHARING_CLASS_NAME), J9NON_SHARING_CLASS_NAME)
127
) {
128
*options |= J9_FINDCLASS_FLAG_DO_NOT_SHARE;
129
}
130
#undef J9NON_SHARING_CLASS_NAME
131
}
132
}
133
134
if (isContiguousClassBytes) {
135
/* For ARRAYLETS case, we get free range checking from GetByteArrayRegion JNI call */
136
if ((offset < 0) || (length < 0) ||
137
(((U_32)offset + (U_32)length) > J9INDEXABLEOBJECT_SIZE(currentThread, *(J9IndexableObject **)classRep))) {
138
vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGINDEXOUTOFBOUNDSEXCEPTION, NULL);
139
goto done;
140
}
141
}
142
143
classLoader = J9VMJAVALANGCLASSLOADER_VMREF(currentThread, J9_JNI_UNWRAP_REFERENCE(classLoaderObject));
144
145
if (NULL == classLoader) {
146
classLoader = vmFuncs->internalAllocateClassLoader(vm, J9_JNI_UNWRAP_REFERENCE(classLoaderObject));
147
if (NULL == classLoader) {
148
goto done;
149
}
150
}
151
152
retry:
153
154
omrthread_monitor_enter(vm->classTableMutex);
155
/* Hidden class is never added into the hash table */
156
if (J9_ARE_NO_BITS_SET(*options, J9_FINDCLASS_FLAG_HIDDEN)) {
157
if (NULL != vmFuncs->hashClassTableAt(classLoader, utf8Name, utf8Length)) {
158
/* Bad, we have already defined this class - fail */
159
omrthread_monitor_exit(vm->classTableMutex);
160
if (J9_ARE_NO_BITS_SET(*options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
161
vmFuncs->setCurrentExceptionNLSWithArgs(currentThread, J9NLS_JCL_DUPLICATE_CLASS_DEFINITION, J9VMCONSTANTPOOL_JAVALANGLINKAGEERROR, utf8Length, utf8Name);
162
}
163
goto done;
164
}
165
}
166
167
if (isContiguousClassBytes) {
168
/* Always re-load the classBytes pointer, as GC required in OutOfMemory case may have moved things */
169
/* TMP_J9JAVACONTIGUOUSARRAYOFBYTE_EA returns I_8 * (since Java bytes are I_8) so this cast will silence the warning */
170
classBytes = (U_8 *) TMP_J9JAVACONTIGUOUSARRAYOFBYTE_EA(currentThread, *(J9IndexableObject **)classRep, offset);
171
}
172
173
tempClassBytes = classBytes;
174
tempLength = length;
175
176
/* Try to find classLocation. Ignore return code because there are valid cases where it might not find it (ie. bytecode spinning).
177
* If the class is not found the default class location is fine.
178
*/
179
dynFuncs->findLocallyDefinedClassFunction(currentThread, NULL, utf8Name, (U_32) utf8Length, classLoader, (UDATA) FALSE, &localBuffer);
180
181
/* skip if we are anonClass or hidden classes */
182
if (J9_ARE_NO_BITS_SET(*options, J9_FINDCLASS_FLAG_ANON | J9_FINDCLASS_FLAG_HIDDEN)) {
183
/* Check for romClass cookie, it indicates that we are defining a class out of a JXE not from class bytes */
184
185
loadedClass = vmFuncs->romClassLoadFromCookie(currentThread, utf8Name, utf8Length, classBytes, (UDATA) length);
186
187
if (NULL != loadedClass) {
188
/* An existing ROMClass is found in the shared class cache.
189
* If -Xshareclasses:enableBCI is present, need to give VM a chance to trigger ClassFileLoadHook event.
190
*/
191
if ((NULL == vm->sharedClassConfig) || (0 == vm->sharedClassConfig->isBCIEnabled(vm))) {
192
clazz = vmFuncs->internalCreateRAMClassFromROMClass(currentThread,
193
classLoader,
194
loadedClass,
195
0,
196
NULL,
197
protectionDomain ? *(j9object_t*)protectionDomain : NULL,
198
NULL,
199
J9_CP_INDEX_NONE,
200
localBuffer.loadLocationType,
201
NULL,
202
hostClass);
203
/* Done if a class was found or and exception is pending, otherwise try to define the bytes */
204
if ((clazz != NULL) || (currentThread->currentException != NULL)) {
205
goto done;
206
} else {
207
loadedClass = NULL;
208
}
209
} else {
210
tempClassBytes = J9ROMCLASS_INTERMEDIATECLASSDATA(loadedClass);
211
tempLength = loadedClass->intermediateClassDataLength;
212
*options |= J9_FINDCLASS_FLAG_SHRC_ROMCLASS_EXISTS;
213
}
214
}
215
}
216
217
/* The defineClass helper requires you hold the class table mutex and releases it for you */
218
219
clazz = dynFuncs->internalDefineClassFunction(currentThread,
220
utf8Name, utf8Length,
221
tempClassBytes, (UDATA) tempLength, NULL,
222
classLoader,
223
protectionDomain ? *(j9object_t*)protectionDomain : NULL,
224
*options | J9_FINDCLASS_FLAG_THROW_ON_FAIL | J9_FINDCLASS_FLAG_NO_CHECK_FOR_EXISTING_CLASS,
225
loadedClass,
226
hostClass,
227
&localBuffer);
228
229
/* If OutOfMemory, try a GC to free up some memory */
230
231
if (currentThread->privateFlags & J9_PRIVATE_FLAGS_CLOAD_NO_MEM) {
232
if (!retried) {
233
/*Trc_VM_internalFindClass_gcAndRetry(vmThread);*/
234
currentThread->javaVM->memoryManagerFunctions->j9gc_modron_global_collect_with_overrides(currentThread, J9MMCONSTANT_EXPLICIT_GC_NATIVE_OUT_OF_MEMORY);
235
retried = TRUE;
236
goto retry;
237
}
238
vmFuncs->setNativeOutOfMemoryError(currentThread, 0, 0);
239
}
240
241
done:
242
if (NULL == clazz) {
243
if (J9_ARE_ANY_BITS_SET(*options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
244
/*
245
* The caller signalled that the name is invalid. Leave the result NULL and
246
* clear any pending exception; the caller will throw NoClassDefFoundError.
247
*/
248
currentThread->currentException = NULL;
249
currentThread->privateFlags &= ~(UDATA)J9_PRIVATE_FLAGS_REPORT_EXCEPTION_THROW;
250
} else if (NULL == currentThread->currentException) {
251
/* should not get here -- throw the default exception just in case */
252
vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGCLASSFORMATERROR, NULL);
253
}
254
} else {
255
result = vmFuncs->j9jni_createLocalRef(env, J9VM_J9CLASS_TO_HEAPCLASS(clazz));
256
}
257
258
vmFuncs->internalExitVMToJNI(currentThread);
259
260
if ((U_8*)utf8NameStackBuffer != utf8Name) {
261
j9mem_free_memory(utf8Name);
262
}
263
264
if (!isContiguousClassBytes) {
265
j9mem_free_memory(classBytes);
266
}
267
268
return result;
269
270
#else /* J9VM_OPT_DYNAMIC_LOAD_SUPPORT */
271
throwNewInternalError(env, "Dynamic loading not supported");
272
return NULL;
273
#endif /* J9VM_OPT_DYNAMIC_LOAD_SUPPORT */
274
}
275
276