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 <string.h> 33 34 #include "jimage.hpp" 35 36 #include "imageFile.hpp" 37 38 #include "jni_util.h" 39 40 /* 41 * Declare jimage library specific JNI_Onload entry for static build. 42 */ 43 extern "C" { 44 DEF_STATIC_JNI_OnLoad 45 } 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 extern "C" JNIEXPORT JImageFile* 68 JIMAGE_Open(const char *name, jint* error) { 69 // TODO - return a meaningful error code 70 *error = 0; 71 ImageFileReader* jfile = ImageFileReader::open(name); 72 return (JImageFile*) jfile; 73 } 74 75 /* 76 * JImageClose - Given the supplied open image file (see JImageOpen), release 77 * memory and resources used by the open file and close the file. If the image 78 * file is shared by other uses, release and close is deferred until the last use 79 * is also closed. 80 * 81 * Ex. 82 * (*JImageClose)(image); 83 */ 84 extern "C" JNIEXPORT void 85 JIMAGE_Close(JImageFile* image) { 86 ImageFileReader::close((ImageFileReader*) image); 87 } 88 89 /* 90 * JImageFindResource - Given an open image file (see JImageOpen), a module 91 * name, a version string and the name of a class/resource, return location 92 * information describing the resource and its size. If no resource is found, the 93 * function returns JIMAGE_NOT_FOUND and the value of size is undefined. 94 * The version number should be "9.0" and is not used in locating the resource. 95 * The resulting location does/should not have to be released. 96 * All strings are utf-8, zero byte terminated. 97 * 98 * Ex. 99 * jlong size; 100 * JImageLocationRef location = (*JImageFindResource)(image, 101 * "java.base", "9.0", "java/lang/String.class", &size); 102 */ 103 extern "C" JNIEXPORT JImageLocationRef 104 JIMAGE_FindResource(JImageFile* image, 105 const char* module_name, const char* version, const char* name, 106 jlong* size) { 107 // Concatenate to get full path 108 char fullpath[IMAGE_MAX_PATH]; 109 size_t moduleNameLen = strlen(module_name); 110 size_t nameLen = strlen(name); 111 size_t index; 112 113 // TBD: assert(moduleNameLen > 0 && "module name must be non-empty"); 114 assert(nameLen > 0 && "name must non-empty"); 115 116 // If the concatenated string is too long for the buffer, return not found 117 if (1 + moduleNameLen + 1 + nameLen + 1 > IMAGE_MAX_PATH) { 118 return 0L; 119 } 120 121 index = 0; 122 fullpath[index++] = '/'; 123 memcpy(&fullpath[index], module_name, moduleNameLen); 124 index += moduleNameLen; 125 fullpath[index++] = '/'; 126 memcpy(&fullpath[index], name, nameLen); 127 index += nameLen; 128 fullpath[index++] = '\0'; 129 130 JImageLocationRef loc = 131 (JImageLocationRef) ((ImageFileReader*) image)->find_location_index(fullpath, (u8*) size); 132 return loc; 133 } 134 135 /* 136 * JImageGetResource - Given an open image file (see JImageOpen), a resource's 137 * location information (see JImageFindResource), a buffer of appropriate 138 * size and the size, retrieve the bytes associated with the 139 * resource. If the size is less than the resource size then the read is truncated. 140 * If the size is greater than the resource size then the remainder of the buffer 141 * is zero filled. The function will return the actual size of the resource. 142 * 143 * Ex. 144 * jlong size; 145 * JImageLocationRef location = (*JImageFindResource)(image, 146 * "java.base", "9.0", "java/lang/String.class", &size); 147 * char* buffer = new char[size]; 148 * (*JImageGetResource)(image, location, buffer, size); 149 */ 150 extern "C" JNIEXPORT jlong 151 JIMAGE_GetResource(JImageFile* image, JImageLocationRef location, 152 char* buffer, jlong size) { 153 ((ImageFileReader*) image)->get_resource((u4) location, (u1*) buffer); 154 return size; 155 } 156 157 /* 158 * JImageResourceIterator - Given an open image file (see JImageOpen), a visitor 159 * function and a visitor argument, iterator through each of the image's resources. 160 * The visitor function is called with the image file, the module name, the 161 * package name, the base name, the extension and the visitor argument. The return 162 * value of the visitor function should be true, unless an early iteration exit is 163 * required. All strings are utf-8, zero byte terminated.file. 164 * 165 * Ex. 166 * bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version, 167 * const char* package, const char* name, const char* extension, void* arg) { 168 * if (strcmp(extension, "class") == 0) { 169 * char path[JIMAGE_MAX_PATH]; 170 * Thread* THREAD = Thread::current(); 171 * jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name); 172 * ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD); 173 * return !HAS_PENDING_EXCEPTION; 174 * } 175 * return true; 176 * } 177 * (*JImageResourceIterator)(image, ctw_visitor, loader); 178 */ 179 extern "C" JNIEXPORT void 180 JIMAGE_ResourceIterator(JImageFile* image, 181 JImageResourceVisitor_t visitor, void* arg) { 182 ImageFileReader* imageFile = (ImageFileReader*) image; 183 u4 nEntries = imageFile->table_length(); 184 const ImageStrings strings = imageFile->get_strings(); 185 for (u4 i = 0; i < nEntries; i++) { 186 ImageLocation location(imageFile->get_location_data(i)); 187 188 u4 moduleOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_MODULE); 189 if (moduleOffset == 0) { 190 continue; // skip non-modules 191 } 192 const char *module = strings.get(moduleOffset); 193 if (strcmp(module, "modules") == 0 194 || strcmp(module, "packages") == 0) { 195 continue; // always skip 196 } 197 198 u4 parentOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_PARENT); 199 const char *parent = strings.get(parentOffset); 200 u4 baseOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_BASE); 201 const char *base = strings.get(baseOffset); 202 u4 extOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION); 203 const char *extension = strings.get(extOffset); 204 205 if (!(*visitor)(image, module, "9", parent, base, extension, arg)) { 206 break; 207 } 208 } 209 }