1 /* 2 * Copyright (c) 1994, 2022, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /*- 27 * Implementation of class Class 28 * 29 * former threadruntime.c, Sun Sep 22 12:09:39 1991 30 */ 31 32 #include <string.h> 33 #include <stdlib.h> 34 35 #include "jni.h" 36 #include "jni_util.h" 37 #include "jvm.h" 38 #include "check_classname.h" 39 #include "java_lang_Class.h" 40 41 /* defined in libverify.so/verify.dll (src file common/check_format.c) */ 42 extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed); 43 extern jboolean VerifyFixClassname(char *utf_name); 44 45 #define OBJ "Ljava/lang/Object;" 46 #define CLS "Ljava/lang/Class;" 47 #define CPL "Ljdk/internal/reflect/ConstantPool;" 48 #define STR "Ljava/lang/String;" 49 #define FLD "Ljava/lang/reflect/Field;" 50 #define MHD "Ljava/lang/reflect/Method;" 51 #define CTR "Ljava/lang/reflect/Constructor;" 52 #define PD "Ljava/security/ProtectionDomain;" 53 #define BA "[B" 54 #define RC "Ljava/lang/reflect/RecordComponent;" 55 56 static JNINativeMethod methods[] = { 57 {"initClassName", "()" STR, (void *)&JVM_InitClassName}, 58 {"getSuperclass", "()" CLS, NULL}, 59 {"getInterfaces0", "()[" CLS, (void *)&JVM_GetClassInterfaces}, 60 {"isInterface", "()Z", (void *)&JVM_IsInterface}, 61 {"isArray", "()Z", (void *)&JVM_IsArrayClass}, 62 {"isHidden", "()Z", (void *)&JVM_IsHiddenClass}, 63 {"isPrimitive", "()Z", (void *)&JVM_IsPrimitiveClass}, 64 {"getModifiers", "()I", (void *)&JVM_GetClassModifiers}, 65 {"getDeclaredFields0","(Z)[" FLD, (void *)&JVM_GetClassDeclaredFields}, 66 {"getDeclaredMethods0","(Z)[" MHD, (void *)&JVM_GetClassDeclaredMethods}, 67 {"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors}, 68 {"getProtectionDomain0", "()" PD, (void *)&JVM_GetProtectionDomain}, 69 {"getDeclaredClasses0", "()[" CLS, (void *)&JVM_GetDeclaredClasses}, 70 {"getDeclaringClass0", "()" CLS, (void *)&JVM_GetDeclaringClass}, 71 {"getSimpleBinaryName0", "()" STR, (void *)&JVM_GetSimpleBinaryName}, 72 {"getGenericSignature0", "()" STR, (void *)&JVM_GetClassSignature}, 73 {"getRawAnnotations", "()" BA, (void *)&JVM_GetClassAnnotations}, 74 {"getConstantPool", "()" CPL, (void *)&JVM_GetClassConstantPool}, 75 {"desiredAssertionStatus0","("CLS")Z", (void *)&JVM_DesiredAssertionStatus}, 76 {"getEnclosingMethod0", "()[" OBJ, (void *)&JVM_GetEnclosingMethodInfo}, 77 {"getRawTypeAnnotations", "()" BA, (void *)&JVM_GetClassTypeAnnotations}, 78 {"getNestHost0", "()" CLS, (void *)&JVM_GetNestHost}, 79 {"getNestMembers0", "()[" CLS, (void *)&JVM_GetNestMembers}, 80 {"getRecordComponents0", "()[" RC, (void *)&JVM_GetRecordComponents}, 81 {"isRecord0", "()Z", (void *)&JVM_IsRecord}, 82 {"getPermittedSubclasses0", "()[" CLS, (void *)&JVM_GetPermittedSubclasses}, 83 {"getClassFileVersion0", "()I", (void *)&JVM_GetClassFileVersion}, 84 {"getClassAccessFlagsRaw0", "()I", (void *)&JVM_GetClassAccessFlags}, 85 }; 86 87 #undef OBJ 88 #undef CLS 89 #undef STR 90 #undef FLD 91 #undef MHD 92 #undef CTR 93 #undef PD 94 95 JNIEXPORT void JNICALL 96 Java_java_lang_Class_registerNatives(JNIEnv *env, jclass cls) 97 { 98 methods[1].fnPtr = (void *)(*env)->GetSuperclass; 99 (*env)->RegisterNatives(env, cls, methods, 100 sizeof(methods)/sizeof(JNINativeMethod)); 101 } 102 103 JNIEXPORT jclass JNICALL 104 Java_java_lang_Class_forName0(JNIEnv *env, jclass this, jstring classname, 105 jboolean initialize, jobject loader, jclass caller) 106 { 107 char *clname; 108 jclass cls = 0; 109 char buf[128]; 110 jsize len; 111 jsize unicode_len; 112 113 if (classname == NULL) { 114 JNU_ThrowNullPointerException(env, 0); 115 return 0; 116 } 117 118 len = (*env)->GetStringUTFLength(env, classname); 119 unicode_len = (*env)->GetStringLength(env, classname); 120 if (len >= (jsize)sizeof(buf)) { 121 clname = malloc(len + 1); 122 if (clname == NULL) { 123 JNU_ThrowOutOfMemoryError(env, NULL); 124 return NULL; 125 } 126 } else { 127 clname = buf; 128 } 129 (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname); 130 131 if (verifyFixClassname(clname) == JNI_TRUE) { 132 /* slashes present in clname, use name b4 translation for exception */ 133 (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname); 134 JNU_ThrowClassNotFoundException(env, clname); 135 goto done; 136 } 137 138 if (!verifyClassname(clname, JNI_TRUE)) { /* expects slashed name */ 139 JNU_ThrowClassNotFoundException(env, clname); 140 goto done; 141 } 142 143 cls = JVM_FindClassFromCaller(env, clname, initialize, loader, caller); 144 145 done: 146 if (clname != buf) { 147 free(clname); 148 } 149 return cls; 150 } 151 152 JNIEXPORT jboolean JNICALL 153 Java_java_lang_Class_isInstance(JNIEnv *env, jobject cls, jobject obj) 154 { 155 if (obj == NULL) { 156 return JNI_FALSE; 157 } 158 return (*env)->IsInstanceOf(env, obj, (jclass)cls); 159 } 160 161 JNIEXPORT jboolean JNICALL 162 Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jobject cls, jobject cls2) 163 { 164 if (cls2 == NULL) { 165 JNU_ThrowNullPointerException(env, 0); 166 return JNI_FALSE; 167 } 168 return (*env)->IsAssignableFrom(env, cls2, cls); 169 } 170 171 JNIEXPORT jclass JNICALL 172 Java_java_lang_Class_getPrimitiveClass(JNIEnv *env, 173 jclass cls, 174 jstring name) 175 { 176 const char *utfName; 177 jclass result; 178 179 if (name == NULL) { 180 JNU_ThrowNullPointerException(env, 0); 181 return NULL; 182 } 183 184 utfName = (*env)->GetStringUTFChars(env, name, 0); 185 if (utfName == 0) 186 return NULL; 187 188 result = JVM_FindPrimitiveClass(env, utfName); 189 190 (*env)->ReleaseStringUTFChars(env, name, utfName); 191 192 return result; 193 }