1 /*
   2  * Copyright (c) 2016, 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "jni.h"
  26 
  27 // Opaque reference to a JImage file.
  28 class JImageFile;
  29 // Opaque reference to an image file resource location.
  30 typedef jlong JImageLocationRef;
  31 
  32 // Max path length limit independent of platform.  Windows max path is 1024,
  33 // other platforms use 4096.
  34 #define JIMAGE_MAX_PATH 4096
  35 
  36 // JImage Error Codes
  37 
  38 // Resource was not found
  39 #define JIMAGE_NOT_FOUND (0)
  40 // The image file is not prefixed with 0xCAFEDADA
  41 #define JIMAGE_BAD_MAGIC (-1)
  42 // The image file does not have a compatible (translatable) version
  43 #define JIMAGE_BAD_VERSION (-2)
  44 // The image file content is malformed
  45 #define JIMAGE_CORRUPTED (-3)
  46 
  47 /*
  48  * JImageOpen - Given the supplied full path file name, open an image file. This
  49  * function will also initialize tables and retrieve meta-data necessary to
  50  * satisfy other functions in the API. If the image file has been previously
  51  * open, a new open request will share memory and resources used by the previous
  52  * open. A call to JImageOpen should be balanced by a call to JImageClose, to
  53  * release memory and resources used. If the image file is not found or cannot
  54  * be open, then NULL is returned and error will contain a reason for the
  55  * failure; a positive value for a system error number, negative for a jimage
  56  * specific error (see JImage Error Codes.)
  57  *
  58  *  Ex.
  59  *   jint error;
  60  *   JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules", &error);
  61  *   if (image == NULL) {
  62  *     tty->print_cr("JImage failed to open: %d", error);
  63  *     ...
  64  *   }
  65  *   ...
  66  */
  67 
  68 extern "C" JImageFile* JIMAGE_Open(const char *name, jint* error);
  69 
  70 typedef JImageFile* (*JImageOpen_t)(const char *name, jint* error);
  71 
  72 /*
  73  * JImageClose - Given the supplied open image file (see JImageOpen), release
  74  * memory and resources used by the open file and close the file. If the image
  75  * file is shared by other uses, release and close is deferred until the last use
  76  * is also closed.
  77  *
  78  * Ex.
  79  *  (*JImageClose)(image);
  80  */
  81 
  82 extern "C" void JIMAGE_Close(JImageFile* jimage);
  83 
  84 typedef void (*JImageClose_t)(JImageFile* jimage);
  85 
  86 
  87 /*
  88  * JImagePackageToModule - Given an open image file (see JImageOpen) and the name
  89  * of a package, return the name of module where the package resides. If the
  90  * package does not exist in the image file, the function returns NULL.
  91  * The resulting string does/should not have to be released. All strings are
  92  * utf-8, zero byte terminated.
  93  *
  94  * Ex.
  95  *  const char* package = (*JImagePackageToModule)(image, "java/lang");
  96  *  tty->print_cr(package);
  97  *  -> java.base
  98  */
  99 
 100 extern "C" const char * JIMAGE_PackageToModule(JImageFile* jimage, const char* package_name);
 101 
 102 typedef const char* (*JImagePackageToModule_t)(JImageFile* jimage, const char* package_name);
 103 
 104 
 105 /*
 106  * JImageFindResource - Given an open image file (see JImageOpen), a module
 107  * name, a version string and the name of a class/resource, return location
 108  * information describing the resource and its size. If no resource is found, the
 109  * function returns JIMAGE_NOT_FOUND and the value of size is undefined.
 110  * The version number should be "9.0" and is not used in locating the resource.
 111  * The resulting location does/should not have to be released.
 112  * All strings are utf-8, zero byte terminated.
 113  *
 114  *  Ex.
 115  *   jlong size;
 116  *   JImageLocationRef location = (*JImageFindResource)(image,
 117  *                                "java.base", "9.0", "java/lang/String.class", &size);
 118  */
 119 extern "C" JImageLocationRef JIMAGE_FindResource(JImageFile* jimage,
 120         const char* module_name, const char* version, const char* name,
 121         jlong* size);
 122 
 123 typedef JImageLocationRef(*JImageFindResource_t)(JImageFile* jimage,
 124         const char* module_name, const char* version, const char* name,
 125         jlong* size);
 126 
 127 
 128 /*
 129  * JImageGetResource - Given an open image file (see JImageOpen), a resource's
 130  * location information (see JImageFindResource), a buffer of appropriate
 131  * size and the size, retrieve the bytes associated with the
 132  * resource. If the size is less than the resource size then the read is truncated.
 133  * If the size is greater than the resource size then the remainder of the buffer
 134  * is zero filled.  The function will return the actual size of the resource.
 135  *
 136  * Ex.
 137  *  jlong size;
 138  *  JImageLocationRef location = (*JImageFindResource)(image,
 139  *                               "java.base", "9.0", "java/lang/String.class", &size);
 140  *  char* buffer = new char[size];
 141  *  (*JImageGetResource)(image, location, buffer, size);
 142  */
 143 extern "C" jlong JIMAGE_GetResource(JImageFile* jimage, JImageLocationRef location,
 144         char* buffer, jlong size);
 145 
 146 typedef jlong(*JImageGetResource_t)(JImageFile* jimage, JImageLocationRef location,
 147         char* buffer, jlong size);
 148 
 149 
 150 /*
 151  * JImageResourceIterator - Given an open image file (see JImageOpen), a visitor
 152  * function and a visitor argument, iterator through each of the image's resources.
 153  * The visitor function is called with the image file, the module name, the
 154  * package name, the base name, the extension and the visitor argument. The return
 155  * value of the visitor function should be true, unless an early iteration exit is
 156  * required. All strings are utf-8, zero byte terminated.file.
 157  *
 158  * Ex.
 159  *   bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version,
 160  *                  const char* package, const char* name, const char* extension, void* arg) {
 161  *     if (strcmp(extension, "class") == 0) {
 162  *       char path[JIMAGE_MAX_PATH];
 163  *       Thread* THREAD = Thread::current();
 164  *       jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name);
 165  *       ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD);
 166  *       return !HAS_PENDING_EXCEPTION;
 167  *     }
 168  *     return true;
 169  *   }
 170  *   (*JImageResourceIterator)(image, ctw_visitor, loader);
 171  */
 172 
 173 typedef bool (*JImageResourceVisitor_t)(JImageFile* jimage,
 174         const char* module_name, const char* version, const char* package,
 175         const char* name, const char* extension, void* arg);
 176 
 177 extern "C" void JIMAGE_ResourceIterator(JImageFile* jimage,
 178         JImageResourceVisitor_t visitor, void *arg);
 179 
 180 typedef void (*JImageResourceIterator_t)(JImageFile* jimage,
 181         JImageResourceVisitor_t visitor, void* arg);
 182 
 183 /*
 184  * JIMAGE_ResourcePath- Given an open image file, a location reference, a buffer
 185  * and a maximum buffer size, copy the path of the resource into the buffer.
 186  * Returns false if not a valid location reference.
 187  *
 188  * Ex.
 189  *   JImageLocationRef location = ...
 190  *   char path[JIMAGE_MAX_PATH];
 191  *    (*JImageResourcePath)(image, location, path, JIMAGE_MAX_PATH);
 192  */
 193 extern "C" bool JIMAGE_ResourcePath(JImageFile* image, JImageLocationRef locationRef,
 194                                     char* path, size_t max);
 195 
 196 typedef bool (*JImage_ResourcePath_t)(JImageFile* jimage, JImageLocationRef location,
 197         char* buffer, jlong size);
 198