Path: blob/master/src/java.base/share/native/libjimage/jimage.hpp
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 "jni.h"3233// Opaque reference to a JImage file.34class JImageFile;35// Opaque reference to an image file resource location.36typedef jlong JImageLocationRef;3738// Max path length limit independent of platform. Windows max path is 1024,39// other platforms use 4096.40#define JIMAGE_MAX_PATH 40964142// JImage Error Codes4344// Resource was not found45#define JIMAGE_NOT_FOUND (0)46// The image file is not prefixed with 0xCAFEDADA47#define JIMAGE_BAD_MAGIC (-1)48// The image file does not have a compatible (translatable) version49#define JIMAGE_BAD_VERSION (-2)50// The image file content is malformed51#define JIMAGE_CORRUPTED (-3)5253/*54* JImageOpen - Given the supplied full path file name, open an image file. This55* function will also initialize tables and retrieve meta-data necessary to56* satisfy other functions in the API. If the image file has been previously57* open, a new open request will share memory and resources used by the previous58* open. A call to JImageOpen should be balanced by a call to JImageClose, to59* release memory and resources used. If the image file is not found or cannot60* be open, then NULL is returned and error will contain a reason for the61* failure; a positive value for a system error number, negative for a jimage62* specific error (see JImage Error Codes.)63*64* Ex.65* jint error;66* JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules", &error);67* if (image == NULL) {68* tty->print_cr("JImage failed to open: %d", error);69* ...70* }71* ...72*/7374extern "C" JNIEXPORT JImageFile*75JIMAGE_Open(const char *name, jint* error);7677typedef JImageFile* (*JImageOpen_t)(const char *name, jint* error);7879/*80* JImageClose - Given the supplied open image file (see JImageOpen), release81* memory and resources used by the open file and close the file. If the image82* file is shared by other uses, release and close is deferred until the last use83* is also closed.84*85* Ex.86* (*JImageClose)(image);87*/8889extern "C" JNIEXPORT void90JIMAGE_Close(JImageFile* jimage);9192typedef void (*JImageClose_t)(JImageFile* jimage);939495/*96* JImagePackageToModule - Given an open image file (see JImageOpen) and the name97* of a package, return the name of module where the package resides. If the98* package does not exist in the image file, the function returns NULL.99* The resulting string does/should not have to be released. All strings are100* utf-8, zero byte terminated.101*102* Ex.103* const char* package = (*JImagePackageToModule)(image, "java/lang");104* tty->print_cr(package);105* -> java.base106*/107108extern "C" JNIEXPORT const char *109JIMAGE_PackageToModule(JImageFile* jimage, const char* package_name);110111typedef const char* (*JImagePackageToModule_t)(JImageFile* jimage, const char* package_name);112113114/*115* JImageFindResource - Given an open image file (see JImageOpen), a module116* name, a version string and the name of a class/resource, return location117* information describing the resource and its size. If no resource is found, the118* function returns JIMAGE_NOT_FOUND and the value of size is undefined.119* The version number should be "9.0" and is not used in locating the resource.120* The resulting location does/should not have to be released.121* All strings are utf-8, zero byte terminated.122*123* Ex.124* jlong size;125* JImageLocationRef location = (*JImageFindResource)(image,126* "java.base", "9.0", "java/lang/String.class", &size);127*/128extern "C" JNIEXPORT JImageLocationRef JIMAGE_FindResource(JImageFile* jimage,129const char* module_name, const char* version, const char* name,130jlong* size);131132typedef JImageLocationRef(*JImageFindResource_t)(JImageFile* jimage,133const char* module_name, const char* version, const char* name,134jlong* size);135136137/*138* JImageGetResource - Given an open image file (see JImageOpen), a resource's139* location information (see JImageFindResource), a buffer of appropriate140* size and the size, retrieve the bytes associated with the141* resource. If the size is less than the resource size then the read is truncated.142* If the size is greater than the resource size then the remainder of the buffer143* is zero filled. The function will return the actual size of the resource.144*145* Ex.146* jlong size;147* JImageLocationRef location = (*JImageFindResource)(image,148* "java.base", "9.0", "java/lang/String.class", &size);149* char* buffer = new char[size];150* (*JImageGetResource)(image, location, buffer, size);151*/152extern "C" JNIEXPORT jlong153JIMAGE_GetResource(JImageFile* jimage, JImageLocationRef location,154char* buffer, jlong size);155156typedef jlong(*JImageGetResource_t)(JImageFile* jimage, JImageLocationRef location,157char* buffer, jlong size);158159160/*161* JImageResourceIterator - Given an open image file (see JImageOpen), a visitor162* function and a visitor argument, iterator through each of the image's resources.163* The visitor function is called with the image file, the module name, the164* package name, the base name, the extension and the visitor argument. The return165* value of the visitor function should be true, unless an early iteration exit is166* required. All strings are utf-8, zero byte terminated.file.167*168* Ex.169* bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version,170* const char* package, const char* name, const char* extension, void* arg) {171* if (strcmp(extension, "class") == 0) {172* char path[JIMAGE_MAX_PATH];173* Thread* THREAD = Thread::current();174* jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name);175* ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD);176* return !HAS_PENDING_EXCEPTION;177* }178* return true;179* }180* (*JImageResourceIterator)(image, ctw_visitor, loader);181*/182183typedef bool (*JImageResourceVisitor_t)(JImageFile* jimage,184const char* module_name, const char* version, const char* package,185const char* name, const char* extension, void* arg);186187extern "C" JNIEXPORT void188JIMAGE_ResourceIterator(JImageFile* jimage,189JImageResourceVisitor_t visitor, void *arg);190191typedef void (*JImageResourceIterator_t)(JImageFile* jimage,192JImageResourceVisitor_t visitor, void* arg);193194195