author | ksrini |
Fri, 17 Aug 2012 08:28:48 -0700 | |
changeset 13577 | afcb909fea86 |
parent 12857 | 0a5f341c2a28 |
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:
5506
diff
changeset
|
28 |
|
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
29 |
import com.sun.java.util.jar.pack.ConstantPool.Entry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
30 |
import com.sun.java.util.jar.pack.ConstantPool.Index; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
31 |
import com.sun.java.util.jar.pack.ConstantPool.NumberEntry; |
12544 | 32 |
import com.sun.java.util.jar.pack.ConstantPool.MethodHandleEntry; |
33 |
import com.sun.java.util.jar.pack.ConstantPool.BootstrapMethodEntry; |
|
2 | 34 |
import com.sun.java.util.jar.pack.Package.Class; |
35 |
import com.sun.java.util.jar.pack.Package.InnerClass; |
|
7192
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
36 |
import java.io.BufferedOutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
37 |
import java.io.ByteArrayOutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
38 |
import java.io.DataOutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
39 |
import java.io.IOException; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
40 |
import java.io.OutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
41 |
import java.util.List; |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
42 |
import static com.sun.java.util.jar.pack.Constants.*; |
2 | 43 |
/** |
44 |
* Writer for a class file that is 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 ClassWriter { |
2 | 48 |
int verbose; |
49 |
||
50 |
Package pkg; |
|
51 |
Class cls; |
|
52 |
DataOutputStream out; |
|
53 |
Index cpIndex; |
|
12544 | 54 |
Index bsmIndex; |
2 | 55 |
|
56 |
ClassWriter(Class cls, OutputStream out) throws IOException { |
|
57 |
this.pkg = cls.getPackage(); |
|
58 |
this.cls = cls; |
|
59 |
this.verbose = pkg.verbose; |
|
60 |
this.out = new DataOutputStream(new BufferedOutputStream(out)); |
|
61 |
this.cpIndex = ConstantPool.makeIndex(cls.toString(), cls.getCPMap()); |
|
62 |
this.cpIndex.flattenSigs = true; |
|
12544 | 63 |
if (cls.hasBootstrapMethods()) { |
64 |
this.bsmIndex = ConstantPool.makeIndex(cpIndex.debugName+".BootstrapMethods", |
|
65 |
cls.getBootstrapMethodMap()); |
|
66 |
} |
|
2 | 67 |
if (verbose > 1) |
68 |
Utils.log.fine("local CP="+(verbose > 2 ? cpIndex.dumpString() : cpIndex.toString())); |
|
69 |
} |
|
70 |
||
71 |
private void writeShort(int x) throws IOException { |
|
72 |
out.writeShort(x); |
|
73 |
} |
|
74 |
||
75 |
private void writeInt(int x) throws IOException { |
|
76 |
out.writeInt(x); |
|
77 |
} |
|
78 |
||
79 |
/** Write a 2-byte int representing a CP entry, using the local cpIndex. */ |
|
80 |
private void writeRef(Entry e) throws IOException { |
|
12544 | 81 |
writeRef(e, cpIndex); |
82 |
} |
|
83 |
||
84 |
/** Write a 2-byte int representing a CP entry, using the given cpIndex. */ |
|
85 |
private void writeRef(Entry e, Index cpIndex) throws IOException { |
|
2 | 86 |
int i = (e == null) ? 0 : cpIndex.indexOf(e); |
87 |
writeShort(i); |
|
88 |
} |
|
89 |
||
90 |
void write() throws IOException { |
|
91 |
boolean ok = false; |
|
92 |
try { |
|
93 |
if (verbose > 1) Utils.log.fine("...writing "+cls); |
|
94 |
writeMagicNumbers(); |
|
95 |
writeConstantPool(); |
|
96 |
writeHeader(); |
|
97 |
writeMembers(false); // fields |
|
98 |
writeMembers(true); // methods |
|
99 |
writeAttributes(ATTR_CONTEXT_CLASS, cls); |
|
100 |
/* Closing here will cause all the underlying |
|
101 |
streams to close, Causing the jar stream |
|
102 |
to close prematurely, instead we just flush. |
|
103 |
out.close(); |
|
104 |
*/ |
|
105 |
out.flush(); |
|
106 |
ok = true; |
|
107 |
} finally { |
|
108 |
if (!ok) { |
|
109 |
Utils.log.warning("Error on output of "+cls); |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
void writeMagicNumbers() throws IOException { |
|
115 |
writeInt(cls.magic); |
|
12857
0a5f341c2a28
7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents:
12544
diff
changeset
|
116 |
writeShort(cls.version.minor); |
0a5f341c2a28
7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents:
12544
diff
changeset
|
117 |
writeShort(cls.version.major); |
2 | 118 |
} |
119 |
||
120 |
void writeConstantPool() throws IOException { |
|
121 |
Entry[] cpMap = cls.cpMap; |
|
122 |
writeShort(cpMap.length); |
|
123 |
for (int i = 0; i < cpMap.length; i++) { |
|
124 |
Entry e = cpMap[i]; |
|
125 |
assert((e == null) == (i == 0 || cpMap[i-1] != null && cpMap[i-1].isDoubleWord())); |
|
126 |
if (e == null) continue; |
|
127 |
byte tag = e.getTag(); |
|
128 |
if (verbose > 2) Utils.log.fine(" CP["+i+"] = "+e); |
|
129 |
out.write(tag); |
|
130 |
switch (tag) { |
|
131 |
case CONSTANT_Signature: |
|
12544 | 132 |
throw new AssertionError("CP should have Signatures remapped to Utf8"); |
2 | 133 |
case CONSTANT_Utf8: |
134 |
out.writeUTF(e.stringValue()); |
|
135 |
break; |
|
136 |
case CONSTANT_Integer: |
|
137 |
out.writeInt(((NumberEntry)e).numberValue().intValue()); |
|
138 |
break; |
|
139 |
case CONSTANT_Float: |
|
140 |
float fval = ((NumberEntry)e).numberValue().floatValue(); |
|
141 |
out.writeInt(Float.floatToRawIntBits(fval)); |
|
142 |
break; |
|
143 |
case CONSTANT_Long: |
|
144 |
out.writeLong(((NumberEntry)e).numberValue().longValue()); |
|
145 |
break; |
|
146 |
case CONSTANT_Double: |
|
147 |
double dval = ((NumberEntry)e).numberValue().doubleValue(); |
|
148 |
out.writeLong(Double.doubleToRawLongBits(dval)); |
|
149 |
break; |
|
150 |
case CONSTANT_Class: |
|
151 |
case CONSTANT_String: |
|
12544 | 152 |
case CONSTANT_MethodType: |
2 | 153 |
writeRef(e.getRef(0)); |
154 |
break; |
|
12544 | 155 |
case CONSTANT_MethodHandle: |
156 |
MethodHandleEntry mhe = (MethodHandleEntry) e; |
|
157 |
out.writeByte(mhe.refKind); |
|
158 |
writeRef(mhe.getRef(0)); |
|
159 |
break; |
|
2 | 160 |
case CONSTANT_Fieldref: |
161 |
case CONSTANT_Methodref: |
|
162 |
case CONSTANT_InterfaceMethodref: |
|
163 |
case CONSTANT_NameandType: |
|
164 |
writeRef(e.getRef(0)); |
|
165 |
writeRef(e.getRef(1)); |
|
166 |
break; |
|
12544 | 167 |
case CONSTANT_InvokeDynamic: |
168 |
writeRef(e.getRef(0), bsmIndex); |
|
169 |
writeRef(e.getRef(1)); |
|
170 |
break; |
|
171 |
case CONSTANT_BootstrapMethod: |
|
172 |
throw new AssertionError("CP should have BootstrapMethods moved to side-table"); |
|
2 | 173 |
default: |
174 |
throw new IOException("Bad constant pool tag "+tag); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
void writeHeader() throws IOException { |
|
180 |
writeShort(cls.flags); |
|
181 |
writeRef(cls.thisClass); |
|
182 |
writeRef(cls.superClass); |
|
183 |
writeShort(cls.interfaces.length); |
|
184 |
for (int i = 0; i < cls.interfaces.length; i++) { |
|
185 |
writeRef(cls.interfaces[i]); |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
void writeMembers(boolean doMethods) throws IOException { |
|
10115 | 190 |
List<? extends Class.Member> mems; |
2 | 191 |
if (!doMethods) |
192 |
mems = cls.getFields(); |
|
193 |
else |
|
194 |
mems = cls.getMethods(); |
|
195 |
writeShort(mems.size()); |
|
10115 | 196 |
for (Class.Member m : mems) { |
2 | 197 |
writeMember(m, doMethods); |
198 |
} |
|
199 |
} |
|
200 |
||
201 |
void writeMember(Class.Member m, boolean doMethod) throws IOException { |
|
202 |
if (verbose > 2) Utils.log.fine("writeMember "+m); |
|
203 |
writeShort(m.flags); |
|
204 |
writeRef(m.getDescriptor().nameRef); |
|
205 |
writeRef(m.getDescriptor().typeRef); |
|
206 |
writeAttributes(!doMethod ? ATTR_CONTEXT_FIELD : ATTR_CONTEXT_METHOD, |
|
207 |
m); |
|
208 |
} |
|
209 |
||
13577
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
210 |
private void reorderBSMandICS(Attribute.Holder h) { |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
211 |
Attribute bsmAttr = h.getAttribute(Package.attrBootstrapMethodsEmpty); |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
212 |
if (bsmAttr == null) return; |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
213 |
|
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
214 |
Attribute icsAttr = h.getAttribute(Package.attrInnerClassesEmpty); |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
215 |
if (icsAttr == null) return; |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
216 |
|
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
217 |
int bsmidx = h.attributes.indexOf(bsmAttr); |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
218 |
int icsidx = h.attributes.indexOf(icsAttr); |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
219 |
if (bsmidx > icsidx) { |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
220 |
h.attributes.remove(bsmAttr); |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
221 |
h.attributes.add(icsidx, bsmAttr); |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
222 |
} |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
223 |
return; |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
224 |
} |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
225 |
|
2 | 226 |
// handy buffer for collecting attrs |
227 |
ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
|
228 |
DataOutputStream bufOut = new DataOutputStream(buf); |
|
229 |
||
230 |
void writeAttributes(int ctype, Attribute.Holder h) throws IOException { |
|
231 |
if (h.attributes == null) { |
|
232 |
writeShort(0); // attribute size |
|
233 |
return; |
|
234 |
} |
|
13577
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
235 |
// there may be cases if an InnerClass attribute is explicit, then the |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
236 |
// ordering could be wrong, fix the ordering before we write it out. |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
237 |
if (h instanceof Package.Class) |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
238 |
reorderBSMandICS(h); |
afcb909fea86
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents:
12857
diff
changeset
|
239 |
|
2 | 240 |
writeShort(h.attributes.size()); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
241 |
for (Attribute a : h.attributes) { |
2 | 242 |
a.finishRefs(cpIndex); |
243 |
writeRef(a.getNameRef()); |
|
244 |
if (a.layout() == Package.attrCodeEmpty || |
|
12544 | 245 |
a.layout() == Package.attrBootstrapMethodsEmpty || |
2 | 246 |
a.layout() == Package.attrInnerClassesEmpty) { |
247 |
// These are hardwired. |
|
248 |
DataOutputStream savedOut = out; |
|
249 |
assert(out != bufOut); |
|
250 |
buf.reset(); |
|
251 |
out = bufOut; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
252 |
if ("Code".equals(a.name())) { |
2 | 253 |
Class.Method m = (Class.Method) h; |
254 |
writeCode(m.code); |
|
12544 | 255 |
} else if ("BootstrapMethods".equals(a.name())) { |
256 |
assert(h == cls); |
|
257 |
writeBootstrapMethods(cls); |
|
258 |
} else if ("InnerClasses".equals(a.name())) { |
|
2 | 259 |
assert(h == cls); |
260 |
writeInnerClasses(cls); |
|
12544 | 261 |
} else { |
262 |
throw new AssertionError(); |
|
2 | 263 |
} |
264 |
out = savedOut; |
|
265 |
if (verbose > 2) |
|
266 |
Utils.log.fine("Attribute "+a.name()+" ["+buf.size()+"]"); |
|
267 |
writeInt(buf.size()); |
|
268 |
buf.writeTo(out); |
|
269 |
} else { |
|
270 |
if (verbose > 2) |
|
271 |
Utils.log.fine("Attribute "+a.name()+" ["+a.size()+"]"); |
|
272 |
writeInt(a.size()); |
|
273 |
out.write(a.bytes()); |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
void writeCode(Code code) throws IOException { |
|
279 |
code.finishRefs(cpIndex); |
|
280 |
writeShort(code.max_stack); |
|
281 |
writeShort(code.max_locals); |
|
282 |
writeInt(code.bytes.length); |
|
283 |
out.write(code.bytes); |
|
284 |
int nh = code.getHandlerCount(); |
|
285 |
writeShort(nh); |
|
286 |
for (int i = 0; i < nh; i++) { |
|
287 |
writeShort(code.handler_start[i]); |
|
288 |
writeShort(code.handler_end[i]); |
|
289 |
writeShort(code.handler_catch[i]); |
|
290 |
writeRef(code.handler_class[i]); |
|
291 |
} |
|
292 |
writeAttributes(ATTR_CONTEXT_CODE, code); |
|
293 |
} |
|
294 |
||
12544 | 295 |
void writeBootstrapMethods(Class cls) throws IOException { |
296 |
List<BootstrapMethodEntry> bsms = cls.getBootstrapMethods(); |
|
297 |
writeShort(bsms.size()); |
|
298 |
for (BootstrapMethodEntry e : bsms) { |
|
299 |
writeRef(e.bsmRef); |
|
300 |
writeShort(e.argRefs.length); |
|
301 |
for (Entry argRef : e.argRefs) { |
|
302 |
writeRef(argRef); |
|
303 |
} |
|
304 |
} |
|
305 |
} |
|
306 |
||
2 | 307 |
void writeInnerClasses(Class cls) throws IOException { |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
308 |
List<InnerClass> ics = cls.getInnerClasses(); |
2 | 309 |
writeShort(ics.size()); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
310 |
for (InnerClass ic : ics) { |
2 | 311 |
writeRef(ic.thisClass); |
312 |
writeRef(ic.outerClass); |
|
313 |
writeRef(ic.name); |
|
314 |
writeShort(ic.flags); |
|
315 |
} |
|
316 |
} |
|
317 |
} |