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