1 /*
   2  * Copyright (c) 1994, 2019, 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     {"getSigners",       "()[" OBJ,         (void *)&JVM_GetClassSigners},
  62     {"setSigners",       "([" OBJ ")V",     (void *)&JVM_SetClassSigners},
  63     {"isArray",          "()Z",             (void *)&JVM_IsArrayClass},
  64     {"isPrimitive",      "()Z",             (void *)&JVM_IsPrimitiveClass},
  65     {"getModifiers",     "()I",             (void *)&JVM_GetClassModifiers},
  66     {"getDeclaredFields0","(Z)[" FLD,       (void *)&JVM_GetClassDeclaredFields},
  67     {"getDeclaredMethods0","(Z)[" MHD,      (void *)&JVM_GetClassDeclaredMethods},
  68     {"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors},
  69     {"getProtectionDomain0", "()" PD,       (void *)&JVM_GetProtectionDomain},
  70     {"getDeclaredClasses0",  "()[" CLS,     (void *)&JVM_GetDeclaredClasses},
  71     {"getDeclaringClass0",   "()" CLS,      (void *)&JVM_GetDeclaringClass},
  72     {"getSimpleBinaryName0", "()" STR,      (void *)&JVM_GetSimpleBinaryName},
  73     {"getGenericSignature0", "()" STR,      (void *)&JVM_GetClassSignature},
  74     {"getRawAnnotations",      "()" BA,     (void *)&JVM_GetClassAnnotations},
  75     {"getConstantPool",     "()" CPL,       (void *)&JVM_GetClassConstantPool},
  76     {"desiredAssertionStatus0","("CLS")Z",  (void *)&JVM_DesiredAssertionStatus},
  77     {"getEnclosingMethod0", "()[" OBJ,      (void *)&JVM_GetEnclosingMethodInfo},
  78     {"getRawTypeAnnotations", "()" BA,      (void *)&JVM_GetClassTypeAnnotations},
  79     {"getNestHost0",         "()" CLS,      (void *)&JVM_GetNestHost},
  80     {"getNestMembers0",      "()[" CLS,     (void *)&JVM_GetNestMembers},
  81     {"getRecordComponents0", "()[" RC,      (void *)&JVM_GetRecordComponents},
  82     {"isRecord0",            "()Z",         (void *)&JVM_IsRecord},
  83 };
  84 
  85 #undef OBJ
  86 #undef CLS
  87 #undef STR
  88 #undef FLD
  89 #undef MHD
  90 #undef CTR
  91 #undef PD
  92 
  93 JNIEXPORT void JNICALL
  94 Java_java_lang_Class_registerNatives(JNIEnv *env, jclass cls)
  95 {
  96     methods[1].fnPtr = (void *)(*env)->GetSuperclass;
  97     (*env)->RegisterNatives(env, cls, methods,
  98                             sizeof(methods)/sizeof(JNINativeMethod));
  99 }
 100 
 101 JNIEXPORT jclass JNICALL
 102 Java_java_lang_Class_forName0(JNIEnv *env, jclass this, jstring classname,
 103                               jboolean initialize, jobject loader, jclass caller)
 104 {
 105     char *clname;
 106     jclass cls = 0;
 107     char buf[128];
 108     jsize len;
 109     jsize unicode_len;
 110 
 111     if (classname == NULL) {
 112         JNU_ThrowNullPointerException(env, 0);
 113         return 0;
 114     }
 115 
 116     len = (*env)->GetStringUTFLength(env, classname);
 117     unicode_len = (*env)->GetStringLength(env, classname);
 118     if (len >= (jsize)sizeof(buf)) {
 119         clname = malloc(len + 1);
 120         if (clname == NULL) {
 121             JNU_ThrowOutOfMemoryError(env, NULL);
 122             return NULL;
 123         }
 124     } else {
 125         clname = buf;
 126     }
 127     (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);
 128 
 129     if (verifyFixClassname(clname) == JNI_TRUE) {
 130         /* slashes present in clname, use name b4 translation for exception */
 131         (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);
 132         JNU_ThrowClassNotFoundException(env, clname);
 133         goto done;
 134     }
 135 
 136     if (!verifyClassname(clname, JNI_TRUE)) {  /* expects slashed name */
 137         JNU_ThrowClassNotFoundException(env, clname);
 138         goto done;
 139     }
 140 
 141     cls = JVM_FindClassFromCaller(env, clname, initialize, loader, caller);
 142 
 143  done:
 144     if (clname != buf) {
 145         free(clname);
 146     }
 147     return cls;
 148 }
 149 
 150 JNIEXPORT jboolean JNICALL
 151 Java_java_lang_Class_isInstance(JNIEnv *env, jobject cls, jobject obj)
 152 {
 153     if (obj == NULL) {
 154         return JNI_FALSE;
 155     }
 156     return (*env)->IsInstanceOf(env, obj, (jclass)cls);
 157 }
 158 
 159 JNIEXPORT jboolean JNICALL
 160 Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jobject cls, jobject cls2)
 161 {
 162     if (cls2 == NULL) {
 163         JNU_ThrowNullPointerException(env, 0);
 164         return JNI_FALSE;
 165     }
 166     return (*env)->IsAssignableFrom(env, cls2, cls);
 167 }
 168 
 169 JNIEXPORT jclass JNICALL
 170 Java_java_lang_Class_getPrimitiveClass(JNIEnv *env,
 171                                        jclass cls,
 172                                        jstring name)
 173 {
 174     const char *utfName;
 175     jclass result;
 176 
 177     if (name == NULL) {
 178         JNU_ThrowNullPointerException(env, 0);
 179         return NULL;
 180     }
 181 
 182     utfName = (*env)->GetStringUTFChars(env, name, 0);
 183     if (utfName == 0)
 184         return NULL;
 185 
 186     result = JVM_FindPrimitiveClass(env, utfName);
 187 
 188     (*env)->ReleaseStringUTFChars(env, name, utfName);
 189 
 190     return result;
 191 }