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