author | ksrini |
Tue, 29 May 2012 14:56:48 -0700 | |
changeset 12857 | 0a5f341c2a28 |
parent 12544 | 5768f2e096de |
child 16075 | 32c3bc19bba7 |
child 15526 | 84de8685a2d0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12544 | 2 |
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.java.util.jar.pack; |
|
27 |
||
7192
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6313
diff
changeset
|
28 |
import java.util.AbstractList; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6313
diff
changeset
|
29 |
import java.util.ArrayList; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6313
diff
changeset
|
30 |
import java.util.Arrays; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6313
diff
changeset
|
31 |
import java.util.Collection; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6313
diff
changeset
|
32 |
import java.util.List; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6313
diff
changeset
|
33 |
import java.util.ListIterator; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6313
diff
changeset
|
34 |
import java.util.Map; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6313
diff
changeset
|
35 |
import java.util.Set; |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
36 |
import static com.sun.java.util.jar.pack.Constants.*; |
2 | 37 |
|
38 |
/** |
|
39 |
* Representation of constant pool entries and indexes. |
|
40 |
* @author John Rose |
|
41 |
*/ |
|
42 |
abstract |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
43 |
class ConstantPool { |
2 | 44 |
private ConstantPool() {} // do not instantiate |
45 |
||
46 |
static int verbose() { |
|
47 |
return Utils.currentPropMap().getInteger(Utils.DEBUG_VERBOSE); |
|
48 |
} |
|
49 |
||
50 |
/** Factory for Utf8 string constants. |
|
51 |
* Used for well-known strings like "SourceFile", "<init>", etc. |
|
52 |
* Also used to back up more complex constant pool entries, like Class. |
|
53 |
*/ |
|
54 |
public static synchronized Utf8Entry getUtf8Entry(String value) { |
|
12544 | 55 |
Map<String, Utf8Entry> utf8Entries = Utils.getTLGlobals().getUtf8Entries(); |
6313 | 56 |
Utf8Entry e = utf8Entries.get(value); |
2 | 57 |
if (e == null) { |
58 |
e = new Utf8Entry(value); |
|
59 |
utf8Entries.put(e.stringValue(), e); |
|
60 |
} |
|
61 |
return e; |
|
62 |
} |
|
63 |
/** Factory for Class constants. */ |
|
12544 | 64 |
public static ClassEntry getClassEntry(String name) { |
65 |
Map<String, ClassEntry> classEntries = Utils.getTLGlobals().getClassEntries(); |
|
6313 | 66 |
ClassEntry e = classEntries.get(name); |
2 | 67 |
if (e == null) { |
6313 | 68 |
e = new ClassEntry(getUtf8Entry(name)); |
2 | 69 |
assert(name.equals(e.stringValue())); |
70 |
classEntries.put(e.stringValue(), e); |
|
71 |
} |
|
72 |
return e; |
|
73 |
} |
|
74 |
/** Factory for literal constants (String, Integer, etc.). */ |
|
12544 | 75 |
public static LiteralEntry getLiteralEntry(Comparable<?> value) { |
76 |
Map<Object, LiteralEntry> literalEntries = Utils.getTLGlobals().getLiteralEntries(); |
|
6313 | 77 |
LiteralEntry e = literalEntries.get(value); |
2 | 78 |
if (e == null) { |
79 |
if (value instanceof String) |
|
80 |
e = new StringEntry(getUtf8Entry((String)value)); |
|
81 |
else |
|
82 |
e = new NumberEntry((Number)value); |
|
83 |
literalEntries.put(value, e); |
|
84 |
} |
|
85 |
return e; |
|
86 |
} |
|
87 |
/** Factory for literal constants (String, Integer, etc.). */ |
|
12544 | 88 |
public static StringEntry getStringEntry(String value) { |
2 | 89 |
return (StringEntry) getLiteralEntry(value); |
90 |
} |
|
91 |
||
92 |
/** Factory for signature (type) constants. */ |
|
12544 | 93 |
public static SignatureEntry getSignatureEntry(String type) { |
94 |
Map<String, SignatureEntry> signatureEntries = Utils.getTLGlobals().getSignatureEntries(); |
|
6313 | 95 |
SignatureEntry e = signatureEntries.get(type); |
2 | 96 |
if (e == null) { |
97 |
e = new SignatureEntry(type); |
|
98 |
assert(e.stringValue().equals(type)); |
|
99 |
signatureEntries.put(type, e); |
|
100 |
} |
|
101 |
return e; |
|
102 |
} |
|
103 |
// Convenience overloading. |
|
104 |
public static SignatureEntry getSignatureEntry(Utf8Entry formRef, ClassEntry[] classRefs) { |
|
105 |
return getSignatureEntry(SignatureEntry.stringValueOf(formRef, classRefs)); |
|
106 |
} |
|
107 |
||
108 |
/** Factory for descriptor (name-and-type) constants. */ |
|
12544 | 109 |
public static DescriptorEntry getDescriptorEntry(Utf8Entry nameRef, SignatureEntry typeRef) { |
110 |
Map<String, DescriptorEntry> descriptorEntries = Utils.getTLGlobals().getDescriptorEntries(); |
|
2 | 111 |
String key = DescriptorEntry.stringValueOf(nameRef, typeRef); |
6313 | 112 |
DescriptorEntry e = descriptorEntries.get(key); |
2 | 113 |
if (e == null) { |
114 |
e = new DescriptorEntry(nameRef, typeRef); |
|
115 |
assert(e.stringValue().equals(key)) |
|
116 |
: (e.stringValue()+" != "+(key)); |
|
117 |
descriptorEntries.put(key, e); |
|
118 |
} |
|
119 |
return e; |
|
120 |
} |
|
121 |
// Convenience overloading. |
|
122 |
public static DescriptorEntry getDescriptorEntry(Utf8Entry nameRef, Utf8Entry typeRef) { |
|
123 |
return getDescriptorEntry(nameRef, getSignatureEntry(typeRef.stringValue())); |
|
124 |
} |
|
125 |
||
126 |
/** Factory for member reference constants. */ |
|
12544 | 127 |
public static MemberEntry getMemberEntry(byte tag, ClassEntry classRef, DescriptorEntry descRef) { |
128 |
Map<String, MemberEntry> memberEntries = Utils.getTLGlobals().getMemberEntries(); |
|
2 | 129 |
String key = MemberEntry.stringValueOf(tag, classRef, descRef); |
6313 | 130 |
MemberEntry e = memberEntries.get(key); |
2 | 131 |
if (e == null) { |
132 |
e = new MemberEntry(tag, classRef, descRef); |
|
133 |
assert(e.stringValue().equals(key)) |
|
134 |
: (e.stringValue()+" != "+(key)); |
|
135 |
memberEntries.put(key, e); |
|
136 |
} |
|
137 |
return e; |
|
138 |
} |
|
139 |
||
12544 | 140 |
/** Factory for MethodHandle constants. */ |
141 |
public static MethodHandleEntry getMethodHandleEntry(byte refKind, MemberEntry memRef) { |
|
142 |
Map<String, MethodHandleEntry> methodHandleEntries = Utils.getTLGlobals().getMethodHandleEntries(); |
|
143 |
String key = MethodHandleEntry.stringValueOf(refKind, memRef); |
|
144 |
MethodHandleEntry e = methodHandleEntries.get(key); |
|
145 |
if (e == null) { |
|
146 |
e = new MethodHandleEntry(refKind, memRef); |
|
147 |
assert(e.stringValue().equals(key)); |
|
148 |
methodHandleEntries.put(key, e); |
|
149 |
} |
|
150 |
return e; |
|
151 |
} |
|
152 |
||
153 |
/** Factory for MethodType constants. */ |
|
154 |
public static MethodTypeEntry getMethodTypeEntry(SignatureEntry sigRef) { |
|
155 |
Map<String, MethodTypeEntry> methodTypeEntries = Utils.getTLGlobals().getMethodTypeEntries(); |
|
156 |
String key = sigRef.stringValue(); |
|
157 |
MethodTypeEntry e = methodTypeEntries.get(key); |
|
158 |
if (e == null) { |
|
159 |
e = new MethodTypeEntry(sigRef); |
|
160 |
assert(e.stringValue().equals(key)); |
|
161 |
methodTypeEntries.put(key, e); |
|
162 |
} |
|
163 |
return e; |
|
164 |
} |
|
165 |
public static MethodTypeEntry getMethodTypeEntry(Utf8Entry typeRef) { |
|
166 |
return getMethodTypeEntry(getSignatureEntry(typeRef.stringValue())); |
|
167 |
} |
|
168 |
||
169 |
/** Factory for InvokeDynamic constants. */ |
|
170 |
public static InvokeDynamicEntry getInvokeDynamicEntry(BootstrapMethodEntry bssRef, DescriptorEntry descRef) { |
|
171 |
Map<String, InvokeDynamicEntry> invokeDynamicEntries = Utils.getTLGlobals().getInvokeDynamicEntries(); |
|
172 |
String key = InvokeDynamicEntry.stringValueOf(bssRef, descRef); |
|
173 |
InvokeDynamicEntry e = invokeDynamicEntries.get(key); |
|
174 |
if (e == null) { |
|
175 |
e = new InvokeDynamicEntry(bssRef, descRef); |
|
176 |
assert(e.stringValue().equals(key)); |
|
177 |
invokeDynamicEntries.put(key, e); |
|
178 |
} |
|
179 |
return e; |
|
180 |
} |
|
181 |
||
182 |
/** Factory for BootstrapMethod pseudo-constants. */ |
|
183 |
public static BootstrapMethodEntry getBootstrapMethodEntry(MethodHandleEntry bsmRef, Entry[] argRefs) { |
|
184 |
Map<String, BootstrapMethodEntry> bootstrapMethodEntries = Utils.getTLGlobals().getBootstrapMethodEntries(); |
|
185 |
String key = BootstrapMethodEntry.stringValueOf(bsmRef, argRefs); |
|
186 |
BootstrapMethodEntry e = bootstrapMethodEntries.get(key); |
|
187 |
if (e == null) { |
|
188 |
e = new BootstrapMethodEntry(bsmRef, argRefs); |
|
189 |
assert(e.stringValue().equals(key)); |
|
190 |
bootstrapMethodEntries.put(key, e); |
|
191 |
} |
|
192 |
return e; |
|
193 |
} |
|
194 |
||
2 | 195 |
|
196 |
/** Entries in the constant pool. */ |
|
197 |
public static abstract |
|
10115 | 198 |
class Entry implements Comparable<Object> { |
2 | 199 |
protected final byte tag; // a CONSTANT_foo code |
200 |
protected int valueHash; // cached hashCode |
|
201 |
||
202 |
protected Entry(byte tag) { |
|
203 |
this.tag = tag; |
|
204 |
} |
|
205 |
||
206 |
public final byte getTag() { |
|
207 |
return tag; |
|
208 |
} |
|
209 |
||
210 |
public Entry getRef(int i) { |
|
211 |
return null; |
|
212 |
} |
|
213 |
||
214 |
public boolean eq(Entry that) { // same reference |
|
215 |
assert(that != null); |
|
216 |
return this == that || this.equals(that); |
|
217 |
} |
|
218 |
||
219 |
// Equality of Entries is value-based. |
|
220 |
public abstract boolean equals(Object o); |
|
221 |
public final int hashCode() { |
|
222 |
if (valueHash == 0) { |
|
223 |
valueHash = computeValueHash(); |
|
224 |
if (valueHash == 0) valueHash = 1; |
|
225 |
} |
|
226 |
return valueHash; |
|
227 |
} |
|
228 |
protected abstract int computeValueHash(); |
|
229 |
||
230 |
public abstract int compareTo(Object o); |
|
231 |
||
232 |
protected int superCompareTo(Object o) { |
|
233 |
Entry that = (Entry) o; |
|
234 |
||
235 |
if (this.tag != that.tag) { |
|
236 |
return TAG_ORDER[this.tag] - TAG_ORDER[that.tag]; |
|
237 |
} |
|
238 |
||
239 |
return 0; // subclasses must refine this |
|
240 |
} |
|
241 |
||
242 |
public final boolean isDoubleWord() { |
|
243 |
return tag == CONSTANT_Double || tag == CONSTANT_Long; |
|
244 |
} |
|
245 |
||
246 |
public final boolean tagMatches(int tag) { |
|
247 |
return (this.tag == tag); |
|
248 |
} |
|
249 |
||
250 |
public String toString() { |
|
251 |
String valuePrint = stringValue(); |
|
252 |
if (verbose() > 4) { |
|
253 |
if (valueHash != 0) |
|
254 |
valuePrint += " hash="+valueHash; |
|
255 |
valuePrint += " id="+System.identityHashCode(this); |
|
256 |
} |
|
257 |
return tagName(tag)+"="+valuePrint; |
|
258 |
} |
|
259 |
public abstract String stringValue(); |
|
260 |
} |
|
261 |
||
262 |
public static |
|
263 |
class Utf8Entry extends Entry { |
|
264 |
final String value; |
|
265 |
||
266 |
Utf8Entry(String value) { |
|
267 |
super(CONSTANT_Utf8); |
|
268 |
this.value = value.intern(); |
|
269 |
hashCode(); // force computation of valueHash |
|
270 |
} |
|
271 |
protected int computeValueHash() { |
|
272 |
return value.hashCode(); |
|
273 |
} |
|
274 |
public boolean equals(Object o) { |
|
275 |
// Use reference equality of interned strings: |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
276 |
return (o != null && o.getClass() == Utf8Entry.class |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
277 |
&& ((Utf8Entry) o).value.equals(value)); |
2 | 278 |
} |
279 |
public int compareTo(Object o) { |
|
280 |
int x = superCompareTo(o); |
|
281 |
if (x == 0) { |
|
282 |
x = value.compareTo(((Utf8Entry)o).value); |
|
283 |
} |
|
284 |
return x; |
|
285 |
} |
|
286 |
public String stringValue() { |
|
287 |
return value; |
|
288 |
} |
|
289 |
} |
|
290 |
||
291 |
static boolean isMemberTag(byte tag) { |
|
292 |
switch (tag) { |
|
293 |
case CONSTANT_Fieldref: |
|
294 |
case CONSTANT_Methodref: |
|
295 |
case CONSTANT_InterfaceMethodref: |
|
296 |
return true; |
|
297 |
} |
|
298 |
return false; |
|
299 |
} |
|
300 |
||
301 |
static byte numberTagOf(Number value) { |
|
302 |
if (value instanceof Integer) return CONSTANT_Integer; |
|
303 |
if (value instanceof Float) return CONSTANT_Float; |
|
304 |
if (value instanceof Long) return CONSTANT_Long; |
|
305 |
if (value instanceof Double) return CONSTANT_Double; |
|
306 |
throw new RuntimeException("bad literal value "+value); |
|
307 |
} |
|
308 |
||
12544 | 309 |
static boolean isRefKind(byte refKind) { |
310 |
return (REF_getField <= refKind && refKind <= REF_invokeInterface); |
|
311 |
} |
|
312 |
||
2 | 313 |
public static abstract |
314 |
class LiteralEntry extends Entry { |
|
315 |
protected LiteralEntry(byte tag) { |
|
316 |
super(tag); |
|
317 |
} |
|
318 |
||
10115 | 319 |
public abstract Comparable<?> literalValue(); |
2 | 320 |
} |
321 |
||
322 |
public static |
|
323 |
class NumberEntry extends LiteralEntry { |
|
324 |
final Number value; |
|
325 |
NumberEntry(Number value) { |
|
326 |
super(numberTagOf(value)); |
|
327 |
this.value = value; |
|
328 |
hashCode(); // force computation of valueHash |
|
329 |
} |
|
330 |
protected int computeValueHash() { |
|
331 |
return value.hashCode(); |
|
332 |
} |
|
333 |
||
334 |
public boolean equals(Object o) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
335 |
return (o != null && o.getClass() == NumberEntry.class |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
336 |
&& ((NumberEntry) o).value.equals(value)); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
337 |
|
2 | 338 |
} |
339 |
public int compareTo(Object o) { |
|
340 |
int x = superCompareTo(o); |
|
341 |
if (x == 0) { |
|
10115 | 342 |
@SuppressWarnings("unchecked") |
343 |
Comparable<Number> compValue = (Comparable<Number>)value; |
|
344 |
x = compValue.compareTo(((NumberEntry)o).value); |
|
2 | 345 |
} |
346 |
return x; |
|
347 |
} |
|
348 |
public Number numberValue() { |
|
349 |
return value; |
|
350 |
} |
|
10115 | 351 |
public Comparable<?> literalValue() { |
352 |
return (Comparable<?>) value; |
|
2 | 353 |
} |
354 |
public String stringValue() { |
|
355 |
return value.toString(); |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
public static |
|
360 |
class StringEntry extends LiteralEntry { |
|
361 |
final Utf8Entry ref; |
|
362 |
public Entry getRef(int i) { return i == 0 ? ref : null; } |
|
363 |
||
364 |
StringEntry(Entry ref) { |
|
365 |
super(CONSTANT_String); |
|
366 |
this.ref = (Utf8Entry) ref; |
|
367 |
hashCode(); // force computation of valueHash |
|
368 |
} |
|
369 |
protected int computeValueHash() { |
|
370 |
return ref.hashCode() + tag; |
|
371 |
} |
|
372 |
public boolean equals(Object o) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
373 |
return (o != null && o.getClass() == StringEntry.class && |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
374 |
((StringEntry)o).ref.eq(ref)); |
2 | 375 |
} |
376 |
public int compareTo(Object o) { |
|
377 |
int x = superCompareTo(o); |
|
378 |
if (x == 0) { |
|
379 |
x = ref.compareTo(((StringEntry)o).ref); |
|
380 |
} |
|
381 |
return x; |
|
382 |
} |
|
10115 | 383 |
public Comparable<?> literalValue() { |
2 | 384 |
return ref.stringValue(); |
385 |
} |
|
386 |
public String stringValue() { |
|
387 |
return ref.stringValue(); |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
public static |
|
392 |
class ClassEntry extends Entry { |
|
393 |
final Utf8Entry ref; |
|
394 |
public Entry getRef(int i) { return i == 0 ? ref : null; } |
|
395 |
||
396 |
protected int computeValueHash() { |
|
397 |
return ref.hashCode() + tag; |
|
398 |
} |
|
399 |
ClassEntry(Entry ref) { |
|
400 |
super(CONSTANT_Class); |
|
401 |
this.ref = (Utf8Entry) ref; |
|
402 |
hashCode(); // force computation of valueHash |
|
403 |
} |
|
404 |
public boolean equals(Object o) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
405 |
return (o != null && o.getClass() == ClassEntry.class |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
406 |
&& ((ClassEntry) o).ref.eq(ref)); |
2 | 407 |
} |
408 |
public int compareTo(Object o) { |
|
409 |
int x = superCompareTo(o); |
|
410 |
if (x == 0) { |
|
411 |
x = ref.compareTo(((ClassEntry)o).ref); |
|
412 |
} |
|
413 |
return x; |
|
414 |
} |
|
415 |
public String stringValue() { |
|
416 |
return ref.stringValue(); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
public static |
|
421 |
class DescriptorEntry extends Entry { |
|
422 |
final Utf8Entry nameRef; |
|
423 |
final SignatureEntry typeRef; |
|
424 |
public Entry getRef(int i) { |
|
425 |
if (i == 0) return nameRef; |
|
426 |
if (i == 1) return typeRef; |
|
427 |
return null; |
|
428 |
} |
|
429 |
DescriptorEntry(Entry nameRef, Entry typeRef) { |
|
430 |
super(CONSTANT_NameandType); |
|
431 |
if (typeRef instanceof Utf8Entry) { |
|
432 |
typeRef = getSignatureEntry(typeRef.stringValue()); |
|
433 |
} |
|
434 |
this.nameRef = (Utf8Entry) nameRef; |
|
435 |
this.typeRef = (SignatureEntry) typeRef; |
|
436 |
hashCode(); // force computation of valueHash |
|
437 |
} |
|
438 |
protected int computeValueHash() { |
|
439 |
int hc2 = typeRef.hashCode(); |
|
440 |
return (nameRef.hashCode() + (hc2 << 8)) ^ hc2; |
|
441 |
} |
|
442 |
public boolean equals(Object o) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
443 |
if (o == null || o.getClass() != DescriptorEntry.class) { |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
444 |
return false; |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
445 |
} |
2 | 446 |
DescriptorEntry that = (DescriptorEntry)o; |
447 |
return this.nameRef.eq(that.nameRef) |
|
448 |
&& this.typeRef.eq(that.typeRef); |
|
449 |
} |
|
450 |
public int compareTo(Object o) { |
|
451 |
int x = superCompareTo(o); |
|
452 |
if (x == 0) { |
|
453 |
DescriptorEntry that = (DescriptorEntry)o; |
|
454 |
// Primary key is typeRef, not nameRef. |
|
455 |
x = this.typeRef.compareTo(that.typeRef); |
|
456 |
if (x == 0) |
|
457 |
x = this.nameRef.compareTo(that.nameRef); |
|
458 |
} |
|
459 |
return x; |
|
460 |
} |
|
461 |
public String stringValue() { |
|
462 |
return stringValueOf(nameRef, typeRef); |
|
463 |
} |
|
464 |
static |
|
465 |
String stringValueOf(Entry nameRef, Entry typeRef) { |
|
12544 | 466 |
return qualifiedStringValue(typeRef, nameRef); |
2 | 467 |
} |
468 |
||
469 |
public String prettyString() { |
|
470 |
return nameRef.stringValue()+typeRef.prettyString(); |
|
471 |
} |
|
472 |
||
473 |
public boolean isMethod() { |
|
474 |
return typeRef.isMethod(); |
|
475 |
} |
|
476 |
||
477 |
public byte getLiteralTag() { |
|
478 |
return typeRef.getLiteralTag(); |
|
479 |
} |
|
480 |
} |
|
481 |
||
12544 | 482 |
static String qualifiedStringValue(Entry e1, Entry e2) { |
483 |
return qualifiedStringValue(e1.stringValue(), e2.stringValue()); |
|
484 |
} |
|
485 |
static String qualifiedStringValue(String s1, String s234) { |
|
486 |
// Qualification by dot must decompose uniquely. Second string might already be qualified. |
|
487 |
assert(s1.indexOf(".") < 0); |
|
488 |
return s1+"."+s234; |
|
489 |
} |
|
490 |
||
2 | 491 |
public static |
492 |
class MemberEntry extends Entry { |
|
493 |
final ClassEntry classRef; |
|
494 |
final DescriptorEntry descRef; |
|
495 |
public Entry getRef(int i) { |
|
496 |
if (i == 0) return classRef; |
|
497 |
if (i == 1) return descRef; |
|
498 |
return null; |
|
499 |
} |
|
500 |
protected int computeValueHash() { |
|
501 |
int hc2 = descRef.hashCode(); |
|
502 |
return (classRef.hashCode() + (hc2 << 8)) ^ hc2; |
|
503 |
} |
|
504 |
||
505 |
MemberEntry(byte tag, ClassEntry classRef, DescriptorEntry descRef) { |
|
506 |
super(tag); |
|
507 |
assert(isMemberTag(tag)); |
|
508 |
this.classRef = classRef; |
|
509 |
this.descRef = descRef; |
|
510 |
hashCode(); // force computation of valueHash |
|
511 |
} |
|
512 |
public boolean equals(Object o) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
513 |
if (o == null || o.getClass() != MemberEntry.class) { |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
514 |
return false; |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
515 |
} |
2 | 516 |
MemberEntry that = (MemberEntry)o; |
517 |
return this.classRef.eq(that.classRef) |
|
518 |
&& this.descRef.eq(that.descRef); |
|
519 |
} |
|
520 |
public int compareTo(Object o) { |
|
521 |
int x = superCompareTo(o); |
|
522 |
if (x == 0) { |
|
523 |
MemberEntry that = (MemberEntry)o; |
|
12544 | 524 |
if (Utils.SORT_MEMBERS_DESCR_MAJOR) |
525 |
// descRef is transmitted as UDELTA5; sort it first? |
|
526 |
x = this.descRef.compareTo(that.descRef); |
|
2 | 527 |
// Primary key is classRef. |
12544 | 528 |
if (x == 0) |
529 |
x = this.classRef.compareTo(that.classRef); |
|
2 | 530 |
if (x == 0) |
531 |
x = this.descRef.compareTo(that.descRef); |
|
532 |
} |
|
533 |
return x; |
|
534 |
} |
|
535 |
public String stringValue() { |
|
536 |
return stringValueOf(tag, classRef, descRef); |
|
537 |
} |
|
538 |
static |
|
539 |
String stringValueOf(byte tag, ClassEntry classRef, DescriptorEntry descRef) { |
|
540 |
assert(isMemberTag(tag)); |
|
541 |
String pfx; |
|
542 |
switch (tag) { |
|
543 |
case CONSTANT_Fieldref: pfx = "Field:"; break; |
|
544 |
case CONSTANT_Methodref: pfx = "Method:"; break; |
|
545 |
case CONSTANT_InterfaceMethodref: pfx = "IMethod:"; break; |
|
546 |
default: pfx = tag+"???"; break; |
|
547 |
} |
|
12544 | 548 |
return pfx+qualifiedStringValue(classRef, descRef); |
2 | 549 |
} |
550 |
||
551 |
public boolean isMethod() { |
|
552 |
return descRef.isMethod(); |
|
553 |
} |
|
554 |
} |
|
555 |
||
556 |
public static |
|
557 |
class SignatureEntry extends Entry { |
|
558 |
final Utf8Entry formRef; |
|
559 |
final ClassEntry[] classRefs; |
|
560 |
String value; |
|
561 |
Utf8Entry asUtf8Entry; |
|
562 |
public Entry getRef(int i) { |
|
563 |
if (i == 0) return formRef; |
|
564 |
return i-1 < classRefs.length ? classRefs[i-1] : null; |
|
565 |
} |
|
566 |
SignatureEntry(String value) { |
|
567 |
super(CONSTANT_Signature); |
|
568 |
value = value.intern(); // always do this |
|
569 |
this.value = value; |
|
570 |
String[] parts = structureSignature(value); |
|
571 |
formRef = getUtf8Entry(parts[0]); |
|
572 |
classRefs = new ClassEntry[parts.length-1]; |
|
6313 | 573 |
for (int i = 1; i < parts.length; i++) { |
574 |
classRefs[i - 1] = getClassEntry(parts[i]); |
|
575 |
} |
|
2 | 576 |
hashCode(); // force computation of valueHash |
577 |
} |
|
578 |
protected int computeValueHash() { |
|
579 |
stringValue(); // force computation of value |
|
580 |
return value.hashCode() + tag; |
|
581 |
} |
|
582 |
||
583 |
public Utf8Entry asUtf8Entry() { |
|
584 |
if (asUtf8Entry == null) { |
|
585 |
asUtf8Entry = getUtf8Entry(stringValue()); |
|
586 |
} |
|
587 |
return asUtf8Entry; |
|
588 |
} |
|
589 |
||
590 |
public boolean equals(Object o) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
591 |
return (o != null && o.getClass() == SignatureEntry.class && |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
592 |
((SignatureEntry)o).value.equals(value)); |
2 | 593 |
} |
594 |
public int compareTo(Object o) { |
|
595 |
int x = superCompareTo(o); |
|
596 |
if (x == 0) { |
|
597 |
SignatureEntry that = (SignatureEntry)o; |
|
598 |
x = compareSignatures(this.value, that.value); |
|
599 |
} |
|
600 |
return x; |
|
601 |
} |
|
602 |
public String stringValue() { |
|
603 |
if (value == null) { |
|
604 |
value = stringValueOf(formRef, classRefs); |
|
605 |
} |
|
606 |
return value; |
|
607 |
} |
|
608 |
static |
|
609 |
String stringValueOf(Utf8Entry formRef, ClassEntry[] classRefs) { |
|
610 |
String[] parts = new String[1+classRefs.length]; |
|
611 |
parts[0] = formRef.stringValue(); |
|
6313 | 612 |
for (int i = 1; i < parts.length; i++) { |
613 |
parts[i] = classRefs[i - 1].stringValue(); |
|
614 |
} |
|
2 | 615 |
return flattenSignature(parts).intern(); |
616 |
} |
|
617 |
||
618 |
public int computeSize(boolean countDoublesTwice) { |
|
619 |
String form = formRef.stringValue(); |
|
620 |
int min = 0; |
|
621 |
int max = 1; |
|
622 |
if (isMethod()) { |
|
623 |
min = 1; |
|
624 |
max = form.indexOf(')'); |
|
625 |
} |
|
626 |
int size = 0; |
|
627 |
for (int i = min; i < max; i++) { |
|
628 |
switch (form.charAt(i)) { |
|
6313 | 629 |
case 'D': |
630 |
case 'J': |
|
631 |
if (countDoublesTwice) { |
|
632 |
size++; |
|
633 |
} |
|
634 |
break; |
|
635 |
case '[': |
|
636 |
// Skip rest of array info. |
|
637 |
while (form.charAt(i) == '[') { |
|
638 |
++i; |
|
639 |
} |
|
640 |
break; |
|
641 |
case ';': |
|
642 |
continue; |
|
643 |
default: |
|
644 |
assert (0 <= JAVA_SIGNATURE_CHARS.indexOf(form.charAt(i))); |
|
645 |
break; |
|
2 | 646 |
} |
647 |
size++; |
|
648 |
} |
|
649 |
return size; |
|
650 |
} |
|
651 |
public boolean isMethod() { |
|
652 |
return formRef.stringValue().charAt(0) == '('; |
|
653 |
} |
|
654 |
public byte getLiteralTag() { |
|
655 |
switch (formRef.stringValue().charAt(0)) { |
|
656 |
case 'I': return CONSTANT_Integer; |
|
657 |
case 'J': return CONSTANT_Long; |
|
658 |
case 'F': return CONSTANT_Float; |
|
659 |
case 'D': return CONSTANT_Double; |
|
660 |
case 'B': case 'S': case 'C': case 'Z': |
|
661 |
return CONSTANT_Integer; |
|
12544 | 662 |
case 'L': |
663 |
/* |
|
664 |
switch (classRefs[0].stringValue()) { |
|
665 |
case "java/lang/String": |
|
666 |
return CONSTANT_String; |
|
667 |
case "java/lang/invoke/MethodHandle": |
|
668 |
return CONSTANT_MethodHandle; |
|
669 |
case "java/lang/invoke/MethodType": |
|
670 |
return CONSTANT_MethodType; |
|
671 |
default: // java/lang/Object, etc. |
|
672 |
return CONSTANT_LoadableValue; |
|
673 |
} |
|
674 |
*/ |
|
675 |
return CONSTANT_String; // JDK 7 ConstantValue limited to String |
|
2 | 676 |
} |
677 |
assert(false); |
|
678 |
return CONSTANT_None; |
|
679 |
} |
|
680 |
public String prettyString() { |
|
681 |
String s; |
|
682 |
if (isMethod()) { |
|
683 |
s = formRef.stringValue(); |
|
684 |
s = s.substring(0, 1+s.indexOf(')')); |
|
685 |
} else { |
|
686 |
s = "/" + formRef.stringValue(); |
|
687 |
} |
|
688 |
int i; |
|
6313 | 689 |
while ((i = s.indexOf(';')) >= 0) { |
690 |
s = s.substring(0, i) + s.substring(i + 1); |
|
691 |
} |
|
2 | 692 |
return s; |
693 |
} |
|
694 |
} |
|
695 |
||
696 |
static int compareSignatures(String s1, String s2) { |
|
697 |
return compareSignatures(s1, s2, null, null); |
|
698 |
} |
|
699 |
static int compareSignatures(String s1, String s2, String[] p1, String[] p2) { |
|
700 |
final int S1_COMES_FIRST = -1; |
|
701 |
final int S2_COMES_FIRST = +1; |
|
702 |
char c1 = s1.charAt(0); |
|
703 |
char c2 = s2.charAt(0); |
|
704 |
// fields before methods (because there are fewer of them) |
|
705 |
if (c1 != '(' && c2 == '(') return S1_COMES_FIRST; |
|
706 |
if (c2 != '(' && c1 == '(') return S2_COMES_FIRST; |
|
707 |
if (p1 == null) p1 = structureSignature(s1); |
|
708 |
if (p2 == null) p2 = structureSignature(s2); |
|
709 |
/* |
|
710 |
// non-classes before classes (because there are fewer of them) |
|
711 |
if (p1.length == 1 && p2.length > 1) return S1_COMES_FIRST; |
|
712 |
if (p2.length == 1 && p1.length > 1) return S2_COMES_FIRST; |
|
713 |
// all else being equal, use the same comparison as for Utf8 strings |
|
714 |
return s1.compareTo(s2); |
|
715 |
*/ |
|
716 |
if (p1.length != p2.length) return p1.length - p2.length; |
|
717 |
int length = p1.length; |
|
718 |
for (int i = length; --i >= 0; ) { |
|
719 |
int res = p1[i].compareTo(p2[i]); |
|
720 |
if (res != 0) return res; |
|
721 |
} |
|
722 |
assert(s1.equals(s2)); |
|
723 |
return 0; |
|
724 |
} |
|
725 |
||
726 |
static int countClassParts(Utf8Entry formRef) { |
|
727 |
int num = 0; |
|
728 |
String s = formRef.stringValue(); |
|
729 |
for (int i = 0; i < s.length(); i++) { |
|
730 |
if (s.charAt(i) == 'L') ++num; |
|
731 |
} |
|
732 |
return num; |
|
733 |
} |
|
734 |
||
735 |
static String flattenSignature(String[] parts) { |
|
736 |
String form = parts[0]; |
|
737 |
if (parts.length == 1) return form; |
|
738 |
int len = form.length(); |
|
739 |
for (int i = 1; i < parts.length; i++) { |
|
740 |
len += parts[i].length(); |
|
741 |
} |
|
742 |
char[] sig = new char[len]; |
|
743 |
int j = 0; |
|
744 |
int k = 1; |
|
745 |
for (int i = 0; i < form.length(); i++) { |
|
746 |
char ch = form.charAt(i); |
|
747 |
sig[j++] = ch; |
|
748 |
if (ch == 'L') { |
|
749 |
String cls = parts[k++]; |
|
750 |
cls.getChars(0, cls.length(), sig, j); |
|
751 |
j += cls.length(); |
|
752 |
//sig[j++] = ';'; |
|
753 |
} |
|
754 |
} |
|
755 |
assert(j == len); |
|
756 |
assert(k == parts.length); |
|
757 |
return new String(sig); |
|
758 |
} |
|
759 |
||
760 |
static private int skipClassNameChars(String sig, int i) { |
|
761 |
int len = sig.length(); |
|
762 |
for (; i < len; i++) { |
|
763 |
char ch = sig.charAt(i); |
|
764 |
if (ch <= ' ') break; |
|
765 |
if (ch >= ';' && ch <= '@') break; |
|
766 |
} |
|
767 |
return i; |
|
768 |
} |
|
769 |
||
770 |
static String[] structureSignature(String sig) { |
|
771 |
sig = sig.intern(); |
|
772 |
||
773 |
int formLen = 0; |
|
774 |
int nparts = 1; |
|
775 |
for (int i = 0; i < sig.length(); i++) { |
|
776 |
char ch = sig.charAt(i); |
|
777 |
formLen++; |
|
778 |
if (ch == 'L') { |
|
779 |
nparts++; |
|
780 |
int i2 = skipClassNameChars(sig, i+1); |
|
781 |
i = i2-1; // keep the semicolon in the form |
|
782 |
int i3 = sig.indexOf('<', i+1); |
|
783 |
if (i3 > 0 && i3 < i2) |
|
784 |
i = i3-1; |
|
785 |
} |
|
786 |
} |
|
787 |
char[] form = new char[formLen]; |
|
788 |
if (nparts == 1) { |
|
789 |
String[] parts = { sig }; |
|
790 |
return parts; |
|
791 |
} |
|
792 |
String[] parts = new String[nparts]; |
|
793 |
int j = 0; |
|
794 |
int k = 1; |
|
795 |
for (int i = 0; i < sig.length(); i++) { |
|
796 |
char ch = sig.charAt(i); |
|
797 |
form[j++] = ch; |
|
798 |
if (ch == 'L') { |
|
799 |
int i2 = skipClassNameChars(sig, i+1); |
|
800 |
parts[k++] = sig.substring(i+1, i2); |
|
801 |
i = i2; |
|
802 |
--i; // keep the semicolon in the form |
|
803 |
} |
|
804 |
} |
|
805 |
assert(j == formLen); |
|
806 |
assert(k == parts.length); |
|
807 |
parts[0] = new String(form); |
|
808 |
//assert(flattenSignature(parts).equals(sig)); |
|
809 |
return parts; |
|
810 |
} |
|
811 |
||
12544 | 812 |
/** @since JDK 7, JSR 292 */ |
813 |
public static |
|
814 |
class MethodHandleEntry extends Entry { |
|
815 |
final int refKind; |
|
816 |
final MemberEntry memRef; |
|
817 |
public Entry getRef(int i) { return i == 0 ? memRef : null; } |
|
818 |
||
819 |
protected int computeValueHash() { |
|
820 |
int hc2 = refKind; |
|
821 |
return (memRef.hashCode() + (hc2 << 8)) ^ hc2; |
|
822 |
} |
|
823 |
||
824 |
MethodHandleEntry(byte refKind, MemberEntry memRef) { |
|
825 |
super(CONSTANT_MethodHandle); |
|
826 |
assert(isRefKind(refKind)); |
|
827 |
this.refKind = refKind; |
|
828 |
this.memRef = memRef; |
|
829 |
hashCode(); // force computation of valueHash |
|
830 |
} |
|
831 |
public boolean equals(Object o) { |
|
832 |
if (o == null || o.getClass() != MethodHandleEntry.class) { |
|
833 |
return false; |
|
834 |
} |
|
835 |
MethodHandleEntry that = (MethodHandleEntry)o; |
|
836 |
return this.refKind == that.refKind |
|
837 |
&& this.memRef.eq(that.memRef); |
|
838 |
} |
|
839 |
public int compareTo(Object o) { |
|
840 |
int x = superCompareTo(o); |
|
841 |
if (x == 0) { |
|
842 |
MethodHandleEntry that = (MethodHandleEntry)o; |
|
843 |
if (Utils.SORT_HANDLES_KIND_MAJOR) |
|
844 |
// Primary key could be refKind. |
|
845 |
x = this.refKind - that.refKind; |
|
846 |
// Primary key is memRef, which is transmitted as UDELTA5. |
|
847 |
if (x == 0) |
|
848 |
x = this.memRef.compareTo(that.memRef); |
|
849 |
if (x == 0) |
|
850 |
x = this.refKind - that.refKind; |
|
851 |
} |
|
852 |
return x; |
|
853 |
} |
|
854 |
public static String stringValueOf(int refKind, MemberEntry memRef) { |
|
855 |
return refKindName(refKind)+":"+memRef.stringValue(); |
|
856 |
} |
|
857 |
public String stringValue() { |
|
858 |
return stringValueOf(refKind, memRef); |
|
859 |
} |
|
860 |
} |
|
861 |
||
862 |
/** @since JDK 7, JSR 292 */ |
|
863 |
public static |
|
864 |
class MethodTypeEntry extends Entry { |
|
865 |
final SignatureEntry typeRef; |
|
866 |
public Entry getRef(int i) { return i == 0 ? typeRef : null; } |
|
867 |
||
868 |
protected int computeValueHash() { |
|
869 |
return typeRef.hashCode() + tag; |
|
870 |
} |
|
871 |
||
872 |
MethodTypeEntry(SignatureEntry typeRef) { |
|
873 |
super(CONSTANT_MethodType); |
|
874 |
this.typeRef = typeRef; |
|
875 |
hashCode(); // force computation of valueHash |
|
876 |
} |
|
877 |
public boolean equals(Object o) { |
|
878 |
if (o == null || o.getClass() != MethodTypeEntry.class) { |
|
879 |
return false; |
|
880 |
} |
|
881 |
MethodTypeEntry that = (MethodTypeEntry)o; |
|
882 |
return this.typeRef.eq(that.typeRef); |
|
883 |
} |
|
884 |
public int compareTo(Object o) { |
|
885 |
int x = superCompareTo(o); |
|
886 |
if (x == 0) { |
|
887 |
MethodTypeEntry that = (MethodTypeEntry)o; |
|
888 |
x = this.typeRef.compareTo(that.typeRef); |
|
889 |
} |
|
890 |
return x; |
|
891 |
} |
|
892 |
public String stringValue() { |
|
893 |
return typeRef.stringValue(); |
|
894 |
} |
|
895 |
} |
|
896 |
||
897 |
/** @since JDK 7, JSR 292 */ |
|
898 |
public static |
|
899 |
class InvokeDynamicEntry extends Entry { |
|
900 |
final BootstrapMethodEntry bssRef; |
|
901 |
final DescriptorEntry descRef; |
|
902 |
public Entry getRef(int i) { |
|
903 |
if (i == 0) return bssRef; |
|
904 |
if (i == 1) return descRef; |
|
905 |
return null; |
|
906 |
} |
|
907 |
protected int computeValueHash() { |
|
908 |
int hc2 = descRef.hashCode(); |
|
909 |
return (bssRef.hashCode() + (hc2 << 8)) ^ hc2; |
|
910 |
} |
|
911 |
||
912 |
InvokeDynamicEntry(BootstrapMethodEntry bssRef, DescriptorEntry descRef) { |
|
913 |
super(CONSTANT_InvokeDynamic); |
|
914 |
this.bssRef = bssRef; |
|
915 |
this.descRef = descRef; |
|
916 |
hashCode(); // force computation of valueHash |
|
917 |
} |
|
918 |
public boolean equals(Object o) { |
|
919 |
if (o == null || o.getClass() != InvokeDynamicEntry.class) { |
|
920 |
return false; |
|
921 |
} |
|
922 |
InvokeDynamicEntry that = (InvokeDynamicEntry)o; |
|
923 |
return this.bssRef.eq(that.bssRef) |
|
924 |
&& this.descRef.eq(that.descRef); |
|
925 |
} |
|
926 |
public int compareTo(Object o) { |
|
927 |
int x = superCompareTo(o); |
|
928 |
if (x == 0) { |
|
929 |
InvokeDynamicEntry that = (InvokeDynamicEntry)o; |
|
930 |
if (Utils.SORT_INDY_BSS_MAJOR) |
|
931 |
// Primary key could be bsmRef. |
|
932 |
x = this.bssRef.compareTo(that.bssRef); |
|
933 |
// Primary key is descriptor, which is transmitted as UDELTA5. |
|
934 |
if (x == 0) |
|
935 |
x = this.descRef.compareTo(that.descRef); |
|
936 |
if (x == 0) |
|
937 |
x = this.bssRef.compareTo(that.bssRef); |
|
938 |
} |
|
939 |
return x; |
|
940 |
} |
|
941 |
public String stringValue() { |
|
942 |
return stringValueOf(bssRef, descRef); |
|
943 |
} |
|
944 |
static |
|
945 |
String stringValueOf(BootstrapMethodEntry bssRef, DescriptorEntry descRef) { |
|
946 |
return "Indy:"+bssRef.stringValue()+"."+descRef.stringValue(); |
|
947 |
} |
|
948 |
} |
|
949 |
||
950 |
/** @since JDK 7, JSR 292 */ |
|
951 |
public static |
|
952 |
class BootstrapMethodEntry extends Entry { |
|
953 |
final MethodHandleEntry bsmRef; |
|
954 |
final Entry[] argRefs; |
|
955 |
public Entry getRef(int i) { |
|
956 |
if (i == 0) return bsmRef; |
|
957 |
if (i-1 < argRefs.length) return argRefs[i-1]; |
|
958 |
return null; |
|
959 |
} |
|
960 |
protected int computeValueHash() { |
|
961 |
int hc2 = bsmRef.hashCode(); |
|
962 |
return (Arrays.hashCode(argRefs) + (hc2 << 8)) ^ hc2; |
|
963 |
} |
|
964 |
||
965 |
BootstrapMethodEntry(MethodHandleEntry bsmRef, Entry[] argRefs) { |
|
966 |
super(CONSTANT_BootstrapMethod); |
|
967 |
this.bsmRef = bsmRef; |
|
968 |
this.argRefs = argRefs.clone(); |
|
969 |
hashCode(); // force computation of valueHash |
|
970 |
} |
|
971 |
public boolean equals(Object o) { |
|
972 |
if (o == null || o.getClass() != BootstrapMethodEntry.class) { |
|
973 |
return false; |
|
974 |
} |
|
975 |
BootstrapMethodEntry that = (BootstrapMethodEntry)o; |
|
976 |
return this.bsmRef.eq(that.bsmRef) |
|
977 |
&& Arrays.equals(this.argRefs, that.argRefs); |
|
978 |
} |
|
979 |
public int compareTo(Object o) { |
|
980 |
int x = superCompareTo(o); |
|
981 |
if (x == 0) { |
|
982 |
BootstrapMethodEntry that = (BootstrapMethodEntry)o; |
|
983 |
if (Utils.SORT_BSS_BSM_MAJOR) |
|
984 |
// Primary key is bsmRef. |
|
985 |
x = this.bsmRef.compareTo(that.bsmRef); |
|
986 |
// Primary key is args array length, which is transmitted as UDELTA5. |
|
987 |
if (x == 0) |
|
988 |
x = compareArgArrays(this.argRefs, that.argRefs); |
|
989 |
if (x == 0) |
|
990 |
x = this.bsmRef.compareTo(that.bsmRef); |
|
991 |
} |
|
992 |
return x; |
|
993 |
} |
|
994 |
public String stringValue() { |
|
995 |
return stringValueOf(bsmRef, argRefs); |
|
996 |
} |
|
997 |
static |
|
998 |
String stringValueOf(MethodHandleEntry bsmRef, Entry[] argRefs) { |
|
999 |
StringBuffer sb = new StringBuffer(bsmRef.stringValue()); |
|
1000 |
// Arguments are formatted as "<foo;bar;baz>" instead of "[foo,bar,baz]". |
|
1001 |
// This ensures there will be no confusion if "[,]" appear inside of names. |
|
1002 |
char nextSep = '<'; |
|
1003 |
boolean didOne = false; |
|
1004 |
for (Entry argRef : argRefs) { |
|
1005 |
sb.append(nextSep).append(argRef.stringValue()); |
|
1006 |
nextSep = ';'; |
|
1007 |
} |
|
1008 |
if (nextSep == '<') sb.append(nextSep); |
|
1009 |
sb.append('>'); |
|
1010 |
return sb.toString(); |
|
1011 |
} |
|
1012 |
static |
|
1013 |
int compareArgArrays(Entry[] a1, Entry[] a2) { |
|
1014 |
int x = a1.length - a2.length; |
|
1015 |
if (x != 0) return x; |
|
1016 |
for (int i = 0; i < a1.length; i++) { |
|
1017 |
x = a1[i].compareTo(a2[i]); |
|
1018 |
if (x != 0) break; |
|
1019 |
} |
|
1020 |
return x; |
|
1021 |
} |
|
1022 |
} |
|
1023 |
||
2 | 1024 |
// Handy constants: |
1025 |
protected static final Entry[] noRefs = {}; |
|
1026 |
protected static final ClassEntry[] noClassRefs = {}; |
|
1027 |
||
1028 |
/** An Index is a mapping between CP entries and small integers. */ |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1029 |
public static final |
10115 | 1030 |
class Index extends AbstractList<Entry> { |
2 | 1031 |
protected String debugName; |
1032 |
protected Entry[] cpMap; |
|
1033 |
protected boolean flattenSigs; |
|
1034 |
protected Entry[] getMap() { |
|
1035 |
return cpMap; |
|
1036 |
} |
|
1037 |
protected Index(String debugName) { |
|
1038 |
this.debugName = debugName; |
|
1039 |
} |
|
1040 |
protected Index(String debugName, Entry[] cpMap) { |
|
1041 |
this(debugName); |
|
1042 |
setMap(cpMap); |
|
1043 |
} |
|
1044 |
protected void setMap(Entry[] cpMap) { |
|
1045 |
clearIndex(); |
|
1046 |
this.cpMap = cpMap; |
|
1047 |
} |
|
6313 | 1048 |
protected Index(String debugName, Collection<Entry> cpMapList) { |
2 | 1049 |
this(debugName); |
1050 |
setMap(cpMapList); |
|
1051 |
} |
|
6313 | 1052 |
protected void setMap(Collection<Entry> cpMapList) { |
2 | 1053 |
cpMap = new Entry[cpMapList.size()]; |
1054 |
cpMapList.toArray(cpMap); |
|
1055 |
setMap(cpMap); |
|
1056 |
} |
|
1057 |
public int size() { |
|
1058 |
return cpMap.length; |
|
1059 |
} |
|
10115 | 1060 |
public Entry get(int i) { |
2 | 1061 |
return cpMap[i]; |
1062 |
} |
|
1063 |
public Entry getEntry(int i) { |
|
1064 |
// same as get(), with covariant return type |
|
1065 |
return cpMap[i]; |
|
1066 |
} |
|
1067 |
||
1068 |
// Find index of e in cpMap, or return -1 if none. |
|
1069 |
// |
|
1070 |
// As a special hack, if flattenSigs, signatures are |
|
1071 |
// treated as equivalent entries of cpMap. This is wrong |
|
6313 | 1072 |
// from a Collection point of view, because contains() |
2 | 1073 |
// reports true for signatures, but the iterator() |
1074 |
// never produces them! |
|
1075 |
private int findIndexOf(Entry e) { |
|
6313 | 1076 |
if (indexKey == null) { |
1077 |
initializeIndex(); |
|
1078 |
} |
|
2 | 1079 |
int probe = findIndexLocation(e); |
1080 |
if (indexKey[probe] != e) { |
|
1081 |
if (flattenSigs && e.tag == CONSTANT_Signature) { |
|
1082 |
SignatureEntry se = (SignatureEntry) e; |
|
1083 |
return findIndexOf(se.asUtf8Entry()); |
|
1084 |
} |
|
1085 |
return -1; |
|
1086 |
} |
|
1087 |
int index = indexValue[probe]; |
|
1088 |
assert(e.equals(cpMap[index])); |
|
1089 |
return index; |
|
1090 |
} |
|
1091 |
public boolean contains(Entry e) { |
|
1092 |
return findIndexOf(e) >= 0; |
|
1093 |
} |
|
1094 |
// Find index of e in cpMap. Should not return -1. |
|
1095 |
public int indexOf(Entry e) { |
|
1096 |
int index = findIndexOf(e); |
|
1097 |
if (index < 0 && verbose() > 0) { |
|
1098 |
System.out.println("not found: "+e); |
|
1099 |
System.out.println(" in: "+this.dumpString()); |
|
1100 |
Thread.dumpStack(); |
|
1101 |
} |
|
1102 |
assert(index >= 0); |
|
1103 |
return index; |
|
1104 |
} |
|
10115 | 1105 |
public int lastIndexOf(Entry e) { |
2 | 1106 |
return indexOf(e); |
1107 |
} |
|
1108 |
||
1109 |
public boolean assertIsSorted() { |
|
1110 |
for (int i = 1; i < cpMap.length; i++) { |
|
1111 |
if (cpMap[i-1].compareTo(cpMap[i]) > 0) { |
|
1112 |
System.out.println("Not sorted at "+(i-1)+"/"+i+": "+this.dumpString()); |
|
1113 |
return false; |
|
1114 |
} |
|
1115 |
} |
|
1116 |
return true; |
|
1117 |
} |
|
1118 |
||
1119 |
// internal hash table |
|
1120 |
protected Entry[] indexKey; |
|
1121 |
protected int[] indexValue; |
|
1122 |
protected void clearIndex() { |
|
1123 |
indexKey = null; |
|
1124 |
indexValue = null; |
|
1125 |
} |
|
1126 |
private int findIndexLocation(Entry e) { |
|
1127 |
int size = indexKey.length; |
|
1128 |
int hash = e.hashCode(); |
|
1129 |
int probe = hash & (size - 1); |
|
1130 |
int stride = ((hash >>> 8) | 1) & (size - 1); |
|
1131 |
for (;;) { |
|
1132 |
Entry e1 = indexKey[probe]; |
|
1133 |
if (e1 == e || e1 == null) |
|
1134 |
return probe; |
|
1135 |
probe += stride; |
|
1136 |
if (probe >= size) probe -= size; |
|
1137 |
} |
|
1138 |
} |
|
1139 |
private void initializeIndex() { |
|
1140 |
if (verbose() > 2) |
|
1141 |
System.out.println("initialize Index "+debugName+" ["+size()+"]"); |
|
1142 |
int hsize0 = (int)((cpMap.length + 10) * 1.5); |
|
1143 |
int hsize = 1; |
|
6313 | 1144 |
while (hsize < hsize0) { |
1145 |
hsize <<= 1; |
|
1146 |
} |
|
2 | 1147 |
indexKey = new Entry[hsize]; |
1148 |
indexValue = new int[hsize]; |
|
1149 |
for (int i = 0; i < cpMap.length; i++) { |
|
1150 |
Entry e = cpMap[i]; |
|
1151 |
if (e == null) continue; |
|
1152 |
int probe = findIndexLocation(e); |
|
1153 |
assert(indexKey[probe] == null); // e has unique index |
|
1154 |
indexKey[probe] = e; |
|
1155 |
indexValue[probe] = i; |
|
1156 |
} |
|
1157 |
} |
|
10115 | 1158 |
public Entry[] toArray(Entry[] a) { |
2 | 1159 |
int sz = size(); |
1160 |
if (a.length < sz) return super.toArray(a); |
|
1161 |
System.arraycopy(cpMap, 0, a, 0, sz); |
|
1162 |
if (a.length > sz) a[sz] = null; |
|
1163 |
return a; |
|
1164 |
} |
|
10115 | 1165 |
public Entry[] toArray() { |
2 | 1166 |
return toArray(new Entry[size()]); |
1167 |
} |
|
1168 |
public Object clone() { |
|
6313 | 1169 |
return new Index(debugName, cpMap.clone()); |
2 | 1170 |
} |
1171 |
public String toString() { |
|
1172 |
return "Index "+debugName+" ["+size()+"]"; |
|
1173 |
} |
|
1174 |
public String dumpString() { |
|
1175 |
String s = toString(); |
|
1176 |
s += " {\n"; |
|
1177 |
for (int i = 0; i < cpMap.length; i++) { |
|
1178 |
s += " "+i+": "+cpMap[i]+"\n"; |
|
1179 |
} |
|
1180 |
s += "}"; |
|
1181 |
return s; |
|
1182 |
} |
|
1183 |
} |
|
1184 |
||
1185 |
// Index methods. |
|
1186 |
||
1187 |
public static |
|
1188 |
Index makeIndex(String debugName, Entry[] cpMap) { |
|
1189 |
return new Index(debugName, cpMap); |
|
1190 |
} |
|
1191 |
||
1192 |
public static |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1193 |
Index makeIndex(String debugName, Collection<Entry> cpMapList) { |
2 | 1194 |
return new Index(debugName, cpMapList); |
1195 |
} |
|
1196 |
||
1197 |
/** Sort this index (destructively) into canonical order. */ |
|
1198 |
public static |
|
1199 |
void sort(Index ix) { |
|
1200 |
// %%% Should move this into class Index. |
|
1201 |
ix.clearIndex(); |
|
1202 |
Arrays.sort(ix.cpMap); |
|
1203 |
if (verbose() > 2) |
|
1204 |
System.out.println("sorted "+ix.dumpString()); |
|
1205 |
} |
|
1206 |
||
1207 |
/** Return a set of indexes partitioning these entries. |
|
1208 |
* The keys array must of length this.size(), and marks entries. |
|
1209 |
* The result array is as long as one plus the largest key value. |
|
1210 |
* Entries with a negative key are dropped from the partition. |
|
1211 |
*/ |
|
1212 |
public static |
|
1213 |
Index[] partition(Index ix, int[] keys) { |
|
1214 |
// %%% Should move this into class Index. |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1215 |
List<List<Entry>> parts = new ArrayList<>(); |
2 | 1216 |
Entry[] cpMap = ix.cpMap; |
1217 |
assert(keys.length == cpMap.length); |
|
1218 |
for (int i = 0; i < keys.length; i++) { |
|
1219 |
int key = keys[i]; |
|
1220 |
if (key < 0) continue; |
|
6313 | 1221 |
while (key >= parts.size()) { |
1222 |
parts.add(null); |
|
1223 |
} |
|
1224 |
List<Entry> part = parts.get(key); |
|
2 | 1225 |
if (part == null) { |
6313 | 1226 |
parts.set(key, part = new ArrayList<>()); |
2 | 1227 |
} |
1228 |
part.add(cpMap[i]); |
|
1229 |
} |
|
1230 |
Index[] indexes = new Index[parts.size()]; |
|
1231 |
for (int key = 0; key < indexes.length; key++) { |
|
6313 | 1232 |
List<Entry> part = parts.get(key); |
2 | 1233 |
if (part == null) continue; |
1234 |
indexes[key] = new Index(ix.debugName+"/part#"+key, part); |
|
1235 |
assert(indexes[key].indexOf(part.get(0)) == 0); |
|
1236 |
} |
|
1237 |
return indexes; |
|
1238 |
} |
|
1239 |
public static |
|
1240 |
Index[] partitionByTag(Index ix) { |
|
1241 |
// Partition by tag. |
|
1242 |
Entry[] cpMap = ix.cpMap; |
|
1243 |
int[] keys = new int[cpMap.length]; |
|
1244 |
for (int i = 0; i < keys.length; i++) { |
|
1245 |
Entry e = cpMap[i]; |
|
1246 |
keys[i] = (e == null)? -1: e.tag; |
|
1247 |
} |
|
1248 |
Index[] byTag = partition(ix, keys); |
|
1249 |
for (int tag = 0; tag < byTag.length; tag++) { |
|
1250 |
if (byTag[tag] == null) continue; |
|
1251 |
byTag[tag].debugName = tagName(tag); |
|
1252 |
} |
|
1253 |
if (byTag.length < CONSTANT_Limit) { |
|
1254 |
Index[] longer = new Index[CONSTANT_Limit]; |
|
1255 |
System.arraycopy(byTag, 0, longer, 0, byTag.length); |
|
1256 |
byTag = longer; |
|
1257 |
} |
|
1258 |
return byTag; |
|
1259 |
} |
|
1260 |
||
1261 |
/** Coherent group of constant pool indexes. */ |
|
1262 |
public static |
|
1263 |
class IndexGroup { |
|
1264 |
private Index[] indexByTag = new Index[CONSTANT_Limit]; |
|
12544 | 1265 |
private Index[] indexByTagGroup; |
2 | 1266 |
private int[] untypedFirstIndexByTag; |
12544 | 1267 |
private int totalSizeQQ; |
2 | 1268 |
private Index[][] indexByTagAndClass; |
1269 |
||
1270 |
/** Index of all CP entries of all types, in definition order. */ |
|
12544 | 1271 |
private Index makeTagGroupIndex(byte tagGroupTag, byte[] tagsInGroup) { |
1272 |
if (indexByTagGroup == null) |
|
1273 |
indexByTagGroup = new Index[CONSTANT_GroupLimit - CONSTANT_GroupFirst]; |
|
1274 |
int which = tagGroupTag - CONSTANT_GroupFirst; |
|
1275 |
assert(indexByTagGroup[which] == null); |
|
1276 |
int fillp = 0; |
|
1277 |
Entry[] cpMap = null; |
|
1278 |
for (int pass = 1; pass <= 2; pass++) { |
|
2 | 1279 |
untypedIndexOf(null); // warm up untypedFirstIndexByTag |
12544 | 1280 |
for (byte tag : tagsInGroup) { |
2 | 1281 |
Index ix = indexByTag[tag]; |
1282 |
if (ix == null) continue; |
|
1283 |
int ixLen = ix.cpMap.length; |
|
1284 |
if (ixLen == 0) continue; |
|
12544 | 1285 |
assert(tagGroupTag == CONSTANT_All |
1286 |
? fillp == untypedFirstIndexByTag[tag] |
|
1287 |
: fillp < untypedFirstIndexByTag[tag]); |
|
1288 |
if (cpMap != null) { |
|
1289 |
assert(cpMap[fillp] == null); |
|
1290 |
assert(cpMap[fillp+ixLen-1] == null); |
|
1291 |
System.arraycopy(ix.cpMap, 0, cpMap, fillp, ixLen); |
|
1292 |
} |
|
1293 |
fillp += ixLen; |
|
2 | 1294 |
} |
12544 | 1295 |
if (cpMap == null) { |
1296 |
assert(pass == 1); |
|
1297 |
// get ready for pass 2 |
|
1298 |
cpMap = new Entry[fillp]; |
|
1299 |
fillp = 0; |
|
1300 |
} |
|
2 | 1301 |
} |
12544 | 1302 |
indexByTagGroup[which] = new Index(tagName(tagGroupTag), cpMap); |
1303 |
return indexByTagGroup[which]; |
|
2 | 1304 |
} |
1305 |
||
1306 |
public int untypedIndexOf(Entry e) { |
|
1307 |
if (untypedFirstIndexByTag == null) { |
|
12544 | 1308 |
untypedFirstIndexByTag = new int[CONSTANT_Limit+1]; |
2 | 1309 |
int fillp = 0; |
1310 |
for (int i = 0; i < TAGS_IN_ORDER.length; i++) { |
|
1311 |
byte tag = TAGS_IN_ORDER[i]; |
|
1312 |
Index ix = indexByTag[tag]; |
|
1313 |
if (ix == null) continue; |
|
1314 |
int ixLen = ix.cpMap.length; |
|
1315 |
untypedFirstIndexByTag[tag] = fillp; |
|
1316 |
fillp += ixLen; |
|
1317 |
} |
|
12544 | 1318 |
untypedFirstIndexByTag[CONSTANT_Limit] = fillp; |
2 | 1319 |
} |
1320 |
if (e == null) return -1; |
|
1321 |
int tag = e.tag; |
|
1322 |
Index ix = indexByTag[tag]; |
|
1323 |
if (ix == null) return -1; |
|
1324 |
int idx = ix.findIndexOf(e); |
|
1325 |
if (idx >= 0) |
|
1326 |
idx += untypedFirstIndexByTag[tag]; |
|
1327 |
return idx; |
|
1328 |
} |
|
1329 |
||
1330 |
public void initIndexByTag(byte tag, Index ix) { |
|
1331 |
assert(indexByTag[tag] == null); // do not init twice |
|
1332 |
Entry[] cpMap = ix.cpMap; |
|
1333 |
for (int i = 0; i < cpMap.length; i++) { |
|
1334 |
// It must be a homogeneous Entry set. |
|
1335 |
assert(cpMap[i].tag == tag); |
|
1336 |
} |
|
1337 |
if (tag == CONSTANT_Utf8) { |
|
1338 |
// Special case: First Utf8 must always be empty string. |
|
1339 |
assert(cpMap.length == 0 || cpMap[0].stringValue().equals("")); |
|
1340 |
} |
|
1341 |
indexByTag[tag] = ix; |
|
1342 |
// decache indexes derived from this one: |
|
1343 |
untypedFirstIndexByTag = null; |
|
12544 | 1344 |
indexByTagGroup = null; |
2 | 1345 |
if (indexByTagAndClass != null) |
1346 |
indexByTagAndClass[tag] = null; |
|
1347 |
} |
|
1348 |
||
1349 |
/** Index of all CP entries of a given tag. */ |
|
1350 |
public Index getIndexByTag(byte tag) { |
|
12544 | 1351 |
if (tag >= CONSTANT_GroupFirst) |
1352 |
return getIndexByTagGroup(tag); |
|
2 | 1353 |
Index ix = indexByTag[tag]; |
1354 |
if (ix == null) { |
|
1355 |
// Make an empty one by default. |
|
1356 |
ix = new Index(tagName(tag), new Entry[0]); |
|
1357 |
indexByTag[tag] = ix; |
|
1358 |
} |
|
1359 |
return ix; |
|
1360 |
} |
|
1361 |
||
12544 | 1362 |
private Index getIndexByTagGroup(byte tag) { |
1363 |
// pool groups: |
|
1364 |
if (indexByTagGroup != null) { |
|
1365 |
Index ix = indexByTagGroup[tag - CONSTANT_GroupFirst]; |
|
1366 |
if (ix != null) return ix; |
|
1367 |
} |
|
1368 |
switch (tag) { |
|
1369 |
case CONSTANT_All: |
|
1370 |
return makeTagGroupIndex(CONSTANT_All, TAGS_IN_ORDER); |
|
1371 |
case CONSTANT_LoadableValue: |
|
1372 |
return makeTagGroupIndex(CONSTANT_LoadableValue, LOADABLE_VALUE_TAGS); |
|
1373 |
case CONSTANT_AnyMember: |
|
1374 |
return makeTagGroupIndex(CONSTANT_AnyMember, ANY_MEMBER_TAGS); |
|
1375 |
case CONSTANT_FieldSpecific: |
|
1376 |
// This one does not have any fixed index, since it is context-specific. |
|
1377 |
return null; |
|
1378 |
} |
|
1379 |
throw new AssertionError("bad tag group "+tag); |
|
1380 |
} |
|
1381 |
||
2 | 1382 |
/** Index of all CP entries of a given tag and class. */ |
1383 |
public Index getMemberIndex(byte tag, ClassEntry classRef) { |
|
1384 |
if (indexByTagAndClass == null) |
|
1385 |
indexByTagAndClass = new Index[CONSTANT_Limit][]; |
|
1386 |
Index allClasses = getIndexByTag(CONSTANT_Class); |
|
1387 |
Index[] perClassIndexes = indexByTagAndClass[tag]; |
|
1388 |
if (perClassIndexes == null) { |
|
1389 |
// Create the partition now. |
|
1390 |
// Divide up all entries of the given tag according to their class. |
|
1391 |
Index allMembers = getIndexByTag(tag); |
|
1392 |
int[] whichClasses = new int[allMembers.size()]; |
|
1393 |
for (int i = 0; i < whichClasses.length; i++) { |
|
1394 |
MemberEntry e = (MemberEntry) allMembers.get(i); |
|
1395 |
int whichClass = allClasses.indexOf(e.classRef); |
|
1396 |
whichClasses[i] = whichClass; |
|
1397 |
} |
|
1398 |
perClassIndexes = partition(allMembers, whichClasses); |
|
6313 | 1399 |
for (int i = 0; i < perClassIndexes.length; i++) { |
1400 |
assert (perClassIndexes[i] == null || |
|
1401 |
perClassIndexes[i].assertIsSorted()); |
|
1402 |
} |
|
2 | 1403 |
indexByTagAndClass[tag] = perClassIndexes; |
1404 |
} |
|
1405 |
int whichClass = allClasses.indexOf(classRef); |
|
1406 |
return perClassIndexes[whichClass]; |
|
1407 |
} |
|
1408 |
||
1409 |
// Given the sequence of all methods of the given name and class, |
|
1410 |
// produce the ordinal of this particular given overloading. |
|
1411 |
public int getOverloadingIndex(MemberEntry methodRef) { |
|
1412 |
Index ix = getMemberIndex(methodRef.tag, methodRef.classRef); |
|
1413 |
Utf8Entry nameRef = methodRef.descRef.nameRef; |
|
1414 |
int ord = 0; |
|
1415 |
for (int i = 0; i < ix.cpMap.length; i++) { |
|
1416 |
MemberEntry e = (MemberEntry) ix.cpMap[i]; |
|
1417 |
if (e.equals(methodRef)) |
|
1418 |
return ord; |
|
1419 |
if (e.descRef.nameRef.equals(nameRef)) |
|
1420 |
// Found a different overloading. Increment the ordinal. |
|
1421 |
ord++; |
|
1422 |
} |
|
1423 |
throw new RuntimeException("should not reach here"); |
|
1424 |
} |
|
1425 |
||
1426 |
// Inverse of getOverloadingIndex |
|
1427 |
public MemberEntry getOverloadingForIndex(byte tag, ClassEntry classRef, String name, int which) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1428 |
assert(name.equals(name.intern())); |
2 | 1429 |
Index ix = getMemberIndex(tag, classRef); |
1430 |
int ord = 0; |
|
1431 |
for (int i = 0; i < ix.cpMap.length; i++) { |
|
1432 |
MemberEntry e = (MemberEntry) ix.cpMap[i]; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1433 |
if (e.descRef.nameRef.stringValue().equals(name)) { |
2 | 1434 |
if (ord == which) return e; |
1435 |
ord++; |
|
1436 |
} |
|
1437 |
} |
|
1438 |
throw new RuntimeException("should not reach here"); |
|
1439 |
} |
|
1440 |
||
1441 |
public boolean haveNumbers() { |
|
12544 | 1442 |
for (byte tag : NUMBER_TAGS) { |
1443 |
if (getIndexByTag(tag).size() > 0) return true; |
|
1444 |
} |
|
1445 |
return false; |
|
1446 |
} |
|
1447 |
||
1448 |
public boolean haveExtraTags() { |
|
1449 |
for (byte tag : EXTRA_TAGS) { |
|
2 | 1450 |
if (getIndexByTag(tag).size() > 0) return true; |
1451 |
} |
|
1452 |
return false; |
|
1453 |
} |
|
1454 |
||
1455 |
} |
|
1456 |
||
1457 |
/** Close the set cpRefs under the getRef(*) relation. |
|
1458 |
* Also, if flattenSigs, replace all signatures in cpRefs |
|
1459 |
* by their equivalent Utf8s. |
|
1460 |
* Also, discard null from cpRefs. |
|
1461 |
*/ |
|
12544 | 1462 |
public static void completeReferencesIn(Set<Entry> cpRefs, boolean flattenSigs) { |
1463 |
completeReferencesIn(cpRefs, flattenSigs, null); |
|
1464 |
} |
|
1465 |
||
2 | 1466 |
public static |
12544 | 1467 |
void completeReferencesIn(Set<Entry> cpRefs, boolean flattenSigs, |
1468 |
List<BootstrapMethodEntry>bsms) { |
|
2 | 1469 |
cpRefs.remove(null); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1470 |
for (ListIterator<Entry> work = |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7795
diff
changeset
|
1471 |
new ArrayList<>(cpRefs).listIterator(cpRefs.size()); |
2 | 1472 |
work.hasPrevious(); ) { |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1473 |
Entry e = work.previous(); |
2 | 1474 |
work.remove(); // pop stack |
1475 |
assert(e != null); |
|
1476 |
if (flattenSigs && e.tag == CONSTANT_Signature) { |
|
1477 |
SignatureEntry se = (SignatureEntry) e; |
|
1478 |
Utf8Entry ue = se.asUtf8Entry(); |
|
1479 |
// Totally replace e by se. |
|
1480 |
cpRefs.remove(se); |
|
1481 |
cpRefs.add(ue); |
|
1482 |
e = ue; // do not descend into the sig |
|
1483 |
} |
|
12544 | 1484 |
if (bsms != null && e.tag == CONSTANT_BootstrapMethod) { |
1485 |
BootstrapMethodEntry bsm = (BootstrapMethodEntry)e; |
|
1486 |
cpRefs.remove(bsm); |
|
1487 |
// move it away to the side table where it belongs |
|
1488 |
if (!bsms.contains(bsm)) |
|
1489 |
bsms.add(bsm); |
|
1490 |
// fall through to recursively add refs for this entry |
|
1491 |
} |
|
2 | 1492 |
// Recursively add the refs of e to cpRefs: |
1493 |
for (int i = 0; ; i++) { |
|
1494 |
Entry re = e.getRef(i); |
|
1495 |
if (re == null) |
|
1496 |
break; // no more refs in e |
|
1497 |
if (cpRefs.add(re)) // output the ref |
|
1498 |
work.add(re); // push stack, if a new ref |
|
1499 |
} |
|
1500 |
} |
|
1501 |
} |
|
1502 |
||
1503 |
static double percent(int num, int den) { |
|
1504 |
return (int)((10000.0*num)/den + 0.5) / 100.0; |
|
1505 |
} |
|
1506 |
||
1507 |
public static String tagName(int tag) { |
|
1508 |
switch (tag) { |
|
1509 |
case CONSTANT_Utf8: return "Utf8"; |
|
1510 |
case CONSTANT_Integer: return "Integer"; |
|
1511 |
case CONSTANT_Float: return "Float"; |
|
1512 |
case CONSTANT_Long: return "Long"; |
|
1513 |
case CONSTANT_Double: return "Double"; |
|
1514 |
case CONSTANT_Class: return "Class"; |
|
1515 |
case CONSTANT_String: return "String"; |
|
1516 |
case CONSTANT_Fieldref: return "Fieldref"; |
|
1517 |
case CONSTANT_Methodref: return "Methodref"; |
|
1518 |
case CONSTANT_InterfaceMethodref: return "InterfaceMethodref"; |
|
1519 |
case CONSTANT_NameandType: return "NameandType"; |
|
12544 | 1520 |
case CONSTANT_MethodHandle: return "MethodHandle"; |
1521 |
case CONSTANT_MethodType: return "MethodType"; |
|
1522 |
case CONSTANT_InvokeDynamic: return "InvokeDynamic"; |
|
2 | 1523 |
|
1524 |
// pseudo-tags: |
|
12544 | 1525 |
case CONSTANT_All: return "**All"; |
1526 |
case CONSTANT_None: return "**None"; |
|
1527 |
case CONSTANT_LoadableValue: return "**LoadableValue"; |
|
1528 |
case CONSTANT_AnyMember: return "**AnyMember"; |
|
1529 |
case CONSTANT_FieldSpecific: return "*FieldSpecific"; |
|
2 | 1530 |
case CONSTANT_Signature: return "*Signature"; |
12544 | 1531 |
case CONSTANT_BootstrapMethod: return "*BootstrapMethod"; |
2 | 1532 |
} |
1533 |
return "tag#"+tag; |
|
1534 |
} |
|
1535 |
||
12544 | 1536 |
public static String refKindName(int refKind) { |
1537 |
switch (refKind) { |
|
1538 |
case REF_getField: return "getField"; |
|
1539 |
case REF_getStatic: return "getStatic"; |
|
1540 |
case REF_putField: return "putField"; |
|
1541 |
case REF_putStatic: return "putStatic"; |
|
1542 |
case REF_invokeVirtual: return "invokeVirtual"; |
|
1543 |
case REF_invokeStatic: return "invokeStatic"; |
|
1544 |
case REF_invokeSpecial: return "invokeSpecial"; |
|
1545 |
case REF_newInvokeSpecial: return "newInvokeSpecial"; |
|
1546 |
case REF_invokeInterface: return "invokeInterface"; |
|
1547 |
} |
|
1548 |
return "refKind#"+refKind; |
|
1549 |
} |
|
1550 |
||
2 | 1551 |
// archive constant pool definition order |
1552 |
static final byte TAGS_IN_ORDER[] = { |
|
1553 |
CONSTANT_Utf8, |
|
1554 |
CONSTANT_Integer, // cp_Int |
|
1555 |
CONSTANT_Float, |
|
1556 |
CONSTANT_Long, |
|
1557 |
CONSTANT_Double, |
|
12544 | 1558 |
CONSTANT_String, // note that String=8 precedes Class=7 |
2 | 1559 |
CONSTANT_Class, |
1560 |
CONSTANT_Signature, |
|
1561 |
CONSTANT_NameandType, // cp_Descr |
|
1562 |
CONSTANT_Fieldref, // cp_Field |
|
1563 |
CONSTANT_Methodref, // cp_Method |
|
12544 | 1564 |
CONSTANT_InterfaceMethodref, // cp_Imethod |
1565 |
||
1566 |
// Constants defined in JDK 7 and later: |
|
1567 |
CONSTANT_MethodHandle, |
|
1568 |
CONSTANT_MethodType, |
|
1569 |
CONSTANT_BootstrapMethod, // pseudo-tag, really stored in a class attribute |
|
1570 |
CONSTANT_InvokeDynamic |
|
2 | 1571 |
}; |
1572 |
static final byte TAG_ORDER[]; |
|
1573 |
static { |
|
1574 |
TAG_ORDER = new byte[CONSTANT_Limit]; |
|
1575 |
for (int i = 0; i < TAGS_IN_ORDER.length; i++) { |
|
1576 |
TAG_ORDER[TAGS_IN_ORDER[i]] = (byte)(i+1); |
|
1577 |
} |
|
1578 |
/* |
|
1579 |
System.out.println("TAG_ORDER[] = {"); |
|
1580 |
for (int i = 0; i < TAG_ORDER.length; i++) |
|
1581 |
System.out.println(" "+TAG_ORDER[i]+","); |
|
1582 |
System.out.println("};"); |
|
1583 |
*/ |
|
1584 |
} |
|
12544 | 1585 |
static final byte[] NUMBER_TAGS = { |
1586 |
CONSTANT_Integer, CONSTANT_Float, CONSTANT_Long, CONSTANT_Double |
|
1587 |
}; |
|
1588 |
static final byte[] EXTRA_TAGS = { |
|
1589 |
CONSTANT_MethodHandle, CONSTANT_MethodType, |
|
1590 |
CONSTANT_BootstrapMethod, // pseudo-tag |
|
1591 |
CONSTANT_InvokeDynamic |
|
1592 |
}; |
|
1593 |
static final byte[] LOADABLE_VALUE_TAGS = { // for CONSTANT_LoadableValue |
|
1594 |
CONSTANT_Integer, CONSTANT_Float, CONSTANT_Long, CONSTANT_Double, |
|
1595 |
CONSTANT_String, CONSTANT_Class, |
|
1596 |
CONSTANT_MethodHandle, CONSTANT_MethodType |
|
1597 |
}; |
|
1598 |
static final byte[] ANY_MEMBER_TAGS = { // for CONSTANT_AnyMember |
|
1599 |
CONSTANT_Fieldref, CONSTANT_Methodref, CONSTANT_InterfaceMethodref |
|
1600 |
}; |
|
1601 |
static final byte[] FIELD_SPECIFIC_TAGS = { // for CONSTANT_FieldSpecific |
|
1602 |
CONSTANT_Integer, CONSTANT_Float, CONSTANT_Long, CONSTANT_Double, |
|
1603 |
CONSTANT_String |
|
1604 |
}; |
|
1605 |
static { |
|
1606 |
assert( |
|
1607 |
verifyTagOrder(TAGS_IN_ORDER) && |
|
1608 |
verifyTagOrder(NUMBER_TAGS) && |
|
1609 |
verifyTagOrder(EXTRA_TAGS) && |
|
1610 |
verifyTagOrder(LOADABLE_VALUE_TAGS) && |
|
1611 |
verifyTagOrder(ANY_MEMBER_TAGS) && |
|
1612 |
verifyTagOrder(FIELD_SPECIFIC_TAGS) |
|
1613 |
); |
|
1614 |
} |
|
1615 |
private static boolean verifyTagOrder(byte[] tags) { |
|
1616 |
int prev = -1; |
|
1617 |
for (byte tag : tags) { |
|
1618 |
int next = TAG_ORDER[tag]; |
|
1619 |
assert(next > 0) : "tag not found: "+tag; |
|
1620 |
assert(TAGS_IN_ORDER[next-1] == tag) : "tag repeated: "+tag+" => "+next+" => "+TAGS_IN_ORDER[next-1]; |
|
1621 |
assert(prev < next) : "tags not in order: "+Arrays.toString(tags)+" at "+tag; |
|
1622 |
prev = next; |
|
1623 |
} |
|
1624 |
return true; |
|
1625 |
} |
|
2 | 1626 |
} |