1 /*
2 * Copyright (c) 2015, 2016, 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
38 /*
39 * JImageOpen - Given the supplied full path file name, open an image file. This
40 * function will also initialize tables and retrieve meta-data necessary to
41 * satisfy other functions in the API. If the image file has been previously
42 * open, a new open request will share memory and resources used by the previous
43 * open. A call to JImageOpen should be balanced by a call to JImageClose, to
44 * release memory and resources used. If the image file is not found or cannot
45 * be open, then NULL is returned and error will contain a reason for the
46 * failure; a positive value for a system error number, negative for a jimage
47 * specific error (see JImage Error Codes.)
48 *
49 * Ex.
50 * jint error;
51 * JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules", &error);
52 * if (image == NULL) {
53 * tty->print_cr("JImage failed to open: %d", error);
54 * ...
55 * }
56 * ...
57 */
58 extern "C" JImageFile* JIMAGE_Open(const char *name, jint* error) {
59 // TODO - return a meaningful error code
60 *error = 0;
61 ImageFileReader* jfile = ImageFileReader::open(name);
62 return (JImageFile*) jfile;
63 }
64
65 /*
66 * JImageClose - Given the supplied open image file (see JImageOpen), release
67 * memory and resources used by the open file and close the file. If the image
68 * file is shared by other uses, release and close is deferred until the last use
69 * is also closed.
70 *
71 * Ex.
72 * (*JImageClose)(image);
73 */
74 extern "C" void JIMAGE_Close(JImageFile* image) {
75 ImageFileReader::close((ImageFileReader*) image);
76 }
77
78 /*
79 * JImagePackageToModule - Given an open image file (see JImageOpen) and the name
80 * of a package, return the name of module where the package resides. If the
81 * package does not exist in the image file, the function returns NULL.
82 * The resulting string does/should not have to be released. All strings are
83 * utf-8, zero byte terminated.
84 *
85 * Ex.
86 * const char* package = (*JImagePackageToModule)(image, "java/lang");
87 * tty->print_cr(package);
88 * -> java.base
89 */
90 extern "C" const char* JIMAGE_PackageToModule(JImageFile* image, const char* package_name) {
91 return ((ImageFileReader*) image)->get_image_module_data()->package_to_module(package_name);
92 }
93
94 /*
95 * JImageFindResource - Given an open image file (see JImageOpen), a module
96 * name, a version string and the name of a class/resource, return location
97 * information describing the resource and its size. If no resource is found, the
98 * function returns JIMAGE_NOT_FOUND and the value of size is undefined.
99 * The version number should be "9.0" and is not used in locating the resource.
100 * The resulting location does/should not have to be released.
101 * All strings are utf-8, zero byte terminated.
102 *
103 * Ex.
104 * jlong size;
105 * JImageLocationRef location = (*JImageFindResource)(image,
106 * "java.base", "9.0", "java/lang/String.class", &size);
107 */
108 extern "C" JImageLocationRef JIMAGE_FindResource(JImageFile* image,
109 const char* module_name, const char* version, const char* name,
110 jlong* size) {
111 // Concatenate to get full path
112 char fullpath[IMAGE_MAX_PATH];
113 size_t moduleNameLen = strlen(module_name);
114 size_t nameLen = strlen(name);
115 size_t index;
116
117 // TBD: assert(moduleNameLen > 0 && "module name must be non-empty");
118 assert(nameLen > 0 && "name must non-empty");
119
120 // If the concatenated string is too long for the buffer, return not found
121 if (1 + moduleNameLen + 1 + nameLen + 1 > IMAGE_MAX_PATH) {
122 return 0L;
123 }
124
125 index = 0;
126 fullpath[index++] = '/';
127 memcpy(&fullpath[index], module_name, moduleNameLen);
128 index += moduleNameLen;
134 JImageLocationRef loc =
135 (JImageLocationRef) ((ImageFileReader*) image)->find_location_index(fullpath, (u8*) size);
136 return loc;
137 }
138
139 /*
140 * JImageGetResource - Given an open image file (see JImageOpen), a resource's
141 * location information (see JImageFindResource), a buffer of appropriate
142 * size and the size, retrieve the bytes associated with the
143 * resource. If the size is less than the resource size then the read is truncated.
144 * If the size is greater than the resource size then the remainder of the buffer
145 * is zero filled. The function will return the actual size of the resource.
146 *
147 * Ex.
148 * jlong size;
149 * JImageLocationRef location = (*JImageFindResource)(image,
150 * "java.base", "9.0", "java/lang/String.class", &size);
151 * char* buffer = new char[size];
152 * (*JImageGetResource)(image, location, buffer, size);
153 */
154 extern "C" jlong JIMAGE_GetResource(JImageFile* image, JImageLocationRef location,
155 char* buffer, jlong size) {
156 ((ImageFileReader*) image)->get_resource((u4) location, (u1*) buffer);
157 return size;
158 }
159
160 /*
161 * JImageResourceIterator - Given an open image file (see JImageOpen), a visitor
162 * 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, the
164 * package name, the base name, the extension and the visitor argument. The return
165 * value of the visitor function should be true, unless an early iteration exit is
166 * 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 */
182 extern "C" void JIMAGE_ResourceIterator(JImageFile* image,
183 JImageResourceVisitor_t visitor, void* arg) {
184 ImageFileReader* imageFile = (ImageFileReader*) image;
185 u4 nEntries = imageFile->table_length();
186 const ImageStrings strings = imageFile->get_strings();
187 for (u4 i = 0; i < nEntries; i++) {
188 ImageLocation location(imageFile->get_location_data(i));
189
190 u4 moduleOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_MODULE);
191 if (moduleOffset == 0) {
192 continue; // skip non-modules
193 }
194 const char *module = strings.get(moduleOffset);
195 if (strcmp(module, "modules") == 0
196 || strcmp(module, "packages") == 0) {
197 continue; // always skip
198 }
199
200 u4 parentOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_PARENT);
201 const char *parent = strings.get(parentOffset);
202 u4 baseOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_BASE);
203 const char *base = strings.get(baseOffset);
204 u4 extOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION);
205 const char *extension = strings.get(extOffset);
206
207 if (!(*visitor)(image, module, "9", parent, base, extension, arg)) {
208 break;
209 }
210 }
211 }
212
213 /*
214 * JIMAGE_ResourcePath- Given an open image file, a location reference, a buffer
215 * and a maximum buffer size, copy the path of the resource into the buffer.
216 * Returns false if not a valid location reference.
217 *
218 * Ex.
219 * JImageLocationRef location = ...
220 * char path[JIMAGE_MAX_PATH];
221 * (*JImageResourcePath)(image, location, path, JIMAGE_MAX_PATH);
222 */
223 extern "C" bool JIMAGE_ResourcePath(JImageFile* image, JImageLocationRef locationRef,
224 char* path, size_t max) {
225 ImageFileReader* imageFile = (ImageFileReader*) image;
226
227 u4 offset = (u4) locationRef;
228 if (offset >= imageFile->locations_size()) {
229 return false;
230 }
231
232 ImageLocation location(imageFile->get_location_offset_data(offset));
233 imageFile->location_path(location, path, max);
234
235 return true;
236 }
|
1 /*
2 * Copyright (c) 2015, 2018, 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
38 /*
39 * JImageOpen - Given the supplied full path file name, open an image file. This
40 * function will also initialize tables and retrieve meta-data necessary to
41 * satisfy other functions in the API. If the image file has been previously
42 * open, a new open request will share memory and resources used by the previous
43 * open. A call to JImageOpen should be balanced by a call to JImageClose, to
44 * release memory and resources used. If the image file is not found or cannot
45 * be open, then NULL is returned and error will contain a reason for the
46 * failure; a positive value for a system error number, negative for a jimage
47 * specific error (see JImage Error Codes.)
48 *
49 * Ex.
50 * jint error;
51 * JImageFile* jimage = (*JImageOpen)(JAVA_HOME "lib/modules", &error);
52 * if (image == NULL) {
53 * tty->print_cr("JImage failed to open: %d", error);
54 * ...
55 * }
56 * ...
57 */
58 extern "C" JNIEXPORT JImageFile* JNICALL
59 JIMAGE_Open(const char *name, jint* error) {
60 // TODO - return a meaningful error code
61 *error = 0;
62 ImageFileReader* jfile = ImageFileReader::open(name);
63 return (JImageFile*) jfile;
64 }
65
66 /*
67 * JImageClose - Given the supplied open image file (see JImageOpen), release
68 * memory and resources used by the open file and close the file. If the image
69 * file is shared by other uses, release and close is deferred until the last use
70 * is also closed.
71 *
72 * Ex.
73 * (*JImageClose)(image);
74 */
75 extern "C" JNIEXPORT void JNICALL
76 JIMAGE_Close(JImageFile* image) {
77 ImageFileReader::close((ImageFileReader*) image);
78 }
79
80 /*
81 * JImagePackageToModule - Given an open image file (see JImageOpen) and the name
82 * of a package, return the name of module where the package resides. If the
83 * package does not exist in the image file, the function returns NULL.
84 * The resulting string does/should not have to be released. All strings are
85 * utf-8, zero byte terminated.
86 *
87 * Ex.
88 * const char* package = (*JImagePackageToModule)(image, "java/lang");
89 * tty->print_cr(package);
90 * -> java.base
91 */
92 extern "C" JNIEXPORT const char* JNICALL
93 JIMAGE_PackageToModule(JImageFile* image, const char* package_name) {
94 return ((ImageFileReader*) image)->get_image_module_data()->package_to_module(package_name);
95 }
96
97 /*
98 * JImageFindResource - Given an open image file (see JImageOpen), a module
99 * name, a version string and the name of a class/resource, return location
100 * information describing the resource and its size. If no resource is found, the
101 * function returns JIMAGE_NOT_FOUND and the value of size is undefined.
102 * The version number should be "9.0" and is not used in locating the resource.
103 * The resulting location does/should not have to be released.
104 * All strings are utf-8, zero byte terminated.
105 *
106 * Ex.
107 * jlong size;
108 * JImageLocationRef location = (*JImageFindResource)(image,
109 * "java.base", "9.0", "java/lang/String.class", &size);
110 */
111 extern "C" JNIEXPORT JImageLocationRef JNICALL
112 JIMAGE_FindResource(JImageFile* image,
113 const char* module_name, const char* version, const char* name,
114 jlong* size) {
115 // Concatenate to get full path
116 char fullpath[IMAGE_MAX_PATH];
117 size_t moduleNameLen = strlen(module_name);
118 size_t nameLen = strlen(name);
119 size_t index;
120
121 // TBD: assert(moduleNameLen > 0 && "module name must be non-empty");
122 assert(nameLen > 0 && "name must non-empty");
123
124 // If the concatenated string is too long for the buffer, return not found
125 if (1 + moduleNameLen + 1 + nameLen + 1 > IMAGE_MAX_PATH) {
126 return 0L;
127 }
128
129 index = 0;
130 fullpath[index++] = '/';
131 memcpy(&fullpath[index], module_name, moduleNameLen);
132 index += moduleNameLen;
138 JImageLocationRef loc =
139 (JImageLocationRef) ((ImageFileReader*) image)->find_location_index(fullpath, (u8*) size);
140 return loc;
141 }
142
143 /*
144 * JImageGetResource - Given an open image file (see JImageOpen), a resource's
145 * location information (see JImageFindResource), a buffer of appropriate
146 * size and the size, retrieve the bytes associated with the
147 * resource. If the size is less than the resource size then the read is truncated.
148 * If the size is greater than the resource size then the remainder of the buffer
149 * is zero filled. The function will return the actual size of the resource.
150 *
151 * Ex.
152 * jlong size;
153 * JImageLocationRef location = (*JImageFindResource)(image,
154 * "java.base", "9.0", "java/lang/String.class", &size);
155 * char* buffer = new char[size];
156 * (*JImageGetResource)(image, location, buffer, size);
157 */
158 extern "C" JNIEXPORT jlong JNICALL
159 JIMAGE_GetResource(JImageFile* image, JImageLocationRef location,
160 char* buffer, jlong size) {
161 ((ImageFileReader*) image)->get_resource((u4) location, (u1*) buffer);
162 return size;
163 }
164
165 /*
166 * JImageResourceIterator - Given an open image file (see JImageOpen), a visitor
167 * function and a visitor argument, iterator through each of the image's resources.
168 * The visitor function is called with the image file, the module name, the
169 * package name, the base name, the extension and the visitor argument. The return
170 * value of the visitor function should be true, unless an early iteration exit is
171 * required. All strings are utf-8, zero byte terminated.file.
172 *
173 * Ex.
174 * bool ctw_visitor(JImageFile* jimage, const char* module_name, const char* version,
175 * const char* package, const char* name, const char* extension, void* arg) {
176 * if (strcmp(extension, "class") == 0) {
177 * char path[JIMAGE_MAX_PATH];
178 * Thread* THREAD = Thread::current();
179 * jio_snprintf(path, JIMAGE_MAX_PATH - 1, "/%s/%s", package, name);
180 * ClassLoader::compile_the_world_in(path, (Handle)arg, THREAD);
181 * return !HAS_PENDING_EXCEPTION;
182 * }
183 * return true;
184 * }
185 * (*JImageResourceIterator)(image, ctw_visitor, loader);
186 */
187 extern "C" JNIEXPORT void JNICALL
188 JIMAGE_ResourceIterator(JImageFile* image,
189 JImageResourceVisitor_t visitor, void* arg) {
190 ImageFileReader* imageFile = (ImageFileReader*) image;
191 u4 nEntries = imageFile->table_length();
192 const ImageStrings strings = imageFile->get_strings();
193 for (u4 i = 0; i < nEntries; i++) {
194 ImageLocation location(imageFile->get_location_data(i));
195
196 u4 moduleOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_MODULE);
197 if (moduleOffset == 0) {
198 continue; // skip non-modules
199 }
200 const char *module = strings.get(moduleOffset);
201 if (strcmp(module, "modules") == 0
202 || strcmp(module, "packages") == 0) {
203 continue; // always skip
204 }
205
206 u4 parentOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_PARENT);
207 const char *parent = strings.get(parentOffset);
208 u4 baseOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_BASE);
209 const char *base = strings.get(baseOffset);
210 u4 extOffset = (u4) location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION);
211 const char *extension = strings.get(extOffset);
212
213 if (!(*visitor)(image, module, "9", parent, base, extension, arg)) {
214 break;
215 }
216 }
217 }
218
219 /*
220 * JIMAGE_ResourcePath- Given an open image file, a location reference, a buffer
221 * and a maximum buffer size, copy the path of the resource into the buffer.
222 * Returns false if not a valid location reference.
223 *
224 * Ex.
225 * JImageLocationRef location = ...
226 * char path[JIMAGE_MAX_PATH];
227 * (*JImageResourcePath)(image, location, path, JIMAGE_MAX_PATH);
228 */
229 extern "C" JNIEXPORT bool JNICALL
230 JIMAGE_ResourcePath(JImageFile* image, JImageLocationRef locationRef,
231 char* path, size_t max) {
232 ImageFileReader* imageFile = (ImageFileReader*) image;
233
234 u4 offset = (u4) locationRef;
235 if (offset >= imageFile->locations_size()) {
236 return false;
237 }
238
239 ImageLocation location(imageFile->get_location_offset_data(offset));
240 imageFile->location_path(location, path, max);
241
242 return true;
243 }
|