jdk/src/java.base/share/native/libjimage/ImageNativeSubstrate.cpp
changeset 33492 051df634418c
parent 32757 79d34d4b9627
child 33661 ad7c7378a002
equal deleted inserted replaced
33408:7884ecc0cc6f 33492:051df634418c
    24  */
    24  */
    25 
    25 
    26 #include <string.h>
    26 #include <string.h>
    27 
    27 
    28 #include "jni.h"
    28 #include "jni.h"
    29 #include "jni_util.h"
    29 
    30 #include "jdk_util.h"
       
    31 #include "endian.hpp"
    30 #include "endian.hpp"
    32 #include "imageDecompressor.hpp"
    31 #include "imageDecompressor.hpp"
    33 #include "imageFile.hpp"
    32 #include "imageFile.hpp"
    34 #include "inttypes.hpp"
    33 #include "inttypes.hpp"
    35 #include "jimage.hpp"
    34 #include "jimage.hpp"
    36 #include "osSupport.hpp"
    35 #include "osSupport.hpp"
    37 
    36 
    38 #include "jdk_internal_jimage_ImageNativeSubstrate.h"
    37 #include "jdk_internal_jimage_ImageNativeSubstrate.h"
    39 
    38 
    40 extern bool MemoryMapImage;
    39 extern bool MemoryMapImage;
       
    40 
       
    41 /////////////////////////////////////////////////////////////////////////////
       
    42 
       
    43 // Static function for primitive throw since libjimage is not linked with libjava
       
    44 static void JNICALL ThrowByName(JNIEnv *env, const char *name, const char *msg)
       
    45 {
       
    46     jclass cls = (env)->FindClass(name);
       
    47 
       
    48     if (cls != 0) /* Otherwise an exception has already been thrown */
       
    49         (env)->ThrowNew(cls, msg);
       
    50 }
    41 
    51 
    42 // jdk.internal.jimage /////////////////////////////////////////////////////////
    52 // jdk.internal.jimage /////////////////////////////////////////////////////////
    43 
    53 
    44 // Java entry to open an image file for sharing.
    54 // Java entry to open an image file for sharing.
    45 
    55 
   443     const char *native_version = NULL;
   453     const char *native_version = NULL;
   444     const char *native_path = NULL;
   454     const char *native_path = NULL;
   445     jlong * native_array = NULL;
   455     jlong * native_array = NULL;
   446     jlong size = 0;
   456     jlong size = 0;
   447     jlong ret = 0;
   457     jlong ret = 0;
       
   458 
       
   459     if (moduleName == NULL) {
       
   460         ThrowByName(env, "java/lang/NullPointerException", "moduleName");
       
   461         return 0;
       
   462     }
       
   463     if (version == NULL) {
       
   464         ThrowByName(env, "java/lang/NullPointerException", "version");
       
   465         return 0;
       
   466     }
       
   467     if (path == NULL) {
       
   468         ThrowByName(env, "java/lang/NullPointerException", "path");
       
   469         return 0;
       
   470     }
       
   471     if (output_size == NULL) {
       
   472         ThrowByName(env, "java/lang/NullPointerException", "size");
       
   473         return 0;
       
   474     }
   448 
   475 
   449     do {
   476     do {
   450         native_module = env->GetStringUTFChars(moduleName, NULL);
   477         native_module = env->GetStringUTFChars(moduleName, NULL);
   451         if (native_module == NULL)
   478         if (native_module == NULL)
   452             break;
   479             break;
   527     JNIEnv* env = vdata->env;
   554     JNIEnv* env = vdata->env;
   528     if (vdata->size < vdata->max) {
   555     if (vdata->size < vdata->max) {
   529         // Store if there is room in the array
   556         // Store if there is room in the array
   530         // Concatenate to get full path
   557         // Concatenate to get full path
   531         char fullpath[IMAGE_MAX_PATH];
   558         char fullpath[IMAGE_MAX_PATH];
   532         fullpath[0] = '\0';
   559         size_t moduleLen = strlen(module);
   533         if (*module != '\0') {
   560         size_t packageLen = strlen(package);
   534             strncpy(fullpath, "/", IMAGE_MAX_PATH - 1);
   561         size_t nameLen = strlen(name);
   535             strncat(fullpath, module, IMAGE_MAX_PATH - 1);
   562         size_t extLen = strlen(extension);
   536             strncat(fullpath, "/", IMAGE_MAX_PATH - 1);
   563         size_t index;
       
   564 
       
   565         if (1 + moduleLen + 1 + packageLen + 1 + nameLen + 1 + extLen + 1 > IMAGE_MAX_PATH) {
       
   566             ThrowByName(env, "java/lang/InternalError", "concatenated name too long");
       
   567             return true;
   537         }
   568         }
   538         if (*package != '\0') {
   569 
   539             strncat(fullpath, package, IMAGE_MAX_PATH - 1);
   570         index = 0;
   540             strncat(fullpath, "/", IMAGE_MAX_PATH - 1);
   571         if (moduleLen > 0) {
       
   572             fullpath[index++] = '/';
       
   573             memcpy(&fullpath[index], module, moduleLen);
       
   574             index += moduleLen;
       
   575             fullpath[index++] = '/';
   541         }
   576         }
   542         strncat(fullpath, name, IMAGE_MAX_PATH - 1);
   577         if (packageLen > 0) {
   543         if (*extension != '\0') {
   578             memcpy(&fullpath[index], package, packageLen);
   544             strncat(fullpath, ".", IMAGE_MAX_PATH - 1);
   579             index += packageLen;
   545             strncat(fullpath, extension, IMAGE_MAX_PATH - 1);
   580             fullpath[index++] = '/';
   546         }
   581         }
       
   582         memcpy(&fullpath[index], name, nameLen);
       
   583         index += nameLen;
       
   584         if (extLen > 0) {
       
   585             fullpath[index++] = '.';
       
   586             memcpy(&fullpath[index], extension, extLen);
       
   587             index += extLen;
       
   588         }
       
   589         fullpath[index++] = '\0';
       
   590 
   547         jobject str = env->NewStringUTF(fullpath);
   591         jobject str = env->NewStringUTF(fullpath);
   548         JNU_CHECK_EXCEPTION_RETURN(env, true);
   592         if (env->ExceptionCheck()) {
       
   593             return true;
       
   594         }
       
   595 
   549         env->SetObjectArrayElement(vdata->array, vdata->size, str);
   596         env->SetObjectArrayElement(vdata->array, vdata->size, str);
   550         JNU_CHECK_EXCEPTION_RETURN(env, true);
   597         if (env->ExceptionCheck()) {
       
   598             return true;
       
   599         }
   551     }
   600     }
   552     vdata->size++; // always count so the total size is returned
   601     vdata->size++; // always count so the total size is returned
   553     return true;
   602     return true;
   554 }
   603 }
   555 
   604 
   582     const char *native_package = NULL;
   631     const char *native_package = NULL;
   583     const char *native_module = NULL;
   632     const char *native_module = NULL;
   584     jstring module = NULL;
   633     jstring module = NULL;
   585 
   634 
   586     native_package = env->GetStringUTFChars(package_name, NULL);
   635     native_package = env->GetStringUTFChars(package_name, NULL);
   587     JNU_CHECK_EXCEPTION_RETURN(env, NULL);
   636     if (env->ExceptionCheck()) {
       
   637         return NULL;
       
   638     }
       
   639 
   588 
   640 
   589     native_module = JIMAGE_PackageToModule((JImageFile*) jimageHandle, native_package);
   641     native_module = JIMAGE_PackageToModule((JImageFile*) jimageHandle, native_package);
   590     if (native_module != NULL) {
   642     if (native_module != NULL) {
   591         module = env->NewStringUTF(native_module);
   643         module = env->NewStringUTF(native_module);
   592     }
   644     }