author | erikj |
Wed, 07 Mar 2018 13:26:15 -0800 | |
changeset 49357 | aaedb8343784 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
31673 | 1 |
/* |
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
2 |
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. |
31673 | 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 |
||
26 |
package jdk.internal.jimage; |
|
27 |
||
36511 | 28 |
import java.io.UTFDataFormatException; |
29 |
import java.nio.ByteBuffer; |
|
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
36511
diff
changeset
|
30 |
import java.util.Objects; |
36511 | 31 |
|
32 |
/** |
|
33 |
* @implNote This class needs to maintain JDK 8 source compatibility. |
|
34 |
* |
|
35 |
* It is used internally in the JDK to implement jimage/jrtfs access, |
|
36 |
* but also compiled and delivered as part of the jrtfs.jar to support access |
|
37 |
* to the jimage file provided by the shipped JDK by tools running on JDK 8. |
|
38 |
*/ |
|
39 |
public class ImageStringsReader implements ImageStrings { |
|
40 |
public static final int HASH_MULTIPLIER = 0x01000193; |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
41 |
public static final int POSITIVE_MASK = 0x7FFFFFFF; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
42 |
|
31673 | 43 |
private final BasicImageReader reader; |
44 |
||
45 |
ImageStringsReader(BasicImageReader reader) { |
|
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
36511
diff
changeset
|
46 |
this.reader = Objects.requireNonNull(reader); |
31673 | 47 |
} |
48 |
||
49 |
@Override |
|
36511 | 50 |
public String get(int offset) { |
51 |
return reader.getString(offset); |
|
31673 | 52 |
} |
53 |
||
54 |
@Override |
|
36511 | 55 |
public int add(final String string) { |
31673 | 56 |
throw new InternalError("Can not add strings at runtime"); |
57 |
} |
|
36511 | 58 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
59 |
public static int hashCode(String s) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
60 |
return hashCode(s, HASH_MULTIPLIER); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
61 |
} |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
36511
diff
changeset
|
62 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
63 |
public static int hashCode(String s, int seed) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
64 |
return unmaskedHashCode(s, seed) & POSITIVE_MASK; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
65 |
} |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
36511
diff
changeset
|
66 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
67 |
public static int hashCode(String module, String name) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
68 |
return hashCode(module, name, HASH_MULTIPLIER); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
69 |
} |
38582
8618f4ccb220
8156209: Add argument checks to BasicImageReader calls
jlaskey
parents:
36511
diff
changeset
|
70 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
71 |
public static int hashCode(String module, String name, int seed) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
72 |
seed = unmaskedHashCode("/", seed); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
73 |
seed = unmaskedHashCode(module, seed); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
74 |
seed = unmaskedHashCode("/", seed); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
75 |
seed = unmaskedHashCode(name, seed); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
76 |
return seed & POSITIVE_MASK; |
36511 | 77 |
} |
78 |
||
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
79 |
public static int unmaskedHashCode(String s, int seed) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
80 |
int slen = s.length(); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
81 |
byte[] buffer = null; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
82 |
|
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
83 |
for (int i = 0; i < slen; i++) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
84 |
char ch = s.charAt(i); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
85 |
int uch = ch & 0xFFFF; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
86 |
|
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
87 |
if ((uch & ~0x7F) != 0) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
88 |
if (buffer == null) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
89 |
buffer = new byte[8]; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
90 |
} |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
91 |
int mask = ~0x3F; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
92 |
int n = 0; |
36511 | 93 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
94 |
do { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
95 |
buffer[n++] = (byte)(0x80 | (uch & 0x3F)); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
96 |
uch >>= 6; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
97 |
mask >>= 1; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
98 |
} while ((uch & mask) != 0); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
99 |
|
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
100 |
buffer[n] = (byte)((mask << 1) | uch); |
36511 | 101 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
102 |
do { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
103 |
seed = (seed * HASH_MULTIPLIER) ^ (buffer[n--] & 0xFF); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
104 |
} while (0 <= n); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
105 |
} else if (uch == 0) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
106 |
seed = (seed * HASH_MULTIPLIER) ^ (0xC0); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
107 |
seed = (seed * HASH_MULTIPLIER) ^ (0x80); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
108 |
} else { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
109 |
seed = (seed * HASH_MULTIPLIER) ^ (uch); |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
110 |
} |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
111 |
} |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
112 |
return seed; |
36511 | 113 |
} |
114 |
||
115 |
static int charsFromMUTF8Length(byte[] bytes, int offset, int count) { |
|
116 |
int length = 0; |
|
117 |
||
118 |
for (int i = offset; i < offset + count; i++) { |
|
119 |
byte ch = bytes[i]; |
|
120 |
||
121 |
if (ch == 0) { |
|
122 |
break; |
|
123 |
} |
|
124 |
||
125 |
if ((ch & 0xC0) != 0x80) { |
|
126 |
length++; |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
return length; |
|
131 |
} |
|
132 |
||
133 |
static void charsFromMUTF8(char[] chars, byte[] bytes, int offset, int count) throws UTFDataFormatException { |
|
134 |
int j = 0; |
|
135 |
||
136 |
for (int i = offset; i < offset + count; i++) { |
|
137 |
byte ch = bytes[i]; |
|
138 |
||
139 |
if (ch == 0) { |
|
140 |
break; |
|
141 |
} |
|
142 |
||
143 |
boolean is_unicode = (ch & 0x80) != 0; |
|
144 |
int uch = ch & 0x7F; |
|
145 |
||
146 |
if (is_unicode) { |
|
147 |
int mask = 0x40; |
|
148 |
||
149 |
while ((uch & mask) != 0) { |
|
150 |
ch = bytes[++i]; |
|
151 |
||
152 |
if ((ch & 0xC0) != 0x80) { |
|
153 |
throw new UTFDataFormatException("bad continuation 0x" + Integer.toHexString(ch)); |
|
154 |
} |
|
155 |
||
156 |
uch = ((uch & ~mask) << 6) | (ch & 0x3F); |
|
157 |
mask <<= 6 - 1; |
|
158 |
} |
|
159 |
||
160 |
if ((uch & 0xFFFF) != uch) { |
|
161 |
throw new UTFDataFormatException("character out of range \\u" + Integer.toHexString(uch)); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
chars[j++] = (char)uch; |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
public static String stringFromMUTF8(byte[] bytes, int offset, int count) { |
|
170 |
int length = charsFromMUTF8Length(bytes, offset, count); |
|
171 |
char[] chars = new char[length]; |
|
172 |
||
173 |
try { |
|
174 |
charsFromMUTF8(chars, bytes, offset, count); |
|
175 |
} catch (UTFDataFormatException ex) { |
|
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38584
diff
changeset
|
176 |
throw new InternalError("Attempt to convert non modified UTF-8 byte sequence", ex); |
36511 | 177 |
} |
178 |
||
179 |
return new String(chars); |
|
180 |
} |
|
181 |
||
182 |
public static String stringFromMUTF8(byte[] bytes) { |
|
183 |
return stringFromMUTF8(bytes, 0, bytes.length); |
|
184 |
} |
|
185 |
||
186 |
static int charsFromByteBufferLength(ByteBuffer buffer) { |
|
187 |
int length = 0; |
|
188 |
||
189 |
while(buffer.hasRemaining()) { |
|
190 |
byte ch = buffer.get(); |
|
191 |
||
192 |
if (ch == 0) { |
|
193 |
return length; |
|
194 |
} |
|
195 |
||
196 |
if ((ch & 0xC0) != 0x80) { |
|
197 |
length++; |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
throw new InternalError("No terminating zero byte for modified UTF-8 byte sequence"); |
|
202 |
} |
|
203 |
||
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
204 |
static void charsFromByteBuffer(char[] chars, ByteBuffer buffer) { |
36511 | 205 |
int j = 0; |
206 |
||
207 |
while(buffer.hasRemaining()) { |
|
208 |
byte ch = buffer.get(); |
|
209 |
||
210 |
if (ch == 0) { |
|
211 |
return; |
|
212 |
} |
|
213 |
||
214 |
boolean is_unicode = (ch & 0x80) != 0; |
|
215 |
int uch = ch & 0x7F; |
|
216 |
||
217 |
if (is_unicode) { |
|
218 |
int mask = 0x40; |
|
219 |
||
220 |
while ((uch & mask) != 0) { |
|
221 |
ch = buffer.get(); |
|
222 |
||
223 |
if ((ch & 0xC0) != 0x80) { |
|
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38584
diff
changeset
|
224 |
throw new InternalError("Bad continuation in " + |
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38584
diff
changeset
|
225 |
"modified UTF-8 byte sequence: " + ch); |
36511 | 226 |
} |
227 |
||
228 |
uch = ((uch & ~mask) << 6) | (ch & 0x3F); |
|
229 |
mask <<= 6 - 1; |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
if ((uch & 0xFFFF) != uch) { |
|
41121
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38584
diff
changeset
|
234 |
throw new InternalError("UTF-32 char in modified UTF-8 " + |
91734a3ed04b
8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents:
38584
diff
changeset
|
235 |
"byte sequence: " + uch); |
36511 | 236 |
} |
237 |
||
238 |
chars[j++] = (char)uch; |
|
239 |
} |
|
240 |
||
241 |
throw new InternalError("No terminating zero byte for modified UTF-8 byte sequence"); |
|
242 |
} |
|
243 |
||
244 |
public static String stringFromByteBuffer(ByteBuffer buffer) { |
|
245 |
int length = charsFromByteBufferLength(buffer); |
|
246 |
buffer.rewind(); |
|
247 |
char[] chars = new char[length]; |
|
248 |
charsFromByteBuffer(chars, buffer); |
|
249 |
||
250 |
return new String(chars); |
|
251 |
} |
|
252 |
||
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
253 |
static int mutf8FromStringLength(String s) { |
36511 | 254 |
int length = 0; |
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
255 |
int slen = s.length(); |
36511 | 256 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
257 |
for (int i = 0; i < slen; i++) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
258 |
char ch = s.charAt(i); |
36511 | 259 |
int uch = ch & 0xFFFF; |
260 |
||
261 |
if ((uch & ~0x7F) != 0) { |
|
262 |
int mask = ~0x3F; |
|
263 |
int n = 0; |
|
264 |
||
265 |
do { |
|
266 |
n++; |
|
267 |
uch >>= 6; |
|
268 |
mask >>= 1; |
|
269 |
} while ((uch & mask) != 0); |
|
270 |
||
271 |
length += n + 1; |
|
272 |
} else if (uch == 0) { |
|
273 |
length += 2; |
|
274 |
} else { |
|
275 |
length++; |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
return length; |
|
280 |
} |
|
281 |
||
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
282 |
static void mutf8FromString(byte[] bytes, int offset, String s) { |
36511 | 283 |
int j = offset; |
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
284 |
byte[] buffer = null; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
285 |
int slen = s.length(); |
36511 | 286 |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
287 |
for (int i = 0; i < slen; i++) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
288 |
char ch = s.charAt(i); |
36511 | 289 |
int uch = ch & 0xFFFF; |
290 |
||
291 |
if ((uch & ~0x7F) != 0) { |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
292 |
if (buffer == null) { |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
293 |
buffer = new byte[8]; |
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
294 |
} |
36511 | 295 |
int mask = ~0x3F; |
296 |
int n = 0; |
|
297 |
||
298 |
do { |
|
299 |
buffer[n++] = (byte)(0x80 | (uch & 0x3F)); |
|
300 |
uch >>= 6; |
|
301 |
mask >>= 1; |
|
302 |
} while ((uch & mask) != 0); |
|
303 |
||
304 |
buffer[n] = (byte)((mask << 1) | uch); |
|
305 |
||
306 |
do { |
|
307 |
bytes[j++] = buffer[n--]; |
|
308 |
} while (0 <= n); |
|
309 |
} else if (uch == 0) { |
|
310 |
bytes[j++] = (byte)0xC0; |
|
311 |
bytes[j++] = (byte)0x80; |
|
312 |
} else { |
|
313 |
bytes[j++] = (byte)uch; |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
public static byte[] mutf8FromString(String string) { |
|
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
319 |
int length = mutf8FromStringLength(string); |
36511 | 320 |
byte[] bytes = new byte[length]; |
44034
0d664fca91a7
8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents:
41121
diff
changeset
|
321 |
mutf8FromString(bytes, 0, string); |
36511 | 322 |
|
323 |
return bytes; |
|
324 |
} |
|
31673 | 325 |
} |