Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/jcl/common/java_lang_invoke_VarHandle.c
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2016, 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 "jclprots.h"
24
25
#include <string.h>
26
#include <stdlib.h>
27
28
#include "j9.h"
29
#include "jcl.h"
30
#include "j9consts.h"
31
#include "jni.h"
32
#include "j9protos.h"
33
#include "jclprots.h"
34
#include "ut_j9jcl.h"
35
#include "VM_MethodHandleKinds.h"
36
37
#include "rommeth.h"
38
#include "j9vmnls.h"
39
#include "j9vmconstantpool.h"
40
#include "j9jclnls.h"
41
42
#if defined(J9VM_OPT_METHOD_HANDLE) && (JAVA_SPEC_VERSION >= 11)
43
static BOOLEAN
44
accessCheckFieldType(J9VMThread *currentThread, J9Class* lookupClass, J9Class* type, J9UTF8 *lookupSig)
45
{
46
J9JavaVM *vm = currentThread->javaVM;
47
J9BytecodeVerificationData *verifyData = vm->bytecodeVerificationData;
48
BOOLEAN result = TRUE;
49
50
/* If the verifier isn't enabled, accept the access check unconditionally */
51
if (NULL != verifyData) {
52
U_8 *lookupSigData = J9UTF8_DATA(lookupSig);
53
/* Only check reference types (not primitive types) */
54
if ('L' == *lookupSigData) {
55
J9ClassLoader *lookupClassloader = lookupClass->classLoader;
56
J9ClassLoader *typeClassloader = type->classLoader;
57
if (typeClassloader != lookupClassloader) {
58
/* Different class loaders - check class loading constraint */
59
j9thread_monitor_enter(vm->classTableMutex);
60
if (verifyData->checkClassLoadingConstraintForNameFunction(
61
currentThread,
62
lookupClassloader,
63
typeClassloader,
64
&lookupSigData[1],
65
&lookupSigData[1],
66
J9UTF8_LENGTH(lookupSig) - 2,
67
TRUE) != 0) {
68
result = FALSE;
69
}
70
j9thread_monitor_exit(vm->classTableMutex);
71
}
72
}
73
}
74
return result;
75
}
76
77
jlong JNICALL
78
Java_java_lang_invoke_FieldVarHandle_lookupField(JNIEnv *env, jobject handle, jclass lookupClass, jstring name, jstring signature, jclass type, jboolean isStatic, jclass accessClass)
79
{
80
J9UTF8 *signatureUTF8 = NULL;
81
char signatureUTF8Buffer[256];
82
J9Class *j9LookupClass; /* J9Class for java.lang.Class lookupClass */
83
J9Class *definingClass = NULL;
84
UDATA field = 0;
85
UDATA romField = 0;
86
J9VMThread *vmThread = (J9VMThread *) env;
87
J9JavaVM *vm = vmThread->javaVM;
88
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
89
j9object_t varHandle = NULL;
90
PORT_ACCESS_FROM_ENV(env);
91
92
vmFuncs->internalEnterVMFromJNI(vmThread);
93
94
signatureUTF8 = vmFuncs->copyStringToJ9UTF8WithMemAlloc(vmThread, J9_JNI_UNWRAP_REFERENCE(signature), J9_STR_NONE, "", 0, signatureUTF8Buffer, sizeof(signatureUTF8Buffer));
95
96
if (signatureUTF8 == NULL) {
97
vmFuncs->setNativeOutOfMemoryError(vmThread, 0, 0);
98
Assert_JCL_notNull(vmThread->currentException);
99
goto _cleanup;
100
}
101
102
j9LookupClass = J9VM_J9CLASS_FROM_JCLASS(vmThread, lookupClass);
103
104
field = lookupField(env, isStatic, j9LookupClass, name, signatureUTF8, &definingClass, &romField, accessClass);
105
106
if (NULL != vmThread->currentException) {
107
goto _cleanup;
108
}
109
110
/* Check signature for classloader visibility */
111
if (!accessCheckFieldType(vmThread, j9LookupClass, J9VM_J9CLASS_FROM_JCLASS(vmThread, type), signatureUTF8)) {
112
setClassLoadingConstraintLinkageError(vmThread, definingClass, signatureUTF8);
113
goto _cleanup;
114
}
115
116
varHandle = J9_JNI_UNWRAP_REFERENCE(handle);
117
J9VMJAVALANGINVOKEVARHANDLE_SET_MODIFIERS(vmThread, varHandle, ((J9ROMFieldShape *)romField)->modifiers);
118
119
/* StaticFieldVarHandle and InstanceFieldVarHandle extend FieldVarHandle. For StaticFieldVarHandle, isStatic
120
* is true, and the definingClass field is set to the owner of the static field. For InstanceFieldVarHandle,
121
* isStatic is false, and the definingClass field is set to the lookupClass, which is also the instance class,
122
* in FieldVarHandle's constructor.
123
*/
124
if (isStatic) {
125
Assert_JCL_notNull(definingClass);
126
J9VMJAVALANGINVOKEFIELDVARHANDLE_SET_DEFININGCLASS(vmThread, varHandle, J9VM_J9CLASS_TO_HEAPCLASS(definingClass));
127
}
128
129
_cleanup:
130
vmFuncs->internalExitVMToJNI(vmThread);
131
132
if (signatureUTF8 != (J9UTF8*)signatureUTF8Buffer) {
133
j9mem_free_memory(signatureUTF8);
134
}
135
136
return field;
137
}
138
139
jlong JNICALL
140
Java_java_lang_invoke_FieldVarHandle_unreflectField(JNIEnv *env, jobject handle, jobject reflectField, jboolean isStatic)
141
{
142
J9VMThread *vmThread = (J9VMThread *) env;
143
J9JavaVM *vm = vmThread->javaVM;
144
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
145
J9ReflectFunctionTable *reflectFunctions = &(vm->reflectFunctions);
146
J9JNIFieldID *fieldID = NULL;
147
j9object_t fieldObject = NULL;
148
UDATA fieldOffset = 0;
149
150
vmFuncs->internalEnterVMFromJNI(vmThread);
151
/* Can't fail as we know the field is not null */
152
fieldObject = J9_JNI_UNWRAP_REFERENCE(reflectField);
153
fieldID = reflectFunctions->idFromFieldObject(vmThread, NULL, fieldObject);
154
155
fieldOffset = fieldID->offset;
156
if (isStatic) {
157
/* ensure this is correctly tagged so that the JIT targets using Unsafe will correctly detect this is static */
158
fieldOffset |= J9_SUN_STATIC_FIELD_OFFSET_TAG;
159
}
160
161
J9VMJAVALANGINVOKEVARHANDLE_SET_MODIFIERS(vmThread, J9_JNI_UNWRAP_REFERENCE(handle), fieldID->field->modifiers);
162
163
vmFuncs->internalExitVMToJNI(vmThread);
164
return fieldOffset;
165
}
166
#endif /* defined(J9VM_OPT_METHOD_HANDLE) && (JAVA_SPEC_VERSION >= 11) */
167
168
jobject JNICALL
169
Java_java_lang_invoke_VarHandle_get(JNIEnv *env, jobject handle, jobject args)
170
{
171
throwNewUnsupportedOperationException(env);
172
return NULL;
173
}
174
175
void JNICALL
176
Java_java_lang_invoke_VarHandle_set(JNIEnv *env, jobject handle, jobject args)
177
{
178
throwNewUnsupportedOperationException(env);
179
}
180
181
jobject JNICALL
182
Java_java_lang_invoke_VarHandle_getVolatile(JNIEnv *env, jobject handle, jobject args)
183
{
184
throwNewUnsupportedOperationException(env);
185
return NULL;
186
}
187
188
void JNICALL
189
Java_java_lang_invoke_VarHandle_setVolatile(JNIEnv *env, jobject handle, jobject args)
190
{
191
throwNewUnsupportedOperationException(env);
192
}
193
194
jobject JNICALL
195
Java_java_lang_invoke_VarHandle_getOpaque(JNIEnv *env, jobject handle, jobject args)
196
{
197
throwNewUnsupportedOperationException(env);
198
return NULL;
199
}
200
201
void JNICALL
202
Java_java_lang_invoke_VarHandle_setOpaque(JNIEnv *env, jobject handle, jobject args)
203
{
204
throwNewUnsupportedOperationException(env);
205
}
206
207
jobject JNICALL
208
Java_java_lang_invoke_VarHandle_getAcquire(JNIEnv *env, jobject handle, jobject args)
209
{
210
throwNewUnsupportedOperationException(env);
211
return NULL;
212
}
213
214
void JNICALL
215
Java_java_lang_invoke_VarHandle_setRelease(JNIEnv *env, jobject handle, jobject args)
216
{
217
throwNewUnsupportedOperationException(env);
218
}
219
220
jboolean JNICALL
221
Java_java_lang_invoke_VarHandle_compareAndSet(JNIEnv *env, jobject handle, jobject args)
222
{
223
throwNewUnsupportedOperationException(env);
224
return JNI_FALSE;
225
}
226
227
jboolean JNICALL
228
Java_java_lang_invoke_VarHandle_compareAndExchange(JNIEnv *env, jobject handle, jobject args)
229
{
230
throwNewUnsupportedOperationException(env);
231
return JNI_FALSE;
232
}
233
234
jboolean JNICALL
235
Java_java_lang_invoke_VarHandle_compareAndExchangeAcquire(JNIEnv *env, jobject handle, jobject args)
236
{
237
throwNewUnsupportedOperationException(env);
238
return JNI_FALSE;
239
}
240
241
jboolean JNICALL
242
Java_java_lang_invoke_VarHandle_compareAndExchangeRelease(JNIEnv *env, jobject handle, jobject args)
243
{
244
throwNewUnsupportedOperationException(env);
245
return JNI_FALSE;
246
}
247
248
jboolean JNICALL
249
Java_java_lang_invoke_VarHandle_weakCompareAndSet(JNIEnv *env, jobject handle, jobject args)
250
{
251
throwNewUnsupportedOperationException(env);
252
return JNI_FALSE;
253
}
254
255
jboolean JNICALL
256
Java_java_lang_invoke_VarHandle_weakCompareAndSetAcquire(JNIEnv *env, jobject handle, jobject args)
257
{
258
throwNewUnsupportedOperationException(env);
259
return JNI_FALSE;
260
}
261
262
jboolean JNICALL
263
Java_java_lang_invoke_VarHandle_weakCompareAndSetRelease(JNIEnv *env, jobject handle, jobject args)
264
{
265
throwNewUnsupportedOperationException(env);
266
return JNI_FALSE;
267
}
268
269
jboolean JNICALL
270
Java_java_lang_invoke_VarHandle_weakCompareAndSetPlain(JNIEnv *env, jobject handle, jobject args)
271
{
272
throwNewUnsupportedOperationException(env);
273
return JNI_FALSE;
274
}
275
276
jobject JNICALL
277
Java_java_lang_invoke_VarHandle_getAndSet(JNIEnv *env, jobject handle, jobject args)
278
{
279
throwNewUnsupportedOperationException(env);
280
return NULL;
281
}
282
283
jobject JNICALL
284
Java_java_lang_invoke_VarHandle_getAndSetAcquire(JNIEnv *env, jobject handle, jobject args)
285
{
286
throwNewUnsupportedOperationException(env);
287
return NULL;
288
}
289
290
jobject JNICALL
291
Java_java_lang_invoke_VarHandle_getAndSetRelease(JNIEnv *env, jobject handle, jobject args)
292
{
293
throwNewUnsupportedOperationException(env);
294
return NULL;
295
}
296
297
jobject JNICALL
298
Java_java_lang_invoke_VarHandle_getAndAdd(JNIEnv *env, jobject handle, jobject args)
299
{
300
throwNewUnsupportedOperationException(env);
301
return NULL;
302
}
303
304
jobject JNICALL
305
Java_java_lang_invoke_VarHandle_getAndAddAcquire(JNIEnv *env, jobject handle, jobject args)
306
{
307
throwNewUnsupportedOperationException(env);
308
return NULL;
309
}
310
311
jobject JNICALL
312
Java_java_lang_invoke_VarHandle_getAndAddRelease(JNIEnv *env, jobject handle, jobject args)
313
{
314
throwNewUnsupportedOperationException(env);
315
return NULL;
316
}
317
318
jobject JNICALL
319
Java_java_lang_invoke_VarHandle_getAndBitwiseAnd(JNIEnv *env, jobject handle, jobject args)
320
{
321
throwNewUnsupportedOperationException(env);
322
return NULL;
323
}
324
325
jobject JNICALL
326
Java_java_lang_invoke_VarHandle_getAndBitwiseAndAcquire(JNIEnv *env, jobject handle, jobject args)
327
{
328
throwNewUnsupportedOperationException(env);
329
return NULL;
330
}
331
332
jobject JNICALL
333
Java_java_lang_invoke_VarHandle_getAndBitwiseAndRelease(JNIEnv *env, jobject handle, jobject args)
334
{
335
throwNewUnsupportedOperationException(env);
336
return NULL;
337
}
338
339
jobject JNICALL
340
Java_java_lang_invoke_VarHandle_getAndBitwiseOr(JNIEnv *env, jobject handle, jobject args)
341
{
342
throwNewUnsupportedOperationException(env);
343
return NULL;
344
}
345
346
jobject JNICALL
347
Java_java_lang_invoke_VarHandle_getAndBitwiseOrAcquire(JNIEnv *env, jobject handle, jobject args)
348
{
349
throwNewUnsupportedOperationException(env);
350
return NULL;
351
}
352
353
jobject JNICALL
354
Java_java_lang_invoke_VarHandle_getAndBitwiseOrRelease(JNIEnv *env, jobject handle, jobject args)
355
{
356
throwNewUnsupportedOperationException(env);
357
return NULL;
358
}
359
360
jobject JNICALL
361
Java_java_lang_invoke_VarHandle_getAndBitwiseXor(JNIEnv *env, jobject handle, jobject args)
362
{
363
throwNewUnsupportedOperationException(env);
364
return NULL;
365
}
366
367
jobject JNICALL
368
Java_java_lang_invoke_VarHandle_getAndBitwiseXorAcquire(JNIEnv *env, jobject handle, jobject args)
369
{
370
throwNewUnsupportedOperationException(env);
371
return NULL;
372
}
373
374
jobject JNICALL
375
Java_java_lang_invoke_VarHandle_getAndBitwiseXorRelease(JNIEnv *env, jobject handle, jobject args)
376
{
377
throwNewUnsupportedOperationException(env);
378
return NULL;
379
}
380
381
jobject JNICALL
382
Java_java_lang_invoke_VarHandle_addAndGet(JNIEnv *env, jobject handle, jobject args)
383
{
384
throwNewUnsupportedOperationException(env);
385
return NULL;
386
}
387
388