1 /*
2 * Copyright (c) 2003, 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
24 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 #include "agent_common.h"
28 #include "JVMTITools.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #ifndef JNI_ENV_ARG
35
36 #ifdef __cplusplus
37 #define JNI_ENV_ARG(x, y) y
38 #define JNI_ENV_PTR(x) x
39 #else
40 #define JNI_ENV_ARG(x,y) x, y
41 #define JNI_ENV_PTR(x) (*x)
42 #endif
43
44 #endif
45
46 #define PASSED 0
47 #define STATUS_FAILED 2
48
49 static jvmtiEnv *jvmti = NULL;
50 static jvmtiCapabilities caps;
51 static jint result = PASSED;
52 static jboolean printdump = JNI_FALSE;
53
54 #ifdef STATIC_BUILD
Agent_OnLoad_redefclass014(JavaVM * jvm,char * options,void * reserved)55 JNIEXPORT jint JNICALL Agent_OnLoad_redefclass014(JavaVM *jvm, char *options, void *reserved) {
56 return Agent_Initialize(jvm, options, reserved);
57 }
Agent_OnAttach_redefclass014(JavaVM * jvm,char * options,void * reserved)58 JNIEXPORT jint JNICALL Agent_OnAttach_redefclass014(JavaVM *jvm, char *options, void *reserved) {
59 return Agent_Initialize(jvm, options, reserved);
60 }
JNI_OnLoad_redefclass014(JavaVM * jvm,char * options,void * reserved)61 JNIEXPORT jint JNI_OnLoad_redefclass014(JavaVM *jvm, char *options, void *reserved) {
62 return JNI_VERSION_1_8;
63 }
64 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)65 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
66 jint res;
67 jvmtiError err;
68
69 if (options != NULL && strcmp(options, "printdump") == 0) {
70 printdump = JNI_TRUE;
71 }
72
73 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
74 JVMTI_VERSION_1_1);
75 if (res != JNI_OK || jvmti == NULL) {
76 printf("Wrong result of a valid call to GetEnv!\n");
77 return JNI_ERR;
78 }
79
80 err = (*jvmti)->GetPotentialCapabilities(jvmti, &caps);
81 if (err != JVMTI_ERROR_NONE) {
82 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
83 TranslateError(err), err);
84 return JNI_ERR;
85 }
86
87 err = (*jvmti)->AddCapabilities(jvmti, &caps);
88 if (err != JVMTI_ERROR_NONE) {
89 printf("(AddCapabilities) unexpected error: %s (%d)\n",
90 TranslateError(err), err);
91 return JNI_ERR;
92 }
93
94 err = (*jvmti)->GetCapabilities(jvmti, &caps);
95 if (err != JVMTI_ERROR_NONE) {
96 printf("(GetCapabilities) unexpected error: %s (%d)\n",
97 TranslateError(err), err);
98 return JNI_ERR;
99 }
100
101 if (!caps.can_redefine_classes) {
102 printf("Warning: RedefineClasses is not implemented\n");
103 }
104
105 return JNI_OK;
106 }
107
108 JNIEXPORT jint JNICALL
Java_nsk_jvmti_RedefineClasses_redefclass014_check(JNIEnv * env,jclass cls,jbyteArray bytes)109 Java_nsk_jvmti_RedefineClasses_redefclass014_check(JNIEnv *env, jclass cls, jbyteArray bytes) {
110 jvmtiError err;
111 jvmtiClassDefinition classDef;
112
113 if (jvmti == NULL) {
114 printf("JVMTI client was not properly loaded!\n");
115 return STATUS_FAILED;
116 }
117
118 /* to check if RedefineClasses is supported */
119 if ((err = ((*jvmti)->GetCapabilities(jvmti, &caps))) != JVMTI_ERROR_NONE) {
120 printf("(GetCapabilities) unexpected error: %s (%d)\n",
121 TranslateError(err), err);
122 return STATUS_FAILED;
123 }
124
125 classDef.klass = cls;
126 classDef.class_byte_count =
127 JNI_ENV_PTR(env)->GetArrayLength(JNI_ENV_ARG(env, bytes));
128 classDef.class_bytes = (unsigned char *)
129 JNI_ENV_PTR(env)->GetByteArrayElements(JNI_ENV_ARG(env, bytes), NULL);
130
131 if (printdump == JNI_TRUE) {
132 printf(">>> circularity error check ...\n");
133 }
134 err = (*jvmti)->RedefineClasses(jvmti, 1, &classDef);
135 if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY &&
136 !caps.can_redefine_classes) {
137 /* It is OK */
138 } else if (err == JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED) {
139 printf("Warning: RedefineClasses is restricted\n");
140 } else if (err != JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION) {
141 printf("Error expected: JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION,\n");
142 printf("\tactual: %s (%d)\n", TranslateError(err), err);
143 result = STATUS_FAILED;
144 }
145
146 if (printdump == JNI_TRUE) {
147 printf(">>> ... done\n");
148 }
149
150 return result;
151 }
152
153 #ifdef __cplusplus
154 }
155 #endif
156