Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/lang/ClassLoader.c
38829 views
/*1* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <assert.h>2728#include "jni.h"29#include "jni_util.h"30#include "jlong.h"31#include "jvm.h"32#include "java_lang_ClassLoader.h"33#include "java_lang_ClassLoader_NativeLibrary.h"34#include <string.h>3536/* defined in libverify.so/verify.dll (src file common/check_format.c) */37extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed);38extern jboolean VerifyFixClassname(char *utf_name);3940static JNINativeMethod methods[] = {41{"retrieveDirectives", "()Ljava/lang/AssertionStatusDirectives;", (void *)&JVM_AssertionStatusDirectives}42};4344JNIEXPORT void JNICALL45Java_java_lang_ClassLoader_registerNatives(JNIEnv *env, jclass cls)46{47(*env)->RegisterNatives(env, cls, methods,48sizeof(methods)/sizeof(JNINativeMethod));49}5051/* Convert java string to UTF char*. Use local buffer if possible,52otherwise malloc new memory. Returns null IFF malloc failed. */53char*54getUTF(JNIEnv *env, jstring str, char* localBuf, int bufSize)55{56char* utfStr = NULL;5758int len = (*env)->GetStringUTFLength(env, str);59int unicode_len = (*env)->GetStringLength(env, str);60if (len >= bufSize) {61utfStr = malloc(len + 1);62if (utfStr == NULL) {63JNU_ThrowOutOfMemoryError(env, NULL);64return NULL;65}66} else {67utfStr = localBuf;68}69(*env)->GetStringUTFRegion(env, str, 0, unicode_len, utfStr);7071return utfStr;72}7374// The existence or signature of this method is not guaranteed since it75// supports a private method. This method will be changed in 1.7.76JNIEXPORT jclass JNICALL77Java_java_lang_ClassLoader_defineClass0(JNIEnv *env,78jobject loader,79jstring name,80jbyteArray data,81jint offset,82jint length,83jobject pd)84{85return Java_java_lang_ClassLoader_defineClass1(env, loader, name, data, offset,86length, pd, NULL);87}8889JNIEXPORT jclass JNICALL90Java_java_lang_ClassLoader_defineClass1(JNIEnv *env,91jobject loader,92jstring name,93jbyteArray data,94jint offset,95jint length,96jobject pd,97jstring source)98{99jbyte *body;100char *utfName;101jclass result = 0;102char buf[128];103char* utfSource;104char sourceBuf[1024];105106if (data == NULL) {107JNU_ThrowNullPointerException(env, 0);108return 0;109}110111/* Work around 4153825. malloc crashes on Solaris when passed a112* negative size.113*/114if (length < 0) {115JNU_ThrowArrayIndexOutOfBoundsException(env, 0);116return 0;117}118119body = (jbyte *)malloc(length);120121if (body == 0) {122JNU_ThrowOutOfMemoryError(env, 0);123return 0;124}125126(*env)->GetByteArrayRegion(env, data, offset, length, body);127128if ((*env)->ExceptionOccurred(env))129goto free_body;130131if (name != NULL) {132utfName = getUTF(env, name, buf, sizeof(buf));133if (utfName == NULL) {134goto free_body;135}136VerifyFixClassname(utfName);137} else {138utfName = NULL;139}140141if (source != NULL) {142utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf));143if (utfSource == NULL) {144goto free_utfName;145}146} else {147utfSource = NULL;148}149result = JVM_DefineClassWithSource(env, utfName, loader, body, length, pd, utfSource);150151if (utfSource && utfSource != sourceBuf)152free(utfSource);153154free_utfName:155if (utfName && utfName != buf)156free(utfName);157158free_body:159free(body);160return result;161}162163JNIEXPORT jclass JNICALL164Java_java_lang_ClassLoader_defineClass2(JNIEnv *env,165jobject loader,166jstring name,167jobject data,168jint offset,169jint length,170jobject pd,171jstring source)172{173jbyte *body;174char *utfName;175jclass result = 0;176char buf[128];177char* utfSource;178char sourceBuf[1024];179180assert(data != NULL); // caller fails if data is null.181assert(length >= 0); // caller passes ByteBuffer.remaining() for length, so never neg.182// caller passes ByteBuffer.position() for offset, and capacity() >= position() + remaining()183assert((*env)->GetDirectBufferCapacity(env, data) >= (offset + length));184185body = (*env)->GetDirectBufferAddress(env, data);186187if (body == 0) {188JNU_ThrowNullPointerException(env, 0);189return 0;190}191192body += offset;193194if (name != NULL) {195utfName = getUTF(env, name, buf, sizeof(buf));196if (utfName == NULL) {197JNU_ThrowOutOfMemoryError(env, NULL);198return result;199}200VerifyFixClassname(utfName);201} else {202utfName = NULL;203}204205if (source != NULL) {206utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf));207if (utfSource == NULL) {208JNU_ThrowOutOfMemoryError(env, NULL);209goto free_utfName;210}211} else {212utfSource = NULL;213}214result = JVM_DefineClassWithSource(env, utfName, loader, body, length, pd, utfSource);215216if (utfSource && utfSource != sourceBuf)217free(utfSource);218219free_utfName:220if (utfName && utfName != buf)221free(utfName);222223return result;224}225226JNIEXPORT void JNICALL227Java_java_lang_ClassLoader_resolveClass0(JNIEnv *env, jobject this,228jclass cls)229{230if (cls == NULL) {231JNU_ThrowNullPointerException(env, 0);232return;233}234235JVM_ResolveClass(env, cls);236}237238/*239* Returns NULL if class not found.240*/241JNIEXPORT jclass JNICALL242Java_java_lang_ClassLoader_findBootstrapClass(JNIEnv *env, jobject loader,243jstring classname)244{245char *clname;246jclass cls = 0;247char buf[128];248249if (classname == NULL) {250return 0;251}252253clname = getUTF(env, classname, buf, sizeof(buf));254if (clname == NULL) {255JNU_ThrowOutOfMemoryError(env, NULL);256return NULL;257}258VerifyFixClassname(clname);259260if (!VerifyClassname(clname, JNI_TRUE)) { /* expects slashed name */261goto done;262}263264cls = JVM_FindClassFromBootLoader(env, clname);265266done:267if (clname != buf) {268free(clname);269}270271return cls;272}273274JNIEXPORT jclass JNICALL275Java_java_lang_ClassLoader_findLoadedClass0(JNIEnv *env, jobject loader,276jstring name)277{278if (name == NULL) {279return 0;280} else {281return JVM_FindLoadedClass(env, loader, name);282}283}284285static jfieldID handleID;286static jfieldID jniVersionID;287static jfieldID loadedID;288static void *procHandle;289290static jboolean initIDs(JNIEnv *env)291{292if (handleID == 0) {293jclass this =294(*env)->FindClass(env, "java/lang/ClassLoader$NativeLibrary");295if (this == 0)296return JNI_FALSE;297handleID = (*env)->GetFieldID(env, this, "handle", "J");298if (handleID == 0)299return JNI_FALSE;300jniVersionID = (*env)->GetFieldID(env, this, "jniVersion", "I");301if (jniVersionID == 0)302return JNI_FALSE;303loadedID = (*env)->GetFieldID(env, this, "loaded", "Z");304if (loadedID == 0)305return JNI_FALSE;306procHandle = getProcessHandle();307}308return JNI_TRUE;309}310311typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);312typedef void (JNICALL *JNI_OnUnload_t)(JavaVM *, void *);313314/*315* Support for finding JNI_On(Un)Load_<lib_name> if it exists.316* If cname == NULL then just find normal JNI_On(Un)Load entry point317*/318static void *findJniFunction(JNIEnv *env, void *handle,319const char *cname, jboolean isLoad) {320const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;321const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;322const char **syms;323int symsLen;324void *entryName = NULL;325char *jniFunctionName;326int i;327int len;328329// Check for JNI_On(Un)Load<_libname> function330if (isLoad) {331syms = onLoadSymbols;332symsLen = sizeof(onLoadSymbols) / sizeof(char *);333} else {334syms = onUnloadSymbols;335symsLen = sizeof(onUnloadSymbols) / sizeof(char *);336}337for (i = 0; i < symsLen; i++) {338// cname + sym + '_' + '\0'339if ((len = (cname != NULL ? strlen(cname) : 0) + strlen(syms[i]) + 2) >340FILENAME_MAX) {341goto done;342}343jniFunctionName = malloc(len);344if (jniFunctionName == NULL) {345JNU_ThrowOutOfMemoryError(env, NULL);346goto done;347}348buildJniFunctionName(syms[i], cname, jniFunctionName);349entryName = JVM_FindLibraryEntry(handle, jniFunctionName);350free(jniFunctionName);351if(entryName) {352break;353}354}355356done:357return entryName;358}359360/*361* Class: java_lang_ClassLoader_NativeLibrary362* Method: load363* Signature: (Ljava/lang/String;Z)V364*/365JNIEXPORT void JNICALL366Java_java_lang_ClassLoader_00024NativeLibrary_load367(JNIEnv *env, jobject this, jstring name, jboolean isBuiltin)368{369const char *cname;370jint jniVersion;371jthrowable cause;372void * handle;373374if (!initIDs(env))375return;376377cname = JNU_GetStringPlatformChars(env, name, 0);378if (cname == 0)379return;380handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname);381if (handle) {382JNI_OnLoad_t JNI_OnLoad;383JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,384isBuiltin ? cname : NULL,385JNI_TRUE);386if (JNI_OnLoad) {387JavaVM *jvm;388(*env)->GetJavaVM(env, &jvm);389jniVersion = (*JNI_OnLoad)(jvm, NULL);390} else {391jniVersion = 0x00010001;392}393394cause = (*env)->ExceptionOccurred(env);395if (cause) {396(*env)->ExceptionClear(env);397(*env)->Throw(env, cause);398if (!isBuiltin) {399JVM_UnloadLibrary(handle);400}401goto done;402}403404if (!JVM_IsSupportedJNIVersion(jniVersion) ||405(isBuiltin && jniVersion < JNI_VERSION_1_8)) {406char msg[256];407jio_snprintf(msg, sizeof(msg),408"unsupported JNI version 0x%08X required by %s",409jniVersion, cname);410JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);411if (!isBuiltin) {412JVM_UnloadLibrary(handle);413}414goto done;415}416(*env)->SetIntField(env, this, jniVersionID, jniVersion);417} else {418cause = (*env)->ExceptionOccurred(env);419if (cause) {420(*env)->ExceptionClear(env);421(*env)->SetLongField(env, this, handleID, (jlong)0);422(*env)->Throw(env, cause);423}424goto done;425}426(*env)->SetLongField(env, this, handleID, ptr_to_jlong(handle));427(*env)->SetBooleanField(env, this, loadedID, JNI_TRUE);428429done:430JNU_ReleaseStringPlatformChars(env, name, cname);431}432433/*434* Class: java_lang_ClassLoader_NativeLibrary435* Method: unload436* Signature: (Z)V437*/438JNIEXPORT void JNICALL439Java_java_lang_ClassLoader_00024NativeLibrary_unload440(JNIEnv *env, jobject this, jstring name, jboolean isBuiltin)441{442const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;443void *handle;444JNI_OnUnload_t JNI_OnUnload;445const char *cname;446447if (!initIDs(env))448return;449cname = JNU_GetStringPlatformChars(env, name, 0);450if (cname == NULL) {451return;452}453handle = jlong_to_ptr((*env)->GetLongField(env, this, handleID));454JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,455isBuiltin ? cname : NULL,456JNI_FALSE);457if (JNI_OnUnload) {458JavaVM *jvm;459(*env)->GetJavaVM(env, &jvm);460(*JNI_OnUnload)(jvm, NULL);461}462if (!isBuiltin) {463JVM_UnloadLibrary(handle);464}465JNU_ReleaseStringPlatformChars(env, name, cname);466}467468/*469* Class: java_lang_ClassLoader_NativeLibrary470* Method: find471* Signature: (Ljava/lang/String;)J472*/473JNIEXPORT jlong JNICALL474Java_java_lang_ClassLoader_00024NativeLibrary_find475(JNIEnv *env, jobject this, jstring name)476{477jlong handle;478const char *cname;479jlong res;480481if (!initIDs(env))482return jlong_zero;483484handle = (*env)->GetLongField(env, this, handleID);485cname = (*env)->GetStringUTFChars(env, name, 0);486if (cname == 0)487return jlong_zero;488res = ptr_to_jlong(JVM_FindLibraryEntry(jlong_to_ptr(handle), cname));489(*env)->ReleaseStringUTFChars(env, name, cname);490return res;491}492/*493* Class: java_lang_ClassLoader494* Method: findBuiltinLib495* Signature: (Ljava/lang/String;)Ljava/lang/String;496*/497JNIEXPORT jstring JNICALL498Java_java_lang_ClassLoader_findBuiltinLib499(JNIEnv *env, jclass cls, jstring name)500{501const char *cname;502char *libName;503int prefixLen = (int) strlen(JNI_LIB_PREFIX);504int suffixLen = (int) strlen(JNI_LIB_SUFFIX);505int len;506jstring lib;507void *ret;508const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;509510if (name == NULL) {511JNU_ThrowInternalError(env, "NULL filename for native library");512return NULL;513}514procHandle = getProcessHandle();515cname = JNU_GetStringPlatformChars(env, name, 0);516if (cname == NULL) {517return NULL;518}519// Copy name Skipping PREFIX520len = strlen(cname);521if (len <= (prefixLen+suffixLen)) {522JNU_ReleaseStringPlatformChars(env, name, cname);523return NULL;524}525libName = malloc(len + 1); //+1 for null if prefix+suffix == 0526if (libName == NULL) {527JNU_ReleaseStringPlatformChars(env, name, cname);528JNU_ThrowOutOfMemoryError(env, NULL);529return NULL;530}531if (len > prefixLen) {532strcpy(libName, cname+prefixLen);533}534JNU_ReleaseStringPlatformChars(env, name, cname);535536// Strip SUFFIX537libName[strlen(libName)-suffixLen] = '\0';538539// Check for JNI_OnLoad_libname function540ret = findJniFunction(env, procHandle, libName, JNI_TRUE);541if (ret != NULL) {542lib = JNU_NewStringPlatform(env, libName);543free(libName);544return lib;545}546free(libName);547return NULL;548}549550551