author | ksrini |
Fri, 08 Jul 2011 10:25:57 -0700 | |
changeset 10115 | eb08d08c7ef7 |
parent 7802 | 74f2ee2b62ba |
child 12544 | 5768f2e096de |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10115 | 2 |
* Copyright (c) 2001, 2011, 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:
7171
diff
changeset
|
28 |
import com.sun.java.util.jar.pack.ConstantPool.ClassEntry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
29 |
import com.sun.java.util.jar.pack.ConstantPool.DescriptorEntry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
30 |
import com.sun.java.util.jar.pack.ConstantPool.Entry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
31 |
import com.sun.java.util.jar.pack.ConstantPool.SignatureEntry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
32 |
import com.sun.java.util.jar.pack.ConstantPool.Utf8Entry; |
2 | 33 |
import com.sun.java.util.jar.pack.Package.Class; |
34 |
import com.sun.java.util.jar.pack.Package.InnerClass; |
|
7192
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
35 |
import java.io.DataInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
36 |
import java.io.FilterInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
37 |
import java.io.IOException; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
38 |
import java.io.InputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
39 |
import java.util.ArrayList; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
40 |
import java.util.Map; |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
41 |
import static com.sun.java.util.jar.pack.Constants.*; |
2 | 42 |
|
43 |
/** |
|
44 |
* Reader for a class file that is being incorporated into a package. |
|
45 |
* @author John Rose |
|
46 |
*/ |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
47 |
class ClassReader { |
2 | 48 |
int verbose; |
49 |
||
50 |
Package pkg; |
|
51 |
Class cls; |
|
52 |
long inPos; |
|
53 |
DataInputStream in; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
54 |
Map<Attribute.Layout, Attribute> attrDefs; |
10115 | 55 |
Map<Attribute.Layout, String> attrCommands; |
2 | 56 |
String unknownAttrCommand = "error";; |
57 |
||
58 |
ClassReader(Class cls, InputStream in) throws IOException { |
|
59 |
this.pkg = cls.getPackage(); |
|
60 |
this.cls = cls; |
|
61 |
this.verbose = pkg.verbose; |
|
62 |
this.in = new DataInputStream(new FilterInputStream(in) { |
|
63 |
public int read(byte b[], int off, int len) throws IOException { |
|
64 |
int nr = super.read(b, off, len); |
|
65 |
if (nr >= 0) inPos += nr; |
|
66 |
return nr; |
|
67 |
} |
|
68 |
public int read() throws IOException { |
|
69 |
int ch = super.read(); |
|
70 |
if (ch >= 0) inPos += 1; |
|
71 |
return ch; |
|
72 |
} |
|
73 |
public long skip(long n) throws IOException { |
|
74 |
long ns = super.skip(n); |
|
75 |
if (ns >= 0) inPos += ns; |
|
76 |
return ns; |
|
77 |
} |
|
78 |
}); |
|
79 |
} |
|
80 |
||
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
81 |
public void setAttrDefs(Map<Attribute.Layout, Attribute> attrDefs) { |
2 | 82 |
this.attrDefs = attrDefs; |
83 |
} |
|
84 |
||
10115 | 85 |
public void setAttrCommands(Map<Attribute.Layout, String> attrCommands) { |
2 | 86 |
this.attrCommands = attrCommands; |
87 |
} |
|
88 |
||
89 |
private void skip(int n, String what) throws IOException { |
|
90 |
Utils.log.warning("skipping "+n+" bytes of "+what); |
|
91 |
long skipped = 0; |
|
92 |
while (skipped < n) { |
|
93 |
long j = in.skip(n - skipped); |
|
94 |
assert(j > 0); |
|
95 |
skipped += j; |
|
96 |
} |
|
97 |
assert(skipped == n); |
|
98 |
} |
|
99 |
||
100 |
private int readUnsignedShort() throws IOException { |
|
101 |
return in.readUnsignedShort(); |
|
102 |
} |
|
103 |
||
104 |
private int readInt() throws IOException { |
|
105 |
return in.readInt(); |
|
106 |
} |
|
107 |
||
108 |
/** Read a 2-byte int, and return the <em>global</em> CP entry for it. */ |
|
109 |
private Entry readRef() throws IOException { |
|
110 |
int i = in.readUnsignedShort(); |
|
111 |
return i == 0 ? null : cls.cpMap[i]; |
|
112 |
} |
|
113 |
||
114 |
private Entry readRef(byte tag) throws IOException { |
|
115 |
Entry e = readRef(); |
|
116 |
assert(e != null); |
|
117 |
assert(e.tagMatches(tag)); |
|
118 |
return e; |
|
119 |
} |
|
120 |
||
121 |
private Entry readRefOrNull(byte tag) throws IOException { |
|
122 |
Entry e = readRef(); |
|
123 |
assert(e == null || e.tagMatches(tag)); |
|
124 |
return e; |
|
125 |
} |
|
126 |
||
127 |
private Utf8Entry readUtf8Ref() throws IOException { |
|
128 |
return (Utf8Entry) readRef(CONSTANT_Utf8); |
|
129 |
} |
|
130 |
||
131 |
private ClassEntry readClassRef() throws IOException { |
|
132 |
return (ClassEntry) readRef(CONSTANT_Class); |
|
133 |
} |
|
134 |
||
135 |
private ClassEntry readClassRefOrNull() throws IOException { |
|
136 |
return (ClassEntry) readRefOrNull(CONSTANT_Class); |
|
137 |
} |
|
138 |
||
139 |
private SignatureEntry readSignatureRef() throws IOException { |
|
140 |
// The class file stores a Utf8, but we want a Signature. |
|
141 |
Entry e = readRef(CONSTANT_Utf8); |
|
142 |
return ConstantPool.getSignatureEntry(e.stringValue()); |
|
143 |
} |
|
144 |
||
145 |
void read() throws IOException { |
|
146 |
boolean ok = false; |
|
147 |
try { |
|
148 |
readMagicNumbers(); |
|
149 |
readConstantPool(); |
|
150 |
readHeader(); |
|
151 |
readMembers(false); // fields |
|
152 |
readMembers(true); // methods |
|
153 |
readAttributes(ATTR_CONTEXT_CLASS, cls); |
|
154 |
cls.finishReading(); |
|
155 |
assert(0 >= in.read(new byte[1])); |
|
156 |
ok = true; |
|
157 |
} finally { |
|
158 |
if (!ok) { |
|
159 |
if (verbose > 0) Utils.log.warning("Erroneous data at input offset "+inPos+" of "+cls.file); |
|
160 |
} |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
void readMagicNumbers() throws IOException { |
|
165 |
cls.magic = in.readInt(); |
|
166 |
if (cls.magic != JAVA_MAGIC) |
|
167 |
throw new Attribute.FormatException |
|
168 |
("Bad magic number in class file " |
|
169 |
+Integer.toHexString(cls.magic), |
|
170 |
ATTR_CONTEXT_CLASS, "magic-number", "pass"); |
|
171 |
cls.minver = (short) readUnsignedShort(); |
|
172 |
cls.majver = (short) readUnsignedShort(); |
|
173 |
//System.out.println("ClassFile.version="+cls.majver+"."+cls.minver); |
|
174 |
String bad = checkVersion(cls.majver, cls.minver); |
|
175 |
if (bad != null) { |
|
176 |
throw new Attribute.FormatException |
|
177 |
("classfile version too "+bad+": " |
|
178 |
+cls.majver+"."+cls.minver+" in "+cls.file, |
|
179 |
ATTR_CONTEXT_CLASS, "version", "pass"); |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
private String checkVersion(int majver, int minver) { |
|
184 |
if (majver < pkg.min_class_majver || |
|
185 |
(majver == pkg.min_class_majver && |
|
186 |
minver < pkg.min_class_minver)) { |
|
187 |
return "small"; |
|
188 |
} |
|
189 |
if (majver > pkg.max_class_majver || |
|
190 |
(majver == pkg.max_class_majver && |
|
191 |
minver > pkg.max_class_minver)) { |
|
192 |
return "large"; |
|
193 |
} |
|
194 |
return null; // OK |
|
195 |
} |
|
196 |
||
197 |
void readConstantPool() throws IOException { |
|
198 |
int length = in.readUnsignedShort(); |
|
199 |
//System.err.println("reading CP, length="+length); |
|
200 |
||
201 |
int[] fixups = new int[length*4]; |
|
202 |
int fptr = 0; |
|
203 |
||
204 |
Entry[] cpMap = new Entry[length]; |
|
205 |
cpMap[0] = null; |
|
206 |
for (int i = 1; i < length; i++) { |
|
207 |
//System.err.println("reading CP elt, i="+i); |
|
208 |
int tag = in.readByte(); |
|
209 |
switch (tag) { |
|
210 |
case CONSTANT_Utf8: |
|
211 |
cpMap[i] = ConstantPool.getUtf8Entry(in.readUTF()); |
|
212 |
break; |
|
213 |
case CONSTANT_Integer: |
|
214 |
{ |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
215 |
cpMap[i] = ConstantPool.getLiteralEntry(in.readInt()); |
2 | 216 |
} |
217 |
break; |
|
218 |
case CONSTANT_Float: |
|
219 |
{ |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
220 |
cpMap[i] = ConstantPool.getLiteralEntry(in.readFloat()); |
2 | 221 |
} |
222 |
break; |
|
223 |
case CONSTANT_Long: |
|
224 |
{ |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
225 |
cpMap[i] = ConstantPool.getLiteralEntry(in.readLong()); |
2 | 226 |
cpMap[++i] = null; |
227 |
} |
|
228 |
break; |
|
229 |
case CONSTANT_Double: |
|
230 |
{ |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
231 |
cpMap[i] = ConstantPool.getLiteralEntry(in.readDouble()); |
2 | 232 |
cpMap[++i] = null; |
233 |
} |
|
234 |
break; |
|
235 |
||
236 |
// just read the refs; do not attempt to resolve while reading |
|
237 |
case CONSTANT_Class: |
|
238 |
case CONSTANT_String: |
|
239 |
fixups[fptr++] = i; |
|
240 |
fixups[fptr++] = tag; |
|
241 |
fixups[fptr++] = in.readUnsignedShort(); |
|
242 |
fixups[fptr++] = -1; // empty ref2 |
|
243 |
break; |
|
244 |
case CONSTANT_Fieldref: |
|
245 |
case CONSTANT_Methodref: |
|
246 |
case CONSTANT_InterfaceMethodref: |
|
247 |
case CONSTANT_NameandType: |
|
248 |
fixups[fptr++] = i; |
|
249 |
fixups[fptr++] = tag; |
|
250 |
fixups[fptr++] = in.readUnsignedShort(); |
|
251 |
fixups[fptr++] = in.readUnsignedShort(); |
|
252 |
break; |
|
253 |
default: |
|
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
254 |
throw new ClassFormatException("Bad constant pool tag " + |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
255 |
tag + " in File: " + cls.file.nameString + |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
256 |
" at pos: " + inPos); |
2 | 257 |
} |
258 |
} |
|
259 |
||
260 |
// Fix up refs, which might be out of order. |
|
261 |
while (fptr > 0) { |
|
262 |
if (verbose > 3) |
|
263 |
Utils.log.fine("CP fixups ["+fptr/4+"]"); |
|
264 |
int flimit = fptr; |
|
265 |
fptr = 0; |
|
266 |
for (int fi = 0; fi < flimit; ) { |
|
267 |
int cpi = fixups[fi++]; |
|
268 |
int tag = fixups[fi++]; |
|
269 |
int ref = fixups[fi++]; |
|
270 |
int ref2 = fixups[fi++]; |
|
271 |
if (verbose > 3) |
|
272 |
Utils.log.fine(" cp["+cpi+"] = "+ConstantPool.tagName(tag)+"{"+ref+","+ref2+"}"); |
|
273 |
if (cpMap[ref] == null || ref2 >= 0 && cpMap[ref2] == null) { |
|
274 |
// Defer. |
|
275 |
fixups[fptr++] = cpi; |
|
276 |
fixups[fptr++] = tag; |
|
277 |
fixups[fptr++] = ref; |
|
278 |
fixups[fptr++] = ref2; |
|
279 |
continue; |
|
280 |
} |
|
281 |
switch (tag) { |
|
282 |
case CONSTANT_Class: |
|
283 |
cpMap[cpi] = ConstantPool.getClassEntry(cpMap[ref].stringValue()); |
|
284 |
break; |
|
285 |
case CONSTANT_String: |
|
286 |
cpMap[cpi] = ConstantPool.getStringEntry(cpMap[ref].stringValue()); |
|
287 |
break; |
|
288 |
case CONSTANT_Fieldref: |
|
289 |
case CONSTANT_Methodref: |
|
290 |
case CONSTANT_InterfaceMethodref: |
|
291 |
ClassEntry mclass = (ClassEntry) cpMap[ref]; |
|
292 |
DescriptorEntry mdescr = (DescriptorEntry) cpMap[ref2]; |
|
293 |
cpMap[cpi] = ConstantPool.getMemberEntry((byte)tag, mclass, mdescr); |
|
294 |
break; |
|
295 |
case CONSTANT_NameandType: |
|
296 |
Utf8Entry mname = (Utf8Entry) cpMap[ref]; |
|
297 |
Utf8Entry mtype = (Utf8Entry) cpMap[ref2]; |
|
298 |
cpMap[cpi] = ConstantPool.getDescriptorEntry(mname, mtype); |
|
299 |
break; |
|
300 |
default: |
|
301 |
assert(false); |
|
302 |
} |
|
303 |
} |
|
304 |
assert(fptr < flimit); // Must make progress. |
|
305 |
} |
|
306 |
||
307 |
cls.cpMap = cpMap; |
|
308 |
} |
|
309 |
||
310 |
void readHeader() throws IOException { |
|
311 |
cls.flags = readUnsignedShort(); |
|
312 |
cls.thisClass = readClassRef(); |
|
313 |
cls.superClass = readClassRefOrNull(); |
|
314 |
int ni = readUnsignedShort(); |
|
315 |
cls.interfaces = new ClassEntry[ni]; |
|
316 |
for (int i = 0; i < ni; i++) { |
|
317 |
cls.interfaces[i] = readClassRef(); |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
void readMembers(boolean doMethods) throws IOException { |
|
322 |
int nm = readUnsignedShort(); |
|
323 |
for (int i = 0; i < nm; i++) { |
|
324 |
readMember(doMethods); |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
void readMember(boolean doMethod) throws IOException { |
|
329 |
int mflags = readUnsignedShort(); |
|
330 |
Utf8Entry mname = readUtf8Ref(); |
|
331 |
SignatureEntry mtype = readSignatureRef(); |
|
332 |
DescriptorEntry descr = ConstantPool.getDescriptorEntry(mname, mtype); |
|
333 |
Class.Member m; |
|
334 |
if (!doMethod) |
|
335 |
m = cls.new Field(mflags, descr); |
|
336 |
else |
|
337 |
m = cls.new Method(mflags, descr); |
|
338 |
readAttributes(!doMethod ? ATTR_CONTEXT_FIELD : ATTR_CONTEXT_METHOD, |
|
339 |
m); |
|
340 |
} |
|
341 |
void readAttributes(int ctype, Attribute.Holder h) throws IOException { |
|
342 |
int na = readUnsignedShort(); |
|
343 |
if (na == 0) return; // nothing to do here |
|
344 |
if (verbose > 3) |
|
345 |
Utils.log.fine("readAttributes "+h+" ["+na+"]"); |
|
346 |
for (int i = 0; i < na; i++) { |
|
347 |
String name = readUtf8Ref().stringValue(); |
|
348 |
int length = readInt(); |
|
349 |
// See if there is a special command that applies. |
|
350 |
if (attrCommands != null) { |
|
10115 | 351 |
Attribute.Layout lkey = Attribute.keyForLookup(ctype, name); |
352 |
String cmd = attrCommands.get(lkey); |
|
7802
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
353 |
if (cmd != null) { |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
354 |
switch (cmd) { |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
355 |
case "pass": |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
356 |
String message1 = "passing attribute bitwise in " + h; |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
357 |
throw new Attribute.FormatException(message1, ctype, name, cmd); |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
358 |
case "error": |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
359 |
String message2 = "attribute not allowed in " + h; |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
360 |
throw new Attribute.FormatException(message2, ctype, name, cmd); |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
361 |
case "strip": |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
362 |
skip(length, name + " attribute in " + h); |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
363 |
continue; |
74f2ee2b62ba
7007157: (pack200) stripping attributes causes a NPE
ksrini
parents:
7795
diff
changeset
|
364 |
} |
2 | 365 |
} |
366 |
} |
|
367 |
// Find canonical instance of the requested attribute. |
|
368 |
Attribute a = Attribute.lookup(Package.attrDefs, ctype, name); |
|
369 |
if (verbose > 4 && a != null) |
|
370 |
Utils.log.fine("pkg_attribute_lookup "+name+" = "+a); |
|
371 |
if (a == null) { |
|
372 |
a = Attribute.lookup(this.attrDefs, ctype, name); |
|
373 |
if (verbose > 4 && a != null) |
|
374 |
Utils.log.fine("this "+name+" = "+a); |
|
375 |
} |
|
376 |
if (a == null) { |
|
377 |
a = Attribute.lookup(null, ctype, name); |
|
378 |
if (verbose > 4 && a != null) |
|
379 |
Utils.log.fine("null_attribute_lookup "+name+" = "+a); |
|
380 |
} |
|
381 |
if (a == null && length == 0) { |
|
382 |
// Any zero-length attr is "known"... |
|
383 |
// We can assume an empty attr. has an empty layout. |
|
384 |
// Handles markers like Enum, Bridge, Synthetic, Deprecated. |
|
385 |
a = Attribute.find(ctype, name, ""); |
|
386 |
} |
|
387 |
boolean isStackMap = (ctype == ATTR_CONTEXT_CODE |
|
388 |
&& (name.equals("StackMap") || |
|
389 |
name.equals("StackMapX"))); |
|
390 |
if (isStackMap) { |
|
391 |
// Known attribute but with a corner case format, "pass" it. |
|
392 |
Code code = (Code) h; |
|
393 |
final int TOO_BIG = 0x10000; |
|
394 |
if (code.max_stack >= TOO_BIG || |
|
395 |
code.max_locals >= TOO_BIG || |
|
396 |
code.getLength() >= TOO_BIG || |
|
397 |
name.endsWith("X")) { |
|
398 |
// No, we don't really know what to do this this one. |
|
399 |
// Do not compress the rare and strange "u4" and "X" cases. |
|
400 |
a = null; |
|
401 |
} |
|
402 |
} |
|
403 |
if (a == null) { |
|
404 |
if (isStackMap) { |
|
405 |
// Known attribute but w/o a format; pass it. |
|
406 |
String message = "unsupported StackMap variant in "+h; |
|
407 |
throw new Attribute.FormatException(message, ctype, name, |
|
408 |
"pass"); |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
409 |
} else if ("strip".equals(unknownAttrCommand)) { |
2 | 410 |
// Skip the unknown attribute. |
411 |
skip(length, "unknown "+name+" attribute in "+h); |
|
412 |
continue; |
|
413 |
} else { |
|
6901 | 414 |
String message = " is unknown attribute in class " + h; |
2 | 415 |
throw new Attribute.FormatException(message, ctype, name, |
416 |
unknownAttrCommand); |
|
417 |
} |
|
418 |
} |
|
419 |
if (a.layout() == Package.attrCodeEmpty || |
|
420 |
a.layout() == Package.attrInnerClassesEmpty) { |
|
421 |
// These are hardwired. |
|
422 |
long pos0 = inPos; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
423 |
if ("Code".equals(a.name())) { |
2 | 424 |
Class.Method m = (Class.Method) h; |
425 |
m.code = new Code(m); |
|
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
426 |
try { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
427 |
readCode(m.code); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
428 |
} catch (Instruction.FormatException iie) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
429 |
String message = iie.getMessage() + " in " + h; |
7171
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6901
diff
changeset
|
430 |
throw new ClassReader.ClassFormatException(message, iie); |
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
431 |
} |
2 | 432 |
} else { |
433 |
assert(h == cls); |
|
434 |
readInnerClasses(cls); |
|
435 |
} |
|
436 |
assert(length == inPos - pos0); |
|
437 |
// Keep empty attribute a... |
|
438 |
} else if (length > 0) { |
|
439 |
byte[] bytes = new byte[length]; |
|
440 |
in.readFully(bytes); |
|
441 |
a = a.addContent(bytes); |
|
442 |
} |
|
6901 | 443 |
if (a.size() == 0 && !a.layout().isEmpty()) { |
444 |
throw new ClassFormatException(name + |
|
445 |
": attribute length cannot be zero, in " + h); |
|
446 |
} |
|
2 | 447 |
h.addAttribute(a); |
448 |
if (verbose > 2) |
|
449 |
Utils.log.fine("read "+a); |
|
450 |
} |
|
451 |
} |
|
452 |
||
453 |
void readCode(Code code) throws IOException { |
|
454 |
code.max_stack = readUnsignedShort(); |
|
455 |
code.max_locals = readUnsignedShort(); |
|
456 |
code.bytes = new byte[readInt()]; |
|
457 |
in.readFully(code.bytes); |
|
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
458 |
Instruction.opcodeChecker(code.bytes); |
2 | 459 |
int nh = readUnsignedShort(); |
460 |
code.setHandlerCount(nh); |
|
461 |
for (int i = 0; i < nh; i++) { |
|
462 |
code.handler_start[i] = readUnsignedShort(); |
|
463 |
code.handler_end[i] = readUnsignedShort(); |
|
464 |
code.handler_catch[i] = readUnsignedShort(); |
|
465 |
code.handler_class[i] = readClassRefOrNull(); |
|
466 |
} |
|
467 |
readAttributes(ATTR_CONTEXT_CODE, code); |
|
468 |
} |
|
469 |
||
470 |
void readInnerClasses(Class cls) throws IOException { |
|
471 |
int nc = readUnsignedShort(); |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
472 |
ArrayList<InnerClass> ics = new ArrayList<>(nc); |
2 | 473 |
for (int i = 0; i < nc; i++) { |
474 |
InnerClass ic = |
|
475 |
new InnerClass(readClassRef(), |
|
476 |
readClassRefOrNull(), |
|
477 |
(Utf8Entry)readRefOrNull(CONSTANT_Utf8), |
|
478 |
readUnsignedShort()); |
|
479 |
ics.add(ic); |
|
480 |
} |
|
481 |
cls.innerClasses = ics; // set directly; do not use setInnerClasses. |
|
482 |
// (Later, ics may be transferred to the pkg.) |
|
483 |
} |
|
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
484 |
|
7171
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6901
diff
changeset
|
485 |
static class ClassFormatException extends IOException { |
10115 | 486 |
private static final long serialVersionUID = -3564121733989501833L; |
487 |
||
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
488 |
public ClassFormatException(String message) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
489 |
super(message); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
490 |
} |
7171
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6901
diff
changeset
|
491 |
|
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6901
diff
changeset
|
492 |
public ClassFormatException(String message, Throwable cause) { |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6901
diff
changeset
|
493 |
super(message, cause); |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6901
diff
changeset
|
494 |
} |
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
5506
diff
changeset
|
495 |
} |
2 | 496 |
} |