jdk/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java
author ksrini
Sun, 03 Mar 2013 20:52:04 -0800
changeset 16050 1eee624cddb3
parent 16013 3569e84e7429
child 17490 46864558d068
permissions -rw-r--r--
8007297: [pack200] allow opcodes with InterfaceMethodRefs Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12857
diff changeset
     2
 * Copyright (c) 2001, 2013, 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
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    28
import com.sun.java.util.jar.pack.ConstantPool.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.java.util.jar.pack.Package.Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.java.util.jar.pack.Package.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.java.util.jar.pack.Package.InnerClass;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    32
import java.io.ByteArrayOutputStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    33
import java.io.EOFException;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    34
import java.io.PrintStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    35
import java.io.FilterInputStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    36
import java.io.BufferedInputStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    37
import java.io.InputStream;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    38
import java.io.IOException;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    39
import java.util.ArrayList;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    40
import java.util.Map;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    41
import java.util.Arrays;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    42
import java.util.Collection;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    43
import java.util.Collections;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    44
import java.util.Comparator;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    45
import java.util.HashSet;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    46
import java.util.HashMap;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    47
import java.util.Iterator;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    48
import java.util.List;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    49
import java.util.ListIterator;
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
 * Reader for a package file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see PackageWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
class PackageReader extends BandStructure {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    Package pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    LimitedBuffer in;
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
    63
    Package.Version packageVersion;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    PackageReader(Package pkg, InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.pkg = pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.in = new LimitedBuffer(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /** A buffered input stream which is careful not to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *  read its underlying stream ahead of a given mark,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *  called the 'readLimit'.  This property declares
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *  the maximum number of characters that future reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *  can consume from the underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    class LimitedBuffer extends BufferedInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        long served;     // total number of charburgers served
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int  servedPos;  // ...as of this value of super.pos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        long limit;      // current declared limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        long buffered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public boolean atLimit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            boolean z = (getBytesServed() == limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            assert(!z || limit == buffered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return z;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public long getBytesServed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return served + (pos - servedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public void setReadLimit(long newLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (newLimit == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                limit = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                limit = getBytesServed() + newLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public long getReadLimit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (limit == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                return limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return limit - getBytesServed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (pos < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                // fast path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return buf[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            served += (pos - servedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            int ch = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            servedPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (ch >= 0)  served += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            assert(served <= limit || limit == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            served += (pos - servedPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int nr = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            servedPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (nr >= 0)  served += nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            assert(served <= limit || limit == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new RuntimeException("no skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        LimitedBuffer(InputStream originalIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            super(null, 1<<14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            servedPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            super.in = new FilterInputStream(originalIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    if (buffered == limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    ++buffered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    return super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    if (buffered == limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    if (limit != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        long remaining = limit - buffered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        if (len > remaining)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            len = (int)remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    int nr = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    if (nr >= 0)  buffered += nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    void read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            //  pack200_archive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            //        file_header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            //        *band_headers :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            //        cp_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            //        attr_definition_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            //        ic_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            //        class_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            //        bc_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            //        file_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            readFileHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            readBandHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            readConstantPool();  // cp_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            readAttrDefs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            readInnerClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            Class[] classes = readClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            readByteCodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            readFiles();     // file_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            assert(archiveSize1 == 0 || in.atLimit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            assert(archiveSize1 == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                   in.getBytesServed() == archiveSize0+archiveSize1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            all_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // As a post-pass, build constant pools and inner classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                reconstructClass(classes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (Exception ee) {
4919
b00f729ee70a 6925868: Eliminate pack200's dependency on logging
mchung
parents: 2
diff changeset
   183
            Utils.log.warning("Error on input: "+ee, ee);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                Utils.log.info("Stream offsets:"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                 " served="+in.getBytesServed()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                 " buffered="+in.buffered+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                 " limit="+in.limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            //if (verbose > 0)  ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (ee instanceof IOException)  throw (IOException)ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (ee instanceof RuntimeException)  throw (RuntimeException)ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new Error("error unpacking", ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // Temporary count values, until band decoding gets rolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    int[] tagCount = new int[CONSTANT_Limit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    int numFiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    int numAttrDefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    int numInnerClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    int numClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    void readFileHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        //  file_header:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        //        archive_magic archive_header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        readArchiveMagic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        readArchiveHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // Local routine used to parse fixed-format scalars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    // in the file_header:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private int getMagicInt32() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int res = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            res <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            res |= (archive_magic.getByte() & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    final static int MAGIC_BYTES = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    void readArchiveMagic() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // Read a minimum of bytes in the first gulp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        in.setReadLimit(MAGIC_BYTES + AH_LENGTH_MIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        //  archive_magic:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        //        #archive_magic_word :BYTE1[4]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        archive_magic.expectLength(MAGIC_BYTES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        archive_magic.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // read and check magic numbers:
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   233
        int magic = getMagicInt32();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   234
        if (pkg.magic != magic) {
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   235
            throw new IOException("Unexpected package magic number: got "
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   236
                    + magic + "; expected " + pkg.magic);
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   237
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        archive_magic.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   241
     // Fixed 6211177, converted to throw IOException
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   242
    void checkArchiveVersion() throws IOException {
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   243
        Package.Version versionFound = null;
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   244
        for (Package.Version v : new Package.Version[] {
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12857
diff changeset
   245
                JAVA8_PACKAGE_VERSION,
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   246
                JAVA7_PACKAGE_VERSION,
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   247
                JAVA6_PACKAGE_VERSION,
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   248
                JAVA5_PACKAGE_VERSION
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   249
            }) {
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   250
            if (packageVersion.equals(v)) {
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   251
                versionFound = v;
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   252
                break;
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   253
            }
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   254
        }
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   255
        if (versionFound == null) {
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12857
diff changeset
   256
            String expVer =   JAVA8_PACKAGE_VERSION.toString()
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12857
diff changeset
   257
                            + "OR"
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12857
diff changeset
   258
                            + JAVA7_PACKAGE_VERSION.toString()
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   259
                            + " OR "
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   260
                            + JAVA6_PACKAGE_VERSION.toString()
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   261
                            + " OR "
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   262
                            + JAVA5_PACKAGE_VERSION.toString();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   263
            throw new IOException("Unexpected package minor version: got "
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   264
                    +  packageVersion.toString() + "; expected " + expVer);
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   265
        }
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   266
    }
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   267
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    void readArchiveHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        //  archive_header:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        //        #archive_minver :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        //        #archive_majver :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        //        #archive_options :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        //        (archive_file_counts) ** (#have_file_headers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        //        (archive_special_counts) ** (#have_special_formats)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        //        cp_counts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        //        class_counts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        //  archive_file_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        //        #archive_size_hi :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        //        #archive_size_lo :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        //        #archive_next_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        //        #archive_modtime :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        //        #file_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        //  class_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        //        #ic_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        //        #default_class_minver :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        //        #default_class_majver :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        //        #class_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        //  archive_special_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        //        #band_headers_size :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        //        #attr_definition_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        archive_header_0.expectLength(AH_LENGTH_0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        archive_header_0.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   298
        int minver = archive_header_0.getInt();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   299
        int majver = archive_header_0.getInt();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   300
        packageVersion = Package.Version.of(majver, minver);
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   301
        checkArchiveVersion();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   302
        this.initHighestClassVersion(JAVA7_MAX_CLASS_VERSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        archiveOptions = archive_header_0.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        archive_header_0.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // detect archive optional fields in archive header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        boolean haveSpecial = testBit(archiveOptions, AO_HAVE_SPECIAL_FORMATS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        boolean haveFiles   = testBit(archiveOptions, AO_HAVE_FILE_HEADERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        boolean haveNumbers = testBit(archiveOptions, AO_HAVE_CP_NUMBERS);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   311
        boolean haveCPExtra = testBit(archiveOptions, AO_HAVE_CP_EXTRAS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        initAttrIndexLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // now we are ready to use the data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        archive_header_S.expectLength(haveFiles? AH_LENGTH_S: 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        archive_header_S.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (haveFiles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            long sizeHi = archive_header_S.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            long sizeLo = archive_header_S.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            archiveSize1 = (sizeHi << 32) + ((sizeLo << 32) >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            // Set the limit, now, up to the file_bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            in.setReadLimit(archiveSize1);  // for debug only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            archiveSize1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            in.setReadLimit(-1);  // remove limitation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        archive_header_S.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        archiveSize0 = in.getBytesServed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   330
        int remainingHeaders = AH_LENGTH_MIN - AH_LENGTH_0 - AH_LENGTH_S;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   331
        if (haveFiles)    remainingHeaders += AH_FILE_HEADER_LEN;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   332
        if (haveSpecial)  remainingHeaders += AH_SPECIAL_FORMAT_LEN;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   333
        if (haveNumbers)  remainingHeaders += AH_CP_NUMBER_LEN;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   334
        if (haveCPExtra)  remainingHeaders += AH_CP_EXTRA_LEN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        archive_header_1.expectLength(remainingHeaders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        archive_header_1.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (haveFiles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            archiveNextCount = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            pkg.default_modtime = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            numFiles = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            archiveNextCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            numFiles = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (haveSpecial) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            band_headers.expectLength(archive_header_1.getInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            numAttrDefs = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            band_headers.expectLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            numAttrDefs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   355
        readConstantPoolCounts(haveNumbers, haveCPExtra);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        numInnerClasses = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   359
        minver = (short) archive_header_1.getInt();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   360
        majver = (short) archive_header_1.getInt();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   361
        pkg.defaultClassVersion = Package.Version.of(majver, minver);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        numClasses = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        archive_header_1.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // set some derived archive bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (testBit(archiveOptions, AO_DEFLATE_HINT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            pkg.default_options |= FO_DEFLATE_HINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    void readBandHeaders() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        band_headers.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        bandHeaderBytePos = 1;  // Leave room to pushback the initial XB byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        bandHeaderBytes = new byte[bandHeaderBytePos + band_headers.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        for (int i = bandHeaderBytePos; i < bandHeaderBytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            bandHeaderBytes[i] = (byte) band_headers.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        band_headers.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   382
    void readConstantPoolCounts(boolean haveNumbers, boolean haveCPExtra) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // size the constant pools:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        for (int k = 0; k < ConstantPool.TAGS_IN_ORDER.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            //  cp_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            //        #cp_Utf8_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            //        (cp_number_counts) ** (#have_cp_numbers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            //        #cp_String_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            //        #cp_Class_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            //        #cp_Signature_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            //        #cp_Descr_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            //        #cp_Field_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            //        #cp_Method_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            //        #cp_Imethod_count :UNSIGNED5[1]
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   395
            //        (cp_attr_counts) ** (#have_cp_attr_counts)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            //  cp_number_counts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            //        #cp_Int_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            //        #cp_Float_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            //        #cp_Long_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            //        #cp_Double_count :UNSIGNED5[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            //
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   403
            //  cp_extra_counts:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   404
            //        #cp_MethodHandle_count :UNSIGNED5[1]
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   405
            //        #cp_MethodType_count :UNSIGNED5[1]
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   406
            //        #cp_InvokeDynamic_count :UNSIGNED5[1]
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   407
            //        #cp_BootstrapMethod_count :UNSIGNED5[1]
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   408
            //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            byte tag = ConstantPool.TAGS_IN_ORDER[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (!haveNumbers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // These four counts are optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                case CONSTANT_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                case CONSTANT_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                case CONSTANT_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                case CONSTANT_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   420
            if (!haveCPExtra) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   421
                // These four counts are optional.
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   422
                switch (tag) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   423
                case CONSTANT_MethodHandle:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   424
                case CONSTANT_MethodType:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   425
                case CONSTANT_InvokeDynamic:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   426
                case CONSTANT_BootstrapMethod:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   427
                    continue;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   428
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   429
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            tagCount[tag] = archive_header_1.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    protected Index getCPIndex(byte tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return pkg.cp.getIndexByTag(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    Index initCPIndex(byte tag, Entry[] cpMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                Utils.log.fine("cp.add "+cpMap[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        Index index = ConstantPool.makeIndex(ConstantPool.tagName(tag), cpMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (verbose > 1)  Utils.log.fine("Read "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        pkg.cp.initIndexByTag(tag, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   449
    void checkLegacy(String bandname) {
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   450
        if (packageVersion.lessThan(JAVA7_PACKAGE_VERSION)) {
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   451
            throw new RuntimeException("unexpected band " + bandname);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   452
        }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   453
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    void readConstantPool() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        //  cp_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        //        cp_Utf8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        //        *cp_Int :UDELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        //        *cp_Float :UDELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        //        cp_Long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        //        cp_Double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        //        *cp_String :UDELTA5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        //        *cp_Class :UDELTA5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        //        cp_Signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        //        cp_Descr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        //        cp_Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        //        cp_Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        //        cp_Imethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (verbose > 0)  Utils.log.info("Reading CP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        for (int k = 0; k < ConstantPool.TAGS_IN_ORDER.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            byte tag = ConstantPool.TAGS_IN_ORDER[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            int  len = tagCount[tag];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            Entry[] cpMap = new Entry[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                Utils.log.info("Reading "+cpMap.length+" "+ConstantPool.tagName(tag)+" entries...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            case CONSTANT_Utf8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                readUtf8Bands(cpMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            case CONSTANT_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                cp_Int.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                cp_Int.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    int x = cp_Int.getInt();  // coding handles signs OK
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   488
                    cpMap[i] = ConstantPool.getLiteralEntry(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                cp_Int.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            case CONSTANT_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                cp_Float.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                cp_Float.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    int x = cp_Float.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    float fx = Float.intBitsToFloat(x);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   498
                    cpMap[i] = ConstantPool.getLiteralEntry(fx);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                cp_Float.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            case CONSTANT_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                //  cp_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                //        *cp_Long_hi :UDELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                //        *cp_Long_lo :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                cp_Long_hi.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                cp_Long_hi.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                cp_Long_lo.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                cp_Long_lo.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    long hi = cp_Long_hi.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    long lo = cp_Long_lo.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    long x = (hi << 32) + ((lo << 32) >>> 32);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   514
                    cpMap[i] = ConstantPool.getLiteralEntry(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                cp_Long_hi.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                cp_Long_lo.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            case CONSTANT_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                //  cp_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                //        *cp_Double_hi :UDELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                //        *cp_Double_lo :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                cp_Double_hi.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                cp_Double_hi.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                cp_Double_lo.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                cp_Double_lo.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    long hi = cp_Double_hi.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    long lo = cp_Double_lo.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    long x = (hi << 32) + ((lo << 32) >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    double dx = Double.longBitsToDouble(x);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   532
                    cpMap[i] = ConstantPool.getLiteralEntry(dx);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                cp_Double_hi.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                cp_Double_lo.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            case CONSTANT_String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                cp_String.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                cp_String.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                cp_String.setIndex(getCPIndex(CONSTANT_Utf8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    cpMap[i] = ConstantPool.getLiteralEntry(cp_String.getRef().stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                cp_String.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            case CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                cp_Class.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                cp_Class.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                cp_Class.setIndex(getCPIndex(CONSTANT_Utf8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    cpMap[i] = ConstantPool.getClassEntry(cp_Class.getRef().stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                cp_Class.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            case CONSTANT_Signature:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                readSignatureBands(cpMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            case CONSTANT_NameandType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                //  cp_Descr:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                //        *cp_Descr_type :DELTA5  (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                //        *cp_Descr_name :UDELTA5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                cp_Descr_name.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                cp_Descr_name.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                cp_Descr_name.setIndex(getCPIndex(CONSTANT_Utf8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                cp_Descr_type.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                cp_Descr_type.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                cp_Descr_type.setIndex(getCPIndex(CONSTANT_Signature));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    Entry ref  = cp_Descr_name.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    Entry ref2 = cp_Descr_type.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    cpMap[i] = ConstantPool.getDescriptorEntry((Utf8Entry)ref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                                        (SignatureEntry)ref2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                cp_Descr_name.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                cp_Descr_type.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            case CONSTANT_Fieldref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                readMemberRefs(tag, cpMap, cp_Field_class, cp_Field_desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            case CONSTANT_Methodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                readMemberRefs(tag, cpMap, cp_Method_class, cp_Method_desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            case CONSTANT_InterfaceMethodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                readMemberRefs(tag, cpMap, cp_Imethod_class, cp_Imethod_desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                break;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   586
            case CONSTANT_MethodHandle:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   587
                if (cpMap.length > 0) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   588
                    checkLegacy(cp_MethodHandle_refkind.name());
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   589
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   590
                cp_MethodHandle_refkind.expectLength(cpMap.length);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   591
                cp_MethodHandle_refkind.readFrom(in);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   592
                cp_MethodHandle_member.expectLength(cpMap.length);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   593
                cp_MethodHandle_member.readFrom(in);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   594
                cp_MethodHandle_member.setIndex(getCPIndex(CONSTANT_AnyMember));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   595
                for (int i = 0; i < cpMap.length; i++) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   596
                    byte        refKind = (byte)        cp_MethodHandle_refkind.getInt();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   597
                    MemberEntry memRef  = (MemberEntry) cp_MethodHandle_member.getRef();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   598
                    cpMap[i] = ConstantPool.getMethodHandleEntry(refKind, memRef);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   599
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   600
                cp_MethodHandle_refkind.doneDisbursing();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   601
                cp_MethodHandle_member.doneDisbursing();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   602
                break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   603
            case CONSTANT_MethodType:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   604
                if (cpMap.length > 0) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   605
                    checkLegacy(cp_MethodType.name());
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   606
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   607
                cp_MethodType.expectLength(cpMap.length);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   608
                cp_MethodType.readFrom(in);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   609
                cp_MethodType.setIndex(getCPIndex(CONSTANT_Signature));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   610
                for (int i = 0; i < cpMap.length; i++) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   611
                    SignatureEntry typeRef  = (SignatureEntry) cp_MethodType.getRef();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   612
                    cpMap[i] = ConstantPool.getMethodTypeEntry(typeRef);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   613
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   614
                cp_MethodType.doneDisbursing();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   615
                break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   616
            case CONSTANT_InvokeDynamic:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   617
                if (cpMap.length > 0) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   618
                    checkLegacy(cp_InvokeDynamic_spec.name());
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   619
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   620
                cp_InvokeDynamic_spec.expectLength(cpMap.length);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   621
                cp_InvokeDynamic_spec.readFrom(in);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   622
                cp_InvokeDynamic_spec.setIndex(getCPIndex(CONSTANT_BootstrapMethod));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   623
                cp_InvokeDynamic_desc.expectLength(cpMap.length);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   624
                cp_InvokeDynamic_desc.readFrom(in);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   625
                cp_InvokeDynamic_desc.setIndex(getCPIndex(CONSTANT_NameandType));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   626
                for (int i = 0; i < cpMap.length; i++) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   627
                    BootstrapMethodEntry bss   = (BootstrapMethodEntry) cp_InvokeDynamic_spec.getRef();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   628
                    DescriptorEntry      descr = (DescriptorEntry)      cp_InvokeDynamic_desc.getRef();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   629
                    cpMap[i] = ConstantPool.getInvokeDynamicEntry(bss, descr);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   630
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   631
                cp_InvokeDynamic_spec.doneDisbursing();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   632
                cp_InvokeDynamic_desc.doneDisbursing();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   633
                break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   634
            case CONSTANT_BootstrapMethod:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   635
                if (cpMap.length > 0) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   636
                    checkLegacy(cp_BootstrapMethod_ref.name());
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   637
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   638
                cp_BootstrapMethod_ref.expectLength(cpMap.length);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   639
                cp_BootstrapMethod_ref.readFrom(in);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   640
                cp_BootstrapMethod_ref.setIndex(getCPIndex(CONSTANT_MethodHandle));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   641
                cp_BootstrapMethod_arg_count.expectLength(cpMap.length);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   642
                cp_BootstrapMethod_arg_count.readFrom(in);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   643
                int totalArgCount = cp_BootstrapMethod_arg_count.getIntTotal();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   644
                cp_BootstrapMethod_arg.expectLength(totalArgCount);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   645
                cp_BootstrapMethod_arg.readFrom(in);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   646
                cp_BootstrapMethod_arg.setIndex(getCPIndex(CONSTANT_LoadableValue));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   647
                for (int i = 0; i < cpMap.length; i++) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   648
                    MethodHandleEntry bsm = (MethodHandleEntry) cp_BootstrapMethod_ref.getRef();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   649
                    int argc = cp_BootstrapMethod_arg_count.getInt();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   650
                    Entry[] argRefs = new Entry[argc];
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   651
                    for (int j = 0; j < argc; j++) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   652
                        argRefs[j] = cp_BootstrapMethod_arg.getRef();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   653
                    }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   654
                    cpMap[i] = ConstantPool.getBootstrapMethodEntry(bsm, argRefs);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   655
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   656
                cp_BootstrapMethod_ref.doneDisbursing();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   657
                cp_BootstrapMethod_arg_count.doneDisbursing();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   658
                cp_BootstrapMethod_arg.doneDisbursing();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   659
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            default:
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   661
                throw new AssertionError("unexpected CP tag in package");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            Index index = initCPIndex(tag, cpMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            if (optDumpBands) {
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   667
                try (PrintStream ps = new PrintStream(getDumpStream(index, ".idx"))) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   668
                    printArrayTo(ps, index.cpMap, 0, index.cpMap.length);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   669
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        cp_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   675
        if (optDumpBands || verbose > 1) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   676
            for (byte tag = CONSTANT_GroupFirst; tag < CONSTANT_GroupLimit; tag++) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   677
                Index index = pkg.cp.getIndexByTag(tag);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   678
                if (index == null || index.isEmpty())  continue;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   679
                Entry[] cpMap = index.cpMap;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   680
                if (verbose > 1)
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   681
                    Utils.log.info("Index group "+ConstantPool.tagName(tag)+" contains "+cpMap.length+" entries.");
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   682
                if (optDumpBands) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   683
                    try (PrintStream ps = new PrintStream(getDumpStream(index.debugName, tag, ".gidx", index))) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   684
                        printArrayTo(ps, cpMap, 0, cpMap.length, true);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   685
                    }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   686
                }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   687
            }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   688
        }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   689
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        setBandIndexes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    void readUtf8Bands(Entry[] cpMap) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        //  cp_Utf8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        //        *cp_Utf8_prefix :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        //        *cp_Utf8_suffix :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        //        *cp_Utf8_chars :CHAR3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        //        *cp_Utf8_big_suffix :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        //        (*cp_Utf8_big_chars :DELTA5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        //          ** length(cp_Utf8_big_suffix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        int len = cpMap.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            return;  // nothing to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        // Bands have implicit leading zeroes, for the empty string:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        final int SUFFIX_SKIP_1 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        final int PREFIX_SKIP_2 = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // First band:  Read lengths of shared prefixes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        cp_Utf8_prefix.expectLength(Math.max(0, len - PREFIX_SKIP_2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        cp_Utf8_prefix.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // Second band:  Read lengths of unshared suffixes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        cp_Utf8_suffix.expectLength(Math.max(0, len - SUFFIX_SKIP_1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        cp_Utf8_suffix.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        char[][] suffixChars = new char[len][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        int bigSuffixCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        // Third band:  Read the char values in the unshared suffixes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        cp_Utf8_chars.expectLength(cp_Utf8_suffix.getIntTotal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        cp_Utf8_chars.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            if (suffix == 0 && i >= SUFFIX_SKIP_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                // chars are packed in cp_Utf8_big_chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                bigSuffixCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            suffixChars[i] = new char[suffix];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            for (int j = 0; j < suffix; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                int ch = cp_Utf8_chars.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                assert(ch == (char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                suffixChars[i][j] = (char)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        cp_Utf8_chars.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        // Fourth band:  Go back and size the specially packed strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        int maxChars = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        cp_Utf8_big_suffix.expectLength(bigSuffixCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        cp_Utf8_big_suffix.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        cp_Utf8_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            int prefix = (i < PREFIX_SKIP_2)? 0: cp_Utf8_prefix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            if (suffix == 0 && i >= SUFFIX_SKIP_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                assert(suffixChars[i] == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                suffix = cp_Utf8_big_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                assert(suffixChars[i] != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            if (maxChars < prefix + suffix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                maxChars = prefix + suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        char[] buf = new char[maxChars];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        // Fifth band(s):  Get the specially packed characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        cp_Utf8_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        cp_Utf8_big_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (i < SUFFIX_SKIP_1)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            int suffix = cp_Utf8_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (suffix != 0)  continue;  // already input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            suffix = cp_Utf8_big_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            suffixChars[i] = new char[suffix];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            if (suffix == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                // Do not bother to add an empty "(Utf8_big_0)" band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            IntBand packed = cp_Utf8_big_chars.newIntBand("(Utf8_big_"+i+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            packed.expectLength(suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            packed.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            for (int j = 0; j < suffix; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                int ch = packed.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                assert(ch == (char)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                suffixChars[i][j] = (char)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            packed.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        cp_Utf8_big_chars.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        // Finally, sew together all the prefixes and suffixes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        cp_Utf8_prefix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        cp_Utf8_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        cp_Utf8_big_suffix.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            int prefix = (i < PREFIX_SKIP_2)? 0: cp_Utf8_prefix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if (suffix == 0 && i >= SUFFIX_SKIP_1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                suffix = cp_Utf8_big_suffix.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            // by induction, the buffer is already filled with the prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            System.arraycopy(suffixChars[i], 0, buf, prefix, suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            cpMap[i] = ConstantPool.getUtf8Entry(new String(buf, 0, prefix+suffix));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        cp_Utf8_prefix.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        cp_Utf8_suffix.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        cp_Utf8_big_suffix.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   804
    Map<Utf8Entry, SignatureEntry> utf8Signatures;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    void readSignatureBands(Entry[] cpMap) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        //  cp_Signature:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        //        *cp_Signature_form :DELTA5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        //        *cp_Signature_classes :UDELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        cp_Signature_form.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        cp_Signature_form.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        cp_Signature_form.setIndex(getCPIndex(CONSTANT_Utf8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        int[] numSigClasses = new int[cpMap.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            Utf8Entry formRef = (Utf8Entry) cp_Signature_form.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            numSigClasses[i] = ConstantPool.countClassParts(formRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        cp_Signature_form.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        cp_Signature_classes.expectLength(getIntTotal(numSigClasses));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        cp_Signature_classes.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        cp_Signature_classes.setIndex(getCPIndex(CONSTANT_Class));
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   822
        utf8Signatures = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            Utf8Entry formRef = (Utf8Entry) cp_Signature_form.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            ClassEntry[] classRefs = new ClassEntry[numSigClasses[i]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            for (int j = 0; j < classRefs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                classRefs[j] = (ClassEntry) cp_Signature_classes.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            SignatureEntry se = ConstantPool.getSignatureEntry(formRef, classRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            cpMap[i] = se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            utf8Signatures.put(se.asUtf8Entry(), se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        cp_Signature_form.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        cp_Signature_classes.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    void readMemberRefs(byte tag, Entry[] cpMap, CPRefBand cp_class, CPRefBand cp_desc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        //  cp_Field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        //        *cp_Field_class :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        //        *cp_Field_desc :UDELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        //  cp_Method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        //        *cp_Method_class :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        //        *cp_Method_desc :UDELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        //  cp_Imethod:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        //        *cp_Imethod_class :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        //        *cp_Imethod_desc :UDELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        cp_class.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        cp_class.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        cp_class.setIndex(getCPIndex(CONSTANT_Class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        cp_desc.expectLength(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        cp_desc.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        cp_desc.setIndex(getCPIndex(CONSTANT_NameandType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            ClassEntry      mclass = (ClassEntry     ) cp_class.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            DescriptorEntry mdescr = (DescriptorEntry) cp_desc.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            cpMap[i] = ConstantPool.getMemberEntry(tag, mclass, mdescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        cp_class.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        cp_desc.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    void readFiles() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        //  file_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        //        *file_name :UNSIGNED5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        //        *file_size_hi :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        //        *file_size_lo :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        //        *file_modtime :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        //        *file_options :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        //        *file_bits :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            Utils.log.info("  ...building "+numFiles+" files...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        file_name.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        file_size_lo.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        int options = archiveOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        boolean haveSizeHi  = testBit(options, AO_HAVE_FILE_SIZE_HI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        boolean haveModtime = testBit(options, AO_HAVE_FILE_MODTIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        boolean haveOptions = testBit(options, AO_HAVE_FILE_OPTIONS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        if (haveSizeHi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            file_size_hi.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        if (haveModtime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            file_modtime.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        if (haveOptions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            file_options.expectLength(numFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        file_name.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        file_size_hi.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        file_size_lo.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        file_modtime.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        file_options.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        file_bits.setInputStreamFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
   892
        Iterator<Class> nextClass = pkg.getClasses().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // Compute file lengths before reading any file bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        long totalFileLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        long[] fileLengths = new long[numFiles];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        for (int i = 0; i < numFiles; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            long size = ((long)file_size_lo.getInt() << 32) >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            if (haveSizeHi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                size += (long)file_size_hi.getInt() << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            fileLengths[i] = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            totalFileLength += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        assert(in.getReadLimit() == -1 || in.getReadLimit() == totalFileLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        byte[] buf = new byte[1<<16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        for (int i = 0; i < numFiles; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            // %%% Use a big temp file for file bits?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            Utf8Entry name = (Utf8Entry) file_name.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            long size = fileLengths[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            File file = pkg.new File(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            file.modtime = pkg.default_modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            file.options = pkg.default_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            if (haveModtime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                file.modtime += file_modtime.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            if (haveOptions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                file.options |= file_options.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                Utils.log.fine("Reading "+size+" bytes of "+name.stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            long toRead = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            while (toRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                int nr = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                if (nr > toRead)  nr = (int) toRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                nr = file_bits.getInputStream().read(buf, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                if (nr < 0)  throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                file.addBytes(buf, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                toRead -= nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            pkg.addFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            if (file.isClassStub()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                assert(file.getFileLength() == 0);
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
   932
                Class cls = nextClass.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                cls.initFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        // Do the rest of the classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        while (nextClass.hasNext()) {
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
   939
            Class cls = nextClass.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            cls.initFile(null);  // implicitly initialize to a trivial one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            cls.file.modtime = pkg.default_modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        file_name.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        file_size_hi.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        file_size_lo.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        file_modtime.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        file_options.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        file_bits.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        file_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (archiveSize1 != 0 && !in.atLimit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            throw new RuntimeException("Predicted archive_size "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                                       archiveSize1+" != "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                                       (in.getBytesServed()-archiveSize0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    void readAttrDefs() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        //  attr_definition_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        //        *attr_definition_headers :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        //        *attr_definition_name :UNSIGNED5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        //        *attr_definition_layout :UNSIGNED5  (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        attr_definition_headers.expectLength(numAttrDefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        attr_definition_name.expectLength(numAttrDefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        attr_definition_layout.expectLength(numAttrDefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        attr_definition_headers.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        attr_definition_name.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        attr_definition_layout.readFrom(in);
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   970
        try (PrintStream dump = !optDumpBands ? null
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   971
                 : new PrintStream(getDumpStream(attr_definition_headers, ".def")))
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   972
        {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   973
            for (int i = 0; i < numAttrDefs; i++) {
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   974
                int       header = attr_definition_headers.getByte();
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   975
                Utf8Entry name   = (Utf8Entry) attr_definition_name.getRef();
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   976
                Utf8Entry layout = (Utf8Entry) attr_definition_layout.getRef();
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   977
                int       ctype  = (header &  ADH_CONTEXT_MASK);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   978
                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
   979
                Attribute.Layout def = new Attribute.Layout(ctype,
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   980
                                                            name.stringValue(),
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   981
                                                            layout.stringValue());
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   982
                // Check layout string for Java 6 extensions.
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   983
                String pvLayout = def.layoutForClassVersion(getHighestClassVersion());
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   984
                if (!pvLayout.equals(def.layout())) {
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   985
                    throw new IOException("Bad attribute layout in archive: "+def.layout());
8803
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   986
                }
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   987
                this.setAttributeLayoutIndex(def, index);
b3f57bfca459 7022382: convert pack200 library code to use try-with-resources
smarks
parents: 8570
diff changeset
   988
                if (dump != null)  dump.println(index+" "+def);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        attr_definition_headers.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        attr_definition_name.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        attr_definition_layout.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        // Attribute layouts define bands, one per layout element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        // Create them now, all at once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        makeNewAttributeBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        attr_definition_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    void readInnerClasses() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        //  ic_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        //        *ic_this_class :UDELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        //        *ic_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        //        *ic_outer_class :DELTA5  (null or cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        //        *ic_name :DELTA5  (null or cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        ic_this_class.expectLength(numInnerClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        ic_this_class.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        ic_flags.expectLength(numInnerClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        ic_flags.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        int longICCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        for (int i = 0; i < numInnerClasses; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            int flags = ic_flags.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            boolean longForm = (flags & ACC_IC_LONG_FORM) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (longForm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                longICCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        ic_outer_class.expectLength(longICCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        ic_outer_class.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        ic_name.expectLength(longICCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        ic_name.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        ic_flags.resetForSecondPass();
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1023
        List<InnerClass> icList = new ArrayList<>(numInnerClasses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        for (int i = 0; i < numInnerClasses; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            int flags = ic_flags.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            boolean longForm = (flags & ACC_IC_LONG_FORM) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            flags &= ~ACC_IC_LONG_FORM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            ClassEntry thisClass = (ClassEntry) ic_this_class.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            ClassEntry outerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            Utf8Entry  thisName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            if (longForm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                outerClass = (ClassEntry) ic_outer_class.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                thisName   = (Utf8Entry)  ic_name.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                String n = thisClass.stringValue();
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1036
                String[] parse = Package.parseInnerClassName(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                assert(parse != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                String pkgOuter = parse[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                //String number = parse[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                String name     = parse[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                if (pkgOuter == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    outerClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    outerClass = ConstantPool.getClassEntry(pkgOuter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    thisName   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                    thisName   = ConstantPool.getUtf8Entry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            InnerClass ic =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                new InnerClass(thisClass, outerClass, thisName, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            assert(longForm || ic.predictable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            icList.add(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        ic_flags.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        ic_this_class.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        ic_outer_class.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        ic_name.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        pkg.setAllInnerClasses(icList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        ic_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    void readLocalInnerClasses(Class cls) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        int nc = class_InnerClasses_N.getInt();
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1065
        List<InnerClass> localICs = new ArrayList<>(nc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        for (int i = 0; i < nc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            ClassEntry thisClass = (ClassEntry) class_InnerClasses_RC.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            int        flags     =              class_InnerClasses_F.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            if (flags == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                // A zero flag means copy a global IC here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                InnerClass ic = pkg.getGlobalInnerClass(thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                assert(ic != null);  // must be a valid global IC reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                localICs.add(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                if (flags == ACC_IC_LONG_FORM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    flags = 0;  // clear the marker bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                ClassEntry outer = (ClassEntry) class_InnerClasses_outer_RCN.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                Utf8Entry name   = (Utf8Entry)  class_InnerClasses_name_RUN.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                localICs.add(new InnerClass(thisClass, outer, name, flags));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        cls.setInnerClasses(localICs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        // cls.expandLocalICs may add more tuples to ics also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        // or may even delete tuples.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        // We cannot do that now, because we do not know the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        // full contents of the local constant pool yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    static final int NO_FLAGS_YET = 0;  // placeholder for later flag read-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    Class[] readClasses() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        //  class_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        //        *class_this :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        //        *class_super :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        //        *class_interface_count :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        //        *class_interface :DELTA5  (cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        //        ...(member bands)...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        //        class_attr_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        //        code_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        Class[] classes = new Class[numClasses];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            Utils.log.info("  ...building "+classes.length+" classes...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        class_this.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        class_super.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        class_interface_count.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        class_this.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        class_super.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        class_interface_count.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        class_interface.expectLength(class_interface_count.getIntTotal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        class_interface.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            ClassEntry   thisClass  = (ClassEntry) class_this.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            ClassEntry   superClass = (ClassEntry) class_super.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            ClassEntry[] interfaces = new ClassEntry[class_interface_count.getInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            for (int j = 0; j < interfaces.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                interfaces[j] = (ClassEntry) class_interface.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            // Packer encoded rare case of null superClass as thisClass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            if (superClass == thisClass)  superClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            Class cls = pkg.new Class(NO_FLAGS_YET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                                      thisClass, superClass, interfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            classes[i] = cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        class_this.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        class_super.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        class_interface_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        class_interface.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        readMembers(classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        countAndReadAttrs(ATTR_CONTEXT_CLASS, Arrays.asList(classes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        pkg.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        readCodeHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        //code_bands.doneDisbursing(); // still need to read code attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        //class_bands.doneDisbursing(); // still need to read code attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        return classes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    private int getOutputIndex(Entry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        // Output CPs do not contain signatures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        assert(e.tag != CONSTANT_Signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        int k = pkg.cp.untypedIndexOf(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        // In the output ordering, input signatures can serve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        // in place of Utf8s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        if (k >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        if (e.tag == CONSTANT_Utf8) {
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1148
            Entry se = utf8Signatures.get(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            return pkg.cp.untypedIndexOf(se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
8570
c9767adab3d2 7023963: (misc) fix diamond anon instances in the jdk
ksrini
parents: 7816
diff changeset
  1154
    Comparator<Entry> entryOutputOrder = new Comparator<Entry>() {
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1155
        public int compare(Entry e0, Entry e1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            int k0 = getOutputIndex(e0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            int k1 = getOutputIndex(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            if (k0 >= 0 && k1 >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                // If both have keys, use the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                return k0 - k1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if (k0 == k1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                // If neither have keys, use their native tags & spellings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                return e0.compareTo(e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            // Otherwise, the guy with the key comes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            return (k0 >= 0)? 0-1: 1-0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    void reconstructClass(Class cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        if (verbose > 1)  Utils.log.fine("reconstruct "+cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // check for local .ClassFile.version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        Attribute retroVersion = cls.getAttribute(attrClassFileVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        if (retroVersion != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            cls.removeAttribute(retroVersion);
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
  1176
            cls.version = parseClassFileVersionAttr(retroVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        } else {
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
  1178
            cls.version = pkg.defaultClassVersion;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        // Replace null SourceFile by "obvious" string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        cls.expandSourceFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        // record the local cp:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        cls.setCPMap(reconstructLocalCPMap(cls));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    Entry[] reconstructLocalCPMap(Class cls) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1189
        Set<Entry> ldcRefs = ldcRefMap.get(cls);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1190
        Set<Entry> cpRefs = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        // look for constant pool entries:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        cls.visitRefs(VRM_CLASSIC, cpRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1195
        ArrayList<BootstrapMethodEntry> bsms = new ArrayList<>();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1196
        /*
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1197
         * BootstrapMethod(BSMs) are added here before InnerClasses(ICs),
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1198
         * so as to ensure the order. Noting that the BSMs  may be
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1199
         * removed if they are not found in the CP, after the ICs expansion.
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1200
         */
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1201
        cls.addAttribute(Package.attrBootstrapMethodsEmpty.canonicalInstance());
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1202
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        // flesh out the local constant pool
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1204
        ConstantPool.completeReferencesIn(cpRefs, true, bsms);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        // Now that we know all our local class references,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        // compute the InnerClasses attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        int changed = cls.expandLocalICs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        if (changed != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            if (changed > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                // Just visit the expanded InnerClasses attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                cls.visitInnerClassRefs(VRM_CLASSIC, cpRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                // Have to recompute from scratch, because of deletions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                cpRefs.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                cls.visitRefs(VRM_CLASSIC, cpRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            // flesh out the local constant pool, again
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1221
            ConstantPool.completeReferencesIn(cpRefs, true, bsms);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1222
        }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1223
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1224
        // remove the attr previously set, otherwise add the bsm and
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1225
        // references as required
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1226
        if (bsms.isEmpty()) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1227
            cls.attributes.remove(Package.attrBootstrapMethodsEmpty.canonicalInstance());
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1228
        } else {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1229
            cpRefs.add(Package.getRefString("BootstrapMethods"));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1230
            Collections.sort(bsms);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1231
            cls.setBootstrapMethods(bsms);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        // construct a local constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        int numDoubles = 0;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1236
        for (Entry e : cpRefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            if (e.isDoubleWord())  numDoubles++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        Entry[] cpMap = new Entry[1+numDoubles+cpRefs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        int fillp = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        // Add all ldc operands first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        if (ldcRefs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            assert(cpRefs.containsAll(ldcRefs));
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1245
            for (Entry e : ldcRefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                cpMap[fillp++] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            assert(fillp == 1+ldcRefs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            cpRefs.removeAll(ldcRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            ldcRefs = null;  // done with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        // Next add all the two-byte references.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1254
        Set<Entry> wideRefs = cpRefs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        cpRefs = null;  // do not use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        int narrowLimit = fillp;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1257
        for (Entry e : wideRefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            cpMap[fillp++] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        assert(fillp == narrowLimit+wideRefs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        Arrays.sort(cpMap, 1, narrowLimit, entryOutputOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        Arrays.sort(cpMap, narrowLimit, fillp, entryOutputOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            Utils.log.fine("CP of "+this+" {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            for (int i = 0; i < fillp; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                Entry e = cpMap[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                Utils.log.fine("  "+((e==null)?-1:getOutputIndex(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                                   +" : "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            Utils.log.fine("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        // Now repack backwards, introducing null elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        int revp = cpMap.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        for (int i = fillp; --i >= 1; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            Entry e = cpMap[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            if (e.isDoubleWord())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                cpMap[--revp] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            cpMap[--revp] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        assert(revp == 1);  // do not process the initial null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        return cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    void readMembers(Class[] classes) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        //  class_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        //        *class_field_count :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        //        *class_method_count :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        //        *field_descr :DELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        //        field_attr_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        //        *method_descr :MDELTA5  (cp_Descr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        //        method_attr_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        assert(classes.length == numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        class_field_count.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        class_method_count.expectLength(numClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        class_field_count.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        class_method_count.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        // Make a pre-pass over field and method counts to size the descrs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        int totalNF = class_field_count.getIntTotal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        int totalNM = class_method_count.getIntTotal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        field_descr.expectLength(totalNF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        method_descr.expectLength(totalNM);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1310
        if (verbose > 1)  Utils.log.fine("expecting #fields="+totalNF+
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1311
                " and #methods="+totalNM+" in #classes="+numClasses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1313
        List<Class.Field> fields = new ArrayList<>(totalNF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        field_descr.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            Class c = classes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            int nf = class_field_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            for (int j = 0; j < nf; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                Class.Field f = c.new Field(NO_FLAGS_YET, (DescriptorEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                                            field_descr.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                fields.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        class_field_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        field_descr.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        countAndReadAttrs(ATTR_CONTEXT_FIELD, fields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        fields = null;  // release to GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1329
        List<Class.Method> methods = new ArrayList<>(totalNM);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        method_descr.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        for (int i = 0; i < classes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            Class c = classes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            int nm = class_method_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            for (int j = 0; j < nm; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                Class.Method m = c.new Method(NO_FLAGS_YET, (DescriptorEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                                              method_descr.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                methods.add(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        class_method_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        method_descr.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        countAndReadAttrs(ATTR_CONTEXT_METHOD, methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        // Up to this point, Code attributes look like empty attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        // Now we start to special-case them.  The empty canonical Code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        // attributes stay in the method attribute lists, however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        allCodes = buildCodeAttrs(methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    Code[] allCodes;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1351
    List<Code> codesWithFlags;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1352
    Map<Class, Set<Entry>> ldcRefMap = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1354
    Code[] buildCodeAttrs(List<Class.Method> methods) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1355
        List<Code> codes = new ArrayList<>(methods.size());
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1356
        for (Class.Method m : methods) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            if (m.getAttribute(attrCodeEmpty) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                m.code = new Code(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                codes.add(m.code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        Code[] a = new Code[codes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        codes.toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    void readCodeHeaders() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        //  code_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        //        *code_headers :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        //        *code_max_stack :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        //        *code_max_na_locals :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        //        *code_handler_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        //        code_attr_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        boolean attrsOK = testBit(archiveOptions, AO_HAVE_ALL_CODE_FLAGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        code_headers.expectLength(allCodes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        code_headers.readFrom(in);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1379
        List<Code> longCodes = new ArrayList<>(allCodes.length / 10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        for (int i = 0; i < allCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            Code c = allCodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            int sc = code_headers.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            assert(sc == (sc & 0xFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                Utils.log.fine("codeHeader "+c+" = "+sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            if (sc == LONG_CODE_HEADER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                // We will read ms/ml/nh/flags from bands shortly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                longCodes.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            // Short code header is the usual case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            c.setMaxStack(     shortCodeHeader_max_stack(sc) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            c.setMaxNALocals(  shortCodeHeader_max_na_locals(sc) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            c.setHandlerCount( shortCodeHeader_handler_count(sc) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            assert(shortCodeHeader(c) == sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        code_headers.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        code_max_stack.expectLength(longCodes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        code_max_na_locals.expectLength(longCodes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        code_handler_count.expectLength(longCodes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        // Do the long headers now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        code_max_stack.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        code_max_na_locals.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        code_handler_count.readFrom(in);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1406
        for (Code c : longCodes) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            c.setMaxStack(     code_max_stack.getInt() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            c.setMaxNALocals(  code_max_na_locals.getInt() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            c.setHandlerCount( code_handler_count.getInt() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        code_max_stack.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        code_max_na_locals.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        code_handler_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        readCodeHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        if (attrsOK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            // Code attributes are common (debug info not stripped).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            codesWithFlags = Arrays.asList(allCodes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            // Code attributes are very sparse (debug info is stripped).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            codesWithFlags = longCodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        countAttrs(ATTR_CONTEXT_CODE, codesWithFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        // do readAttrs later, after BCs are scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    void readCodeHandlers() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        //  code_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        //        *code_handler_start_P :BCI5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        //        *code_handler_end_PO :BRANCH5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        //        *code_handler_catch_PO :BRANCH5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        //        *code_handler_class_RCN :UNSIGNED5  (null or cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        //        ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        int nh = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        for (int i = 0; i < allCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            Code c = allCodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            nh += c.getHandlerCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        ValueBand[] code_handler_bands = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            code_handler_start_P,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            code_handler_end_PO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            code_handler_catch_PO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            code_handler_class_RCN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        for (int i = 0; i < code_handler_bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            code_handler_bands[i].expectLength(nh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            code_handler_bands[i].readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        for (int i = 0; i < allCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            Code c = allCodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            for (int j = 0, jmax = c.getHandlerCount(); j < jmax; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                c.handler_class[j] = code_handler_class_RCN.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                // For now, just record the raw BCI codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                // We must wait until we have instruction boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                c.handler_start[j] = code_handler_start_P.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                c.handler_end[j]   = code_handler_end_PO.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                c.handler_catch[j] = code_handler_catch_PO.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        for (int i = 0; i < code_handler_bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            code_handler_bands[i].doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    void fixupCodeHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        // Actually decode (renumber) the BCIs now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        for (int i = 0; i < allCodes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            Code c = allCodes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            for (int j = 0, jmax = c.getHandlerCount(); j < jmax; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                int sum = c.handler_start[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                c.handler_start[j] = c.decodeBCI(sum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                sum += c.handler_end[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                c.handler_end[j]   = c.decodeBCI(sum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                sum += c.handler_catch[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                c.handler_catch[j] = c.decodeBCI(sum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    // Generic routines for reading attributes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    // classes, fields, methods, and codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    // The holders is a global list, already collected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    // of attribute "customers".
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1489
    void countAndReadAttrs(int ctype, Collection<? extends Attribute.Holder> holders)
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1490
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        //  class_attr_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        //        *class_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        //        *class_attr_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        //        *class_attr_indexes :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        //        *class_attr_calls :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        //        *class_Signature_RS :UNSIGNED5 (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        //        class_metadata_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        //        *class_SourceFile_RU :UNSIGNED5 (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        //        *class_EnclosingMethod_RM :UNSIGNED5 (cp_Method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        //        ic_local_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        //        *class_ClassFile_version_minor_H :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        //        *class_ClassFile_version_major_H :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        //  field_attr_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        //        *field_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        //        *field_attr_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        //        *field_attr_indexes :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        //        *field_attr_calls :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        //        *field_Signature_RS :UNSIGNED5 (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        //        field_metadata_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        //        *field_ConstantValue_KQ :UNSIGNED5 (cp_Int, etc.; see note)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        //  method_attr_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        //        *method_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        //        *method_attr_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        //        *method_attr_indexes :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        //        *method_attr_calls :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        //        *method_Signature_RS :UNSIGNED5 (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        //        method_metadata_bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        //        *method_Exceptions_N :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        //        *method_Exceptions_RC :UNSIGNED5  (cp_Class)
15261
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12857
diff changeset
  1522
        //        *method_MethodParameters_NB: BYTE1
c5b882836677 8005252: pack200 should support MethodParameters
ksrini
parents: 12857
diff changeset
  1523
        //        *method_MethodParameters_RUN: UNSIGNED5 (cp_Utf8)
16013
3569e84e7429 8008262: pack200 should support MethodParameters - part 2
ksrini
parents: 15261
diff changeset
  1524
        //        *method_MethodParameters_FH:  UNSIGNED5 (flag)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        //  code_attr_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        //        *code_flags :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        //        *code_attr_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        //        *code_attr_indexes :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        //        *code_attr_calls :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        //        *code_LineNumberTable_N :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        //        *code_LineNumberTable_bci_P :BCI5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        //        *code_LineNumberTable_line :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        //        *code_LocalVariableTable_N :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        //        *code_LocalVariableTable_bci_P :BCI5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        //        *code_LocalVariableTable_span_O :BRANCH5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        //        *code_LocalVariableTable_name_RU :UNSIGNED5 (cp_Utf8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        //        *code_LocalVariableTable_type_RS :UNSIGNED5 (cp_Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        //        *code_LocalVariableTable_slot :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        countAttrs(ctype, holders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        readAttrs(ctype, holders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    // Read flags and count the attributes that are to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    // on the given holders.
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1547
    void countAttrs(int ctype, Collection<? extends Attribute.Holder> holders)
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1548
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        // Here, xxx stands for one of class, field, method, code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        MultiBand xxx_attr_bands = attrBands[ctype];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        long flagMask = attrFlagMask[ctype];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        if (verbose > 1) {
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1553
            Utils.log.fine("scanning flags and attrs for "+
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1554
                    Attribute.contextName(ctype)+"["+holders.size()+"]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        // Fetch the attribute layout definitions which govern the bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        // we are about to read.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1559
        List<Attribute.Layout> defList = attrDefs.get(ctype);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1560
        Attribute.Layout[] defs = new Attribute.Layout[defList.size()];
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1561
        defList.toArray(defs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        IntBand xxx_flags_hi = getAttrBand(xxx_attr_bands, AB_FLAGS_HI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        IntBand xxx_flags_lo = getAttrBand(xxx_attr_bands, AB_FLAGS_LO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        IntBand xxx_attr_count = getAttrBand(xxx_attr_bands, AB_ATTR_COUNT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        IntBand xxx_attr_indexes = getAttrBand(xxx_attr_bands, AB_ATTR_INDEXES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        IntBand xxx_attr_calls = getAttrBand(xxx_attr_bands, AB_ATTR_CALLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        // Count up the number of holders which have overflow attrs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        int overflowMask = attrOverflowMask[ctype];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        int overflowHolderCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        boolean haveLongFlags = haveFlagsHi(ctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        xxx_flags_hi.expectLength(haveLongFlags? holders.size(): 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        xxx_flags_hi.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        xxx_flags_lo.expectLength(holders.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        xxx_flags_lo.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        assert((flagMask & overflowMask) == overflowMask);
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1577
        for (Attribute.Holder h : holders) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            int flags = xxx_flags_lo.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            h.flags = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            if ((flags & overflowMask) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                overflowHolderCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        // For each holder with overflow attrs, read a count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        xxx_attr_count.expectLength(overflowHolderCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        xxx_attr_count.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        xxx_attr_indexes.expectLength(xxx_attr_count.getIntTotal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        xxx_attr_indexes.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        // Now it's time to check flag bits that indicate attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        // We accumulate (a) a list of attribute types for each holder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        // (class/field/method/code), and also we accumulate (b) a total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        // count for each attribute type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        int[] totalCounts = new int[defs.length];
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1595
        for (Attribute.Holder h : holders) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            assert(h.attributes == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            // System.out.println("flags="+h.flags+" using fm="+flagMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            long attrBits = ((h.flags & flagMask) << 32) >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            // Clean up the flags now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            h.flags -= (int)attrBits;   // strip attr bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            assert(h.flags == (char)h.flags);  // 16 bits only now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            assert((ctype != ATTR_CONTEXT_CODE) || h.flags == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            if (haveLongFlags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                attrBits += (long)xxx_flags_hi.getInt() << 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            if (attrBits == 0)  continue;  // no attrs on this guy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            int noa = 0;  // number of overflow attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            long overflowBit = (attrBits & overflowMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            assert(overflowBit >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            attrBits -= overflowBit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            if (overflowBit != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                noa = xxx_attr_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            int nfa = 0;  // number of flag attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            long bits = attrBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            for (int ai = 0; bits != 0; ai++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                if ((bits & (1L<<ai)) == 0)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                bits -= (1L<<ai);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                nfa += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1622
            List<Attribute> ha = new ArrayList<>(nfa + noa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            h.attributes = ha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            bits = attrBits;  // iterate again
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            for (int ai = 0; bits != 0; ai++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                if ((bits & (1L<<ai)) == 0)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                bits -= (1L<<ai);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                totalCounts[ai] += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                // This definition index is live in this holder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                if (defs[ai] == null)  badAttrIndex(ai, ctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                Attribute canonical = defs[ai].canonicalInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                ha.add(canonical);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                nfa -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            assert(nfa == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            for (; noa > 0; noa--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                int ai = xxx_attr_indexes.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                totalCounts[ai] += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                // This definition index is live in this holder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                if (defs[ai] == null)  badAttrIndex(ai, ctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                Attribute canonical = defs[ai].canonicalInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                ha.add(canonical);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        xxx_flags_hi.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        xxx_flags_lo.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        xxx_attr_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        xxx_attr_indexes.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        // Now each holder has a list of canonical attribute instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        // For layouts with no elements, we are done.  However, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        // layouts with bands, we must replace each canonical (empty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        // instance with a value-bearing one, initialized from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        // appropriate bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        // Make a small pass to detect and read backward call counts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        int callCounts = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        for (boolean predef = true; ; predef = false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            for (int ai = 0; ai < defs.length; ai++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                Attribute.Layout def = defs[ai];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                if (def == null)  continue;  // unused index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                if (predef != isPredefinedAttr(ctype, ai))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                    continue;  // wrong pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                int totalCount = totalCounts[ai];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                if (totalCount == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                    continue;  // irrelevant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                Attribute.Layout.Element[] cbles = def.getCallables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                for (int j = 0; j < cbles.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                    assert(cbles[j].kind == Attribute.EK_CBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                    if (cbles[j].flagTest(Attribute.EF_BACK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                        callCounts += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            if (!predef)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        xxx_attr_calls.expectLength(callCounts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        xxx_attr_calls.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        // Finally, size all the attribute bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        for (boolean predef = true; ; predef = false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            for (int ai = 0; ai < defs.length; ai++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                Attribute.Layout def = defs[ai];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                if (def == null)  continue;  // unused index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                if (predef != isPredefinedAttr(ctype, ai))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                    continue;  // wrong pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                int totalCount = totalCounts[ai];
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1688
                Band[] ab = attrBandTable.get(def);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                if (def == attrInnerClassesEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                    // Special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                    // Size the bands as if using the following layout:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                    //    [RCH TI[ (0)[] ()[RCNH RUNH] ]].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    class_InnerClasses_N.expectLength(totalCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                    class_InnerClasses_N.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                    int tupleCount = class_InnerClasses_N.getIntTotal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                    class_InnerClasses_RC.expectLength(tupleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                    class_InnerClasses_RC.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                    class_InnerClasses_F.expectLength(tupleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                    class_InnerClasses_F.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                    // Drop remaining columns wherever flags are zero:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                    tupleCount -= class_InnerClasses_F.getIntCount(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                    class_InnerClasses_outer_RCN.expectLength(tupleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                    class_InnerClasses_outer_RCN.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                    class_InnerClasses_name_RUN.expectLength(tupleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    class_InnerClasses_name_RUN.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                } else if (totalCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                    // Expect no elements at all.  Skip quickly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                    for (int j = 0; j < ab.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                        ab[j].doneWithUnusedBand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                    // Read these bands in sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                    boolean hasCallables = def.hasCallables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                    if (!hasCallables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                        readAttrBands(def.elems, totalCount, new int[0], ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                        Attribute.Layout.Element[] cbles = def.getCallables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                        // At first, record initial calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                        // Later, forward calls may also accumulate here:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                        int[] forwardCounts = new int[cbles.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                        forwardCounts[0] = totalCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                        for (int j = 0; j < cbles.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                            assert(cbles[j].kind == Attribute.EK_CBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                            int entryCount = forwardCounts[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                            forwardCounts[j] = -1;  // No more, please!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                            if (cbles[j].flagTest(Attribute.EF_BACK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                                entryCount += xxx_attr_calls.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                            readAttrBands(cbles[j].body, entryCount, forwardCounts, ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            if (!predef)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        xxx_attr_calls.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    void badAttrIndex(int ai, int ctype) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        throw new IOException("Unknown attribute index "+ai+" for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                                   ATTR_CONTEXT_NAME[ctype]+" attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1743
    void readAttrs(int ctype, Collection<? extends Attribute.Holder> holders)
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1744
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        // Decode band values into attributes.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1746
        Set<Attribute.Layout> sawDefs = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 8803
diff changeset
  1748
        for (final Attribute.Holder h : holders) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            if (h.attributes == null)  continue;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1750
            for (ListIterator<Attribute> j = h.attributes.listIterator(); j.hasNext(); ) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1751
                Attribute a = j.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                Attribute.Layout def = a.layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                if (def.bandCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                    if (def == attrInnerClassesEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                        // Special logic to read this attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                        readLocalInnerClasses((Class) h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    // Canonical empty attr works fine (e.g., Synthetic).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                sawDefs.add(def);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                boolean isCV = (ctype == ATTR_CONTEXT_FIELD && def == attrConstantValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                if (isCV)  setConstantValueIndex((Class.Field)h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                    Utils.log.fine("read "+a+" in "+h);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1767
                final Band[] ab = attrBandTable.get(def);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                // Read one attribute of type def from ab into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                buf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                Object fixups = a.unparse(new Attribute.ValueStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                    public int getInt(int bandIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                        return ((IntBand) ab[bandIndex]).getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                    public Entry getRef(int bandIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                        return ((CPRefBand) ab[bandIndex]).getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                    public int decodeBCI(int bciCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                        Code code = (Code) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                        return code.decodeBCI(bciCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                }, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                // Replace the canonical attr with the one just read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                j.set(a.addContent(buf.toByteArray(), fixups));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                if (isCV)  setConstantValueIndex(null);  // clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        // Mark the bands we just used as done disbursing.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1789
        for (Attribute.Layout def : sawDefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            if (def == null)  continue;  // unused index
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1791
            Band[] ab = attrBandTable.get(def);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            for (int j = 0; j < ab.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                ab[j].doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        if (ctype == ATTR_CONTEXT_CLASS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            class_InnerClasses_N.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            class_InnerClasses_RC.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            class_InnerClasses_F.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            class_InnerClasses_outer_RCN.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            class_InnerClasses_name_RUN.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        MultiBand xxx_attr_bands = attrBands[ctype];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        for (int i = 0; i < xxx_attr_bands.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            Band b = xxx_attr_bands.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            if (b instanceof MultiBand)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                b.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        xxx_attr_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    void readAttrBands(Attribute.Layout.Element[] elems,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                       int count, int[] forwardCounts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                       Band[] ab)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        for (int i = 0; i < elems.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            Attribute.Layout.Element e = elems[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            Band eBand = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            if (e.hasBand()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                eBand = ab[e.bandIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                eBand.expectLength(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                eBand.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            switch (e.kind) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            case Attribute.EK_REPL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                // Recursive call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                int repCount = ((IntBand)eBand).getIntTotal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                // Note:  getIntTotal makes an extra pass over this band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                readAttrBands(e.body, repCount, forwardCounts, ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            case Attribute.EK_UN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                int remainingCount = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                for (int j = 0; j < e.body.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                    int caseCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                    if (j == e.body.length-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                        caseCount = remainingCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                        caseCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                        for (int j0 = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                             (j == j0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                             || (j < e.body.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                                 && e.body[j].flagTest(Attribute.EF_BACK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                             j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                            caseCount += ((IntBand)eBand).getIntCount(e.body[j].value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                        --j;  // back up to last occurrence of this body
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                    remainingCount -= caseCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                    readAttrBands(e.body[j].body, caseCount, forwardCounts, ab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                assert(remainingCount == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            case Attribute.EK_CALL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                assert(e.body.length == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                assert(e.body[0].kind == Attribute.EK_CBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                if (!e.flagTest(Attribute.EF_BACK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                    // Backward calls are pre-counted, but forwards are not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                    // Push the present count forward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                    assert(forwardCounts[e.value] >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                    forwardCounts[e.value] += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            case Attribute.EK_CBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    void readByteCodes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        //  bc_bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        //        *bc_codes :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        //        *bc_case_count :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        //        *bc_case_value :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        //        *bc_byte :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        //        *bc_short :DELTA5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        //        *bc_local :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        //        *bc_label :BRANCH5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        //        *bc_intref :DELTA5  (cp_Int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        //        *bc_floatref :DELTA5  (cp_Float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        //        *bc_longref :DELTA5  (cp_Long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        //        *bc_doubleref :DELTA5  (cp_Double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        //        *bc_stringref :DELTA5  (cp_String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        //        *bc_classref :UNSIGNED5  (current class or cp_Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        //        *bc_fieldref :DELTA5  (cp_Field)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        //        *bc_methodref :UNSIGNED5  (cp_Method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        //        *bc_imethodref :DELTA5  (cp_Imethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        //        *bc_thisfield :UNSIGNED5 (cp_Field, only for current class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        //        *bc_superfield :UNSIGNED5 (cp_Field, only for current super)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        //        *bc_thismethod :UNSIGNED5 (cp_Method, only for current class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        //        *bc_supermethod :UNSIGNED5 (cp_Method, only for current super)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        //        *bc_initref :UNSIGNED5 (cp_Field, only for most recent new)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        //        *bc_escref :UNSIGNED5 (cp_All)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        //        *bc_escrefsize :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        //        *bc_escsize :UNSIGNED5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        //        *bc_escbyte :BYTE1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        bc_codes.elementCountForDebug = allCodes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        bc_codes.setInputStreamFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        readByteCodeOps();  // reads from bc_codes and bc_case_count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        bc_codes.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        // All the operand bands have now been sized.  Read them all in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        Band[] operand_bands = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            bc_case_value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            bc_byte, bc_short,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            bc_local, bc_label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            bc_intref, bc_floatref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            bc_longref, bc_doubleref, bc_stringref,
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1912
            bc_loadablevalueref,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            bc_classref, bc_fieldref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            bc_methodref, bc_imethodref,
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  1915
            bc_indyref,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            bc_thisfield, bc_superfield,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            bc_thismethod, bc_supermethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            bc_initref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            bc_escref, bc_escrefsize, bc_escsize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        for (int i = 0; i < operand_bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            operand_bands[i].readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        bc_escbyte.expectLength(bc_escsize.getIntTotal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        bc_escbyte.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        expandByteCodeOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        // Done fetching values from operand bands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        bc_case_count.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        for (int i = 0; i < operand_bands.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            operand_bands[i].doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        bc_escbyte.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        bc_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        // We must delay the parsing of Code attributes until we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        // have a complete model of bytecodes, for BCI encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        readAttrs(ATTR_CONTEXT_CODE, codesWithFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        // Ditto for exception handlers in codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        fixupCodeHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        // Now we can finish with class_bands; cf. readClasses().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        code_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        class_bands.doneDisbursing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    private void readByteCodeOps() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        // scratch buffer for collecting code::
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        byte[] buf = new byte[1<<12];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        // record of all switch opcodes (these are variable-length)
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1951
        List<Integer> allSwitchOps = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        for (int k = 0; k < allCodes.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            Code c = allCodes[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        scanOneMethod:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            for (int i = 0; ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                int bc = bc_codes.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                if (i + 10 > buf.length)  buf = realloc(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                buf[i] = (byte)bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                boolean isWide = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                if (bc == _wide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                    bc = bc_codes.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                    buf[++i] = (byte)bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                    isWide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                assert(bc == (0xFF & bc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                // Adjust expectations of various band sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                switch (bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                case _tableswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                case _lookupswitch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    bc_case_count.expectMoreLength(1);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1971
                    allSwitchOps.add(bc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                case _iinc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                    bc_local.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                    if (isWide)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                        bc_short.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                        bc_byte.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                case _sipush:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    bc_short.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                case _bipush:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                    bc_byte.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                case _newarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                    bc_byte.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                case _multianewarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                    assert(getCPRefOpBand(bc) == bc_classref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                    bc_classref.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                    bc_byte.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                case _ref_escape:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                    bc_escrefsize.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                    bc_escref.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                case _byte_escape:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                    bc_escsize.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                    // bc_escbyte will have to be counted too
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                    if (Instruction.isInvokeInitOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                        bc_initref.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                    if (Instruction.isSelfLinkerOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                        CPRefBand bc_which = selfOpRefBand(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                        bc_which.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                    if (Instruction.isBranchOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                        bc_label.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                    if (Instruction.isCPRefOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                        CPRefBand bc_which = getCPRefOpBand(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                        bc_which.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                        assert(bc != _multianewarray);  // handled elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                    if (Instruction.isLocalSlotOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                        bc_local.expectMoreLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                case _end_marker:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                        // Transfer from buf to a more permanent place:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                        c.bytes = realloc(buf, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                        break scanOneMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        // To size instruction bands correctly, we need info on switches:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        bc_case_count.readFrom(in);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  2039
        for (Integer i : allSwitchOps) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  2040
            int bc = i.intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            int caseCount = bc_case_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            bc_label.expectMoreLength(1+caseCount); // default label + cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            bc_case_value.expectMoreLength(bc == _tableswitch ? 1 : caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        bc_case_count.resetForSecondPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    private void expandByteCodeOps() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        // scratch buffer for collecting code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        byte[] buf = new byte[1<<12];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        // scratch buffer for collecting instruction boundaries:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        int[] insnMap = new int[1<<12];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        // list of label carriers, for label decoding post-pass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        int[] labels = new int[1<<10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        // scratch buffer for registering CP refs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        Fixups fixupBuf = new Fixups();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        for (int k = 0; k < allCodes.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            Code code = allCodes[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            byte[] codeOps = code.bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
            code.bytes = null;  // just for now, while we accumulate bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            Class curClass = code.thisClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  2065
            Set<Entry> ldcRefSet = ldcRefMap.get(curClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            if (ldcRefSet == null)
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  2067
                ldcRefMap.put(curClass, ldcRefSet = new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            ClassEntry thisClass  = curClass.thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            ClassEntry superClass = curClass.superClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            ClassEntry newClass   = null;  // class of last _new opcode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            int pc = 0;  // fill pointer in buf; actual bytecode PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            int numInsns = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            int numLabels = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            boolean hasEscs = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            fixupBuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            for (int i = 0; i < codeOps.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                int bc = Instruction.getByte(codeOps, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                int curPC = pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                insnMap[numInsns++] = curPC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                if (pc + 10 > buf.length)  buf = realloc(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                if (numInsns+10 > insnMap.length)  insnMap = realloc(insnMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                if (numLabels+10 > labels.length)  labels = realloc(labels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                boolean isWide = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                if (bc == _wide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                    buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                    bc = Instruction.getByte(codeOps, ++i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                    isWide = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                switch (bc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                case _tableswitch: // apc:  (df, lo, hi, (hi-lo+1)*(label))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                case _lookupswitch: // apc:  (df, nc, nc*(case, label))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                        int caseCount = bc_case_count.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                        while ((pc + 30 + caseCount*8) > buf.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                            buf = realloc(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                        //initialize apc, df, lo, hi bytes to reasonable bits:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                        Arrays.fill(buf, pc, pc+30, (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                        Instruction.Switch isw = (Instruction.Switch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                            Instruction.at(buf, curPC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                        //isw.setDefaultLabel(getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                        isw.setCaseCount(caseCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                        if (bc == _tableswitch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                            isw.setCaseValue(0, bc_case_value.getInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                            for (int j = 0; j < caseCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                                isw.setCaseValue(j, bc_case_value.getInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                        // Make our getLabel calls later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                        labels[numLabels++] = curPC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                        pc = isw.getNextPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                case _iinc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                        int local = bc_local.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                        int delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                        if (isWide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                            delta = bc_short.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                            Instruction.setShort(buf, pc, local); pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                            Instruction.setShort(buf, pc, delta); pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                            delta = (byte) bc_byte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                            buf[pc++] = (byte)local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                            buf[pc++] = (byte)delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                case _sipush:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                        int val = bc_short.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                        Instruction.setShort(buf, pc, val); pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                case _bipush:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                case _newarray:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                        int val = bc_byte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                        buf[pc++] = (byte) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                case _ref_escape:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                        // Note that insnMap has one entry for this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                        hasEscs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                        int size = bc_escrefsize.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                        Entry ref = bc_escref.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                        if (size == 1)  ldcRefSet.add(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                        int fmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                        switch (size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                        case 1: fmt = Fixups.U1_FORMAT; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                        case 2: fmt = Fixups.U2_FORMAT; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                        default: assert(false); fmt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                        fixupBuf.add(pc, fmt, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                        buf[pc+0] = buf[pc+1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                        pc += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                case _byte_escape:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                        // Note that insnMap has one entry for all these bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                        hasEscs = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                        int size = bc_escsize.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                        while ((pc + size) > buf.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                            buf = realloc(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                        while (size-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                            buf[pc++] = (byte) bc_escbyte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                    if (Instruction.isInvokeInitOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                        int idx = (bc - _invokeinit_op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                        int origBC = _invokespecial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                        ClassEntry classRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                        switch (idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                        case _invokeinit_self_option:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                            classRef = thisClass; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                        case _invokeinit_super_option:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                            classRef = superClass; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                            assert(idx == _invokeinit_new_option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                            classRef = newClass; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                        buf[pc++] = (byte) origBC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                        int coding = bc_initref.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                        // Find the nth overloading of <init> in classRef.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                        MemberEntry ref = pkg.cp.getOverloadingForIndex(CONSTANT_Methodref, classRef, "<init>", coding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                        fixupBuf.add(pc, Fixups.U2_FORMAT, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                        buf[pc+0] = buf[pc+1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                        pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                        assert(Instruction.opLength(origBC) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                    if (Instruction.isSelfLinkerOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                        int idx = (bc - _self_linker_op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                        boolean isSuper = (idx >= _self_linker_super_flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                        if (isSuper)  idx -= _self_linker_super_flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                        boolean isAload = (idx >= _self_linker_aload_flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                        if (isAload)  idx -= _self_linker_aload_flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                        int origBC = _first_linker_op + idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                        boolean isField = Instruction.isFieldOp(origBC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                        CPRefBand bc_which;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                        ClassEntry which_cls  = isSuper ? superClass : thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                        Index which_ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                        if (isField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                            bc_which = isSuper ? bc_superfield  : bc_thisfield;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                            which_ix = pkg.cp.getMemberIndex(CONSTANT_Fieldref, which_cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                            bc_which = isSuper ? bc_supermethod : bc_thismethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                            which_ix = pkg.cp.getMemberIndex(CONSTANT_Methodref, which_cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                        assert(bc_which == selfOpRefBand(bc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                        MemberEntry ref = (MemberEntry) bc_which.getRef(which_ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                        if (isAload) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                            buf[pc++] = (byte) _aload_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                            curPC = pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                            // Note: insnMap keeps the _aload_0 separate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                            insnMap[numInsns++] = curPC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                        buf[pc++] = (byte) origBC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                        fixupBuf.add(pc, Fixups.U2_FORMAT, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                        buf[pc+0] = buf[pc+1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                        pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                        assert(Instruction.opLength(origBC) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                    if (Instruction.isBranchOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                        assert(!isWide);  // no wide prefix for branches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                        int nextPC = curPC + Instruction.opLength(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                        // Make our getLabel calls later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                        labels[numLabels++] = curPC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                        //Instruction.at(buf, curPC).setBranchLabel(getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                        while (pc < nextPC)  buf[pc++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                    if (Instruction.isCPRefOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                        CPRefBand bc_which = getCPRefOpBand(bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                        Entry ref = bc_which.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                        if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                            if (bc_which == bc_classref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                                // Shorthand for class self-references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                                ref = thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                                assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                        int origBC = bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                        int size = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                        switch (bc) {
16050
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 16013
diff changeset
  2259
                        case _invokestatic_int:
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 16013
diff changeset
  2260
                            origBC = _invokestatic;
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 16013
diff changeset
  2261
                            break;
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 16013
diff changeset
  2262
                        case _invokespecial_int:
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 16013
diff changeset
  2263
                            origBC = _invokespecial;
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 16013
diff changeset
  2264
                            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                        case _ildc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                        case _cldc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                        case _fldc:
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  2268
                        case _sldc:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  2269
                        case _qldc:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                            origBC = _ldc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                            size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                            ldcRefSet.add(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                        case _ildc_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                        case _cldc_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                        case _fldc_w:
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  2277
                        case _sldc_w:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  2278
                        case _qldc_w:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                            origBC = _ldc_w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                        case _lldc2_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                        case _dldc2_w:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                            origBC = _ldc2_w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                        case _new:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                            newClass = (ClassEntry) ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                        buf[pc++] = (byte) origBC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                        int fmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                        switch (size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                        case 1: fmt = Fixups.U1_FORMAT; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                        case 2: fmt = Fixups.U2_FORMAT; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                        default: assert(false); fmt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                        fixupBuf.add(pc, fmt, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                        buf[pc+0] = buf[pc+1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                        pc += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                        if (origBC == _multianewarray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                            // Copy the trailing byte also.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                            int val = bc_byte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                            buf[pc++] = (byte) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                        } else if (origBC == _invokeinterface) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                            int argSize = ((MemberEntry)ref).descRef.typeRef.computeSize(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                            buf[pc++] = (byte)( 1 + argSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                            buf[pc++] = 0;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  2307
                        } else if (origBC == _invokedynamic) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  2308
                            buf[pc++] = 0;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
  2309
                            buf[pc++] = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                        assert(Instruction.opLength(origBC) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                    if (Instruction.isLocalSlotOp(bc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                        buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                        int local = bc_local.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                        if (isWide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                            Instruction.setShort(buf, pc, local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                            pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                            if (bc == _iinc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                                int iVal = bc_short.getInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                                Instruction.setShort(buf, pc, iVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                                pc += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                            Instruction.setByte(buf, pc, local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                            pc += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                            if (bc == _iinc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                                int iVal = bc_byte.getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                                Instruction.setByte(buf, pc, iVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                                pc += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                        assert(Instruction.opLength(bc) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                    // Random bytecode.  Just copy it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                    if (bc >= _bytecode_limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                        Utils.log.warning("unrecognized bytescode "+bc
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                                            +" "+Instruction.byteName(bc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                    assert(bc < _bytecode_limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                    buf[pc++] = (byte) bc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                    assert(Instruction.opLength(bc) == (pc - curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            // now make a permanent copy of the bytecodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            code.setBytes(realloc(buf, pc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            code.setInstructionMap(insnMap, numInsns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            // fix up labels, now that code has its insnMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            Instruction ibr = null;  // temporary branch instruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            for (int i = 0; i < numLabels; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                int curPC = labels[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                // (Note:  Passing ibr in allows reuse, a speed hack.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                ibr = Instruction.at(code.bytes, curPC, ibr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                if (ibr instanceof Instruction.Switch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                    Instruction.Switch isw = (Instruction.Switch) ibr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                    isw.setDefaultLabel(getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                    int caseCount = isw.getCaseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                    for (int j = 0; j < caseCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                        isw.setCaseLabel(j, getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                    ibr.setBranchLabel(getLabel(bc_label, code, curPC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            if (fixupBuf.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                    Utils.log.fine("Fixups in code: "+fixupBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                code.addFixups(fixupBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
}