Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/IsModifiableModule/libIsModifiableModuleTest.c
41155 views
1
/*
2
* Copyright (c) 2017, 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
24
#include <stdio.h>
25
#include <string.h>
26
#include "jvmti.h"
27
28
#ifdef __cplusplus
29
extern "C" {
30
#endif
31
32
#ifndef JNI_ENV_ARG
33
34
#ifdef __cplusplus
35
#define JNI_ENV_ARG(x, y) y
36
#define JNI_ENV_PTR(x) x
37
#else
38
#define JNI_ENV_ARG(x,y) x, y
39
#define JNI_ENV_PTR(x) (*x)
40
#endif
41
42
#endif
43
44
#define TranslateError(err) "JVMTI error"
45
46
#define PASSED 0
47
#define FAILED 2
48
49
static const char *EXC_CNAME = "java/lang/AssertionError";
50
51
static jvmtiEnv *jvmti = NULL;
52
static jint result = PASSED;
53
static jboolean printdump = JNI_FALSE;
54
55
static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
56
57
JNIEXPORT
58
jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
59
return Agent_Initialize(jvm, options, reserved);
60
}
61
62
JNIEXPORT
63
jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
64
return Agent_Initialize(jvm, options, reserved);
65
}
66
67
JNIEXPORT
68
jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
69
return JNI_VERSION_1_8;
70
}
71
72
static
73
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
74
jint res;
75
76
if (options != NULL && strcmp(options, "printdump") == 0) {
77
printdump = JNI_TRUE;
78
}
79
80
res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
81
JVMTI_VERSION_9);
82
if (res != JNI_OK || jvmti == NULL) {
83
printf(" Error: wrong result of a valid call to GetEnv!\n");
84
return JNI_ERR;
85
}
86
87
return JNI_OK;
88
}
89
90
static
91
jclass find_class(JNIEnv *env, const char* cname) {
92
jclass cls = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, cname));
93
94
if (cls == NULL) {
95
printf("find_class: Error: FindClass(env, \"%s\") returned NULL\n", cname);
96
}
97
return cls;
98
}
99
100
static
101
jint throw_exc(JNIEnv *env, char *msg) {
102
jclass exc_class = find_class(env, EXC_CNAME);
103
104
if (exc_class == NULL) {
105
printf("throw_exc: Error in find_class(env, \"%s\")\n", EXC_CNAME);
106
return -1;
107
}
108
return JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);
109
}
110
111
static jobject get_module_by_class_name(JNIEnv *env, const char* cname) {
112
jobject module = NULL;
113
jclass cls = find_class(env, cname);
114
115
printf(">>> getting module by class name: \"%s\"\n", cname);
116
if (cls == NULL) {
117
printf("get_module_by_class_name: Error in find_class(env, \"%s\")\n", cname);
118
return NULL;
119
}
120
module = JNI_ENV_PTR(env)->GetModule(JNI_ENV_ARG(env, cls));
121
if (module == NULL) {
122
printf("get_module_by_class_name: Error in GetModule for class \"%s\"\n", cname);
123
}
124
return module;
125
}
126
127
static
128
jint check_is_modifiable_error_codes(jobject module, jobject not_a_module) {
129
jvmtiError err = JVMTI_ERROR_NONE;
130
jboolean is_modifiable = JNI_FALSE;
131
132
printf(">>> passing a bad module argument to JVMTI IsModifiableModule\n");
133
err = (*jvmti)->IsModifiableModule(jvmti, not_a_module, &is_modifiable);
134
if (err != JVMTI_ERROR_INVALID_MODULE) {
135
printf(" Error #EC0: Did not get expected INVALID_MODULE error code from"
136
" IsModifiableModule: %s (%d)\n", TranslateError(err), err);
137
return FAILED;
138
}
139
printf(">>> passing NULL module argument to JVMTI IsModifiableModule\n");
140
err = (*jvmti)->IsModifiableModule(jvmti, NULL, &is_modifiable);
141
if (err != JVMTI_ERROR_NULL_POINTER) {
142
printf(" Error #EC1: Did not get expected NULL_POINTER error code from"
143
" IsModifiableModule: %s (%d)\n", TranslateError(err), err);
144
return FAILED;
145
}
146
printf(">>> passing NULL status pointer to JVMTI IsModifiableModule\n");
147
err = (*jvmti)->IsModifiableModule(jvmti, module, NULL);
148
if (err != JVMTI_ERROR_NULL_POINTER) {
149
printf(" Error #EC2: Did not get expected NULL_POINTER error code from"
150
" IsModifiableModule: %s (%d)\n", TranslateError(err), err);
151
return FAILED;
152
}
153
return PASSED;
154
}
155
156
static
157
jint check_is_modifiable(jobject module) {
158
jvmtiError err = JVMTI_ERROR_NONE;
159
jboolean is_modifiable = JNI_FALSE;
160
161
printf(">>> checking module %p is modifiable\n", module);
162
err = (*jvmti)->IsModifiableModule(jvmti, module, &is_modifiable);
163
if (err != JVMTI_ERROR_NONE) {
164
printf(" Error in IsModifiableModule for module %p: %s (%d)\n",
165
module, TranslateError(err), err);
166
return FAILED;
167
}
168
if (is_modifiable == JNI_FALSE) {
169
printf(" unexpected non-modifiable status for module: %p\n", module);
170
return FAILED;
171
}
172
return PASSED;
173
}
174
175
JNIEXPORT jint JNICALL
176
Java_MyPackage_IsModifiableModuleTest_check(JNIEnv *env, jclass cls) {
177
jobject module = NULL;
178
179
if (jvmti == NULL) {
180
throw_exc(env, "JVMTI client was not properly loaded!\n");
181
return FAILED;
182
}
183
184
printf("\n*** Testing IsModifiableModule ***\n\n");
185
186
if (check_is_modifiable_error_codes(module, cls) == FAILED) {
187
throw_exc(env, "check #MM0: failed to return expected error code from "
188
"a bad call to JVMTI IsModifiableModule");
189
return FAILED;
190
}
191
192
module = get_module_by_class_name(env, "java/lang/Class");
193
if (check_is_modifiable(module) == FAILED) {
194
throw_exc(env, "check #MM1: failed to return modifiable module status");
195
return FAILED;
196
}
197
198
module = get_module_by_class_name(env, "com/sun/jdi/VirtualMachine");
199
if (check_is_modifiable(module) == FAILED) {
200
throw_exc(env, "check #MM2: failed to return modifiable module status");
201
return FAILED;
202
}
203
204
module = get_module_by_class_name(env, "MyPackage/IsModifiableModuleTest");
205
if (check_is_modifiable(module) == FAILED) {
206
throw_exc(env, "check #MM3: failed to return modifiable module status");
207
return FAILED;
208
}
209
210
return PASSED;
211
}
212
213
#ifdef __cplusplus
214
}
215
#endif
216
217