author | erikj |
Wed, 07 Mar 2018 13:26:15 -0800 | |
changeset 49357 | aaedb8343784 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
27565 | 1 |
/* |
43795
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
2 |
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. |
27565 | 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 |
||
31673 | 27 |
import java.io.ByteArrayInputStream; |
27565 | 28 |
import java.io.IOException; |
31673 | 29 |
import java.io.InputStream; |
38445
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
30 |
import java.lang.reflect.InvocationTargetException; |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
31 |
import java.lang.reflect.Method; |
27565 | 32 |
import java.nio.ByteBuffer; |
33 |
import java.nio.ByteOrder; |
|
34 |
import java.nio.IntBuffer; |
|
36511 | 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.Objects; |
|
31673 | 41 |
import java.util.stream.IntStream; |
36511 | 42 |
import jdk.internal.jimage.decompressor.Decompressor; |
27565 | 43 |
|
36511 | 44 |
/** |
45 |
* @implNote This class needs to maintain JDK 8 source compatibility. |
|
46 |
* |
|
47 |
* It is used internally in the JDK to implement jimage/jrtfs access, |
|
48 |
* but also compiled and delivered as part of the jrtfs.jar to support access |
|
49 |
* to the jimage file provided by the shipped JDK by tools running on JDK 8. |
|
50 |
*/ |
|
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
31673
diff
changeset
|
51 |
public class BasicImageReader implements AutoCloseable { |
36511 | 52 |
private static boolean isSystemProperty(String key, String value, String def) { |
53 |
// No lambdas during bootstrap |
|
54 |
return AccessController.doPrivileged( |
|
55 |
new PrivilegedAction<Boolean>() { |
|
56 |
@Override |
|
57 |
public Boolean run() { |
|
58 |
return value.equals(System.getProperty(key, def)); |
|
59 |
} |
|
60 |
}); |
|
27565 | 61 |
} |
62 |
||
36511 | 63 |
static private final boolean IS_64_BIT = |
64 |
isSystemProperty("sun.arch.data.model", "64", "32"); |
|
65 |
static private final boolean USE_JVM_MAP = |
|
66 |
isSystemProperty("jdk.image.use.jvm.map", "true", "true"); |
|
67 |
static private final boolean MAP_ALL = |
|
68 |
isSystemProperty("jdk.image.map.all", "true", IS_64_BIT ? "true" : "false"); |
|
69 |
||
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
70 |
private final Path imagePath; |
36511 | 71 |
private final ByteOrder byteOrder; |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
72 |
private final String name; |
36511 | 73 |
private final ByteBuffer memoryMap; |
74 |
private final FileChannel channel; |
|
75 |
private final ImageHeader header; |
|
76 |
private final long indexSize; |
|
77 |
private final IntBuffer redirect; |
|
78 |
private final IntBuffer offsets; |
|
79 |
private final ByteBuffer locations; |
|
80 |
private final ByteBuffer strings; |
|
81 |
private final ImageStringsReader stringsReader; |
|
82 |
private final Decompressor decompressor; |
|
83 |
||
84 |
protected BasicImageReader(Path path, ByteOrder byteOrder) |
|
85 |
throws IOException { |
|
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
86 |
this.imagePath = Objects.requireNonNull(path); |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
87 |
this.byteOrder = Objects.requireNonNull(byteOrder); |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
88 |
this.name = this.imagePath.toString(); |
36511 | 89 |
|
90 |
ByteBuffer map; |
|
91 |
||
92 |
if (USE_JVM_MAP && BasicImageReader.class.getClassLoader() == null) { |
|
93 |
// Check to see if the jvm has opened the file using libjimage |
|
94 |
// native entry when loading the image for this runtime |
|
95 |
map = NativeImageBuffer.getNativeMap(name); |
|
96 |
} else { |
|
97 |
map = null; |
|
98 |
} |
|
99 |
||
100 |
// Open the file only if no memory map yet or is 32 bit jvm |
|
38445
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
101 |
if (map != null && MAP_ALL) { |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
102 |
channel = null; |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
103 |
} else { |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
104 |
channel = FileChannel.open(imagePath, StandardOpenOption.READ); |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
105 |
// No lambdas during bootstrap |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
106 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
107 |
@Override |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
108 |
public Void run() { |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
109 |
if (BasicImageReader.class.getClassLoader() == null) { |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
110 |
try { |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
111 |
Class<?> fileChannelImpl = |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
112 |
Class.forName("sun.nio.ch.FileChannelImpl"); |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
113 |
Method setUninterruptible = |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
114 |
fileChannelImpl.getMethod("setUninterruptible"); |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
115 |
setUninterruptible.invoke(channel); |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
116 |
} catch (ClassNotFoundException | |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
117 |
NoSuchMethodException | |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
118 |
IllegalAccessException | |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
119 |
InvocationTargetException ex) { |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
120 |
// fall thru - will only happen on JDK-8 systems where this code |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
121 |
// is only used by tools using jrt-fs (non-critical.) |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
122 |
} |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
123 |
} |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
124 |
|
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
125 |
return null; |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
126 |
} |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
127 |
}); |
0a88d86065f9
8156602: javac crashes again on Windows 32-bit with ClosedChannelException
jlaskey
parents:
37617
diff
changeset
|
128 |
} |
36511 | 129 |
|
130 |
// If no memory map yet and 64 bit jvm then memory map entire file |
|
131 |
if (MAP_ALL && map == null) { |
|
132 |
map = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); |
|
133 |
} |
|
134 |
||
135 |
// Assume we have a memory map to read image file header |
|
136 |
ByteBuffer headerBuffer = map; |
|
137 |
int headerSize = ImageHeader.getHeaderSize(); |
|
138 |
||
139 |
// If no memory map then read header from image file |
|
43795
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
140 |
if (headerBuffer == null) { |
36511 | 141 |
headerBuffer = ByteBuffer.allocateDirect(headerSize); |
43795
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
142 |
if (channel.read(headerBuffer, 0L) == headerSize) { |
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
143 |
headerBuffer.rewind(); |
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
144 |
} else { |
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
145 |
throw new IOException("\"" + name + "\" is not an image file"); |
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
146 |
} |
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
147 |
} else if (headerBuffer.capacity() < headerSize) { |
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
148 |
throw new IOException("\"" + name + "\" is not an image file"); |
36511 | 149 |
} |
150 |
||
151 |
// Interpret the image file header |
|
152 |
header = readHeader(intBuffer(headerBuffer, 0, headerSize)); |
|
153 |
indexSize = header.getIndexSize(); |
|
154 |
||
155 |
// If no memory map yet then must be 32 bit jvm not previously mapped |
|
156 |
if (map == null) { |
|
157 |
// Just map the image index |
|
158 |
map = channel.map(FileChannel.MapMode.READ_ONLY, 0, indexSize); |
|
159 |
} |
|
160 |
||
161 |
memoryMap = map.asReadOnlyBuffer(); |
|
162 |
||
163 |
// Interpret the image index |
|
43795
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
164 |
if (memoryMap.capacity() < indexSize) { |
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
165 |
throw new IOException("The image file \"" + name + "\" is corrupted"); |
526103bd9c57
8169715: jimage fails with IAE when attempts to inspect an empty file
dkononenko
parents:
41121
diff
changeset
|
166 |
} |
36511 | 167 |
redirect = intBuffer(memoryMap, header.getRedirectOffset(), header.getRedirectSize()); |
168 |
offsets = intBuffer(memoryMap, header.getOffsetsOffset(), header.getOffsetsSize()); |
|
169 |
locations = slice(memoryMap, header.getLocationsOffset(), header.getLocationsSize()); |
|
170 |
strings = slice(memoryMap, header.getStringsOffset(), header.getStringsSize()); |
|
171 |
||
172 |
stringsReader = new ImageStringsReader(this); |
|
173 |
decompressor = new Decompressor(); |
|
174 |
} |
|
175 |
||
176 |
protected BasicImageReader(Path imagePath) throws IOException { |
|
27565 | 177 |
this(imagePath, ByteOrder.nativeOrder()); |
178 |
} |
|
179 |
||
36511 | 180 |
public static BasicImageReader open(Path imagePath) throws IOException { |
181 |
return new BasicImageReader(imagePath, ByteOrder.nativeOrder()); |
|
182 |
} |
|
31673 | 183 |
|
36511 | 184 |
public ImageHeader getHeader() { |
185 |
return header; |
|
186 |
} |
|
187 |
||
188 |
private ImageHeader readHeader(IntBuffer buffer) throws IOException { |
|
189 |
ImageHeader result = ImageHeader.readFrom(buffer); |
|
190 |
||
191 |
if (result.getMagic() != ImageHeader.MAGIC) { |
|
192 |
throw new IOException("\"" + name + "\" is not an image file"); |
|
31673 | 193 |
} |
194 |
||
36511 | 195 |
if (result.getMajorVersion() != ImageHeader.MAJOR_VERSION || |
196 |
result.getMinorVersion() != ImageHeader.MINOR_VERSION) { |
|
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
197 |
throw new IOException("The image file \"" + name + "\" is not " + |
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
198 |
"the correct version. Major: " + result.getMajorVersion() + |
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
199 |
". Minor: " + result.getMinorVersion()); |
36511 | 200 |
} |
201 |
||
202 |
return result; |
|
31673 | 203 |
} |
204 |
||
36511 | 205 |
private static ByteBuffer slice(ByteBuffer buffer, int position, int capacity) { |
206 |
// Note that this is the only limit and position manipulation of |
|
207 |
// BasicImageReader private ByteBuffers. The synchronize could be avoided |
|
208 |
// by cloning the buffer to make a local copy, but at the cost of creating |
|
209 |
// a new object. |
|
210 |
synchronized(buffer) { |
|
211 |
buffer.limit(position + capacity); |
|
212 |
buffer.position(position); |
|
213 |
return buffer.slice(); |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
private IntBuffer intBuffer(ByteBuffer buffer, int offset, int size) { |
|
218 |
return slice(buffer, offset, size).order(byteOrder).asIntBuffer(); |
|
27565 | 219 |
} |
220 |
||
31673 | 221 |
public static void releaseByteBuffer(ByteBuffer buffer) { |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
222 |
Objects.requireNonNull(buffer); |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
223 |
|
37617
d91c47cb2992
8154179: BasicImageReader activating ImageBufferCache when not used
jlaskey
parents:
37342
diff
changeset
|
224 |
if (!MAP_ALL) { |
d91c47cb2992
8154179: BasicImageReader activating ImageBufferCache when not used
jlaskey
parents:
37342
diff
changeset
|
225 |
ImageBufferCache.releaseBuffer(buffer); |
d91c47cb2992
8154179: BasicImageReader activating ImageBufferCache when not used
jlaskey
parents:
37342
diff
changeset
|
226 |
} |
31673 | 227 |
} |
228 |
||
36511 | 229 |
public String getName() { |
230 |
return name; |
|
231 |
} |
|
232 |
||
31673 | 233 |
public ByteOrder getByteOrder() { |
234 |
return byteOrder; |
|
235 |
} |
|
236 |
||
36511 | 237 |
public Path getImagePath() { |
27565 | 238 |
return imagePath; |
239 |
} |
|
240 |
||
36511 | 241 |
@Override |
242 |
public void close() throws IOException { |
|
243 |
if (channel != null) { |
|
244 |
channel.close(); |
|
31673 | 245 |
} |
246 |
} |
|
247 |
||
248 |
public ImageStringsReader getStrings() { |
|
36511 | 249 |
return stringsReader; |
27565 | 250 |
} |
251 |
||
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
252 |
public synchronized ImageLocation findLocation(String module, String name) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
253 |
Objects.requireNonNull(module); |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
254 |
Objects.requireNonNull(name); |
36511 | 255 |
// Details of the algorithm used here can be found in |
256 |
// jdk.tools.jlink.internal.PerfectHashBuilder. |
|
257 |
int count = header.getTableLength(); |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
258 |
int index = redirect.get(ImageStringsReader.hashCode(module, name) % count); |
27565 | 259 |
|
36511 | 260 |
if (index < 0) { |
261 |
// index is twos complement of location attributes index. |
|
262 |
index = -index - 1; |
|
263 |
} else if (index > 0) { |
|
264 |
// index is hash seed needed to compute location attributes index. |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
265 |
index = ImageStringsReader.hashCode(module, name, index) % count; |
36511 | 266 |
} else { |
267 |
// No entry. |
|
268 |
return null; |
|
269 |
} |
|
270 |
||
271 |
long[] attributes = getAttributes(offsets.get(index)); |
|
272 |
||
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
273 |
if (!ImageLocation.verify(module, name, attributes, stringsReader)) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
274 |
return null; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
275 |
} |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
276 |
return new ImageLocation(attributes, stringsReader); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
277 |
} |
36511 | 278 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
279 |
public synchronized ImageLocation findLocation(String name) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
280 |
Objects.requireNonNull(name); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
281 |
// Details of the algorithm used here can be found in |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
282 |
// jdk.tools.jlink.internal.PerfectHashBuilder. |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
283 |
int count = header.getTableLength(); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
284 |
int index = redirect.get(ImageStringsReader.hashCode(name) % count); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
285 |
|
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
286 |
if (index < 0) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
287 |
// index is twos complement of location attributes index. |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
288 |
index = -index - 1; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
289 |
} else if (index > 0) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
290 |
// index is hash seed needed to compute location attributes index. |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
291 |
index = ImageStringsReader.hashCode(name, index) % count; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
292 |
} else { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
293 |
// No entry. |
36511 | 294 |
return null; |
295 |
} |
|
296 |
||
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
297 |
long[] attributes = getAttributes(offsets.get(index)); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
298 |
|
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
299 |
if (!ImageLocation.verify(name, attributes, stringsReader)) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
300 |
return null; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
301 |
} |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
43795
diff
changeset
|
302 |
return new ImageLocation(attributes, stringsReader); |
27565 | 303 |
} |
304 |
||
305 |
public String[] getEntryNames() { |
|
36511 | 306 |
int[] attributeOffsets = new int[offsets.capacity()]; |
307 |
offsets.get(attributeOffsets); |
|
308 |
return IntStream.of(attributeOffsets) |
|
31673 | 309 |
.filter(o -> o != 0) |
36511 | 310 |
.mapToObj(o -> ImageLocation.readFrom(this, o).getFullName()) |
31673 | 311 |
.sorted() |
312 |
.toArray(String[]::new); |
|
27565 | 313 |
} |
314 |
||
31673 | 315 |
ImageLocation getLocation(int offset) { |
316 |
return ImageLocation.readFrom(this, offset); |
|
27565 | 317 |
} |
318 |
||
31673 | 319 |
public long[] getAttributes(int offset) { |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
320 |
if (offset < 0 || offset >= locations.limit()) { |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
321 |
throw new IndexOutOfBoundsException("offset"); |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
322 |
} |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
323 |
|
36511 | 324 |
ByteBuffer buffer = slice(locations, offset, locations.limit() - offset); |
325 |
return ImageLocation.decompress(buffer); |
|
27565 | 326 |
} |
327 |
||
328 |
public String getString(int offset) { |
|
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
329 |
if (offset < 0 || offset >= strings.limit()) { |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
330 |
throw new IndexOutOfBoundsException("offset"); |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
331 |
} |
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
332 |
|
36511 | 333 |
ByteBuffer buffer = slice(strings, offset, strings.limit() - offset); |
334 |
return ImageStringsReader.stringFromByteBuffer(buffer); |
|
31673 | 335 |
} |
336 |
||
36511 | 337 |
private byte[] getBufferBytes(ByteBuffer buffer) { |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
338 |
Objects.requireNonNull(buffer); |
36511 | 339 |
byte[] bytes = new byte[buffer.limit()]; |
31673 | 340 |
buffer.get(bytes); |
341 |
||
342 |
return bytes; |
|
343 |
} |
|
344 |
||
36511 | 345 |
private ByteBuffer readBuffer(long offset, long size) { |
346 |
if (offset < 0 || Integer.MAX_VALUE <= offset) { |
|
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
347 |
throw new IndexOutOfBoundsException("Bad offset: " + offset); |
36511 | 348 |
} |
27565 | 349 |
|
36511 | 350 |
if (size < 0 || Integer.MAX_VALUE <= size) { |
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
351 |
throw new IndexOutOfBoundsException("Bad size: " + size); |
31673 | 352 |
} |
353 |
||
36511 | 354 |
if (MAP_ALL) { |
355 |
ByteBuffer buffer = slice(memoryMap, (int)offset, (int)size); |
|
356 |
buffer.order(ByteOrder.BIG_ENDIAN); |
|
27565 | 357 |
|
36511 | 358 |
return buffer; |
27565 | 359 |
} else { |
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
360 |
if (channel == null) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
361 |
throw new InternalError("Image file channel not open"); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
362 |
} |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
363 |
|
36511 | 364 |
ByteBuffer buffer = ImageBufferCache.getBuffer(size); |
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
365 |
int read; |
36511 | 366 |
try { |
367 |
read = channel.read(buffer, offset); |
|
368 |
buffer.rewind(); |
|
369 |
} catch (IOException ex) { |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
370 |
ImageBufferCache.releaseBuffer(buffer); |
36511 | 371 |
throw new RuntimeException(ex); |
372 |
} |
|
31673 | 373 |
|
36511 | 374 |
if (read != size) { |
375 |
ImageBufferCache.releaseBuffer(buffer); |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
376 |
throw new RuntimeException("Short read: " + read + |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
377 |
" instead of " + size + " bytes"); |
36511 | 378 |
} |
31673 | 379 |
|
36511 | 380 |
return buffer; |
381 |
} |
|
27565 | 382 |
} |
383 |
||
31673 | 384 |
public byte[] getResource(String name) { |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
385 |
Objects.requireNonNull(name); |
27565 | 386 |
ImageLocation location = findLocation(name); |
387 |
||
388 |
return location != null ? getResource(location) : null; |
|
389 |
} |
|
390 |
||
36511 | 391 |
public byte[] getResource(ImageLocation loc) { |
392 |
ByteBuffer buffer = getResourceBuffer(loc); |
|
393 |
||
394 |
if (buffer != null) { |
|
395 |
byte[] bytes = getBufferBytes(buffer); |
|
396 |
ImageBufferCache.releaseBuffer(buffer); |
|
397 |
||
398 |
return bytes; |
|
399 |
} |
|
400 |
||
401 |
return null; |
|
402 |
} |
|
403 |
||
31673 | 404 |
public ByteBuffer getResourceBuffer(ImageLocation loc) { |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
405 |
Objects.requireNonNull(loc); |
36511 | 406 |
long offset = loc.getContentOffset() + indexSize; |
31673 | 407 |
long compressedSize = loc.getCompressedSize(); |
408 |
long uncompressedSize = loc.getUncompressedSize(); |
|
409 |
||
36511 | 410 |
if (compressedSize < 0 || Integer.MAX_VALUE < compressedSize) { |
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
411 |
throw new IndexOutOfBoundsException( |
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
412 |
"Bad compressed size: " + compressedSize); |
31673 | 413 |
} |
414 |
||
36511 | 415 |
if (uncompressedSize < 0 || Integer.MAX_VALUE < uncompressedSize) { |
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
416 |
throw new IndexOutOfBoundsException( |
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38582
diff
changeset
|
417 |
"Bad uncompressed size: " + uncompressedSize); |
31673 | 418 |
} |
419 |
||
36511 | 420 |
if (compressedSize == 0) { |
421 |
return readBuffer(offset, uncompressedSize); |
|
31673 | 422 |
} else { |
36511 | 423 |
ByteBuffer buffer = readBuffer(offset, compressedSize); |
424 |
||
425 |
if (buffer != null) { |
|
426 |
byte[] bytesIn = getBufferBytes(buffer); |
|
427 |
ImageBufferCache.releaseBuffer(buffer); |
|
428 |
byte[] bytesOut; |
|
31673 | 429 |
|
36511 | 430 |
try { |
431 |
bytesOut = decompressor.decompressResource(byteOrder, |
|
432 |
(int strOffset) -> getString(strOffset), bytesIn); |
|
433 |
} catch (IOException ex) { |
|
434 |
throw new RuntimeException(ex); |
|
435 |
} |
|
436 |
||
437 |
return ByteBuffer.wrap(bytesOut); |
|
438 |
} |
|
31673 | 439 |
} |
27565 | 440 |
|
36511 | 441 |
return null; |
31673 | 442 |
} |
27565 | 443 |
|
31673 | 444 |
public InputStream getResourceStream(ImageLocation loc) { |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
38445
diff
changeset
|
445 |
Objects.requireNonNull(loc); |
31673 | 446 |
byte[] bytes = getResource(loc); |
27565 | 447 |
|
31673 | 448 |
return new ByteArrayInputStream(bytes); |
449 |
} |
|
27565 | 450 |
} |