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_redefclass020(JavaVM * jvm,char * options,void * reserved)55 JNIEXPORT jint JNICALL Agent_OnLoad_redefclass020(JavaVM *jvm, char *options, void *reserved) {
56     return Agent_Initialize(jvm, options, reserved);
57 }
Agent_OnAttach_redefclass020(JavaVM * jvm,char * options,void * reserved)58 JNIEXPORT jint JNICALL Agent_OnAttach_redefclass020(JavaVM *jvm, char *options, void *reserved) {
59     return Agent_Initialize(jvm, options, reserved);
60 }
JNI_OnLoad_redefclass020(JavaVM * jvm,char * options,void * reserved)61 JNIEXPORT jint JNI_OnLoad_redefclass020(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_redefclass020_check(JNIEnv * env,jclass cls,jclass clazz,jbyteArray bytes)109 Java_nsk_jvmti_RedefineClasses_redefclass020_check(JNIEnv *env, jclass cls,
110          jclass clazz, jbyteArray bytes) {
111     jvmtiError err;
112     jvmtiClassDefinition classDef;
113 
114     if (jvmti == NULL) {
115         printf("JVMTI client was not properly loaded!\n");
116         return STATUS_FAILED;
117     }
118 
119     classDef.klass = clazz;
120     classDef.class_byte_count =
121         JNI_ENV_PTR(env)->GetArrayLength(JNI_ENV_ARG(env, bytes));
122     classDef.class_bytes = (unsigned char *)
123         JNI_ENV_PTR(env)->GetByteArrayElements(JNI_ENV_ARG(env, bytes), NULL);
124 
125     if (printdump == JNI_TRUE) {
126         printf(">>> add method capability check ...\n");
127     }
128     err = (*jvmti)->RedefineClasses(jvmti, 1, &classDef);
129 
130     if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY &&
131             !caps.can_redefine_classes) {
132         /* It is OK */
133     } else
134         if (err != JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED) {
135             printf("Error expected: ");
136             printf("JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED,\n");
137             printf("\tactual: %s (%d)\n", TranslateError(err), err);
138             result = STATUS_FAILED;
139         }
140 
141     if (printdump == JNI_TRUE) {
142         printf(">>> ... done\n");
143     }
144 
145     return result;
146 }
147 
148 #ifdef __cplusplus
149 }
150 #endif
151