Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/jcl/common/com_ibm_oti_vm_VM.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 <string.h>
24
#include "jni.h"
25
#include "jcl.h"
26
#include "jclglob.h"
27
#include "jclprots.h"
28
#include "jcl_internal.h"
29
#include "util_api.h"
30
31
/* private static native byte[][] getVMArgsImpl(); */
32
33
jobjectArray JNICALL
34
Java_com_ibm_oti_vm_VM_getVMArgsImpl(JNIEnv *env, jobject recv)
35
{
36
J9VMThread *vmThread = (J9VMThread *) env;
37
J9JavaVM *vm = vmThread->javaVM;
38
JavaVMInitArgs *vmOptionsStruct = vm->vmArgsArray->actualVMArgs;
39
jint originalSize = vmOptionsStruct->nOptions;
40
jint resultSize = 0;
41
JavaVMOption *options = vmOptionsStruct->options;
42
jobjectArray result = NULL;
43
jint i;
44
jclass byteArrayClass;
45
46
/* Count only options that begin with "-" */
47
48
for (i = 0; i < originalSize; ++i) {
49
if ('-' == options[i].optionString[0]) {
50
resultSize += 1;
51
}
52
}
53
54
/* Create the result array and fill it in, including only options that begin with "-" */
55
56
byteArrayClass = (*env)->FindClass(env, "[B");
57
if (NULL != byteArrayClass) {
58
result = (*env)->NewObjectArray(env, resultSize, byteArrayClass, NULL);
59
if (NULL != result) {
60
jint writeIndex = 0;
61
62
for (i = 0; i < originalSize; ++i) {
63
char * optionString = options[i].optionString;
64
65
if ('-' == optionString[0]) {
66
jint optionLength = (jint) strlen(optionString);
67
jbyteArray option = (*env)->NewByteArray(env, optionLength);
68
69
if (NULL == option) {
70
/* Don't use break here to avoid triggering the assertion below */
71
return NULL;
72
}
73
(*env)->SetByteArrayRegion(env, option, 0, optionLength, (jbyte*)optionString);
74
(*env)->SetObjectArrayElement(env, result, writeIndex, option);
75
(*env)->DeleteLocalRef(env, option);
76
writeIndex += 1;
77
}
78
}
79
Assert_JCL_true(writeIndex == resultSize);
80
}
81
}
82
83
return result;
84
}
85
86
/**
87
* @return process ID of the caller. This is upcast from UDATA.
88
*/
89
jlong JNICALL
90
Java_com_ibm_oti_vm_VM_getProcessId(JNIEnv *env, jclass clazz)
91
{
92
93
PORT_ACCESS_FROM_VMC( ((J9VMThread *) env) );
94
jlong pid;
95
pid = (jlong) j9sysinfo_get_pid();
96
return pid;
97
}
98
99
/**
100
* @return numeric user ID of the caller. This is upcast from a UDATA.
101
*/
102
jlong JNICALL
103
Java_com_ibm_oti_vm_VM_getUid(JNIEnv *env, jclass clazz)
104
{
105
106
PORT_ACCESS_FROM_VMC( ((J9VMThread *) env) );
107
jlong uid;
108
uid = (jlong) j9sysinfo_get_euid();
109
return uid;
110
}
111
112
jobject JNICALL
113
Java_com_ibm_oti_vm_VM_getNonBootstrapClassLoader(JNIEnv *env, jclass jlClass)
114
{
115
J9VMThread *currentThread = (J9VMThread*)env;
116
J9JavaVM *vm = currentThread->javaVM;
117
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
118
J9ClassLoader *bootstrapLoader = vm->systemClassLoader;
119
jobject result = NULL;
120
J9StackWalkState walkState;
121
122
vmFuncs->internalEnterVMFromJNI(currentThread);
123
walkState.skipCount = 2; /* Skip this native and its caller */
124
walkState.flags = J9_STACKWALK_CACHE_CPS | J9_STACKWALK_VISIBLE_ONLY | J9_STACKWALK_INCLUDE_NATIVES;
125
walkState.walkThread = currentThread;
126
if (J9_STACKWALK_RC_NONE != vm->walkStackFrames(currentThread, &walkState)) {
127
vmFuncs->setNativeOutOfMemoryError(currentThread, 0, 0);
128
} else {
129
J9ConstantPool **cacheCursor = (J9ConstantPool**)walkState.cache;
130
UDATA i = 0;
131
for (i = 0; i < walkState.framesWalked; ++i, ++cacheCursor) {
132
J9ClassLoader *classLoader = J9_CLASS_FROM_CP(*cacheCursor)->classLoader;
133
if (classLoader != bootstrapLoader) {
134
result = vmFuncs->j9jni_createLocalRef(env, classLoader->classLoaderObject);
135
break;
136
}
137
}
138
vmFuncs->freeStackWalkCaches(currentThread, &walkState);
139
}
140
vmFuncs->internalExitVMToJNI(currentThread);
141
return result;
142
}
143
144
145
/**
146
* Set the thread as a JVM System thread type
147
* @return 0 if successful, -1 if failed
148
*/
149
jint JNICALL
150
Java_com_ibm_oti_vm_VM_markCurrentThreadAsSystemImpl(JNIEnv *env)
151
{
152
J9VMThread *vmThread = (J9VMThread *) env;
153
jint rc = 0;
154
155
rc = (jint) omrthread_set_category(vmThread->osThread, J9THREAD_CATEGORY_SYSTEM_THREAD, J9THREAD_TYPE_SET_CREATE);
156
157
return rc;
158
}
159
160
/**
161
* Gets the J9ConstantPool address from a J9Class address
162
* @param j9clazz J9Class address
163
* @return address of J9ConstantPool
164
*/
165
jlong JNICALL
166
Java_com_ibm_oti_vm_VM_getJ9ConstantPoolFromJ9Class(JNIEnv *env, jclass unused, jlong j9clazz)
167
{
168
J9Class *clazz = (J9Class *)(IDATA)j9clazz;
169
/*
170
* Casting to UDATA first means the value will be zero-extended
171
* instead of sign-extended on platforms where jlong and UDATA
172
* are different sizes.
173
*/
174
return (jlong)(UDATA)clazz->ramConstantPool;
175
}
176
177