1 /*
  2  * Copyright (c) 2003, 2021, 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      0x0001
 37 #define ACC_PRIVATE     0x0002
 38 #define ACC_PROTECTED   0x0004
 39 #define ACC_STATIC      0x0008
 40 #define ACC_FINAL       0x0010
 41 #define ACC_SUPER       0x0020
 42 #define ACC_INTERFACE   0x0200
 43 #define ACC_ABSTRACT    0x0400
 44 
 45 static jvmtiEnv *jvmti = NULL;
 46 static jint result = PASSED;
 47 static jboolean printdump = JNI_FALSE;
 48 
 49 #ifdef STATIC_BUILD
 50 JNIEXPORT jint JNICALL Agent_OnLoad_getclmdf006(JavaVM *jvm, char *options, void *reserved) {
 51     return Agent_Initialize(jvm, options, reserved);
 52 }
 53 JNIEXPORT jint JNICALL Agent_OnAttach_getclmdf006(JavaVM *jvm, char *options, void *reserved) {
 54     return Agent_Initialize(jvm, options, reserved);
 55 }
 56 JNIEXPORT jint JNI_OnLoad_getclmdf006(JavaVM *jvm, char *options, void *reserved) {
 57     return JNI_VERSION_1_8;
 58 }
 59 #endif
 60 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 61     jint res;
 62 
 63     if (options != NULL && strcmp(options, "printdump") == 0) {
 64         printdump = JNI_TRUE;
 65     }
 66 
 67     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
 68     if (res != JNI_OK || jvmti == NULL) {
 69         printf("Wrong result of a valid call to GetEnv!\n");
 70         return JNI_ERR;
 71     }
 72 
 73     return JNI_OK;
 74 }
 75 
 76 void printModifiers(jint mod) {
 77     if (mod & ACC_PUBLIC) printf(" PUBLIC");
 78     if (mod & ACC_PRIVATE) printf(" PRIVATE");
 79     if (mod & ACC_PROTECTED) printf(" PROTECTED");
 80     if (mod & ACC_STATIC) printf(" STATIC");
 81     if (mod & ACC_FINAL) printf(" FINAL");
 82     if (mod & ACC_SUPER) printf(" SUPER");
 83     if (mod & ACC_INTERFACE) printf(" INTERFACE");
 84     if (mod & ACC_ABSTRACT) printf(" ABSTRACT");
 85     printf(" (0x%0x)\n", mod);
 86 }
 87 
 88 JNIEXPORT void JNICALL
 89 Java_nsk_jvmti_GetClassModifiers_getclmdf006_check(JNIEnv *env, jclass cls, jclass clazz, jint mod) {
 90     jvmtiError err;
 91     jint modifiers;
 92 
 93     if (jvmti == NULL) {
 94         printf("JVMTI client was not properly loaded!\n");
 95         result = STATUS_FAILED;
 96         return;
 97     }
 98 
 99     err = jvmti->GetClassModifiers(clazz, &modifiers);
100     if (err != JVMTI_ERROR_NONE) {
101         printf("(GetClassModifiers:0x%x) unexpected error: %s (%d)\n",
102                mod, TranslateError(err), err);
103         result = STATUS_FAILED;
104         return;
105     }
106 
107     if (printdump == JNI_TRUE) {
108         printf(">>>");
109         printModifiers(modifiers);
110     }
111 
112     if ((modifiers & (~ACC_SUPER)) != mod) {
113         printf("Access flags expected:");
114         printModifiers(mod);
115         printf("\t       actual:");
116         printModifiers(modifiers);
117         result = STATUS_FAILED;
118     }
119 }
120 
121 JNIEXPORT int JNICALL Java_nsk_jvmti_GetClassModifiers_getclmdf006_getRes(JNIEnv *env, jclass cls) {
122     return result;
123 }
124 
125 }