author | ksrini |
Sun, 22 Apr 2012 06:54:38 -0700 | |
changeset 12544 | 5768f2e096de |
parent 10135 | 8186b3499405 |
child 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 |
||
12544 | 28 |
import com.sun.java.util.jar.pack.ConstantPool.*; |
2 | 29 |
import com.sun.java.util.jar.pack.Package.Class; |
30 |
import com.sun.java.util.jar.pack.Package.File; |
|
31 |
import com.sun.java.util.jar.pack.Package.InnerClass; |
|
7192
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
32 |
import java.io.IOException; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
33 |
import java.io.OutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
34 |
import java.io.PrintStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
35 |
import java.util.ArrayList; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
36 |
import java.util.Arrays; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
37 |
import java.util.Comparator; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
38 |
import java.util.HashMap; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
39 |
import java.util.HashSet; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
40 |
import java.util.List; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
5506
diff
changeset
|
41 |
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
|
42 |
import java.util.Set; |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
43 |
import static com.sun.java.util.jar.pack.Constants.*; |
2 | 44 |
|
45 |
/** |
|
46 |
* Writer for a package file. |
|
47 |
* @author John Rose |
|
48 |
*/ |
|
49 |
class PackageWriter extends BandStructure { |
|
50 |
Package pkg; |
|
51 |
OutputStream finalOut; |
|
52 |
||
53 |
PackageWriter(Package pkg, OutputStream out) throws IOException { |
|
54 |
this.pkg = pkg; |
|
55 |
this.finalOut = out; |
|
56 |
// Caller has specified archive version in the package: |
|
57 |
initPackageMajver(pkg.package_majver); |
|
58 |
} |
|
59 |
||
60 |
void write() throws IOException { |
|
61 |
boolean ok = false; |
|
62 |
try { |
|
63 |
if (verbose > 0) { |
|
64 |
Utils.log.info("Setting up constant pool..."); |
|
65 |
} |
|
66 |
setup(); |
|
67 |
||
68 |
if (verbose > 0) { |
|
69 |
Utils.log.info("Packing..."); |
|
70 |
} |
|
71 |
||
72 |
// writeFileHeader() is done last, since it has ultimate counts |
|
73 |
// writeBandHeaders() is called after all other bands are done |
|
74 |
writeConstantPool(); |
|
75 |
writeFiles(); |
|
76 |
writeAttrDefs(); |
|
77 |
writeInnerClasses(); |
|
78 |
writeClassesAndByteCodes(); |
|
79 |
writeAttrCounts(); |
|
80 |
||
81 |
if (verbose > 1) printCodeHist(); |
|
82 |
||
83 |
// choose codings (fill band_headers if needed) |
|
84 |
if (verbose > 0) { |
|
85 |
Utils.log.info("Coding..."); |
|
86 |
} |
|
87 |
all_bands.chooseBandCodings(); |
|
88 |
||
89 |
// now we can write the headers: |
|
90 |
writeFileHeader(); |
|
91 |
||
92 |
writeAllBandsTo(finalOut); |
|
93 |
||
94 |
ok = true; |
|
95 |
} catch (Exception ee) { |
|
4919 | 96 |
Utils.log.warning("Error on output: "+ee, ee); |
2 | 97 |
//if (verbose > 0) ee.printStackTrace(); |
98 |
// Write partial output only if we are verbose. |
|
99 |
if (verbose > 0) finalOut.close(); |
|
100 |
if (ee instanceof IOException) throw (IOException)ee; |
|
101 |
if (ee instanceof RuntimeException) throw (RuntimeException)ee; |
|
102 |
throw new Error("error packing", ee); |
|
103 |
} |
|
104 |
} |
|
105 |
||
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
106 |
Set<Entry> requiredEntries; // for the CP |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
107 |
Map<Attribute.Layout, int[]> backCountTable; // for layout callables |
2 | 108 |
int[][] attrCounts; // count attr. occurences |
109 |
||
110 |
void setup() { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
111 |
requiredEntries = new HashSet<>(); |
2 | 112 |
setArchiveOptions(); |
113 |
trimClassAttributes(); |
|
114 |
collectAttributeLayouts(); |
|
115 |
pkg.buildGlobalConstantPool(requiredEntries); |
|
116 |
setBandIndexes(); |
|
117 |
makeNewAttributeBands(); |
|
118 |
collectInnerClasses(); |
|
119 |
} |
|
120 |
||
121 |
void setArchiveOptions() { |
|
122 |
// Decide on some archive options early. |
|
123 |
// Does not decide on: AO_HAVE_SPECIAL_FORMATS, |
|
124 |
// AO_HAVE_CP_NUMBERS, AO_HAVE_FILE_HEADERS. |
|
125 |
// Also, AO_HAVE_FILE_OPTIONS may be forced on later. |
|
126 |
int minModtime = pkg.default_modtime; |
|
127 |
int maxModtime = pkg.default_modtime; |
|
128 |
int minOptions = -1; |
|
129 |
int maxOptions = 0; |
|
130 |
||
131 |
// Import defaults from package (deflate hint, etc.). |
|
132 |
archiveOptions |= pkg.default_options; |
|
133 |
||
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
134 |
for (File file : pkg.files) { |
2 | 135 |
int modtime = file.modtime; |
136 |
int options = file.options; |
|
137 |
||
138 |
if (minModtime == NO_MODTIME) { |
|
139 |
minModtime = maxModtime = modtime; |
|
140 |
} else { |
|
141 |
if (minModtime > modtime) minModtime = modtime; |
|
142 |
if (maxModtime < modtime) maxModtime = modtime; |
|
143 |
} |
|
144 |
minOptions &= options; |
|
145 |
maxOptions |= options; |
|
146 |
} |
|
147 |
if (pkg.default_modtime == NO_MODTIME) { |
|
148 |
// Make everything else be a positive offset from here. |
|
149 |
pkg.default_modtime = minModtime; |
|
150 |
} |
|
151 |
if (minModtime != NO_MODTIME && minModtime != maxModtime) { |
|
152 |
// Put them into a band. |
|
153 |
archiveOptions |= AO_HAVE_FILE_MODTIME; |
|
154 |
} |
|
155 |
// If the archive deflation is set do not bother with each file. |
|
156 |
if (!testBit(archiveOptions,AO_DEFLATE_HINT) && minOptions != -1) { |
|
157 |
if (testBit(minOptions, FO_DEFLATE_HINT)) { |
|
158 |
// Every file has the deflate_hint set. |
|
159 |
// Set it for the whole archive, and omit options. |
|
160 |
archiveOptions |= AO_DEFLATE_HINT; |
|
161 |
minOptions -= FO_DEFLATE_HINT; |
|
162 |
maxOptions -= FO_DEFLATE_HINT; |
|
163 |
} |
|
164 |
pkg.default_options |= minOptions; |
|
165 |
if (minOptions != maxOptions |
|
166 |
|| minOptions != pkg.default_options) { |
|
167 |
archiveOptions |= AO_HAVE_FILE_OPTIONS; |
|
168 |
} |
|
169 |
} |
|
170 |
// Decide on default version number (majority rule). |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
171 |
Map<Integer, int[]> verCounts = new HashMap<>(); |
2 | 172 |
int bestCount = 0; |
173 |
int bestVersion = -1; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
174 |
for (Class cls : pkg.classes) { |
2 | 175 |
int version = cls.getVersion(); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
176 |
int[] var = verCounts.get(version); |
2 | 177 |
if (var == null) { |
178 |
var = new int[1]; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
179 |
verCounts.put(version, var); |
2 | 180 |
} |
181 |
int count = (var[0] += 1); |
|
182 |
//System.out.println("version="+version+" count="+count); |
|
183 |
if (bestCount < count) { |
|
184 |
bestCount = count; |
|
185 |
bestVersion = version; |
|
186 |
} |
|
187 |
} |
|
188 |
verCounts.clear(); |
|
189 |
if (bestVersion == -1) bestVersion = 0; // degenerate case |
|
190 |
int bestMajver = (char)(bestVersion >>> 16); |
|
191 |
int bestMinver = (char)(bestVersion); |
|
192 |
pkg.default_class_majver = (short) bestMajver; |
|
193 |
pkg.default_class_minver = (short) bestMinver; |
|
194 |
String bestVerStr = Package.versionStringOf(bestMajver, bestMinver); |
|
195 |
if (verbose > 0) |
|
196 |
Utils.log.info("Consensus version number in segment is "+bestVerStr); |
|
197 |
if (verbose > 0) |
|
198 |
Utils.log.info("Highest version number in segment is "+ |
|
199 |
Package.versionStringOf(pkg.getHighestClassVersion())); |
|
200 |
||
201 |
// Now add explicit pseudo-attrs. to classes with odd versions. |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
202 |
for (Class cls : pkg.classes) { |
2 | 203 |
if (cls.getVersion() != bestVersion) { |
204 |
Attribute a = makeClassFileVersionAttr(cls.minver, cls.majver); |
|
205 |
if (verbose > 1) { |
|
206 |
String clsVer = cls.getVersionString(); |
|
207 |
String pkgVer = bestVerStr; |
|
208 |
Utils.log.fine("Version "+clsVer+" of "+cls |
|
209 |
+" doesn't match package version " |
|
210 |
+pkgVer); |
|
211 |
} |
|
212 |
// Note: Does not add in "natural" order. (Who cares?) |
|
213 |
cls.addAttribute(a); |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
// Decide if we are transmitting a huge resource file: |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
218 |
for (File file : pkg.files) { |
2 | 219 |
long len = file.getFileLength(); |
220 |
if (len != (int)len) { |
|
221 |
archiveOptions |= AO_HAVE_FILE_SIZE_HI; |
|
222 |
if (verbose > 0) |
|
223 |
Utils.log.info("Note: Huge resource file "+file.getFileName()+" forces 64-bit sizing"); |
|
224 |
break; |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
// Decide if code attributes typically have sub-attributes. |
|
229 |
// In that case, to preserve compact 1-byte code headers, |
|
230 |
// we must declare unconditional presence of code flags. |
|
231 |
int cost0 = 0; |
|
232 |
int cost1 = 0; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
233 |
for (Class cls : pkg.classes) { |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
234 |
for (Class.Method m : cls.getMethods()) { |
2 | 235 |
if (m.code != null) { |
236 |
if (m.code.attributeSize() == 0) { |
|
237 |
// cost of a useless unconditional flags byte |
|
238 |
cost1 += 1; |
|
239 |
} else if (shortCodeHeader(m.code) != LONG_CODE_HEADER) { |
|
240 |
// cost of inflating a short header |
|
241 |
cost0 += 3; |
|
242 |
} |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
if (cost0 > cost1) { |
|
247 |
archiveOptions |= AO_HAVE_ALL_CODE_FLAGS; |
|
248 |
} |
|
249 |
if (verbose > 0) |
|
250 |
Utils.log.info("archiveOptions = " |
|
251 |
+"0b"+Integer.toBinaryString(archiveOptions)); |
|
252 |
} |
|
253 |
||
254 |
void writeFileHeader() throws IOException { |
|
255 |
pkg.checkVersion(); |
|
256 |
writeArchiveMagic(); |
|
257 |
writeArchiveHeader(); |
|
258 |
} |
|
259 |
||
260 |
// Local routine used to format fixed-format scalars |
|
261 |
// in the file_header: |
|
262 |
private void putMagicInt32(int val) throws IOException { |
|
263 |
int res = val; |
|
264 |
for (int i = 0; i < 4; i++) { |
|
265 |
archive_magic.putByte(0xFF & (res >>> 24)); |
|
266 |
res <<= 8; |
|
267 |
} |
|
268 |
} |
|
269 |
||
270 |
void writeArchiveMagic() throws IOException { |
|
271 |
putMagicInt32(pkg.magic); |
|
272 |
} |
|
273 |
||
274 |
void writeArchiveHeader() throws IOException { |
|
275 |
// for debug only: number of words optimized away |
|
12544 | 276 |
int headerSizeForDebug = AH_LENGTH_MIN; |
2 | 277 |
|
278 |
// AO_HAVE_SPECIAL_FORMATS is set if non-default |
|
279 |
// coding techniques are used, or if there are |
|
280 |
// compressor-defined attributes transmitted. |
|
281 |
boolean haveSpecial = testBit(archiveOptions, AO_HAVE_SPECIAL_FORMATS); |
|
282 |
if (!haveSpecial) { |
|
283 |
haveSpecial |= (band_headers.length() != 0); |
|
284 |
haveSpecial |= (attrDefsWritten.length != 0); |
|
285 |
if (haveSpecial) |
|
286 |
archiveOptions |= AO_HAVE_SPECIAL_FORMATS; |
|
287 |
} |
|
12544 | 288 |
if (haveSpecial) |
289 |
headerSizeForDebug += AH_SPECIAL_FORMAT_LEN; |
|
2 | 290 |
|
291 |
// AO_HAVE_FILE_HEADERS is set if there is any |
|
292 |
// file or segment envelope information present. |
|
293 |
boolean haveFiles = testBit(archiveOptions, AO_HAVE_FILE_HEADERS); |
|
294 |
if (!haveFiles) { |
|
295 |
haveFiles |= (archiveNextCount > 0); |
|
296 |
haveFiles |= (pkg.default_modtime != NO_MODTIME); |
|
297 |
if (haveFiles) |
|
298 |
archiveOptions |= AO_HAVE_FILE_HEADERS; |
|
299 |
} |
|
12544 | 300 |
if (haveFiles) |
301 |
headerSizeForDebug += AH_FILE_HEADER_LEN; |
|
2 | 302 |
|
303 |
// AO_HAVE_CP_NUMBERS is set if there are any numbers |
|
304 |
// in the global constant pool. (Numbers are in 15% of classes.) |
|
305 |
boolean haveNumbers = testBit(archiveOptions, AO_HAVE_CP_NUMBERS); |
|
306 |
if (!haveNumbers) { |
|
307 |
haveNumbers |= pkg.cp.haveNumbers(); |
|
308 |
if (haveNumbers) |
|
309 |
archiveOptions |= AO_HAVE_CP_NUMBERS; |
|
310 |
} |
|
12544 | 311 |
if (haveNumbers) |
312 |
headerSizeForDebug += AH_CP_NUMBER_LEN; |
|
313 |
||
314 |
// AO_HAVE_CP_EXTRAS is set if there are constant pool entries |
|
315 |
// beyond the Java 6 version of the class file format. |
|
316 |
boolean haveCPExtra = testBit(archiveOptions, AO_HAVE_CP_EXTRAS); |
|
317 |
if (!haveCPExtra) { |
|
318 |
haveCPExtra |= pkg.cp.haveExtraTags(); |
|
319 |
if (haveCPExtra) |
|
320 |
archiveOptions |= AO_HAVE_CP_EXTRAS; |
|
321 |
} |
|
322 |
if (haveCPExtra) |
|
323 |
headerSizeForDebug += AH_CP_EXTRA_LEN; |
|
2 | 324 |
|
325 |
assert(pkg.package_majver > 0); // caller must specify! |
|
326 |
archive_header_0.putInt(pkg.package_minver); |
|
327 |
archive_header_0.putInt(pkg.package_majver); |
|
328 |
if (verbose > 0) |
|
329 |
Utils.log.info("Package Version for this segment:"+ |
|
330 |
Package.versionStringOf(pkg.getPackageVersion())); |
|
331 |
archive_header_0.putInt(archiveOptions); // controls header format |
|
332 |
assert(archive_header_0.length() == AH_LENGTH_0); |
|
333 |
||
334 |
final int DUMMY = 0; |
|
335 |
if (haveFiles) { |
|
336 |
assert(archive_header_S.length() == AH_ARCHIVE_SIZE_HI); |
|
337 |
archive_header_S.putInt(DUMMY); // (archiveSize1 >>> 32) |
|
338 |
assert(archive_header_S.length() == AH_ARCHIVE_SIZE_LO); |
|
339 |
archive_header_S.putInt(DUMMY); // (archiveSize1 >>> 0) |
|
340 |
assert(archive_header_S.length() == AH_LENGTH_S); |
|
341 |
} |
|
342 |
||
343 |
// Done with unsized part of header.... |
|
344 |
||
345 |
if (haveFiles) { |
|
346 |
archive_header_1.putInt(archiveNextCount); // usually zero |
|
347 |
archive_header_1.putInt(pkg.default_modtime); |
|
348 |
archive_header_1.putInt(pkg.files.size()); |
|
349 |
} else { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
350 |
assert(pkg.files.isEmpty()); |
2 | 351 |
} |
352 |
||
353 |
if (haveSpecial) { |
|
354 |
archive_header_1.putInt(band_headers.length()); |
|
355 |
archive_header_1.putInt(attrDefsWritten.length); |
|
356 |
} else { |
|
357 |
assert(band_headers.length() == 0); |
|
358 |
assert(attrDefsWritten.length == 0); |
|
359 |
} |
|
360 |
||
12544 | 361 |
writeConstantPoolCounts(haveNumbers, haveCPExtra); |
2 | 362 |
|
363 |
archive_header_1.putInt(pkg.getAllInnerClasses().size()); |
|
364 |
archive_header_1.putInt(pkg.default_class_minver); |
|
365 |
archive_header_1.putInt(pkg.default_class_majver); |
|
366 |
archive_header_1.putInt(pkg.classes.size()); |
|
367 |
||
12544 | 368 |
// Sanity: Make sure we came out to 29 (less optional fields): |
2 | 369 |
assert(archive_header_0.length() + |
370 |
archive_header_S.length() + |
|
371 |
archive_header_1.length() |
|
12544 | 372 |
== headerSizeForDebug); |
2 | 373 |
|
374 |
// Figure out all the sizes now, first cut: |
|
375 |
archiveSize0 = 0; |
|
376 |
archiveSize1 = all_bands.outputSize(); |
|
377 |
// Second cut: |
|
378 |
archiveSize0 += archive_magic.outputSize(); |
|
379 |
archiveSize0 += archive_header_0.outputSize(); |
|
380 |
archiveSize0 += archive_header_S.outputSize(); |
|
381 |
// Make the adjustments: |
|
382 |
archiveSize1 -= archiveSize0; |
|
383 |
||
384 |
// Patch the header: |
|
385 |
if (haveFiles) { |
|
386 |
int archiveSizeHi = (int)(archiveSize1 >>> 32); |
|
387 |
int archiveSizeLo = (int)(archiveSize1 >>> 0); |
|
388 |
archive_header_S.patchValue(AH_ARCHIVE_SIZE_HI, archiveSizeHi); |
|
389 |
archive_header_S.patchValue(AH_ARCHIVE_SIZE_LO, archiveSizeLo); |
|
390 |
int zeroLen = UNSIGNED5.getLength(DUMMY); |
|
391 |
archiveSize0 += UNSIGNED5.getLength(archiveSizeHi) - zeroLen; |
|
392 |
archiveSize0 += UNSIGNED5.getLength(archiveSizeLo) - zeroLen; |
|
393 |
} |
|
394 |
if (verbose > 1) |
|
395 |
Utils.log.fine("archive sizes: "+ |
|
396 |
archiveSize0+"+"+archiveSize1); |
|
397 |
assert(all_bands.outputSize() == archiveSize0+archiveSize1); |
|
398 |
} |
|
399 |
||
12544 | 400 |
void writeConstantPoolCounts(boolean haveNumbers, boolean haveCPExtra) throws IOException { |
401 |
for (byte tag : ConstantPool.TAGS_IN_ORDER) { |
|
2 | 402 |
int count = pkg.cp.getIndexByTag(tag).size(); |
403 |
switch (tag) { |
|
404 |
case CONSTANT_Utf8: |
|
405 |
// The null string is always first. |
|
406 |
if (count > 0) |
|
407 |
assert(pkg.cp.getIndexByTag(tag).get(0) |
|
408 |
== ConstantPool.getUtf8Entry("")); |
|
409 |
break; |
|
410 |
||
411 |
case CONSTANT_Integer: |
|
412 |
case CONSTANT_Float: |
|
413 |
case CONSTANT_Long: |
|
414 |
case CONSTANT_Double: |
|
415 |
// Omit counts for numbers if possible. |
|
416 |
if (!haveNumbers) { |
|
417 |
assert(count == 0); |
|
418 |
continue; |
|
419 |
} |
|
420 |
break; |
|
12544 | 421 |
|
422 |
case CONSTANT_MethodHandle: |
|
423 |
case CONSTANT_MethodType: |
|
424 |
case CONSTANT_InvokeDynamic: |
|
425 |
case CONSTANT_BootstrapMethod: |
|
426 |
// Omit counts for newer entities if possible. |
|
427 |
if (!haveCPExtra) { |
|
428 |
assert(count == 0); |
|
429 |
continue; |
|
430 |
} |
|
431 |
break; |
|
2 | 432 |
} |
433 |
archive_header_1.putInt(count); |
|
434 |
} |
|
435 |
} |
|
436 |
||
437 |
protected Index getCPIndex(byte tag) { |
|
438 |
return pkg.cp.getIndexByTag(tag); |
|
439 |
} |
|
440 |
||
441 |
// (The following observations are out of date; they apply only to |
|
442 |
// "banding" the constant pool itself. Later revisions of this algorithm |
|
443 |
// applied the banding technique to every part of the package file, |
|
444 |
// applying the benefits more broadly.) |
|
445 |
||
446 |
// Note: Keeping the data separate in passes (or "bands") allows the |
|
447 |
// compressor to issue significantly shorter indexes for repeated data. |
|
448 |
// The difference in zipped size is 4%, which is remarkable since the |
|
449 |
// unzipped sizes are the same (only the byte order differs). |
|
450 |
||
451 |
// After moving similar data into bands, it becomes natural to delta-encode |
|
452 |
// each band. (This is especially useful if we sort the constant pool first.) |
|
453 |
// Delta encoding saves an extra 5% in the output size (13% of the CP itself). |
|
454 |
// Because a typical delta usees much less data than a byte, the savings after |
|
455 |
// zipping is even better: A zipped delta-encoded package is 8% smaller than |
|
456 |
// a zipped non-delta-encoded package. Thus, in the zipped file, a banded, |
|
457 |
// delta-encoded constant pool saves over 11% (of the total file size) compared |
|
458 |
// with a zipped unbanded file. |
|
459 |
||
460 |
void writeConstantPool() throws IOException { |
|
461 |
IndexGroup cp = pkg.cp; |
|
462 |
||
463 |
if (verbose > 0) Utils.log.info("Writing CP"); |
|
464 |
||
12544 | 465 |
for (byte tag : ConstantPool.TAGS_IN_ORDER) { |
2 | 466 |
Index index = cp.getIndexByTag(tag); |
467 |
||
468 |
Entry[] cpMap = index.cpMap; |
|
469 |
if (verbose > 0) |
|
470 |
Utils.log.info("Writing "+cpMap.length+" "+ConstantPool.tagName(tag)+" entries..."); |
|
471 |
||
472 |
if (optDumpBands) { |
|
8803
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
473 |
try (PrintStream ps = new PrintStream(getDumpStream(index, ".idx"))) { |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
474 |
printArrayTo(ps, cpMap, 0, cpMap.length); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
475 |
} |
2 | 476 |
} |
477 |
||
478 |
switch (tag) { |
|
479 |
case CONSTANT_Utf8: |
|
480 |
writeUtf8Bands(cpMap); |
|
481 |
break; |
|
482 |
case CONSTANT_Integer: |
|
483 |
for (int i = 0; i < cpMap.length; i++) { |
|
484 |
NumberEntry e = (NumberEntry) cpMap[i]; |
|
485 |
int x = ((Integer)e.numberValue()).intValue(); |
|
486 |
cp_Int.putInt(x); |
|
487 |
} |
|
488 |
break; |
|
489 |
case CONSTANT_Float: |
|
490 |
for (int i = 0; i < cpMap.length; i++) { |
|
491 |
NumberEntry e = (NumberEntry) cpMap[i]; |
|
492 |
float fx = ((Float)e.numberValue()).floatValue(); |
|
493 |
int x = Float.floatToIntBits(fx); |
|
494 |
cp_Float.putInt(x); |
|
495 |
} |
|
496 |
break; |
|
497 |
case CONSTANT_Long: |
|
498 |
for (int i = 0; i < cpMap.length; i++) { |
|
499 |
NumberEntry e = (NumberEntry) cpMap[i]; |
|
500 |
long x = ((Long)e.numberValue()).longValue(); |
|
501 |
cp_Long_hi.putInt((int)(x >>> 32)); |
|
502 |
cp_Long_lo.putInt((int)(x >>> 0)); |
|
503 |
} |
|
504 |
break; |
|
505 |
case CONSTANT_Double: |
|
506 |
for (int i = 0; i < cpMap.length; i++) { |
|
507 |
NumberEntry e = (NumberEntry) cpMap[i]; |
|
508 |
double dx = ((Double)e.numberValue()).doubleValue(); |
|
509 |
long x = Double.doubleToLongBits(dx); |
|
510 |
cp_Double_hi.putInt((int)(x >>> 32)); |
|
511 |
cp_Double_lo.putInt((int)(x >>> 0)); |
|
512 |
} |
|
513 |
break; |
|
514 |
case CONSTANT_String: |
|
515 |
for (int i = 0; i < cpMap.length; i++) { |
|
516 |
StringEntry e = (StringEntry) cpMap[i]; |
|
517 |
cp_String.putRef(e.ref); |
|
518 |
} |
|
519 |
break; |
|
520 |
case CONSTANT_Class: |
|
521 |
for (int i = 0; i < cpMap.length; i++) { |
|
522 |
ClassEntry e = (ClassEntry) cpMap[i]; |
|
523 |
cp_Class.putRef(e.ref); |
|
524 |
} |
|
525 |
break; |
|
526 |
case CONSTANT_Signature: |
|
527 |
writeSignatureBands(cpMap); |
|
528 |
break; |
|
529 |
case CONSTANT_NameandType: |
|
530 |
for (int i = 0; i < cpMap.length; i++) { |
|
531 |
DescriptorEntry e = (DescriptorEntry) cpMap[i]; |
|
532 |
cp_Descr_name.putRef(e.nameRef); |
|
533 |
cp_Descr_type.putRef(e.typeRef); |
|
534 |
} |
|
535 |
break; |
|
536 |
case CONSTANT_Fieldref: |
|
537 |
writeMemberRefs(tag, cpMap, cp_Field_class, cp_Field_desc); |
|
538 |
break; |
|
539 |
case CONSTANT_Methodref: |
|
540 |
writeMemberRefs(tag, cpMap, cp_Method_class, cp_Method_desc); |
|
541 |
break; |
|
542 |
case CONSTANT_InterfaceMethodref: |
|
543 |
writeMemberRefs(tag, cpMap, cp_Imethod_class, cp_Imethod_desc); |
|
544 |
break; |
|
12544 | 545 |
case CONSTANT_MethodHandle: |
546 |
for (int i = 0; i < cpMap.length; i++) { |
|
547 |
MethodHandleEntry e = (MethodHandleEntry) cpMap[i]; |
|
548 |
cp_MethodHandle_refkind.putInt(e.refKind); |
|
549 |
cp_MethodHandle_member.putRef(e.memRef); |
|
550 |
} |
|
551 |
break; |
|
552 |
case CONSTANT_MethodType: |
|
553 |
for (int i = 0; i < cpMap.length; i++) { |
|
554 |
MethodTypeEntry e = (MethodTypeEntry) cpMap[i]; |
|
555 |
cp_MethodType.putRef(e.typeRef); |
|
556 |
} |
|
557 |
break; |
|
558 |
case CONSTANT_InvokeDynamic: |
|
559 |
for (int i = 0; i < cpMap.length; i++) { |
|
560 |
InvokeDynamicEntry e = (InvokeDynamicEntry) cpMap[i]; |
|
561 |
cp_InvokeDynamic_spec.putRef(e.bssRef); |
|
562 |
cp_InvokeDynamic_desc.putRef(e.descRef); |
|
563 |
} |
|
564 |
break; |
|
565 |
case CONSTANT_BootstrapMethod: |
|
566 |
for (int i = 0; i < cpMap.length; i++) { |
|
567 |
BootstrapMethodEntry e = (BootstrapMethodEntry) cpMap[i]; |
|
568 |
cp_BootstrapMethod_ref.putRef(e.bsmRef); |
|
569 |
cp_BootstrapMethod_arg_count.putInt(e.argRefs.length); |
|
570 |
for (Entry argRef : e.argRefs) { |
|
571 |
cp_BootstrapMethod_arg.putRef(argRef); |
|
572 |
} |
|
573 |
} |
|
574 |
break; |
|
2 | 575 |
default: |
12544 | 576 |
throw new AssertionError("unexpected CP tag in package"); |
577 |
} |
|
578 |
} |
|
579 |
if (optDumpBands || verbose > 1) { |
|
580 |
for (byte tag = CONSTANT_GroupFirst; tag < CONSTANT_GroupLimit; tag++) { |
|
581 |
Index index = cp.getIndexByTag(tag); |
|
582 |
if (index == null || index.isEmpty()) continue; |
|
583 |
Entry[] cpMap = index.cpMap; |
|
584 |
if (verbose > 1) |
|
585 |
Utils.log.info("Index group "+ConstantPool.tagName(tag)+" contains "+cpMap.length+" entries."); |
|
586 |
if (optDumpBands) { |
|
587 |
try (PrintStream ps = new PrintStream(getDumpStream(index.debugName, tag, ".gidx", index))) { |
|
588 |
printArrayTo(ps, cpMap, 0, cpMap.length, true); |
|
589 |
} |
|
590 |
} |
|
2 | 591 |
} |
592 |
} |
|
593 |
} |
|
594 |
||
595 |
void writeUtf8Bands(Entry[] cpMap) throws IOException { |
|
596 |
if (cpMap.length == 0) |
|
597 |
return; // nothing to write |
|
598 |
||
599 |
// The first element must always be the empty string. |
|
600 |
assert(cpMap[0].stringValue().equals("")); |
|
601 |
final int SUFFIX_SKIP_1 = 1; |
|
602 |
final int PREFIX_SKIP_2 = 2; |
|
603 |
||
604 |
// Fetch the char arrays, first of all. |
|
605 |
char[][] chars = new char[cpMap.length][]; |
|
606 |
for (int i = 0; i < chars.length; i++) { |
|
607 |
chars[i] = cpMap[i].stringValue().toCharArray(); |
|
608 |
} |
|
609 |
||
610 |
// First band: Write lengths of shared prefixes. |
|
611 |
int[] prefixes = new int[cpMap.length]; // includes 2 skipped zeroes |
|
612 |
char[] prevChars = {}; |
|
613 |
for (int i = 0; i < chars.length; i++) { |
|
614 |
int prefix = 0; |
|
615 |
char[] curChars = chars[i]; |
|
616 |
int limit = Math.min(curChars.length, prevChars.length); |
|
617 |
while (prefix < limit && curChars[prefix] == prevChars[prefix]) |
|
618 |
prefix++; |
|
619 |
prefixes[i] = prefix; |
|
620 |
if (i >= PREFIX_SKIP_2) |
|
621 |
cp_Utf8_prefix.putInt(prefix); |
|
622 |
else |
|
623 |
assert(prefix == 0); |
|
624 |
prevChars = curChars; |
|
625 |
} |
|
626 |
||
627 |
// Second band: Write lengths of unshared suffixes. |
|
628 |
// Third band: Write the char values in the unshared suffixes. |
|
629 |
for (int i = 0; i < chars.length; i++) { |
|
630 |
char[] str = chars[i]; |
|
631 |
int prefix = prefixes[i]; |
|
632 |
int suffix = str.length - prefixes[i]; |
|
633 |
boolean isPacked = false; |
|
634 |
if (suffix == 0) { |
|
635 |
// Zero suffix length is special flag to indicate |
|
636 |
// separate treatment in cp_Utf8_big bands. |
|
637 |
// This suffix length never occurs naturally, |
|
638 |
// except in the one case of a zero-length string. |
|
639 |
// (If it occurs, it is the first, due to sorting.) |
|
640 |
// The zero length string must, paradoxically, be |
|
641 |
// encoded as a zero-length cp_Utf8_big band. |
|
642 |
// This wastes exactly (& tolerably) one null byte. |
|
643 |
isPacked = (i >= SUFFIX_SKIP_1); |
|
644 |
// Do not bother to add an empty "(Utf8_big_0)" band. |
|
645 |
// Also, the initial empty string does not require a band. |
|
646 |
} else if (optBigStrings && effort > 1 && suffix > 100) { |
|
647 |
int numWide = 0; |
|
648 |
for (int n = 0; n < suffix; n++) { |
|
649 |
if (str[prefix+n] > 127) { |
|
650 |
numWide++; |
|
651 |
} |
|
652 |
} |
|
653 |
if (numWide > 100) { |
|
654 |
// Try packing the chars with an alternate encoding. |
|
655 |
isPacked = tryAlternateEncoding(i, numWide, str, prefix); |
|
656 |
} |
|
657 |
} |
|
658 |
if (i < SUFFIX_SKIP_1) { |
|
659 |
// No output. |
|
660 |
assert(!isPacked); |
|
661 |
assert(suffix == 0); |
|
662 |
} else if (isPacked) { |
|
663 |
// Mark packed string with zero-length suffix count. |
|
664 |
// This tells the unpacker to go elsewhere for the suffix bits. |
|
665 |
// Fourth band: Write unshared suffix with alternate coding. |
|
666 |
cp_Utf8_suffix.putInt(0); |
|
667 |
cp_Utf8_big_suffix.putInt(suffix); |
|
668 |
} else { |
|
669 |
assert(suffix != 0); // would be ambiguous |
|
670 |
// Normal string. Save suffix in third and fourth bands. |
|
671 |
cp_Utf8_suffix.putInt(suffix); |
|
672 |
for (int n = 0; n < suffix; n++) { |
|
673 |
int ch = str[prefix+n]; |
|
674 |
cp_Utf8_chars.putInt(ch); |
|
675 |
} |
|
676 |
} |
|
677 |
} |
|
678 |
if (verbose > 0) { |
|
679 |
int normCharCount = cp_Utf8_chars.length(); |
|
680 |
int packCharCount = cp_Utf8_big_chars.length(); |
|
681 |
int charCount = normCharCount + packCharCount; |
|
682 |
Utils.log.info("Utf8string #CHARS="+charCount+" #PACKEDCHARS="+packCharCount); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
private boolean tryAlternateEncoding(int i, int numWide, |
|
687 |
char[] str, int prefix) { |
|
688 |
int suffix = str.length - prefix; |
|
689 |
int[] cvals = new int[suffix]; |
|
690 |
for (int n = 0; n < suffix; n++) { |
|
691 |
cvals[n] = str[prefix+n]; |
|
692 |
} |
|
693 |
CodingChooser cc = getCodingChooser(); |
|
694 |
Coding bigRegular = cp_Utf8_big_chars.regularCoding; |
|
695 |
String bandName = "(Utf8_big_"+i+")"; |
|
696 |
int[] sizes = { 0, 0 }; |
|
697 |
final int BYTE_SIZE = CodingChooser.BYTE_SIZE; |
|
698 |
final int ZIP_SIZE = CodingChooser.ZIP_SIZE; |
|
699 |
if (verbose > 1 || cc.verbose > 1) { |
|
700 |
Utils.log.fine("--- chooseCoding "+bandName); |
|
701 |
} |
|
702 |
CodingMethod special = cc.choose(cvals, bigRegular, sizes); |
|
703 |
Coding charRegular = cp_Utf8_chars.regularCoding; |
|
704 |
if (verbose > 1) |
|
705 |
Utils.log.fine("big string["+i+"] len="+suffix+" #wide="+numWide+" size="+sizes[BYTE_SIZE]+"/z="+sizes[ZIP_SIZE]+" coding "+special); |
|
706 |
if (special != charRegular) { |
|
707 |
int specialZipSize = sizes[ZIP_SIZE]; |
|
708 |
int[] normalSizes = cc.computeSize(charRegular, cvals); |
|
709 |
int normalZipSize = normalSizes[ZIP_SIZE]; |
|
710 |
int minWin = Math.max(5, normalZipSize/1000); |
|
711 |
if (verbose > 1) |
|
712 |
Utils.log.fine("big string["+i+"] normalSize="+normalSizes[BYTE_SIZE]+"/z="+normalSizes[ZIP_SIZE]+" win="+(specialZipSize<normalZipSize-minWin)); |
|
713 |
if (specialZipSize < normalZipSize-minWin) { |
|
714 |
IntBand big = cp_Utf8_big_chars.newIntBand(bandName); |
|
715 |
big.initializeValues(cvals); |
|
716 |
return true; |
|
717 |
} |
|
718 |
} |
|
719 |
return false; |
|
720 |
} |
|
721 |
||
722 |
void writeSignatureBands(Entry[] cpMap) throws IOException { |
|
723 |
for (int i = 0; i < cpMap.length; i++) { |
|
724 |
SignatureEntry e = (SignatureEntry) cpMap[i]; |
|
725 |
cp_Signature_form.putRef(e.formRef); |
|
726 |
for (int j = 0; j < e.classRefs.length; j++) { |
|
727 |
cp_Signature_classes.putRef(e.classRefs[j]); |
|
728 |
} |
|
729 |
} |
|
730 |
} |
|
731 |
||
732 |
void writeMemberRefs(byte tag, Entry[] cpMap, CPRefBand cp_class, CPRefBand cp_desc) throws IOException { |
|
733 |
for (int i = 0; i < cpMap.length; i++) { |
|
734 |
MemberEntry e = (MemberEntry) cpMap[i]; |
|
735 |
cp_class.putRef(e.classRef); |
|
736 |
cp_desc.putRef(e.descRef); |
|
737 |
} |
|
738 |
} |
|
739 |
||
740 |
void writeFiles() throws IOException { |
|
741 |
int numFiles = pkg.files.size(); |
|
742 |
if (numFiles == 0) return; |
|
743 |
int options = archiveOptions; |
|
744 |
boolean haveSizeHi = testBit(options, AO_HAVE_FILE_SIZE_HI); |
|
745 |
boolean haveModtime = testBit(options, AO_HAVE_FILE_MODTIME); |
|
746 |
boolean haveOptions = testBit(options, AO_HAVE_FILE_OPTIONS); |
|
747 |
if (!haveOptions) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
748 |
for (File file : pkg.files) { |
2 | 749 |
if (file.isClassStub()) { |
750 |
haveOptions = true; |
|
751 |
options |= AO_HAVE_FILE_OPTIONS; |
|
752 |
archiveOptions = options; |
|
753 |
break; |
|
754 |
} |
|
755 |
} |
|
756 |
} |
|
757 |
if (haveSizeHi || haveModtime || haveOptions || !pkg.files.isEmpty()) { |
|
758 |
options |= AO_HAVE_FILE_HEADERS; |
|
759 |
archiveOptions = options; |
|
760 |
} |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
761 |
for (File file : pkg.files) { |
2 | 762 |
file_name.putRef(file.name); |
763 |
long len = file.getFileLength(); |
|
764 |
file_size_lo.putInt((int)len); |
|
765 |
if (haveSizeHi) |
|
766 |
file_size_hi.putInt((int)(len >>> 32)); |
|
767 |
if (haveModtime) |
|
768 |
file_modtime.putInt(file.modtime - pkg.default_modtime); |
|
769 |
if (haveOptions) |
|
770 |
file_options.putInt(file.options); |
|
771 |
file.writeTo(file_bits.collectorStream()); |
|
772 |
if (verbose > 1) |
|
773 |
Utils.log.fine("Wrote "+len+" bytes of "+file.name.stringValue()); |
|
774 |
} |
|
775 |
if (verbose > 0) |
|
776 |
Utils.log.info("Wrote "+numFiles+" resource files"); |
|
777 |
} |
|
778 |
||
779 |
void collectAttributeLayouts() { |
|
780 |
maxFlags = new int[ATTR_CONTEXT_LIMIT]; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
781 |
allLayouts = new FixedList<>(ATTR_CONTEXT_LIMIT); |
2 | 782 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) { |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
783 |
allLayouts.set(i, new HashMap<Attribute.Layout, int[]>()); |
2 | 784 |
} |
785 |
// Collect maxFlags and allLayouts. |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
786 |
for (Class cls : pkg.classes) { |
2 | 787 |
visitAttributeLayoutsIn(ATTR_CONTEXT_CLASS, cls); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
788 |
for (Class.Field f : cls.getFields()) { |
2 | 789 |
visitAttributeLayoutsIn(ATTR_CONTEXT_FIELD, f); |
790 |
} |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
791 |
for (Class.Method m : cls.getMethods()) { |
2 | 792 |
visitAttributeLayoutsIn(ATTR_CONTEXT_METHOD, m); |
793 |
if (m.code != null) { |
|
794 |
visitAttributeLayoutsIn(ATTR_CONTEXT_CODE, m.code); |
|
795 |
} |
|
796 |
} |
|
797 |
} |
|
798 |
// If there are many species of attributes, use 63-bit flags. |
|
799 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
800 |
int nl = allLayouts.get(i).size(); |
2 | 801 |
boolean haveLongFlags = haveFlagsHi(i); |
802 |
final int TOO_MANY_ATTRS = 32 /*int flag size*/ |
|
803 |
- 12 /*typical flag bits in use*/ |
|
804 |
+ 4 /*typical number of OK overflows*/; |
|
805 |
if (nl >= TOO_MANY_ATTRS) { // heuristic |
|
806 |
int mask = 1<<(LG_AO_HAVE_XXX_FLAGS_HI+i); |
|
807 |
archiveOptions |= mask; |
|
808 |
haveLongFlags = true; |
|
809 |
if (verbose > 0) |
|
810 |
Utils.log.info("Note: Many "+Attribute.contextName(i)+" attributes forces 63-bit flags"); |
|
811 |
} |
|
812 |
if (verbose > 1) { |
|
813 |
Utils.log.fine(Attribute.contextName(i)+".maxFlags = 0x"+Integer.toHexString(maxFlags[i])); |
|
814 |
Utils.log.fine(Attribute.contextName(i)+".#layouts = "+nl); |
|
815 |
} |
|
816 |
assert(haveFlagsHi(i) == haveLongFlags); |
|
817 |
} |
|
818 |
initAttrIndexLimit(); |
|
819 |
||
820 |
// Standard indexes can never conflict with flag bits. Assert it. |
|
821 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) { |
|
822 |
assert((attrFlagMask[i] & maxFlags[i]) == 0); |
|
823 |
} |
|
824 |
// Collect counts for both predefs. and custom defs. |
|
825 |
// Decide on custom, local attribute definitions. |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
826 |
backCountTable = new HashMap<>(); |
2 | 827 |
attrCounts = new int[ATTR_CONTEXT_LIMIT][]; |
828 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) { |
|
829 |
// Now the remaining defs in allLayouts[i] need attr. indexes. |
|
830 |
// Fill up unused flag bits with new defs. |
|
831 |
// Unused bits are those which are not used by predefined attrs, |
|
832 |
// and which are always clear in the classfiles. |
|
833 |
long avHiBits = ~(maxFlags[i] | attrFlagMask[i]); |
|
834 |
assert(attrIndexLimit[i] > 0); |
|
835 |
assert(attrIndexLimit[i] < 64); // all bits fit into a Java long |
|
836 |
avHiBits &= (1L<<attrIndexLimit[i])-1; |
|
837 |
int nextLoBit = 0; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
838 |
Map<Attribute.Layout, int[]> defMap = allLayouts.get(i); |
10135
8186b3499405
7069870: Parts of the JDK erroneously rely on generic array initializers with diamond
jjg
parents:
10115
diff
changeset
|
839 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
10115 | 840 |
Map.Entry<Attribute.Layout, int[]>[] layoutsAndCounts = |
10135
8186b3499405
7069870: Parts of the JDK erroneously rely on generic array initializers with diamond
jjg
parents:
10115
diff
changeset
|
841 |
new Map.Entry[defMap.size()]; |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
842 |
defMap.entrySet().toArray(layoutsAndCounts); |
2 | 843 |
// Sort by count, most frequent first. |
844 |
// Predefs. participate in this sort, though it does not matter. |
|
10115 | 845 |
Arrays.sort(layoutsAndCounts, |
846 |
new Comparator<Map.Entry<Attribute.Layout, int[]>>() { |
|
847 |
public int compare(Map.Entry<Attribute.Layout, int[]> e0, |
|
848 |
Map.Entry<Attribute.Layout, int[]> e1) { |
|
2 | 849 |
// Primary sort key is count, reversed. |
10115 | 850 |
int r = -(e0.getValue()[0] - e1.getValue()[0]); |
2 | 851 |
if (r != 0) return r; |
10115 | 852 |
return e0.getKey().compareTo(e1.getKey()); |
2 | 853 |
} |
854 |
}); |
|
855 |
attrCounts[i] = new int[attrIndexLimit[i]+layoutsAndCounts.length]; |
|
856 |
for (int j = 0; j < layoutsAndCounts.length; j++) { |
|
10115 | 857 |
Map.Entry<Attribute.Layout, int[]> e = layoutsAndCounts[j]; |
858 |
Attribute.Layout def = e.getKey(); |
|
859 |
int count = e.getValue()[0]; |
|
2 | 860 |
int index; |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
861 |
Integer predefIndex = attrIndexTable.get(def); |
2 | 862 |
if (predefIndex != null) { |
863 |
// The index is already set. |
|
864 |
index = predefIndex.intValue(); |
|
865 |
} else if (avHiBits != 0) { |
|
866 |
while ((avHiBits & 1) == 0) { |
|
867 |
avHiBits >>>= 1; |
|
868 |
nextLoBit += 1; |
|
869 |
} |
|
870 |
avHiBits -= 1; // clear low bit; we are using it now |
|
871 |
// Update attrIndexTable: |
|
872 |
index = setAttributeLayoutIndex(def, nextLoBit); |
|
873 |
} else { |
|
874 |
// Update attrIndexTable: |
|
875 |
index = setAttributeLayoutIndex(def, ATTR_INDEX_OVERFLOW); |
|
876 |
} |
|
877 |
||
878 |
// Now that we know the index, record the count of this def. |
|
879 |
attrCounts[i][index] = count; |
|
880 |
||
881 |
// For all callables in the def, keep a tally of back-calls. |
|
882 |
Attribute.Layout.Element[] cbles = def.getCallables(); |
|
883 |
final int[] bc = new int[cbles.length]; |
|
884 |
for (int k = 0; k < cbles.length; k++) { |
|
885 |
assert(cbles[k].kind == Attribute.EK_CBLE); |
|
886 |
if (!cbles[k].flagTest(Attribute.EF_BACK)) { |
|
887 |
bc[k] = -1; // no count to accumulate here |
|
888 |
} |
|
889 |
} |
|
890 |
backCountTable.put(def, bc); |
|
891 |
||
892 |
if (predefIndex == null) { |
|
893 |
// Make sure the package CP can name the local attribute. |
|
894 |
Entry ne = ConstantPool.getUtf8Entry(def.name()); |
|
895 |
String layout = def.layoutForPackageMajver(getPackageMajver()); |
|
896 |
Entry le = ConstantPool.getUtf8Entry(layout); |
|
897 |
requiredEntries.add(ne); |
|
898 |
requiredEntries.add(le); |
|
899 |
if (verbose > 0) { |
|
900 |
if (index < attrIndexLimit[i]) |
|
901 |
Utils.log.info("Using free flag bit 1<<"+index+" for "+count+" occurrences of "+def); |
|
902 |
else |
|
903 |
Utils.log.info("Using overflow index "+index+" for "+count+" occurrences of "+def); |
|
904 |
} |
|
905 |
} |
|
906 |
} |
|
907 |
} |
|
908 |
// Later, when emitting attr_definition_bands, we will look at |
|
909 |
// attrDefSeen and attrDefs at position 32/63 and beyond. |
|
910 |
// The attrIndexTable will provide elements of xxx_attr_indexes bands. |
|
911 |
||
912 |
// Done with scratch variables: |
|
913 |
maxFlags = null; |
|
914 |
allLayouts = null; |
|
915 |
} |
|
916 |
||
917 |
// Scratch variables for processing attributes and flags. |
|
918 |
int[] maxFlags; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
919 |
List<Map<Attribute.Layout, int[]>> allLayouts; |
2 | 920 |
|
921 |
void visitAttributeLayoutsIn(int ctype, Attribute.Holder h) { |
|
922 |
// Make note of which flags appear in the class file. |
|
923 |
// Set them in maxFlags. |
|
924 |
maxFlags[ctype] |= h.flags; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
925 |
for (Attribute a : h.getAttributes()) { |
2 | 926 |
Attribute.Layout def = a.layout(); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
927 |
Map<Attribute.Layout, int[]> defMap = allLayouts.get(ctype); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
928 |
int[] count = defMap.get(def); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
929 |
if (count == null) { |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
930 |
defMap.put(def, count = new int[1]); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
931 |
} |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
932 |
if (count[0] < Integer.MAX_VALUE) { |
2 | 933 |
count[0] += 1; |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
934 |
} |
2 | 935 |
} |
936 |
} |
|
937 |
||
938 |
Attribute.Layout[] attrDefsWritten; |
|
939 |
||
940 |
void writeAttrDefs() throws IOException { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
941 |
List<Object[]> defList = new ArrayList<>(); |
2 | 942 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) { |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
943 |
int limit = attrDefs.get(i).size(); |
2 | 944 |
for (int j = 0; j < limit; j++) { |
945 |
int header = i; // ctype |
|
946 |
if (j < attrIndexLimit[i]) { |
|
947 |
header |= ((j + ADH_BIT_IS_LSB) << ADH_BIT_SHIFT); |
|
948 |
assert(header < 0x100); // must fit into a byte |
|
949 |
// (...else header is simply ctype, with zero high bits.) |
|
950 |
if (!testBit(attrDefSeen[i], 1L<<j)) { |
|
951 |
// either undefined or predefined; nothing to write |
|
952 |
continue; |
|
953 |
} |
|
954 |
} |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
955 |
Attribute.Layout def = attrDefs.get(i).get(j); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
956 |
defList.add(new Object[]{ Integer.valueOf(header), def }); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
957 |
assert(Integer.valueOf(j).equals(attrIndexTable.get(def))); |
2 | 958 |
} |
959 |
} |
|
960 |
// Sort the new attr defs into some "natural" order. |
|
961 |
int numAttrDefs = defList.size(); |
|
962 |
Object[][] defs = new Object[numAttrDefs][]; |
|
963 |
defList.toArray(defs); |
|
10115 | 964 |
Arrays.sort(defs, new Comparator<Object[]>() { |
965 |
public int compare(Object[] a0, Object[] a1) { |
|
2 | 966 |
// Primary sort key is attr def header. |
10115 | 967 |
@SuppressWarnings("unchecked") |
2 | 968 |
int r = ((Comparable)a0[0]).compareTo(a1[0]); |
969 |
if (r != 0) return r; |
|
10115 | 970 |
Integer ind0 = attrIndexTable.get(a0[1]); |
971 |
Integer ind1 = attrIndexTable.get(a1[1]); |
|
2 | 972 |
// Secondary sort key is attribute index. |
973 |
// (This must be so, in order to keep overflow attr order.) |
|
974 |
assert(ind0 != null); |
|
975 |
assert(ind1 != null); |
|
10115 | 976 |
return ind0.compareTo(ind1); |
2 | 977 |
} |
978 |
}); |
|
979 |
attrDefsWritten = new Attribute.Layout[numAttrDefs]; |
|
8803
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
980 |
try (PrintStream dump = !optDumpBands ? null |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
981 |
: new PrintStream(getDumpStream(attr_definition_headers, ".def"))) |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
982 |
{ |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
983 |
int[] indexForDebug = Arrays.copyOf(attrIndexLimit, ATTR_CONTEXT_LIMIT); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
984 |
for (int i = 0; i < defs.length; i++) { |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
985 |
int header = ((Integer)defs[i][0]).intValue(); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
986 |
Attribute.Layout def = (Attribute.Layout) defs[i][1]; |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
987 |
attrDefsWritten[i] = def; |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
988 |
assert((header & ADH_CONTEXT_MASK) == def.ctype()); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
989 |
attr_definition_headers.putByte(header); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
990 |
attr_definition_name.putRef(ConstantPool.getUtf8Entry(def.name())); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
991 |
String layout = def.layoutForPackageMajver(getPackageMajver()); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
992 |
attr_definition_layout.putRef(ConstantPool.getUtf8Entry(layout)); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
993 |
// Check that we are transmitting that correct attribute index: |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
994 |
boolean debug = false; |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
995 |
assert(debug = true); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
996 |
if (debug) { |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
997 |
int hdrIndex = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB; |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
998 |
if (hdrIndex < 0) hdrIndex = indexForDebug[def.ctype()]++; |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
999 |
int realIndex = (attrIndexTable.get(def)).intValue(); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
1000 |
assert(hdrIndex == realIndex); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
1001 |
} |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
1002 |
if (dump != null) { |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
1003 |
int index = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB; |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
1004 |
dump.println(index+" "+def); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
8570
diff
changeset
|
1005 |
} |
2 | 1006 |
} |
1007 |
} |
|
1008 |
} |
|
1009 |
||
1010 |
void writeAttrCounts() throws IOException { |
|
1011 |
// Write the four xxx_attr_calls bands. |
|
1012 |
for (int ctype = 0; ctype < ATTR_CONTEXT_LIMIT; ctype++) { |
|
1013 |
MultiBand xxx_attr_bands = attrBands[ctype]; |
|
1014 |
IntBand xxx_attr_calls = getAttrBand(xxx_attr_bands, AB_ATTR_CALLS); |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1015 |
Attribute.Layout[] defs = new Attribute.Layout[attrDefs.get(ctype).size()]; |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1016 |
attrDefs.get(ctype).toArray(defs); |
2 | 1017 |
for (boolean predef = true; ; predef = false) { |
1018 |
for (int ai = 0; ai < defs.length; ai++) { |
|
1019 |
Attribute.Layout def = defs[ai]; |
|
1020 |
if (def == null) continue; // unused index |
|
1021 |
if (predef != isPredefinedAttr(ctype, ai)) |
|
1022 |
continue; // wrong pass |
|
1023 |
int totalCount = attrCounts[ctype][ai]; |
|
1024 |
if (totalCount == 0) |
|
1025 |
continue; // irrelevant |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1026 |
int[] bc = backCountTable.get(def); |
2 | 1027 |
for (int j = 0; j < bc.length; j++) { |
1028 |
if (bc[j] >= 0) { |
|
1029 |
int backCount = bc[j]; |
|
1030 |
bc[j] = -1; // close out; do not collect further counts |
|
1031 |
xxx_attr_calls.putInt(backCount); |
|
1032 |
assert(def.getCallables()[j].flagTest(Attribute.EF_BACK)); |
|
1033 |
} else { |
|
1034 |
assert(!def.getCallables()[j].flagTest(Attribute.EF_BACK)); |
|
1035 |
} |
|
1036 |
} |
|
1037 |
} |
|
1038 |
if (!predef) break; |
|
1039 |
} |
|
1040 |
} |
|
1041 |
} |
|
1042 |
||
1043 |
void trimClassAttributes() { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1044 |
for (Class cls : pkg.classes) { |
2 | 1045 |
// Replace "obvious" SourceFile attrs by null. |
1046 |
cls.minimizeSourceFile(); |
|
12544 | 1047 |
// BootstrapMethods should never have been inserted. |
1048 |
assert(cls.getAttribute(Package.attrBootstrapMethodsEmpty) == null); |
|
2 | 1049 |
} |
1050 |
} |
|
1051 |
||
1052 |
void collectInnerClasses() { |
|
1053 |
// Capture inner classes, removing them from individual classes. |
|
1054 |
// Irregular inner classes must stay local, though. |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1055 |
Map<ClassEntry, InnerClass> allICMap = new HashMap<>(); |
2 | 1056 |
// First, collect a consistent global set. |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1057 |
for (Class cls : pkg.classes) { |
2 | 1058 |
if (!cls.hasInnerClasses()) continue; |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1059 |
for (InnerClass ic : cls.getInnerClasses()) { |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1060 |
InnerClass pic = allICMap.put(ic.thisClass, ic); |
2 | 1061 |
if (pic != null && !pic.equals(ic) && pic.predictable) { |
1062 |
// Different ICs. Choose the better to make global. |
|
1063 |
allICMap.put(pic.thisClass, pic); |
|
1064 |
} |
|
1065 |
} |
|
1066 |
} |
|
1067 |
||
1068 |
InnerClass[] allICs = new InnerClass[allICMap.size()]; |
|
1069 |
allICMap.values().toArray(allICs); |
|
1070 |
allICMap = null; // done with it |
|
1071 |
||
1072 |
// Note: The InnerClasses attribute must be in a valid order, |
|
1073 |
// so that A$B always occurs earlier than A$B$C. This is an |
|
1074 |
// important side-effect of sorting lexically by class name. |
|
1075 |
Arrays.sort(allICs); // put in canonical order |
|
1076 |
pkg.setAllInnerClasses(Arrays.asList(allICs)); |
|
1077 |
||
1078 |
// Next, empty out of every local set the consistent entries. |
|
1079 |
// Calculate whether there is any remaining need to have a local |
|
1080 |
// set, and whether it needs to be locked. |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1081 |
for (Class cls : pkg.classes) { |
2 | 1082 |
cls.minimizeLocalICs(); |
1083 |
} |
|
1084 |
} |
|
1085 |
||
1086 |
void writeInnerClasses() throws IOException { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1087 |
for (InnerClass ic : pkg.getAllInnerClasses()) { |
2 | 1088 |
int flags = ic.flags; |
1089 |
assert((flags & ACC_IC_LONG_FORM) == 0); |
|
1090 |
if (!ic.predictable) { |
|
1091 |
flags |= ACC_IC_LONG_FORM; |
|
1092 |
} |
|
1093 |
ic_this_class.putRef(ic.thisClass); |
|
1094 |
ic_flags.putInt(flags); |
|
1095 |
if (!ic.predictable) { |
|
1096 |
ic_outer_class.putRef(ic.outerClass); |
|
1097 |
ic_name.putRef(ic.name); |
|
1098 |
} |
|
1099 |
} |
|
1100 |
} |
|
1101 |
||
1102 |
/** If there are any extra InnerClasses entries to write which are |
|
1103 |
* not already implied by the global table, put them into a |
|
1104 |
* local attribute. This is expected to be rare. |
|
1105 |
*/ |
|
1106 |
void writeLocalInnerClasses(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
|
1107 |
List<InnerClass> localICs = cls.getInnerClasses(); |
2 | 1108 |
class_InnerClasses_N.putInt(localICs.size()); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1109 |
for(InnerClass ic : localICs) { |
2 | 1110 |
class_InnerClasses_RC.putRef(ic.thisClass); |
1111 |
// Is it redundant with the global version? |
|
1112 |
if (ic.equals(pkg.getGlobalInnerClass(ic.thisClass))) { |
|
1113 |
// A zero flag means copy a global IC here. |
|
1114 |
class_InnerClasses_F.putInt(0); |
|
1115 |
} else { |
|
1116 |
int flags = ic.flags; |
|
1117 |
if (flags == 0) |
|
1118 |
flags = ACC_IC_LONG_FORM; // force it to be non-zero |
|
1119 |
class_InnerClasses_F.putInt(flags); |
|
1120 |
class_InnerClasses_outer_RCN.putRef(ic.outerClass); |
|
1121 |
class_InnerClasses_name_RUN.putRef(ic.name); |
|
1122 |
} |
|
1123 |
} |
|
1124 |
} |
|
1125 |
||
1126 |
void writeClassesAndByteCodes() throws IOException { |
|
1127 |
Class[] classes = new Class[pkg.classes.size()]; |
|
1128 |
pkg.classes.toArray(classes); |
|
1129 |
// Note: This code respects the order in which caller put classes. |
|
1130 |
if (verbose > 0) |
|
1131 |
Utils.log.info(" ...scanning "+classes.length+" classes..."); |
|
1132 |
||
1133 |
int nwritten = 0; |
|
1134 |
for (int i = 0; i < classes.length; i++) { |
|
1135 |
// Collect the class body, sans bytecodes. |
|
1136 |
Class cls = classes[i]; |
|
1137 |
if (verbose > 1) |
|
1138 |
Utils.log.fine("Scanning "+cls); |
|
1139 |
||
1140 |
ClassEntry thisClass = cls.thisClass; |
|
1141 |
ClassEntry superClass = cls.superClass; |
|
1142 |
ClassEntry[] interfaces = cls.interfaces; |
|
1143 |
// Encode rare case of null superClass as thisClass: |
|
1144 |
assert(superClass != thisClass); // bad class file!? |
|
1145 |
if (superClass == null) superClass = thisClass; |
|
1146 |
class_this.putRef(thisClass); |
|
1147 |
class_super.putRef(superClass); |
|
1148 |
class_interface_count.putInt(cls.interfaces.length); |
|
1149 |
for (int j = 0; j < interfaces.length; j++) { |
|
1150 |
class_interface.putRef(interfaces[j]); |
|
1151 |
} |
|
1152 |
||
1153 |
writeMembers(cls); |
|
1154 |
writeAttrs(ATTR_CONTEXT_CLASS, cls, cls); |
|
1155 |
||
1156 |
nwritten++; |
|
1157 |
if (verbose > 0 && (nwritten % 1000) == 0) |
|
1158 |
Utils.log.info("Have scanned "+nwritten+" classes..."); |
|
1159 |
} |
|
1160 |
} |
|
1161 |
||
1162 |
void writeMembers(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
|
1163 |
List<Class.Field> fields = cls.getFields(); |
2 | 1164 |
class_field_count.putInt(fields.size()); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1165 |
for (Class.Field f : fields) { |
2 | 1166 |
field_descr.putRef(f.getDescriptor()); |
1167 |
writeAttrs(ATTR_CONTEXT_FIELD, f, cls); |
|
1168 |
} |
|
1169 |
||
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1170 |
List<Class.Method> methods = cls.getMethods(); |
2 | 1171 |
class_method_count.putInt(methods.size()); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1172 |
for (Class.Method m : methods) { |
2 | 1173 |
method_descr.putRef(m.getDescriptor()); |
1174 |
writeAttrs(ATTR_CONTEXT_METHOD, m, cls); |
|
1175 |
assert((m.code != null) == (m.getAttribute(attrCodeEmpty) != null)); |
|
1176 |
if (m.code != null) { |
|
1177 |
writeCodeHeader(m.code); |
|
1178 |
writeByteCodes(m.code); |
|
1179 |
} |
|
1180 |
} |
|
1181 |
} |
|
1182 |
||
1183 |
void writeCodeHeader(Code c) throws IOException { |
|
1184 |
boolean attrsOK = testBit(archiveOptions, AO_HAVE_ALL_CODE_FLAGS); |
|
1185 |
int na = c.attributeSize(); |
|
1186 |
int sc = shortCodeHeader(c); |
|
1187 |
if (!attrsOK && na > 0) |
|
1188 |
// We must write flags, and can only do so for long headers. |
|
1189 |
sc = LONG_CODE_HEADER; |
|
1190 |
if (verbose > 2) { |
|
1191 |
int siglen = c.getMethod().getArgumentSize(); |
|
1192 |
Utils.log.fine("Code sizes info "+c.max_stack+" "+c.max_locals+" "+c.getHandlerCount()+" "+siglen+" "+na+(sc > 0 ? " SHORT="+sc : "")); |
|
1193 |
} |
|
1194 |
code_headers.putByte(sc); |
|
1195 |
if (sc == LONG_CODE_HEADER) { |
|
1196 |
code_max_stack.putInt(c.getMaxStack()); |
|
1197 |
code_max_na_locals.putInt(c.getMaxNALocals()); |
|
1198 |
code_handler_count.putInt(c.getHandlerCount()); |
|
1199 |
} else { |
|
1200 |
assert(attrsOK || na == 0); |
|
1201 |
assert(c.getHandlerCount() < shortCodeHeader_h_limit); |
|
1202 |
} |
|
1203 |
writeCodeHandlers(c); |
|
1204 |
if (sc == LONG_CODE_HEADER || attrsOK) |
|
1205 |
writeAttrs(ATTR_CONTEXT_CODE, c, c.thisClass()); |
|
1206 |
} |
|
1207 |
||
1208 |
void writeCodeHandlers(Code c) throws IOException { |
|
1209 |
int sum, del; |
|
1210 |
for (int j = 0, jmax = c.getHandlerCount(); j < jmax; j++) { |
|
1211 |
code_handler_class_RCN.putRef(c.handler_class[j]); // null OK |
|
1212 |
// Encode end as offset from start, and catch as offset from end, |
|
1213 |
// because they are strongly correlated. |
|
1214 |
sum = c.encodeBCI(c.handler_start[j]); |
|
1215 |
code_handler_start_P.putInt(sum); |
|
1216 |
del = c.encodeBCI(c.handler_end[j]) - sum; |
|
1217 |
code_handler_end_PO.putInt(del); |
|
1218 |
sum += del; |
|
1219 |
del = c.encodeBCI(c.handler_catch[j]) - sum; |
|
1220 |
code_handler_catch_PO.putInt(del); |
|
1221 |
} |
|
1222 |
} |
|
1223 |
||
1224 |
// Generic routines for writing attributes and flags of |
|
1225 |
// classes, fields, methods, and codes. |
|
1226 |
void writeAttrs(int ctype, |
|
1227 |
final Attribute.Holder h, |
|
1228 |
Class cls) throws IOException { |
|
1229 |
MultiBand xxx_attr_bands = attrBands[ctype]; |
|
1230 |
IntBand xxx_flags_hi = getAttrBand(xxx_attr_bands, AB_FLAGS_HI); |
|
1231 |
IntBand xxx_flags_lo = getAttrBand(xxx_attr_bands, AB_FLAGS_LO); |
|
1232 |
boolean haveLongFlags = haveFlagsHi(ctype); |
|
1233 |
assert(attrIndexLimit[ctype] == (haveLongFlags? 63: 32)); |
|
1234 |
if (h.attributes == null) { |
|
1235 |
xxx_flags_lo.putInt(h.flags); // no extra bits to set here |
|
1236 |
if (haveLongFlags) |
|
1237 |
xxx_flags_hi.putInt(0); |
|
1238 |
return; |
|
1239 |
} |
|
1240 |
if (verbose > 3) |
|
1241 |
Utils.log.fine("Transmitting attrs for "+h+" flags="+Integer.toHexString(h.flags)); |
|
1242 |
||
1243 |
long flagMask = attrFlagMask[ctype]; // which flags are attr bits? |
|
1244 |
long flagsToAdd = 0; |
|
1245 |
int overflowCount = 0; |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1246 |
for (Attribute a : h.attributes) { |
2 | 1247 |
Attribute.Layout def = a.layout(); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1248 |
int index = (attrIndexTable.get(def)).intValue(); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1249 |
assert(attrDefs.get(ctype).get(index) == def); |
2 | 1250 |
if (verbose > 3) |
1251 |
Utils.log.fine("add attr @"+index+" "+a+" in "+h); |
|
1252 |
if (index < attrIndexLimit[ctype] && testBit(flagMask, 1L<<index)) { |
|
1253 |
if (verbose > 3) |
|
1254 |
Utils.log.fine("Adding flag bit 1<<"+index+" in "+Long.toHexString(flagMask)); |
|
1255 |
assert(!testBit(h.flags, 1L<<index)); |
|
1256 |
flagsToAdd |= (1L<<index); |
|
1257 |
flagMask -= (1L<<index); // do not use this bit twice here |
|
1258 |
} else { |
|
1259 |
// an overflow attr. |
|
1260 |
flagsToAdd |= (1L<<X_ATTR_OVERFLOW); |
|
1261 |
overflowCount += 1; |
|
1262 |
if (verbose > 3) |
|
1263 |
Utils.log.fine("Adding overflow attr #"+overflowCount); |
|
1264 |
IntBand xxx_attr_indexes = getAttrBand(xxx_attr_bands, AB_ATTR_INDEXES); |
|
1265 |
xxx_attr_indexes.putInt(index); |
|
1266 |
// System.out.println("overflow @"+index); |
|
1267 |
} |
|
1268 |
if (def.bandCount == 0) { |
|
1269 |
if (def == attrInnerClassesEmpty) { |
|
1270 |
// Special logic to write this attr. |
|
1271 |
writeLocalInnerClasses((Class) h); |
|
1272 |
continue; |
|
1273 |
} |
|
1274 |
// Empty attr; nothing more to write here. |
|
1275 |
continue; |
|
1276 |
} |
|
1277 |
assert(a.fixups == null); |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1278 |
final Band[] ab = attrBandTable.get(def); |
2 | 1279 |
assert(ab != null); |
1280 |
assert(ab.length == def.bandCount); |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1281 |
final int[] bc = backCountTable.get(def); |
2 | 1282 |
assert(bc != null); |
1283 |
assert(bc.length == def.getCallables().length); |
|
1284 |
// Write one attribute of type def into ab. |
|
1285 |
if (verbose > 2) Utils.log.fine("writing "+a+" in "+h); |
|
1286 |
boolean isCV = (ctype == ATTR_CONTEXT_FIELD && def == attrConstantValue); |
|
1287 |
if (isCV) setConstantValueIndex((Class.Field)h); |
|
1288 |
a.parse(cls, a.bytes(), 0, a.size(), |
|
1289 |
new Attribute.ValueStream() { |
|
1290 |
public void putInt(int bandIndex, int value) { |
|
1291 |
((IntBand) ab[bandIndex]).putInt(value); |
|
1292 |
} |
|
1293 |
public void putRef(int bandIndex, Entry ref) { |
|
1294 |
((CPRefBand) ab[bandIndex]).putRef(ref); |
|
1295 |
} |
|
1296 |
public int encodeBCI(int bci) { |
|
1297 |
Code code = (Code) h; |
|
1298 |
return code.encodeBCI(bci); |
|
1299 |
} |
|
1300 |
public void noteBackCall(int whichCallable) { |
|
1301 |
assert(bc[whichCallable] >= 0); |
|
1302 |
bc[whichCallable] += 1; |
|
1303 |
} |
|
1304 |
}); |
|
1305 |
if (isCV) setConstantValueIndex(null); // clean up |
|
1306 |
} |
|
1307 |
||
1308 |
if (overflowCount > 0) { |
|
1309 |
IntBand xxx_attr_count = getAttrBand(xxx_attr_bands, AB_ATTR_COUNT); |
|
1310 |
xxx_attr_count.putInt(overflowCount); |
|
1311 |
} |
|
1312 |
||
1313 |
xxx_flags_lo.putInt(h.flags | (int)flagsToAdd); |
|
1314 |
if (haveLongFlags) |
|
1315 |
xxx_flags_hi.putInt((int)(flagsToAdd >>> 32)); |
|
1316 |
else |
|
1317 |
assert((flagsToAdd >>> 32) == 0); |
|
1318 |
assert((h.flags & flagsToAdd) == 0) |
|
1319 |
: (h+".flags=" |
|
1320 |
+Integer.toHexString(h.flags)+"^" |
|
1321 |
+Long.toHexString(flagsToAdd)); |
|
1322 |
} |
|
1323 |
||
1324 |
// temporary scratch variables for processing code blocks |
|
1325 |
private Code curCode; |
|
1326 |
private Class curClass; |
|
1327 |
private Entry[] curCPMap; |
|
1328 |
private void beginCode(Code c) { |
|
1329 |
assert(curCode == null); |
|
1330 |
curCode = c; |
|
1331 |
curClass = c.m.thisClass(); |
|
1332 |
curCPMap = c.getCPMap(); |
|
1333 |
} |
|
1334 |
private void endCode() { |
|
1335 |
curCode = null; |
|
1336 |
curClass = null; |
|
1337 |
curCPMap = null; |
|
1338 |
} |
|
1339 |
||
1340 |
// Return an _invokeinit_op variant, if the instruction matches one, |
|
1341 |
// else -1. |
|
1342 |
private int initOpVariant(Instruction i, Entry newClass) { |
|
1343 |
if (i.getBC() != _invokespecial) return -1; |
|
1344 |
MemberEntry ref = (MemberEntry) i.getCPRef(curCPMap); |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1345 |
if ("<init>".equals(ref.descRef.nameRef.stringValue()) == false) |
2 | 1346 |
return -1; |
1347 |
ClassEntry refClass = ref.classRef; |
|
1348 |
if (refClass == curClass.thisClass) |
|
1349 |
return _invokeinit_op+_invokeinit_self_option; |
|
1350 |
if (refClass == curClass.superClass) |
|
1351 |
return _invokeinit_op+_invokeinit_super_option; |
|
1352 |
if (refClass == newClass) |
|
1353 |
return _invokeinit_op+_invokeinit_new_option; |
|
1354 |
return -1; |
|
1355 |
} |
|
1356 |
||
1357 |
// Return a _self_linker_op variant, if the instruction matches one, |
|
1358 |
// else -1. |
|
1359 |
private int selfOpVariant(Instruction i) { |
|
1360 |
int bc = i.getBC(); |
|
1361 |
if (!(bc >= _first_linker_op && bc <= _last_linker_op)) return -1; |
|
1362 |
MemberEntry ref = (MemberEntry) i.getCPRef(curCPMap); |
|
1363 |
ClassEntry refClass = ref.classRef; |
|
1364 |
int self_bc = _self_linker_op + (bc - _first_linker_op); |
|
1365 |
if (refClass == curClass.thisClass) |
|
1366 |
return self_bc; |
|
1367 |
if (refClass == curClass.superClass) |
|
1368 |
return self_bc + _self_linker_super_flag; |
|
1369 |
return -1; |
|
1370 |
} |
|
1371 |
||
1372 |
void writeByteCodes(Code code) throws IOException { |
|
1373 |
beginCode(code); |
|
1374 |
IndexGroup cp = pkg.cp; |
|
1375 |
||
1376 |
// true if the previous instruction is an aload to absorb |
|
1377 |
boolean prevAload = false; |
|
1378 |
||
1379 |
// class of most recent new; helps compress <init> calls |
|
1380 |
Entry newClass = null; |
|
1381 |
||
1382 |
for (Instruction i = code.instructionAt(0); i != null; i = i.next()) { |
|
1383 |
// %%% Add a stress mode which issues _ref/_byte_escape. |
|
1384 |
if (verbose > 3) Utils.log.fine(i.toString()); |
|
1385 |
||
12544 | 1386 |
if (i.isNonstandard()) { |
2 | 1387 |
// Crash and burn with a complaint if there are funny |
1388 |
// bytecodes in this class file. |
|
1389 |
String complaint = code.getMethod() |
|
1390 |
+" contains an unrecognized bytecode "+i |
|
1391 |
+"; please use the pass-file option on this class."; |
|
1392 |
Utils.log.warning(complaint); |
|
1393 |
throw new IOException(complaint); |
|
1394 |
} |
|
1395 |
||
1396 |
if (i.isWide()) { |
|
1397 |
if (verbose > 1) { |
|
1398 |
Utils.log.fine("_wide opcode in "+code); |
|
1399 |
Utils.log.fine(i.toString()); |
|
1400 |
} |
|
1401 |
bc_codes.putByte(_wide); |
|
1402 |
codeHist[_wide]++; |
|
1403 |
} |
|
1404 |
||
1405 |
int bc = i.getBC(); |
|
1406 |
||
1407 |
// Begin "bc_linker" compression. |
|
1408 |
if (bc == _aload_0) { |
|
1409 |
// Try to group aload_0 with a following operation. |
|
1410 |
Instruction ni = code.instructionAt(i.getNextPC()); |
|
1411 |
if (selfOpVariant(ni) >= 0) { |
|
1412 |
prevAload = true; |
|
1413 |
continue; |
|
1414 |
} |
|
1415 |
} |
|
1416 |
||
1417 |
// Test for <init> invocations: |
|
1418 |
int init_bc = initOpVariant(i, newClass); |
|
1419 |
if (init_bc >= 0) { |
|
1420 |
if (prevAload) { |
|
1421 |
// get rid of it |
|
1422 |
bc_codes.putByte(_aload_0); |
|
1423 |
codeHist[_aload_0]++; |
|
1424 |
prevAload = false; //used up |
|
1425 |
} |
|
1426 |
// Write special bytecode. |
|
1427 |
bc_codes.putByte(init_bc); |
|
1428 |
codeHist[init_bc]++; |
|
1429 |
MemberEntry ref = (MemberEntry) i.getCPRef(curCPMap); |
|
1430 |
// Write operand to a separate band. |
|
1431 |
int coding = cp.getOverloadingIndex(ref); |
|
1432 |
bc_initref.putInt(coding); |
|
1433 |
continue; |
|
1434 |
} |
|
1435 |
||
1436 |
int self_bc = selfOpVariant(i); |
|
1437 |
if (self_bc >= 0) { |
|
1438 |
boolean isField = Instruction.isFieldOp(bc); |
|
1439 |
boolean isSuper = (self_bc >= _self_linker_op+_self_linker_super_flag); |
|
1440 |
boolean isAload = prevAload; |
|
1441 |
prevAload = false; //used up |
|
1442 |
if (isAload) |
|
1443 |
self_bc += _self_linker_aload_flag; |
|
1444 |
// Write special bytecode. |
|
1445 |
bc_codes.putByte(self_bc); |
|
1446 |
codeHist[self_bc]++; |
|
1447 |
// Write field or method ref to a separate band. |
|
1448 |
MemberEntry ref = (MemberEntry) i.getCPRef(curCPMap); |
|
1449 |
CPRefBand bc_which = selfOpRefBand(self_bc); |
|
1450 |
Index which_ix = cp.getMemberIndex(ref.tag, ref.classRef); |
|
1451 |
bc_which.putRef(ref, which_ix); |
|
1452 |
continue; |
|
1453 |
} |
|
1454 |
assert(!prevAload); |
|
1455 |
// End "bc_linker" compression. |
|
1456 |
||
1457 |
// Normal bytecode. |
|
1458 |
codeHist[bc]++; |
|
1459 |
switch (bc) { |
|
1460 |
case _tableswitch: // apc: (df, lo, hi, (hi-lo+1)*(label)) |
|
1461 |
case _lookupswitch: // apc: (df, nc, nc*(case, label)) |
|
1462 |
bc_codes.putByte(bc); |
|
1463 |
Instruction.Switch isw = (Instruction.Switch) i; |
|
1464 |
// Note that we do not write the alignment bytes. |
|
1465 |
int apc = isw.getAlignedPC(); |
|
1466 |
int npc = isw.getNextPC(); |
|
1467 |
// write a length specification into the bytecode stream |
|
1468 |
int caseCount = isw.getCaseCount(); |
|
1469 |
bc_case_count.putInt(caseCount); |
|
1470 |
putLabel(bc_label, code, i.getPC(), isw.getDefaultLabel()); |
|
1471 |
for (int j = 0; j < caseCount; j++) { |
|
1472 |
putLabel(bc_label, code, i.getPC(), isw.getCaseLabel(j)); |
|
1473 |
} |
|
1474 |
// Transmit case values in their own band. |
|
1475 |
if (bc == _tableswitch) { |
|
1476 |
bc_case_value.putInt(isw.getCaseValue(0)); |
|
1477 |
} else { |
|
1478 |
for (int j = 0; j < caseCount; j++) { |
|
1479 |
bc_case_value.putInt(isw.getCaseValue(j)); |
|
1480 |
} |
|
1481 |
} |
|
1482 |
// Done with the switch. |
|
1483 |
continue; |
|
1484 |
} |
|
1485 |
||
1486 |
int branch = i.getBranchLabel(); |
|
1487 |
if (branch >= 0) { |
|
1488 |
bc_codes.putByte(bc); |
|
1489 |
putLabel(bc_label, code, i.getPC(), branch); |
|
1490 |
continue; |
|
1491 |
} |
|
1492 |
Entry ref = i.getCPRef(curCPMap); |
|
1493 |
if (ref != null) { |
|
1494 |
if (bc == _new) newClass = ref; |
|
1495 |
if (bc == _ldc) ldcHist[ref.tag]++; |
|
1496 |
CPRefBand bc_which; |
|
1497 |
int vbc = bc; |
|
1498 |
switch (i.getCPTag()) { |
|
12544 | 1499 |
case CONSTANT_LoadableValue: |
2 | 1500 |
switch (ref.tag) { |
1501 |
case CONSTANT_Integer: |
|
1502 |
bc_which = bc_intref; |
|
1503 |
switch (bc) { |
|
1504 |
case _ldc: vbc = _ildc; break; |
|
1505 |
case _ldc_w: vbc = _ildc_w; break; |
|
1506 |
default: assert(false); |
|
1507 |
} |
|
1508 |
break; |
|
1509 |
case CONSTANT_Float: |
|
1510 |
bc_which = bc_floatref; |
|
1511 |
switch (bc) { |
|
1512 |
case _ldc: vbc = _fldc; break; |
|
1513 |
case _ldc_w: vbc = _fldc_w; break; |
|
1514 |
default: assert(false); |
|
1515 |
} |
|
1516 |
break; |
|
1517 |
case CONSTANT_Long: |
|
1518 |
bc_which = bc_longref; |
|
1519 |
assert(bc == _ldc2_w); |
|
1520 |
vbc = _lldc2_w; |
|
1521 |
break; |
|
1522 |
case CONSTANT_Double: |
|
1523 |
bc_which = bc_doubleref; |
|
1524 |
assert(bc == _ldc2_w); |
|
1525 |
vbc = _dldc2_w; |
|
1526 |
break; |
|
1527 |
case CONSTANT_String: |
|
1528 |
bc_which = bc_stringref; |
|
1529 |
switch (bc) { |
|
12544 | 1530 |
case _ldc: vbc = _sldc; break; |
1531 |
case _ldc_w: vbc = _sldc_w; break; |
|
2 | 1532 |
default: assert(false); |
1533 |
} |
|
1534 |
break; |
|
1535 |
case CONSTANT_Class: |
|
1536 |
bc_which = bc_classref; |
|
1537 |
switch (bc) { |
|
1538 |
case _ldc: vbc = _cldc; break; |
|
1539 |
case _ldc_w: vbc = _cldc_w; break; |
|
1540 |
default: assert(false); |
|
1541 |
} |
|
1542 |
break; |
|
1543 |
default: |
|
12544 | 1544 |
// CONSTANT_MethodHandle, etc. |
1545 |
if (getPackageMajver() < JAVA7_PACKAGE_MAJOR_VERSION) { |
|
1546 |
throw new IOException("bad package major version for Java 7 ldc"); |
|
1547 |
} |
|
1548 |
bc_which = bc_loadablevalueref; |
|
1549 |
switch (bc) { |
|
1550 |
case _ldc: vbc = _qldc; break; |
|
1551 |
case _ldc_w: vbc = _qldc_w; break; |
|
1552 |
default: assert(false); |
|
1553 |
} |
|
2 | 1554 |
} |
1555 |
break; |
|
1556 |
case CONSTANT_Class: |
|
1557 |
// Use a special shorthand for the current class: |
|
1558 |
if (ref == curClass.thisClass) ref = null; |
|
1559 |
bc_which = bc_classref; break; |
|
1560 |
case CONSTANT_Fieldref: |
|
1561 |
bc_which = bc_fieldref; break; |
|
1562 |
case CONSTANT_Methodref: |
|
1563 |
bc_which = bc_methodref; break; |
|
1564 |
case CONSTANT_InterfaceMethodref: |
|
1565 |
bc_which = bc_imethodref; break; |
|
12544 | 1566 |
case CONSTANT_InvokeDynamic: |
1567 |
bc_which = bc_indyref; break; |
|
2 | 1568 |
default: |
1569 |
bc_which = null; |
|
1570 |
assert(false); |
|
1571 |
} |
|
1572 |
bc_codes.putByte(vbc); |
|
1573 |
bc_which.putRef(ref); |
|
1574 |
// handle trailing junk |
|
1575 |
if (bc == _multianewarray) { |
|
1576 |
assert(i.getConstant() == code.getByte(i.getPC()+3)); |
|
1577 |
// Just dump the byte into the bipush pile |
|
1578 |
bc_byte.putByte(0xFF & i.getConstant()); |
|
1579 |
} else if (bc == _invokeinterface) { |
|
1580 |
assert(i.getLength() == 5); |
|
1581 |
// Make sure the discarded bytes are sane: |
|
1582 |
assert(i.getConstant() == (1+((MemberEntry)ref).descRef.typeRef.computeSize(true)) << 8); |
|
12544 | 1583 |
} else if (bc == _invokedynamic) { |
1584 |
if (getPackageMajver() < JAVA7_PACKAGE_MAJOR_VERSION) { |
|
1585 |
throw new IOException("bad package major version for Java 7 invokedynamic"); |
|
1586 |
} |
|
1587 |
assert(i.getLength() == 5); |
|
1588 |
assert(i.getConstant() == 0); // last 2 bytes MBZ |
|
2 | 1589 |
} else { |
1590 |
// Make sure there is nothing else to write. |
|
1591 |
assert(i.getLength() == ((bc == _ldc)?2:3)); |
|
1592 |
} |
|
1593 |
continue; |
|
1594 |
} |
|
1595 |
int slot = i.getLocalSlot(); |
|
1596 |
if (slot >= 0) { |
|
1597 |
bc_codes.putByte(bc); |
|
1598 |
bc_local.putInt(slot); |
|
1599 |
int con = i.getConstant(); |
|
1600 |
if (bc == _iinc) { |
|
1601 |
if (!i.isWide()) { |
|
1602 |
bc_byte.putByte(0xFF & con); |
|
1603 |
} else { |
|
1604 |
bc_short.putInt(0xFFFF & con); |
|
1605 |
} |
|
1606 |
} else { |
|
1607 |
assert(con == 0); |
|
1608 |
} |
|
1609 |
continue; |
|
1610 |
} |
|
1611 |
// Generic instruction. Copy the body. |
|
1612 |
bc_codes.putByte(bc); |
|
1613 |
int pc = i.getPC()+1; |
|
1614 |
int npc = i.getNextPC(); |
|
1615 |
if (pc < npc) { |
|
1616 |
// Do a few remaining multi-byte instructions. |
|
1617 |
switch (bc) { |
|
1618 |
case _sipush: |
|
1619 |
bc_short.putInt(0xFFFF & i.getConstant()); |
|
1620 |
break; |
|
1621 |
case _bipush: |
|
1622 |
bc_byte.putByte(0xFF & i.getConstant()); |
|
1623 |
break; |
|
1624 |
case _newarray: |
|
1625 |
bc_byte.putByte(0xFF & i.getConstant()); |
|
1626 |
break; |
|
1627 |
default: |
|
1628 |
assert(false); // that's it |
|
1629 |
} |
|
1630 |
} |
|
1631 |
} |
|
1632 |
bc_codes.putByte(_end_marker); |
|
1633 |
bc_codes.elementCountForDebug++; |
|
1634 |
codeHist[_end_marker]++; |
|
1635 |
endCode(); |
|
1636 |
} |
|
1637 |
||
1638 |
int[] codeHist = new int[1<<8]; |
|
1639 |
int[] ldcHist = new int[20]; |
|
1640 |
void printCodeHist() { |
|
1641 |
assert(verbose > 0); |
|
1642 |
String[] hist = new String[codeHist.length]; |
|
1643 |
int totalBytes = 0; |
|
1644 |
for (int bc = 0; bc < codeHist.length; bc++) { |
|
1645 |
totalBytes += codeHist[bc]; |
|
1646 |
} |
|
1647 |
for (int bc = 0; bc < codeHist.length; bc++) { |
|
1648 |
if (codeHist[bc] == 0) { hist[bc] = ""; continue; } |
|
1649 |
String iname = Instruction.byteName(bc); |
|
1650 |
String count = "" + codeHist[bc]; |
|
1651 |
count = " ".substring(count.length()) + count; |
|
1652 |
String pct = "" + (codeHist[bc] * 10000 / totalBytes); |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1653 |
while (pct.length() < 4) { |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1654 |
pct = "0" + pct; |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1655 |
} |
2 | 1656 |
pct = pct.substring(0, pct.length()-2) + "." + pct.substring(pct.length()-2); |
1657 |
hist[bc] = count + " " + pct + "% " + iname; |
|
1658 |
} |
|
1659 |
Arrays.sort(hist); |
|
1660 |
System.out.println("Bytecode histogram ["+totalBytes+"]"); |
|
1661 |
for (int i = hist.length; --i >= 0; ) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
1662 |
if ("".equals(hist[i])) continue; |
2 | 1663 |
System.out.println(hist[i]); |
1664 |
} |
|
1665 |
for (int tag = 0; tag < ldcHist.length; tag++) { |
|
1666 |
int count = ldcHist[tag]; |
|
1667 |
if (count == 0) continue; |
|
1668 |
System.out.println("ldc "+ConstantPool.tagName(tag)+" "+count); |
|
1669 |
} |
|
1670 |
} |
|
1671 |
} |