1 /*
2 * Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package jdk.internal.jimage;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32 import java.nio.ByteBuffer;
33 import java.nio.ByteOrder;
34 import java.nio.IntBuffer;
35 import java.nio.channels.FileChannel;
36 import java.nio.file.Path;
37 import java.nio.file.StandardOpenOption;
38 import java.security.AccessController;
39 import java.security.PrivilegedAction;
40 import java.util.NoSuchElementException;
41 import java.util.Objects;
42 import java.util.Random;
43 import java.util.stream.IntStream;
44 import java.util.stream.Stream;
45
46 import jdk.internal.jimage.decompressor.Decompressor;
47
48 /**
49 * @implNote This class needs to maintain JDK 8 source compatibility.
50 *
51 * It is used internally in the JDK to implement jimage/jrtfs access,
52 * but also compiled and delivered as part of the jrtfs.jar to support access
53 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
54 */
55 public class BasicImageReader implements AutoCloseable {
56 @SuppressWarnings({ "removal", "suppression" })
57 private static boolean isSystemProperty(String key, String value, String def) {
58 // No lambdas during bootstrap
59 return AccessController.doPrivileged(
60 new PrivilegedAction<Boolean>() {
61 @Override
62 public Boolean run() {
63 return value.equals(System.getProperty(key, def));
64 }
65 });
66 }
67
68 private static final boolean IS_64_BIT =
69 isSystemProperty("sun.arch.data.model", "64", "32");
70 private static final boolean USE_JVM_MAP =
71 isSystemProperty("jdk.image.use.jvm.map", "true", "true");
72 private static final boolean MAP_ALL =
73 isSystemProperty("jdk.image.map.all", "true", IS_64_BIT ? "true" : "false");
74
75 private final Path imagePath;
76 private final ByteOrder byteOrder;
77 private final String name;
78 private final ByteBuffer memoryMap;
79 private final FileChannel channel;
80 private final ImageHeader header;
81 private final long indexSize;
82 private final IntBuffer redirect;
83 private final IntBuffer offsets;
84 private final ByteBuffer locations;
85 private final ByteBuffer strings;
86 private final ImageStringsReader stringsReader;
87 private final Decompressor decompressor;
88
89 @SuppressWarnings({ "removal", "this-escape", "suppression" })
90 protected BasicImageReader(Path path, ByteOrder byteOrder)
91 throws IOException {
92 this.imagePath = Objects.requireNonNull(path);
93 this.byteOrder = Objects.requireNonNull(byteOrder);
94 this.name = this.imagePath.toString();
95
96 ByteBuffer map;
97
98 if (USE_JVM_MAP && BasicImageReader.class.getClassLoader() == null) {
99 // Check to see if the jvm has opened the file using libjimage
100 // native entry when loading the image for this runtime
101 map = NativeImageBuffer.getNativeMap(name);
102 } else {
103 map = null;
104 }
105
106 // Open the file only if no memory map yet or is 32 bit jvm
107 if (map != null && MAP_ALL) {
108 channel = null;
109 } else {
110 channel = FileChannel.open(imagePath, StandardOpenOption.READ);
111 // No lambdas during bootstrap
112 AccessController.doPrivileged(new PrivilegedAction<Void>() {
113 @Override
114 public Void run() {
115 if (BasicImageReader.class.getClassLoader() == null) {
116 try {
117 Class<?> fileChannelImpl =
118 Class.forName("sun.nio.ch.FileChannelImpl");
119 Method setUninterruptible =
120 fileChannelImpl.getMethod("setUninterruptible");
121 setUninterruptible.invoke(channel);
122 } catch (ClassNotFoundException |
123 NoSuchMethodException |
124 IllegalAccessException |
125 InvocationTargetException ex) {
126 // fall thru - will only happen on JDK-8 systems where this code
127 // is only used by tools using jrt-fs (non-critical.)
128 }
129 }
130
131 return null;
132 }
133 });
134 }
135
136 // If no memory map yet and 64 bit jvm then memory map entire file
137 if (MAP_ALL && map == null) {
138 map = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
139 }
140
141 // Assume we have a memory map to read image file header
142 ByteBuffer headerBuffer = map;
143 int headerSize = ImageHeader.getHeaderSize();
144
145 // If no memory map then read header from image file
146 if (headerBuffer == null) {
147 headerBuffer = ByteBuffer.allocateDirect(headerSize);
148 if (channel.read(headerBuffer, 0L) == headerSize) {
149 headerBuffer.rewind();
150 } else {
151 throw new IOException("\"" + name + "\" is not an image file");
152 }
153 } else if (headerBuffer.capacity() < headerSize) {
154 throw new IOException("\"" + name + "\" is not an image file");
155 }
156
157 // Interpret the image file header
158 header = readHeader(intBuffer(headerBuffer, 0, headerSize));
159 indexSize = header.getIndexSize();
160
161 // If no memory map yet then must be 32 bit jvm not previously mapped
162 if (map == null) {
163 // Just map the image index
164 map = channel.map(FileChannel.MapMode.READ_ONLY, 0, indexSize);
165 }
166
167 memoryMap = map.asReadOnlyBuffer();
168
169 // Interpret the image index
170 if (memoryMap.capacity() < indexSize) {
171 throw new IOException("The image file \"" + name + "\" is corrupted");
172 }
173 redirect = intBuffer(memoryMap, header.getRedirectOffset(), header.getRedirectSize());
174 offsets = intBuffer(memoryMap, header.getOffsetsOffset(), header.getOffsetsSize());
175 locations = slice(memoryMap, header.getLocationsOffset(), header.getLocationsSize());
176 strings = slice(memoryMap, header.getStringsOffset(), header.getStringsSize());
177
178 stringsReader = new ImageStringsReader(this);
179 decompressor = new Decompressor();
180 }
181
182 protected BasicImageReader(Path imagePath) throws IOException {
183 this(imagePath, ByteOrder.nativeOrder());
184 }
185
186 public static BasicImageReader open(Path imagePath) throws IOException {
187 return new BasicImageReader(imagePath, ByteOrder.nativeOrder());
188 }
189
190 public ImageHeader getHeader() {
191 return header;
192 }
193
194 private ImageHeader readHeader(IntBuffer buffer) throws IOException {
195 ImageHeader result = ImageHeader.readFrom(buffer);
196
197 if (result.getMagic() != ImageHeader.MAGIC) {
198 throw new IOException("\"" + name + "\" is not an image file");
199 }
200
201 if (result.getMajorVersion() != ImageHeader.MAJOR_VERSION ||
202 result.getMinorVersion() != ImageHeader.MINOR_VERSION) {
203 throw new IOException("The image file \"" + name + "\" is not " +
204 "the correct version. Major: " + result.getMajorVersion() +
205 ". Minor: " + result.getMinorVersion());
206 }
207
208 return result;
209 }
210
211 private static ByteBuffer slice(ByteBuffer buffer, int position, int capacity) {
212 // Note that this is the only limit and position manipulation of
213 // BasicImageReader private ByteBuffers. The synchronize could be avoided
214 // by cloning the buffer to make a local copy, but at the cost of creating
215 // a new object.
216 synchronized(buffer) {
217 buffer.limit(position + capacity);
218 buffer.position(position);
219 return buffer.slice();
220 }
221 }
222
223 private IntBuffer intBuffer(ByteBuffer buffer, int offset, int size) {
224 return slice(buffer, offset, size).order(byteOrder).asIntBuffer();
225 }
226
227 public static void releaseByteBuffer(ByteBuffer buffer) {
228 Objects.requireNonNull(buffer);
229
230 if (!MAP_ALL) {
231 ImageBufferCache.releaseBuffer(buffer);
232 }
233 }
234
235 public String getName() {
236 return name;
237 }
238
239 public ByteOrder getByteOrder() {
240 return byteOrder;
241 }
242
243 public Path getImagePath() {
244 return imagePath;
245 }
246
247 @Override
248 public void close() throws IOException {
249 if (channel != null) {
250 channel.close();
251 }
252 }
253
254 public ImageStringsReader getStrings() {
255 return stringsReader;
256 }
257
258 public ImageLocation findLocation(String module, String name) {
259 int index = getLocationIndex(module, name);
260 if (index < 0) {
261 return null;
262 }
263 long[] attributes = getAttributes(offsets.get(index));
264 if (!ImageLocation.verify(module, name, attributes, stringsReader)) {
265 return null;
266 }
267 return new ImageLocation(attributes, stringsReader);
268 }
269
270 public ImageLocation findLocation(String name) {
271 int index = getLocationIndex(name);
272 if (index < 0) {
273 return null;
274 }
275 long[] attributes = getAttributes(offsets.get(index));
276 if (!ImageLocation.verify(name, attributes, stringsReader)) {
277 return null;
278 }
279 return new ImageLocation(attributes, stringsReader);
280 }
281
282 public boolean verifyLocation(String module, String name) {
283 int index = getLocationIndex(module, name);
284 if (index < 0) {
285 return false;
286 }
287 int locationOffset = offsets.get(index);
288 return ImageLocation.verify(module, name, locations, locationOffset, stringsReader);
289 }
290
291 // Details of the algorithm used here can be found in
292 // jdk.tools.jlink.internal.PerfectHashBuilder.
293 public int getLocationIndex(String name) {
294 int count = header.getTableLength();
295 int index = redirect.get(ImageStringsReader.hashCode(name) % count);
296 if (index < 0) {
297 // index is twos complement of location attributes index.
298 return -index - 1;
299 } else if (index > 0) {
300 // index is hash seed needed to compute location attributes index.
301 return ImageStringsReader.hashCode(name, index) % count;
302 } else {
303 // No entry.
304 return -1;
305 }
306 }
307
308 private int getLocationIndex(String module, String name) {
309 int count = header.getTableLength();
310 int index = redirect.get(ImageStringsReader.hashCode(module, name) % count);
311 if (index < 0) {
312 // index is twos complement of location attributes index.
313 return -index - 1;
314 } else if (index > 0) {
315 // index is hash seed needed to compute location attributes index.
316 return ImageStringsReader.hashCode(module, name, index) % count;
317 } else {
318 // No entry.
319 return -1;
320 }
321 }
322
323 public String[] getEntryNames() {
324 return IntStream.range(0, offsets.capacity())
325 .map(offsets::get)
326 .filter(o -> o != 0)
327 .mapToObj(o -> ImageLocation.readFrom(this, o).getFullName())
328 .sorted()
329 .toArray(String[]::new);
330 }
331
332 /**
333 * Returns the "raw" API for accessing underlying jimage resource entries.
334 *
335 * <p>This is only meaningful for use by code dealing directly with jimage
336 * files, and cannot be used to reliably lookup resources used at runtime.
337 *
338 * <p>This API remains valid until the image reader from which it was
339 * obtained is closed.
340 */
341 // Package visible for use by ImageReader.
342 ResourceEntries getResourceEntries() {
343 return new ResourceEntries() {
344 @Override
345 public Stream<String> getEntryNames(String module) {
346 if (module.isEmpty() || module.equals("modules") || module.equals("packages")) {
347 throw new IllegalArgumentException("Invalid module name: " + module);
348 }
349 return IntStream.range(0, offsets.capacity())
350 .map(offsets::get)
351 .filter(offset -> offset != 0)
352 // Reusing a location instance or getting the module
353 // offset directly would save a lot of allocations here.
354 .mapToObj(offset -> ImageLocation.readFrom(BasicImageReader.this, offset))
355 // Reverse lookup of module offset would be faster here.
356 .filter(loc -> module.equals(loc.getModule()))
357 .map(ImageLocation::getFullName);
358 }
359
360 private ImageLocation getResourceLocation(String name) {
361 if (!name.startsWith("/modules/") && !name.startsWith("/packages/")) {
362 ImageLocation location = BasicImageReader.this.findLocation(name);
363 if (location != null) {
364 return location;
365 }
366 }
367 throw new NoSuchElementException("No such resource entry: " + name);
368 }
369
370 @Override
371 public long getSize(String name) {
372 return getResourceLocation(name).getUncompressedSize();
373 }
374
375 @Override
376 public byte[] getBytes(String name) {
377 return BasicImageReader.this.getResource(getResourceLocation(name));
378 }
379 };
380 }
381
382 ImageLocation getLocation(int offset) {
383 return ImageLocation.readFrom(this, offset);
384 }
385
386 public long[] getAttributes(int offset) {
387 if (offset < 0 || offset >= locations.limit()) {
388 throw new IndexOutOfBoundsException("offset");
389 }
390 return ImageLocation.decompress(locations, offset);
391 }
392
393 public String getString(int offset) {
394 if (offset < 0 || offset >= strings.limit()) {
395 throw new IndexOutOfBoundsException("offset");
396 }
397 return ImageStringsReader.stringFromByteBuffer(strings, offset);
398 }
399
400 public int match(int offset, String string, int stringOffset) {
401 if (offset < 0 || offset >= strings.limit()) {
402 throw new IndexOutOfBoundsException("offset");
403 }
404 return ImageStringsReader.stringFromByteBufferMatches(strings, offset, string, stringOffset);
405 }
406
407 private byte[] getBufferBytes(ByteBuffer buffer) {
408 Objects.requireNonNull(buffer);
409 byte[] bytes = new byte[buffer.limit()];
410 buffer.get(bytes);
411
412 return bytes;
413 }
414
415 private ByteBuffer readBuffer(long offset, long size) {
416 if (offset < 0 || Integer.MAX_VALUE <= offset) {
417 throw new IndexOutOfBoundsException("Bad offset: " + offset);
418 }
419
420 if (size < 0 || Integer.MAX_VALUE <= size) {
421 throw new IndexOutOfBoundsException("Bad size: " + size);
422 }
423
424 if (MAP_ALL) {
425 ByteBuffer buffer = slice(memoryMap, (int)offset, (int)size);
426 buffer.order(ByteOrder.BIG_ENDIAN);
427
428 return buffer;
429 } else {
430 if (channel == null) {
431 throw new InternalError("Image file channel not open");
432 }
433
434 ByteBuffer buffer = ImageBufferCache.getBuffer(size);
435 int read;
436 try {
437 read = channel.read(buffer, offset);
438 buffer.rewind();
439 } catch (IOException ex) {
440 ImageBufferCache.releaseBuffer(buffer);
441 throw new RuntimeException(ex);
442 }
443
444 if (read != size) {
445 ImageBufferCache.releaseBuffer(buffer);
446 throw new RuntimeException("Short read: " + read +
447 " instead of " + size + " bytes");
448 }
449
450 return buffer;
451 }
452 }
453
454 public byte[] getResource(String name) {
455 Objects.requireNonNull(name);
456 ImageLocation location = findLocation(name);
457
458 return location != null ? getResource(location) : null;
459 }
460
461 public byte[] getResource(ImageLocation loc) {
462 ByteBuffer buffer = getResourceBuffer(loc);
463
464 if (buffer != null) {
465 byte[] bytes = getBufferBytes(buffer);
466 ImageBufferCache.releaseBuffer(buffer);
467
468 return bytes;
469 }
470
471 return null;
472 }
473
474 public ByteBuffer getResourceBuffer(ImageLocation loc) {
475 Objects.requireNonNull(loc);
476 long offset = loc.getContentOffset() + indexSize;
477 long compressedSize = loc.getCompressedSize();
478 long uncompressedSize = loc.getUncompressedSize();
479
480 if (compressedSize < 0 || Integer.MAX_VALUE < compressedSize) {
481 throw new IndexOutOfBoundsException(
482 "Bad compressed size: " + compressedSize);
483 }
484
485 if (uncompressedSize < 0 || Integer.MAX_VALUE < uncompressedSize) {
486 throw new IndexOutOfBoundsException(
487 "Bad uncompressed size: " + uncompressedSize);
488 }
489
490 if (compressedSize == 0) {
491 return readBuffer(offset, uncompressedSize);
492 } else {
493 ByteBuffer buffer = readBuffer(offset, compressedSize);
494
495 if (buffer != null) {
496 byte[] bytesIn = getBufferBytes(buffer);
497 ImageBufferCache.releaseBuffer(buffer);
498 byte[] bytesOut;
499
500 try {
501 bytesOut = decompressor.decompressResource(byteOrder,
502 (int strOffset) -> getString(strOffset), bytesIn);
503 } catch (IOException ex) {
504 throw new RuntimeException(ex);
505 }
506
507 return ByteBuffer.wrap(bytesOut);
508 }
509 }
510
511 return null;
512 }
513
514 public InputStream getResourceStream(ImageLocation loc) {
515 Objects.requireNonNull(loc);
516 byte[] bytes = getResource(loc);
517
518 return new ByteArrayInputStream(bytes);
519 }
520 }