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 "java_lang_Class.h"
  39 
  40 /* defined in libverify.so/verify.dll (src file common/check_format.c) */
  41 extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed);
  42 extern jboolean VerifyFixClassname(char *utf_name);
  43 
  44 #define OBJ "Ljava/lang/Object;"
  45 #define CLS "Ljava/lang/Class;"
  46 #define CPL "Ljdk/internal/reflect/ConstantPool;"
  47 #define STR "Ljava/lang/String;"
  48 #define FLD "Ljava/lang/reflect/Field;"
  49 #define MHD "Ljava/lang/reflect/Method;"
  50 #define CTR "Ljava/lang/reflect/Constructor;"
  51 #define PD  "Ljava/security/ProtectionDomain;"
  52 #define BA  "[B"
  53 
  54 static JNINativeMethod methods[] = {
  55     {"initClassName",    "()" STR,          (void *)&JVM_InitClassName},
  56     {"getSuperclass",    "()" CLS,          NULL},
  57     {"getInterfaces0",   "()[" CLS,         (void *)&JVM_GetClassInterfaces},
  58     {"isInterface",      "()Z",             (void *)&JVM_IsInterface},
  59     {"getSigners",       "()[" OBJ,         (void *)&JVM_GetClassSigners},
  60     {"setSigners",       "([" OBJ ")V",     (void *)&JVM_SetClassSigners},
  61     {"isArray",          "()Z",             (void *)&JVM_IsArrayClass},
  62     {"isPrimitive",      "()Z",             (void *)&JVM_IsPrimitiveClass},
  63     {"isHiddenClass",    "()Z",             (void *)&JVM_IsHiddenClass},
  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",         "(Z)" CLS,      (void *)&JVM_GetNestHost},
  79     {"getNestMembers0",      "()[" CLS,     (void *)&JVM_GetNestMembers},
  80 };
  81 
  82 #undef OBJ
  83 #undef CLS
  84 #undef STR
  85 #undef FLD
  86 #undef MHD
  87 #undef CTR
  88 #undef PD
  89 
  90 JNIEXPORT void JNICALL
  91 Java_java_lang_Class_registerNatives(JNIEnv *env, jclass cls)
  92 {
  93     methods[1].fnPtr = (void *)(*env)->GetSuperclass;
  94     (*env)->RegisterNatives(env, cls, methods,
  95                             sizeof(methods)/sizeof(JNINativeMethod));
  96 }
  97 
  98 JNIEXPORT jclass JNICALL
  99 Java_java_lang_Class_forName0(JNIEnv *env, jclass this, jstring classname,
 100                               jboolean initialize, jobject loader, jclass caller)
 101 {
 102     char *clname;
 103     jclass cls = 0;
 104     char buf[128];
 105     jsize len;
 106     jsize unicode_len;
 107 
 108     if (classname == NULL) {
 109         JNU_ThrowNullPointerException(env, 0);
 110         return 0;
 111     }
 112 
 113     len = (*env)->GetStringUTFLength(env, classname);
 114     unicode_len = (*env)->GetStringLength(env, classname);
 115     if (len >= (jsize)sizeof(buf)) {
 116         clname = malloc(len + 1);
 117         if (clname == NULL) {
 118             JNU_ThrowOutOfMemoryError(env, NULL);
 119             return NULL;
 120         }
 121     } else {
 122         clname = buf;
 123     }
 124     (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);
 125 
 126     if (VerifyFixClassname(clname) == JNI_TRUE) {
 127         /* slashes present in clname, use name b4 translation for exception */
 128         (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);
 129         JNU_ThrowClassNotFoundException(env, clname);
 130         goto done;
 131     }
 132 
 133     if (!VerifyClassname(clname, JNI_TRUE)) {  /* expects slashed name */
 134         JNU_ThrowClassNotFoundException(env, clname);
 135         goto done;
 136     }
 137 
 138     cls = JVM_FindClassFromCaller(env, clname, initialize, loader, caller);
 139 
 140  done:
 141     if (clname != buf) {
 142         free(clname);
 143     }
 144     return cls;
 145 }
 146 
 147 JNIEXPORT jboolean JNICALL
 148 Java_java_lang_Class_isInstance(JNIEnv *env, jobject cls, jobject obj)
 149 {
 150     if (obj == NULL) {
 151         return JNI_FALSE;
 152     }
 153     return (*env)->IsInstanceOf(env, obj, (jclass)cls);
 154 }
 155 
 156 JNIEXPORT jboolean JNICALL
 157 Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jobject cls, jobject cls2)
 158 {
 159     if (cls2 == NULL) {
 160         JNU_ThrowNullPointerException(env, 0);
 161         return JNI_FALSE;
 162     }
 163     return (*env)->IsAssignableFrom(env, cls2, cls);
 164 }
 165 
 166 JNIEXPORT jclass JNICALL
 167 Java_java_lang_Class_getPrimitiveClass(JNIEnv *env,
 168                                        jclass cls,
 169                                        jstring name)
 170 {
 171     const char *utfName;
 172     jclass result;
 173 
 174     if (name == NULL) {
 175         JNU_ThrowNullPointerException(env, 0);
 176         return NULL;
 177     }
 178 
 179     utfName = (*env)->GetStringUTFChars(env, name, 0);
 180     if (utfName == 0)
 181         return NULL;
 182 
 183     result = JVM_FindPrimitiveClass(env, utfName);
 184 
 185     (*env)->ReleaseStringUTFChars(env, name, utfName);
 186 
 187     return result;
 188 }