Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/libdefine.cpp
41159 views
1
/*
2
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
#include <jni.h>
24
#include <stdio.h>
25
26
#include <string.h>
27
#include <jvmti.h>
28
29
#define STATUS_FAILED 2
30
#define STATUS_PASSED 0
31
32
#define REFERENCES_ARRAY_SIZE 10000000
33
34
#ifndef _Included_gc_g1_unloading_unloading_classloaders_JNIClassloader
35
#define _Included_gc_g1_unloading_unloading_classloaders_JNIClassloader
36
37
extern "C" {
38
39
/*
40
* Class: gc_g1_unloading_unloading_classloaders_JNIClassloader
41
* Method: loadThroughJNI0
42
* Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;[B)Ljava/lang/Class;
43
*/
44
JNIEXPORT jclass JNICALL Java_gc_g1_unloading_classloaders_JNIClassloader_loadThroughJNI0 (JNIEnv * env,
45
jclass clazz, jstring className, jobject classLoader, jbyteArray bytecode) {
46
47
const char * classNameChar = env->GetStringUTFChars(className, NULL);
48
jbyte * arrayContent = env->GetByteArrayElements(bytecode, NULL);
49
jsize bytecodeLength = env->GetArrayLength(bytecode);
50
jclass returnValue = env->DefineClass(classNameChar, classLoader, arrayContent, bytecodeLength);
51
env->ReleaseByteArrayElements(bytecode, arrayContent, JNI_ABORT);
52
env->ReleaseStringUTFChars(className, classNameChar);
53
if (!returnValue) {
54
printf("ERROR: DefineClass call returned NULL by some reason. Classloading failed.\n");
55
}
56
57
return returnValue;
58
}
59
60
/*
61
* Class: gc_g1_unloading_unloading_loading_ClassLoadingHelper
62
* Method: makeRedefinition0
63
* Signature: (ILjava/lang/Class;[B)I
64
*/
65
JNIEXPORT jint JNICALL Java_gc_g1_unloading_loading_ClassLoadingHelper_makeRedefinition0(JNIEnv *env,
66
jclass clazz, jint fl, jclass redefCls, jbyteArray classBytes) {
67
JavaVM * jvm;
68
jvmtiEnv * jvmti;
69
jvmtiError err;
70
jvmtiCapabilities caps;
71
jvmtiClassDefinition classDef;
72
jint jint_err = env->GetJavaVM(&jvm);
73
if (jint_err) {
74
printf("GetJavaVM returned nonzero: %d", jint_err);
75
return STATUS_FAILED;
76
}
77
78
jint_err = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_0);
79
if (jint_err) {
80
printf("GetEnv returned nonzero: %d", jint_err);
81
return STATUS_FAILED;
82
}
83
84
err = jvmti->GetPotentialCapabilities(&caps);
85
if (err != JVMTI_ERROR_NONE) {
86
printf("(GetPotentialCapabilities) unexpected error: %d\n",err);
87
return JNI_ERR;
88
}
89
90
err = jvmti->AddCapabilities(&caps);
91
if (err != JVMTI_ERROR_NONE) {
92
printf("(AddCapabilities) unexpected error: %d\n", err);
93
return JNI_ERR;
94
}
95
96
if (!caps.can_redefine_classes) {
97
printf("ERROR: Can't redefine classes. jvmtiCapabilities.can_redefine_classes isn't set up.");
98
return STATUS_FAILED;
99
}
100
101
classDef.klass = redefCls;
102
classDef.class_byte_count =
103
env->GetArrayLength(classBytes);
104
jbyte * class_bytes = env->GetByteArrayElements(classBytes, NULL);
105
classDef.class_bytes = (unsigned char *)class_bytes;
106
107
if (fl == 2) {
108
printf(">>>>>>>> Invoke RedefineClasses():\n");
109
printf("\tnew class byte count=%d\n", classDef.class_byte_count);
110
}
111
err = jvmti->RedefineClasses(1, &classDef);
112
env->ReleaseByteArrayElements(classBytes, class_bytes, JNI_ABORT);
113
if (err != JVMTI_ERROR_NONE) {
114
printf("%s: Failed to call RedefineClasses():\n", __FILE__);
115
printf("\tthe function returned error %d\n", err);
116
printf("\tFor more info about this error see the JVMTI spec.\n");
117
return STATUS_FAILED;
118
}
119
if (fl == 2)
120
printf("<<<<<<<< RedefineClasses() is successfully done\n");
121
122
return STATUS_PASSED;
123
}
124
125
jobject referencesArray[REFERENCES_ARRAY_SIZE];
126
int firstFreeIndex = 0;
127
128
/*
129
* Class: gc_g1_unloading_unloading_keepref_JNIGlobalRefHolder
130
* Method: keepGlobalJNIReference
131
* Signature: (Ljava/lang/Object;)I
132
*/
133
JNIEXPORT jint JNICALL Java_gc_g1_unloading_keepref_JNIGlobalRefHolder_keepGlobalJNIReference
134
(JNIEnv * env, jclass clazz, jobject obj) {
135
int returnValue;
136
referencesArray[firstFreeIndex] = env->NewGlobalRef(obj);
137
printf("checkpoint1 %d \n", firstFreeIndex);
138
returnValue = firstFreeIndex;
139
firstFreeIndex++;
140
return returnValue;
141
}
142
143
/*
144
* Class: gc_g1_unloading_unloading_keepref_JNIGlobalRefHolder
145
* Method: deleteGlobalJNIReference
146
* Signature: (I)V
147
*/
148
JNIEXPORT void JNICALL Java_gc_g1_unloading_keepref_JNIGlobalRefHolder_deleteGlobalJNIReference
149
(JNIEnv * env, jclass clazz, jint index) {
150
env->DeleteGlobalRef(referencesArray[index]);
151
}
152
153
154
/*
155
* Class: gc_g1_unloading_unloading_keepref_JNILocalRefHolder
156
* Method: holdWithJNILocalReference
157
* Signature: (Ljava/lang/Object;)V
158
*/
159
JNIEXPORT void JNICALL Java_gc_g1_unloading_keepref_JNILocalRefHolder_holdWithJNILocalReference
160
(JNIEnv * env, jobject thisObject, jobject syncObject) {
161
jclass clazz, objectClazz;
162
jfieldID objectToKeepField;
163
jobject objectToKeep, localRef;
164
jmethodID waitMethod;
165
166
clazz = env->GetObjectClass(thisObject);
167
objectToKeepField = env->GetFieldID(clazz, "objectToKeep", "Ljava/lang/Object;");
168
objectToKeep = env->GetObjectField(thisObject, objectToKeepField);
169
localRef = env->NewLocalRef(objectToKeep);
170
env->SetObjectField(thisObject, objectToKeepField, NULL);
171
172
objectClazz = env->FindClass("Ljava/lang/Object;");
173
waitMethod = env->GetMethodID(objectClazz, "wait", "()V");
174
env->CallVoidMethod(syncObject, waitMethod);
175
printf("checkpoint2 \n");
176
}
177
}
178
#endif
179
180