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 extern "C" {
31 
32 
33 #define PASSED 0
34 #define STATUS_FAILED 2
35 
36 #define ACC_PUBLIC        0x001
37 #define ACC_PRIVATE       0x002
38 #define ACC_PROTECTED     0x004
39 #define ACC_STATIC        0x008
40 #define ACC_FINAL         0x010
41 #define ACC_SYNCHRONIZED  0x020
42 #define ACC_NATIVE        0x100
43 #define ACC_ABSTRACT      0x400
44 #define ACC_STRICT        0x800
45 
46 static jvmtiEnv *jvmti = NULL;
47 static jvmtiEventCallbacks callbacks;
48 static jint result = PASSED;
49 static jboolean printdump = JNI_FALSE;
50 
printModifiers(jint mod)51 void printModifiers(jint mod) {
52     if (mod & ACC_PUBLIC) printf(" PUBLIC");
53     if (mod & ACC_PRIVATE) printf(" PRIVATE");
54     if (mod & ACC_PROTECTED) printf(" PROTECTED");
55     if (mod & ACC_STATIC) printf(" STATIC");
56     if (mod & ACC_FINAL) printf(" FINAL");
57     if (mod & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
58     if (mod & ACC_NATIVE) printf(" NATIVE");
59     if (mod & ACC_ABSTRACT) printf(" ABSTRACT");
60     if (mod & ACC_STRICT) printf(" STRICT");
61     printf(" (0x%0x)\n", mod);
62 }
63 
checkMeth(jvmtiEnv * jvmti_env,JNIEnv * env,jclass cl,const char * name,const char * sig,int stat,int flags)64 void checkMeth(jvmtiEnv *jvmti_env, JNIEnv *env, jclass cl,
65         const char *name, const char *sig, int stat, int flags) {
66     jvmtiError err;
67     jmethodID mid;
68     jint modifiers;
69 
70     if (stat) {
71         mid = env->GetStaticMethodID(cl, name, sig);
72     } else {
73         mid = env->GetMethodID(cl, name, sig);
74     }
75     if (mid == NULL) {
76         printf("Cannot find MethodID for \"%s%s\"\n", name, sig);
77         result = STATUS_FAILED;
78         return;
79     }
80 
81     err = jvmti_env->GetMethodModifiers(mid, &modifiers);
82     if (err != JVMTI_ERROR_NONE) {
83         printf("\"%s%s\"\n", name, sig);
84         printf("(GetClassModifiers) unexpected error: %s (%d)\n",
85                TranslateError(err), err);
86         result = STATUS_FAILED;
87         return;
88     }
89 
90     if (printdump == JNI_TRUE) {
91         printf(">>> %s%s" , name, sig);
92         printModifiers(modifiers);
93     }
94 
95     if (modifiers != flags) {
96         printf("\"%s%s\" access flags expected:", name, sig);
97         printModifiers(flags);
98         printf("\t       actual:");
99         printModifiers(modifiers);
100         result = STATUS_FAILED;
101     }
102 }
103 
104 void JNICALL
ClassLoad(jvmtiEnv * jvmti_env,JNIEnv * env,jthread thr,jclass cls)105 ClassLoad(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr, jclass cls) {
106     jvmtiError err;
107     char *sig, *generic;
108 
109     err = jvmti_env->GetClassSignature(cls, &sig, &generic);
110     if (err != JVMTI_ERROR_NONE) {
111         printf("(GetClassSignature) unexpected error: %s (%d)\n",
112                TranslateError(err), err);
113         return;
114     }
115     if (strcmp(sig, "Lnsk/jvmti/GetMethodModifiers/methmod001;") == 0) {
116         checkMeth(jvmti_env, (JNIEnv *)env, cls,
117             "run", "([Ljava/lang/String;Ljava/io/PrintStream;)I",
118             1, ACC_PUBLIC |  ACC_STATIC);
119         checkMeth(jvmti_env, (JNIEnv *)env, cls,
120             "meth_stat", "(ILjava/lang/String;)[F",
121             1, ACC_PROTECTED |  ACC_STATIC |  ACC_FINAL);
122     }
123 }
124 
125 #ifdef STATIC_BUILD
Agent_OnLoad_methmod001(JavaVM * jvm,char * options,void * reserved)126 JNIEXPORT jint JNICALL Agent_OnLoad_methmod001(JavaVM *jvm, char *options, void *reserved) {
127     return Agent_Initialize(jvm, options, reserved);
128 }
Agent_OnAttach_methmod001(JavaVM * jvm,char * options,void * reserved)129 JNIEXPORT jint JNICALL Agent_OnAttach_methmod001(JavaVM *jvm, char *options, void *reserved) {
130     return Agent_Initialize(jvm, options, reserved);
131 }
JNI_OnLoad_methmod001(JavaVM * jvm,char * options,void * reserved)132 JNIEXPORT jint JNI_OnLoad_methmod001(JavaVM *jvm, char *options, void *reserved) {
133     return JNI_VERSION_1_8;
134 }
135 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)136 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
137     jint res;
138     jvmtiError err;
139 
140     if (options != NULL && strcmp(options, "printdump") == 0) {
141         printdump = JNI_TRUE;
142     }
143 
144     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
145     if (res != JNI_OK || jvmti == NULL) {
146         printf("Wrong result of a valid call to GetEnv!\n");
147         return JNI_ERR;
148     }
149 
150     callbacks.ClassLoad = &ClassLoad;
151     err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
152     if (err != JVMTI_ERROR_NONE) {
153         printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
154                TranslateError(err), err);
155         return JNI_ERR;
156     }
157 
158     err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
159         JVMTI_EVENT_CLASS_LOAD, NULL);
160     if (err != JVMTI_ERROR_NONE) {
161         printf("Failed to enable event JVMTI_EVENT_CLASS_LOAD: %s (%d)\n",
162                TranslateError(err), err);
163         return JNI_ERR;
164     }
165 
166     return JNI_OK;
167 }
168 
Java_nsk_jvmti_GetMethodModifiers_methmod001_check(JNIEnv * env,jclass cls)169 JNIEXPORT jint JNICALL Java_nsk_jvmti_GetMethodModifiers_methmod001_check(JNIEnv *env, jclass cls) {
170     jclass clsId;
171 
172     checkMeth(jvmti, env, cls, "<init>", "()V", 0,  ACC_PUBLIC);
173     checkMeth(jvmti, env, cls, "meth_1", "(C)C", 0,  ACC_PRIVATE);
174     checkMeth(jvmti, env, cls, "meth_2", "(FF)F", 0,  ACC_STRICT);
175     checkMeth(jvmti, env, cls, "check", "()I", 1,  ACC_NATIVE |  ACC_STATIC);
176     clsId = env->FindClass("nsk/jvmti/GetMethodModifiers/methmod001a");
177     checkMeth(jvmti, env, clsId, "meth_new", "()Lnsk/jvmti/GetMethodModifiers/methmod001;", 0,  ACC_SYNCHRONIZED);
178     checkMeth(jvmti, env, clsId, "meth_abs", "()V", 0,  ACC_ABSTRACT);
179     clsId = env->FindClass("nsk/jvmti/GetMethodModifiers/methmod001$Inn");
180     checkMeth(jvmti, env, clsId, "meth_inn", "(Ljava/lang/String;)V", 0, ACC_PUBLIC |  ACC_SYNCHRONIZED |  ACC_FINAL);
181 
182     return result;
183 }
184 
185 }
186