1 /* 2 * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include "jni.h" 33 34 // Opaque reference to a JImage file. 35 class JImageFile; 36 // Opaque reference to an image file resource location. 37 typedef jlong JImageLocationRef; 38 39 // Max path length limit independent of platform. Windows max path is 1024, 40 // other platforms use 4096. 41 #define JIMAGE_MAX_PATH 4096 42 43 // JImage Error Codes 44 45 // Resource was not found 46 #define JIMAGE_NOT_FOUND (0) 47 // The image file is not prefixed with 0xCAFEDADA 48 #define JIMAGE_BAD_MAGIC (-1) 49 // The image file does not have a compatible (translatable) version 50 #define JIMAGE_BAD_VERSION (-2) 51 // The image file content is malformed 52 #define JIMAGE_CORRUPTED (-3) 53 54 /* 55 * JImageOpen - Given the supplied full path file name, open an image file. This 56 * function will also initialize tables and retrieve meta-data necessary to 57 * satisfy other functions in the API. If the image file has been previously 58 * open, a new open request will share memory and resources used by the previous 59 * open. A call to JImageOpen should be balanced by a call to JImageClose, to 60 * release memory and resources used. If the image file is not found or cannot 61 * be open, then NULL is returned and error will contain a reason for the 62 * failure; a positive value for a system error number, negative for a jimage 63 * specific error (see JImage Error Codes.) 64 * 65 * Ex. 66 * jint error; 67 * JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules", &error); 68 * if (image == NULL) { 69 * tty->print_cr("JImage failed to open: %d", error); 70 * ... 71 * } 72 * ... 73 */ 74 75 extern "C" JNIEXPORT JImageFile* 76 JIMAGE_Open(const char *name, jint* error); 77 78 typedef JImageFile* (*JImageOpen_t)(const char *name, jint* error); 79 80 /* 81 * JImageClose - Given the supplied open image file (see JImageOpen), release 82 * memory and resources used by the open file and close the file. If the image 83 * file is shared by other uses, release and close is deferred until the last use 84 * is also closed. 85 * 86 * Ex. 87 * (*JImageClose)(image); 88 */ 89 90 extern "C" JNIEXPORT void 91 JIMAGE_Close(JImageFile* jimage); 92 93 typedef void (*JImageClose_t)(JImageFile* jimage); 94 95 96 /* 97 * JImageFindResource - Given an open image file (see JImageOpen), a module 98 * name, a version string and the name of a class/resource, return location 99 * information describing the resource and its size. If no resource is found, the 100 * function returns JIMAGE_NOT_FOUND and the value of size is undefined. 101 * The version number should be "9.0" and is not used in locating the resource. 102 * The resulting location does/should not have to be released. 103 * All strings are utf-8, zero byte terminated. 104 * 105 * Ex. 106 * jlong size; 107 * JImageLocationRef location = (*JImageFindResource)(image, 108 * "java.base", "9.0", "java/lang/String.class", &size); 109 */ 110 extern "C" JNIEXPORT JImageLocationRef JIMAGE_FindResource(JImageFile* jimage, 111 const char* module_name, const char* version, const char* name, 112 jlong* size); 113 114 typedef JImageLocationRef(*JImageFindResource_t)(JImageFile* jimage, 115 const char* module_name, const char* version, const char* name, 116 jlong* size); 117 118 119 /* 120 * JImageGetResource - Given an open image file (see JImageOpen), a resource's 121 * location information (see JImageFindResource), a buffer of appropriate 122 * size and the size, retrieve the bytes associated with the 123 * resource. If the size is less than the resource size then the read is truncated. 124 * If the size is greater than the resource size then the remainder of the buffer 125 * is zero filled. The function will return the actual size of the resource. 126 * 127 * Ex. 128 * jlong size; 129 * JImageLocationRef location = (*JImageFindResource)(image, 130 * "java.base", "9.0", "java/lang/String.class", &size); 131 * char* buffer = new char[size]; 132 * (*JImageGetResource)(image, location, buffer, size); 133 */ 134 extern "C" JNIEXPORT jlong 135 JIMAGE_GetResource(JImageFile* jimage, JImageLocationRef location, 136 char* buffer, jlong size); 137 138 typedef jlong(*JImageGetResource_t)(JImageFile* jimage, JImageLocationRef location, 139 char* buffer, jlong size); 140 141 142 /* 143 * JImageResourceIterator - Given an open image file (see JImageOpen), a visitor 144 * function and a visitor argument, iterator through each of the image's resources. 145 * The visitor function is called with the image file, the module name, the 146 * package name, the base name, the extension and the visitor argument. The return 147 * value of the visitor function should be true, unless an early iteration exit is 148 * required. All strings are utf-8, zero byte terminated.file. 149 * 150 * Ex. 151 * bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version, 152 * const char* package, const char* name, const char* extension, void* arg) { 153 * if (strcmp(extension, "class") == 0) { 154 * char path[JIMAGE_MAX_PATH]; 155 * Thread* THREAD = Thread::current(); 156 * jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name); 157 * ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD); 158 * return !HAS_PENDING_EXCEPTION; 159 * } 160 * return true; 161 * } 162 * (*JImageResourceIterator)(image, ctw_visitor, loader); 163 */ 164 165 typedef bool (*JImageResourceVisitor_t)(JImageFile* jimage, 166 const char* module_name, const char* version, const char* package, 167 const char* name, const char* extension, void* arg); 168 169 extern "C" JNIEXPORT void 170 JIMAGE_ResourceIterator(JImageFile* jimage, 171 JImageResourceVisitor_t visitor, void *arg); 172 173 typedef void (*JImageResourceIterator_t)(JImageFile* jimage, 174 JImageResourceVisitor_t visitor, void* arg);