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_redefclass013(JavaVM * jvm,char * options,void * reserved)55 JNIEXPORT jint JNICALL Agent_OnLoad_redefclass013(JavaVM *jvm, char *options, void *reserved) {
56     return Agent_Initialize(jvm, options, reserved);
57 }
Agent_OnAttach_redefclass013(JavaVM * jvm,char * options,void * reserved)58 JNIEXPORT jint JNICALL Agent_OnAttach_redefclass013(JavaVM *jvm, char *options, void *reserved) {
59     return Agent_Initialize(jvm, options, reserved);
60 }
JNI_OnLoad_redefclass013(JavaVM * jvm,char * options,void * reserved)61 JNIEXPORT jint JNI_OnLoad_redefclass013(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_redefclass013_check(JNIEnv * env,jclass cls,jbyteArray jbytes)109 Java_nsk_jvmti_RedefineClasses_redefclass013_check(JNIEnv *env,
110         jclass cls, jbyteArray jbytes) {
111     jvmtiError err;
112     unsigned char *bytes;
113     jvmtiClassDefinition classDef;
114 
115     if (jvmti == NULL) {
116         printf("JVMTI client was not properly loaded!\n");
117         return STATUS_FAILED;
118     }
119 
120     classDef.klass = cls;
121     classDef.class_byte_count =
122         JNI_ENV_PTR(env)->GetArrayLength(JNI_ENV_ARG(env, jbytes));
123     bytes = (unsigned char *)
124         JNI_ENV_PTR(env)->GetByteArrayElements(JNI_ENV_ARG(env, jbytes), NULL);
125 
126     /* put 0 into magic field */
127     bytes[0] = 0;
128     bytes[1] = 0;
129     bytes[2] = 0;
130     bytes[3] = 0;
131 
132     classDef.class_bytes = bytes;
133 
134     if (printdump == JNI_TRUE) {
135         printf(">>> invalid class format check ...\n");
136     }
137     err = (*jvmti)->RedefineClasses(jvmti, 1, &classDef);
138     if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY &&
139             !caps.can_redefine_classes) {
140         /* It is OK */
141     } else if (err != JVMTI_ERROR_INVALID_CLASS_FORMAT) {
142         printf("Error expected: JVMTI_ERROR_INVALID_CLASS_FORMAT,\n");
143         printf("\tactual: %s (%d)\n", TranslateError(err), err);
144         result = STATUS_FAILED;
145     }
146 
147     if (printdump == JNI_TRUE) {
148         printf(">>> ... done\n");
149     }
150 
151     return result;
152 }
153 
154 #ifdef __cplusplus
155 }
156 #endif
157