Path: blob/master/src/java.base/share/native/libjimage/jimage.cpp
41149 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031#include <string.h>3233#include "jimage.hpp"3435#include "imageFile.hpp"3637/*38* JImageOpen - Given the supplied full path file name, open an image file. This39* function will also initialize tables and retrieve meta-data necessary to40* satisfy other functions in the API. If the image file has been previously41* open, a new open request will share memory and resources used by the previous42* open. A call to JImageOpen should be balanced by a call to JImageClose, to43* release memory and resources used. If the image file is not found or cannot44* be open, then NULL is returned and error will contain a reason for the45* failure; a positive value for a system error number, negative for a jimage46* specific error (see JImage Error Codes.)47*48* Ex.49* jint error;50* JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules", &error);51* if (image == NULL) {52* tty->print_cr("JImage failed to open: %d", error);53* ...54* }55* ...56*/57extern "C" JNIEXPORT JImageFile*58JIMAGE_Open(const char *name, jint* error) {59// TODO - return a meaningful error code60*error = 0;61ImageFileReader* jfile = ImageFileReader::open(name);62return (JImageFile*) jfile;63}6465/*66* JImageClose - Given the supplied open image file (see JImageOpen), release67* memory and resources used by the open file and close the file. If the image68* file is shared by other uses, release and close is deferred until the last use69* is also closed.70*71* Ex.72* (*JImageClose)(image);73*/74extern "C" JNIEXPORT void75JIMAGE_Close(JImageFile* image) {76ImageFileReader::close((ImageFileReader*) image);77}7879/*80* JImagePackageToModule - Given an open image file (see JImageOpen) and the name81* of a package, return the name of module where the package resides. If the82* package does not exist in the image file, the function returns NULL.83* The resulting string does/should not have to be released. All strings are84* utf-8, zero byte terminated.85*86* Ex.87* const char* package = (*JImagePackageToModule)(image, "java/lang");88* tty->print_cr(package);89* -> java.base90*/91extern "C" JNIEXPORT const char*92JIMAGE_PackageToModule(JImageFile* image, const char* package_name) {93return ((ImageFileReader*) image)->get_image_module_data()->package_to_module(package_name);94}9596/*97* JImageFindResource - Given an open image file (see JImageOpen), a module98* name, a version string and the name of a class/resource, return location99* information describing the resource and its size. If no resource is found, the100* 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*/110extern "C" JNIEXPORT JImageLocationRef111JIMAGE_FindResource(JImageFile* image,112const char* module_name, const char* version, const char* name,113jlong* size) {114// Concatenate to get full path115char fullpath[IMAGE_MAX_PATH];116size_t moduleNameLen = strlen(module_name);117size_t nameLen = strlen(name);118size_t index;119120// TBD: assert(moduleNameLen > 0 && "module name must be non-empty");121assert(nameLen > 0 && "name must non-empty");122123// If the concatenated string is too long for the buffer, return not found124if (1 + moduleNameLen + 1 + nameLen + 1 > IMAGE_MAX_PATH) {125return 0L;126}127128index = 0;129fullpath[index++] = '/';130memcpy(&fullpath[index], module_name, moduleNameLen);131index += moduleNameLen;132fullpath[index++] = '/';133memcpy(&fullpath[index], name, nameLen);134index += nameLen;135fullpath[index++] = '\0';136137JImageLocationRef loc =138(JImageLocationRef) ((ImageFileReader*) image)->find_location_index(fullpath, (u8*) size);139return loc;140}141142/*143* JImageGetResource - Given an open image file (see JImageOpen), a resource's144* location information (see JImageFindResource), a buffer of appropriate145* size and the size, retrieve the bytes associated with the146* resource. If the size is less than the resource size then the read is truncated.147* If the size is greater than the resource size then the remainder of the buffer148* is zero filled. The function will return the actual size of the resource.149*150* Ex.151* jlong size;152* JImageLocationRef location = (*JImageFindResource)(image,153* "java.base", "9.0", "java/lang/String.class", &size);154* char* buffer = new char[size];155* (*JImageGetResource)(image, location, buffer, size);156*/157extern "C" JNIEXPORT jlong158JIMAGE_GetResource(JImageFile* image, JImageLocationRef location,159char* buffer, jlong size) {160((ImageFileReader*) image)->get_resource((u4) location, (u1*) buffer);161return size;162}163164/*165* JImageResourceIterator - Given an open image file (see JImageOpen), a visitor166* function and a visitor argument, iterator through each of the image's resources.167* The visitor function is called with the image file, the module name, the168* package name, the base name, the extension and the visitor argument. The return169* value of the visitor function should be true, unless an early iteration exit is170* required. All strings are utf-8, zero byte terminated.file.171*172* Ex.173* bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version,174* const char* package, const char* name, const char* extension, void* arg) {175* if (strcmp(extension, "class") == 0) {176* char path[JIMAGE_MAX_PATH];177* Thread* THREAD = Thread::current();178* jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name);179* ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD);180* return !HAS_PENDING_EXCEPTION;181* }182* return true;183* }184* (*JImageResourceIterator)(image, ctw_visitor, loader);185*/186extern "C" JNIEXPORT void187JIMAGE_ResourceIterator(JImageFile* image,188JImageResourceVisitor_t visitor, void* arg) {189ImageFileReader* imageFile = (ImageFileReader*) image;190u4 nEntries = imageFile->table_length();191const ImageStrings strings = imageFile->get_strings();192for (u4 i = 0; i < nEntries; i++) {193ImageLocation location(imageFile->get_location_data(i));194195u4 moduleOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_MODULE);196if (moduleOffset == 0) {197continue; // skip non-modules198}199const char *module = strings.get(moduleOffset);200if (strcmp(module, "modules") == 0201|| strcmp(module, "packages") == 0) {202continue; // always skip203}204205u4 parentOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_PARENT);206const char *parent = strings.get(parentOffset);207u4 baseOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_BASE);208const char *base = strings.get(baseOffset);209u4 extOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION);210const char *extension = strings.get(extOffset);211212if (!(*visitor)(image, module, "9", parent, base, extension, arg)) {213break;214}215}216}217218219