src/java.base/share/classes/com/sun/java/util/jar/pack/ClassReader.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    28
import com.sun.java.util.jar.pack.ConstantPool.ClassEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    29
import com.sun.java.util.jar.pack.ConstantPool.DescriptorEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    30
import com.sun.java.util.jar.pack.ConstantPool.Entry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    31
import com.sun.java.util.jar.pack.ConstantPool.SignatureEntry;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    32
import com.sun.java.util.jar.pack.ConstantPool.MemberEntry;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    33
import com.sun.java.util.jar.pack.ConstantPool.MethodHandleEntry;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    34
import com.sun.java.util.jar.pack.ConstantPool.BootstrapMethodEntry;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    35
import com.sun.java.util.jar.pack.ConstantPool.Utf8Entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.java.util.jar.pack.Package.Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.java.util.jar.pack.Package.InnerClass;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    38
import java.io.DataInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    39
import java.io.FilterInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    40
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    41
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    42
import java.util.ArrayList;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    43
import java.util.Arrays;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    44
import java.util.Map;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    45
import static com.sun.java.util.jar.pack.Constants.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Reader for a class file that is being incorporated into a package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    51
class ClassReader {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    Package pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    Class cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    long inPos;
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
    57
    long constantPoolLimit = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    DataInputStream in;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    59
    Map<Attribute.Layout, Attribute> attrDefs;
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7802
diff changeset
    60
    Map<Attribute.Layout, String> attrCommands;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    String unknownAttrCommand = "error";;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    ClassReader(Class cls, InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.pkg = cls.getPackage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.cls = cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.verbose = pkg.verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.in = new DataInputStream(new FilterInputStream(in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                int nr = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (nr >= 0)  inPos += nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                int ch = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (ch >= 0)  inPos += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                long ns = super.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if (ns >= 0)  inPos += ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                return ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    86
    public void setAttrDefs(Map<Attribute.Layout, Attribute> attrDefs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.attrDefs = attrDefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7802
diff changeset
    90
    public void setAttrCommands(Map<Attribute.Layout, String> attrCommands) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.attrCommands = attrCommands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private void skip(int n, String what) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Utils.log.warning("skipping "+n+" bytes of "+what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        long skipped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        while (skipped < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            long j = in.skip(n - skipped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            assert(j > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            skipped += j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        assert(skipped == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private int readUnsignedShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private int readInt() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /** Read a 2-byte int, and return the <em>global</em> CP entry for it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private Entry readRef() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int i = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return i == 0 ? null : cls.cpMap[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private Entry readRef(byte tag) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Entry e = readRef();
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   121
        assert(!(e instanceof UnresolvedEntry));
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   122
        checkTag(e, tag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   126
    /** Throw a ClassFormatException if the entry does not match the expected tag type. */
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   127
    private Entry checkTag(Entry e, byte tag) throws ClassFormatException {
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   128
        if (e == null || !e.tagMatches(tag)) {
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   129
            String where = (inPos == constantPoolLimit
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   130
                                ? " in constant pool"
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   131
                                : " at pos: " + inPos);
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   132
            String got = (e == null
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   133
                            ? "null CP index"
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   134
                            : "type=" + ConstantPool.tagName(e.tag));
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   135
            throw new ClassFormatException("Bad constant, expected type=" +
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   136
                    ConstantPool.tagName(tag) +
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   137
                    " got "+ got + ", in File: " + cls.file.nameString + where);
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   138
        }
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   139
        return e;
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   140
    }
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   141
    private Entry checkTag(Entry e, byte tag, boolean nullOK) throws ClassFormatException {
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   142
        return nullOK && e == null ? null : checkTag(e, tag);
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   143
    }
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   144
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private Entry readRefOrNull(byte tag) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Entry e = readRef();
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   147
        checkTag(e, tag, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private Utf8Entry readUtf8Ref() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return (Utf8Entry) readRef(CONSTANT_Utf8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private ClassEntry readClassRef() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return (ClassEntry) readRef(CONSTANT_Class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private ClassEntry readClassRefOrNull() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return (ClassEntry) readRefOrNull(CONSTANT_Class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private SignatureEntry readSignatureRef() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // The class file stores a Utf8, but we want a Signature.
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   165
        Entry e = readRef(CONSTANT_Signature);
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   166
        return (e != null && e.getTag() == CONSTANT_Utf8)
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   167
                ? ConstantPool.getSignatureEntry(e.stringValue())
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   168
                : (SignatureEntry) e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    void read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            readMagicNumbers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            readConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            readMembers(false);  // fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            readMembers(true);   // methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            readAttributes(ATTR_CONTEXT_CLASS, cls);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   180
            fixUnresolvedEntries();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            cls.finishReading();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            assert(0 >= in.read(new byte[1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if (verbose > 0) Utils.log.warning("Erroneous data at input offset "+inPos+" of "+cls.file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    void readMagicNumbers() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        cls.magic = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (cls.magic != JAVA_MAGIC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new Attribute.FormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                ("Bad magic number in class file "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                 +Integer.toHexString(cls.magic),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                 ATTR_CONTEXT_CLASS, "magic-number", "pass");
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   198
        int minver = (short) readUnsignedShort();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   199
        int majver = (short) readUnsignedShort();
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   200
        cls.version = 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
   201
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        //System.out.println("ClassFile.version="+cls.majver+"."+cls.minver);
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   203
        String bad = checkVersion(cls.version);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new Attribute.FormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                ("classfile version too "+bad+": "
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   207
                 +cls.version+" in "+cls.file,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                 ATTR_CONTEXT_CLASS, "version", "pass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   212
    private String checkVersion(Package.Version ver) {
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   213
        int majver = ver.major;
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   214
        int minver = ver.minor;
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   215
        if (majver < pkg.minClassVersion.major ||
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   216
            (majver == pkg.minClassVersion.major &&
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   217
             minver < pkg.minClassVersion.minor)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return "small";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   220
        if (majver > pkg.maxClassVersion.major ||
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   221
            (majver == pkg.maxClassVersion.major &&
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   222
             minver > pkg.maxClassVersion.minor)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return "large";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return null;  // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    void readConstantPool() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int length = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        //System.err.println("reading CP, length="+length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int[] fixups = new int[length*4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int fptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        Entry[] cpMap = new Entry[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        cpMap[0] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (int i = 1; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            //System.err.println("reading CP elt, i="+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int tag = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                case CONSTANT_Utf8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    cpMap[i] = ConstantPool.getUtf8Entry(in.readUTF());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                case CONSTANT_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   246
                        cpMap[i] = ConstantPool.getLiteralEntry(in.readInt());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                case CONSTANT_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   251
                        cpMap[i] = ConstantPool.getLiteralEntry(in.readFloat());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                case CONSTANT_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   256
                        cpMap[i] = ConstantPool.getLiteralEntry(in.readLong());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        cpMap[++i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                case CONSTANT_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   262
                        cpMap[i] = ConstantPool.getLiteralEntry(in.readDouble());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        cpMap[++i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                // just read the refs; do not attempt to resolve while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                case CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                case CONSTANT_String:
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   270
                case CONSTANT_MethodType:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    fixups[fptr++] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    fixups[fptr++] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    fixups[fptr++] = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    fixups[fptr++] = -1;  // empty ref2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                case CONSTANT_Fieldref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                case CONSTANT_Methodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                case CONSTANT_InterfaceMethodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                case CONSTANT_NameandType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    fixups[fptr++] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    fixups[fptr++] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    fixups[fptr++] = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    fixups[fptr++] = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    break;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   285
                case CONSTANT_InvokeDynamic:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   286
                    fixups[fptr++] = i;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   287
                    fixups[fptr++] = tag;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   288
                    fixups[fptr++] = -1 ^ in.readUnsignedShort();  // not a ref
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   289
                    fixups[fptr++] = in.readUnsignedShort();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   290
                    break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   291
                case CONSTANT_MethodHandle:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   292
                    fixups[fptr++] = i;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   293
                    fixups[fptr++] = tag;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   294
                    fixups[fptr++] = -1 ^ in.readUnsignedByte();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   295
                    fixups[fptr++] = in.readUnsignedShort();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   296
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                default:
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   298
                    throw new ClassFormatException("Bad constant pool tag " +
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   299
                            tag + " in File: " + cls.file.nameString +
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   300
                            " at pos: " + inPos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   303
        constantPoolLimit = inPos;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // Fix up refs, which might be out of order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        while (fptr > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (verbose > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                Utils.log.fine("CP fixups ["+fptr/4+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            int flimit = fptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            fptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            for (int fi = 0; fi < flimit; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                int cpi = fixups[fi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                int tag = fixups[fi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                int ref = fixups[fi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                int ref2 = fixups[fi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (verbose > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    Utils.log.fine("  cp["+cpi+"] = "+ConstantPool.tagName(tag)+"{"+ref+","+ref2+"}");
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   318
                if (ref >= 0 && cpMap[ref] == null || ref2 >= 0 && cpMap[ref2] == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    // Defer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    fixups[fptr++] = cpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    fixups[fptr++] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    fixups[fptr++] = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    fixups[fptr++] = ref2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                case CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    cpMap[cpi] = ConstantPool.getClassEntry(cpMap[ref].stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                case CONSTANT_String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    cpMap[cpi] = ConstantPool.getStringEntry(cpMap[ref].stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                case CONSTANT_Fieldref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                case CONSTANT_Methodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                case CONSTANT_InterfaceMethodref:
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   336
                    ClassEntry      mclass = (ClassEntry)      checkTag(cpMap[ref],  CONSTANT_Class);
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   337
                    DescriptorEntry mdescr = (DescriptorEntry) checkTag(cpMap[ref2], CONSTANT_NameandType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    cpMap[cpi] = ConstantPool.getMemberEntry((byte)tag, mclass, mdescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                case CONSTANT_NameandType:
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   341
                    Utf8Entry mname = (Utf8Entry) checkTag(cpMap[ref],  CONSTANT_Utf8);
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   342
                    Utf8Entry mtype = (Utf8Entry) checkTag(cpMap[ref2], CONSTANT_Signature);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    cpMap[cpi] = ConstantPool.getDescriptorEntry(mname, mtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    break;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   345
                case CONSTANT_MethodType:
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   346
                    cpMap[cpi] = ConstantPool.getMethodTypeEntry((Utf8Entry) checkTag(cpMap[ref], CONSTANT_Signature));
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   347
                    break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   348
                case CONSTANT_MethodHandle:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   349
                    byte refKind = (byte)(-1 ^ ref);
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   350
                    MemberEntry memRef = (MemberEntry) checkTag(cpMap[ref2], CONSTANT_AnyMember);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   351
                    cpMap[cpi] = ConstantPool.getMethodHandleEntry(refKind, memRef);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   352
                    break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   353
                case CONSTANT_InvokeDynamic:
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   354
                    DescriptorEntry idescr = (DescriptorEntry) checkTag(cpMap[ref2], CONSTANT_NameandType);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   355
                    cpMap[cpi] = new UnresolvedEntry((byte)tag, (-1 ^ ref), idescr);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   356
                    // Note that ref must be resolved later, using the BootstrapMethods attribute.
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   357
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            assert(fptr < flimit);  // Must make progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        cls.cpMap = cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   368
    private /*non-static*/
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   369
    class UnresolvedEntry extends Entry {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   370
        final Object[] refsOrIndexes;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   371
        UnresolvedEntry(byte tag, Object... refsOrIndexes) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   372
            super(tag);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   373
            this.refsOrIndexes = refsOrIndexes;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   374
            ClassReader.this.haveUnresolvedEntry = true;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   375
        }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   376
        Entry resolve() {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   377
            Class cls = ClassReader.this.cls;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   378
            Entry res;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   379
            switch (tag) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   380
            case CONSTANT_InvokeDynamic:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   381
                BootstrapMethodEntry iboots = cls.bootstrapMethods.get((Integer) refsOrIndexes[0]);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   382
                DescriptorEntry         idescr = (DescriptorEntry) refsOrIndexes[1];
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   383
                res = ConstantPool.getInvokeDynamicEntry(iboots, idescr);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   384
                break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   385
            default:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   386
                throw new AssertionError();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   387
            }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   388
            return res;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   389
        }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   390
        private void unresolved() { throw new RuntimeException("unresolved entry has no string"); }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   391
        public int compareTo(Object x) { unresolved(); return 0; }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   392
        public boolean equals(Object x) { unresolved(); return false; }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   393
        protected int computeValueHash() { unresolved(); return 0; }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   394
        public String stringValue() { unresolved(); return toString(); }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   395
        public String toString() { return "(unresolved "+ConstantPool.tagName(tag)+")"; }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   396
    }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   397
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   398
    boolean haveUnresolvedEntry;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   399
    private void fixUnresolvedEntries() {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   400
        if (!haveUnresolvedEntry)  return;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   401
        Entry[] cpMap = cls.getCPMap();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   402
        for (int i = 0; i < cpMap.length; i++) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   403
            Entry e = cpMap[i];
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   404
            if (e instanceof UnresolvedEntry) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   405
                cpMap[i] = e = ((UnresolvedEntry)e).resolve();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   406
                assert(!(e instanceof UnresolvedEntry));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   407
            }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   408
        }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   409
        haveUnresolvedEntry = false;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   410
    }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   411
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    void readHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        cls.flags = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        cls.thisClass = readClassRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        cls.superClass = readClassRefOrNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int ni = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        cls.interfaces = new ClassEntry[ni];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        for (int i = 0; i < ni; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            cls.interfaces[i] = readClassRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    void readMembers(boolean doMethods) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        int nm = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        for (int i = 0; i < nm; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            readMember(doMethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    void readMember(boolean doMethod) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int    mflags = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        Utf8Entry       mname = readUtf8Ref();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        SignatureEntry  mtype = readSignatureRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        DescriptorEntry descr = ConstantPool.getDescriptorEntry(mname, mtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        Class.Member m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (!doMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            m = cls.new Field(mflags, descr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            m = cls.new Method(mflags, descr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        readAttributes(!doMethod ? ATTR_CONTEXT_FIELD : ATTR_CONTEXT_METHOD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                       m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    void readAttributes(int ctype, Attribute.Holder h) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        int na = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (na == 0)  return;  // nothing to do here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (verbose > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            Utils.log.fine("readAttributes "+h+" ["+na+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        for (int i = 0; i < na; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            String name = readUtf8Ref().stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            int length = readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            // See if there is a special command that applies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (attrCommands != null) {
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7802
diff changeset
   453
                Attribute.Layout lkey = Attribute.keyForLookup(ctype, name);
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7802
diff changeset
   454
                String cmd = attrCommands.get(lkey);
7802
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   455
                if (cmd != null) {
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   456
                    switch (cmd) {
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   457
                        case "pass":
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   458
                            String message1 = "passing attribute bitwise in " + h;
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   459
                            throw new Attribute.FormatException(message1, ctype, name, cmd);
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   460
                        case "error":
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   461
                            String message2 = "attribute not allowed in " + h;
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   462
                            throw new Attribute.FormatException(message2, ctype, name, cmd);
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   463
                        case "strip":
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   464
                            skip(length, name + " attribute in " + h);
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   465
                            continue;
74f2ee2b62ba 7007157: (pack200) stripping attributes causes a NPE
ksrini
parents: 7795
diff changeset
   466
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            // Find canonical instance of the requested attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            Attribute a = Attribute.lookup(Package.attrDefs, ctype, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (verbose > 4 && a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                Utils.log.fine("pkg_attribute_lookup "+name+" = "+a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                a = Attribute.lookup(this.attrDefs, ctype, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (verbose > 4 && a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    Utils.log.fine("this "+name+" = "+a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                a = Attribute.lookup(null, ctype, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                if (verbose > 4 && a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    Utils.log.fine("null_attribute_lookup "+name+" = "+a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            if (a == null && length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                // Any zero-length attr is "known"...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                // We can assume an empty attr. has an empty layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                // Handles markers like Enum, Bridge, Synthetic, Deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                a = Attribute.find(ctype, name, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            boolean isStackMap = (ctype == ATTR_CONTEXT_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                  && (name.equals("StackMap") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                      name.equals("StackMapX")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (isStackMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                // Known attribute but with a corner case format, "pass" it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                Code code = (Code) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                final int TOO_BIG = 0x10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                if (code.max_stack   >= TOO_BIG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    code.max_locals  >= TOO_BIG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    code.getLength() >= TOO_BIG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    name.endsWith("X")) {
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   500
                    // No, we don't really know what to do with this one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    // Do not compress the rare and strange "u4" and "X" cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    a = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                if (isStackMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    // Known attribute but w/o a format; pass it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    String message = "unsupported StackMap variant in "+h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    throw new Attribute.FormatException(message, ctype, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                                        "pass");
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   511
                } else if ("strip".equals(unknownAttrCommand)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    // Skip the unknown attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    skip(length, "unknown "+name+" attribute in "+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                } else {
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   516
                    String message = " is unknown attribute in class " + h;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    throw new Attribute.FormatException(message, ctype, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                                        unknownAttrCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   521
            long pos0 = inPos;  // in case we want to check it
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   522
            if (a.layout() == Package.attrCodeEmpty) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                // These are hardwired.
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   524
                Class.Method m = (Class.Method) h;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   525
                m.code = new Code(m);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   526
                try {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   527
                    readCode(m.code);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   528
                } catch (Instruction.FormatException iie) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   529
                    String message = iie.getMessage() + " in " + h;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   530
                    throw new ClassReader.ClassFormatException(message, iie);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                assert(length == inPos - pos0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                // Keep empty attribute a...
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   534
            } else if (a.layout() == Package.attrBootstrapMethodsEmpty) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   535
                assert(h == cls);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   536
                readBootstrapMethods(cls);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   537
                assert(length == inPos - pos0);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   538
                // Delete the attribute; it is logically part of the constant pool.
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   539
                continue;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   540
            } else if (a.layout() == Package.attrInnerClassesEmpty) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   541
                // These are hardwired also.
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   542
                assert(h == cls);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   543
                readInnerClasses(cls);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   544
                assert(length == inPos - pos0);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   545
                // Keep empty attribute a...
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            } else if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                byte[] bytes = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                in.readFully(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                a = a.addContent(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   551
            if (a.size() == 0 && !a.layout().isEmpty()) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   552
                throw new ClassFormatException(name +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   553
                        ": attribute length cannot be zero, in " + h);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   554
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            h.addAttribute(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                Utils.log.fine("read "+a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    void readCode(Code code) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        code.max_stack = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        code.max_locals = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        code.bytes = new byte[readInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        in.readFully(code.bytes);
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   566
        Entry[] cpMap = cls.getCPMap();
16050
1eee624cddb3 8007297: [pack200] allow opcodes with InterfaceMethodRefs
ksrini
parents: 15526
diff changeset
   567
        Instruction.opcodeChecker(code.bytes, cpMap, this.cls.version);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        int nh = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        code.setHandlerCount(nh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        for (int i = 0; i < nh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            code.handler_start[i] = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            code.handler_end[i]   = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            code.handler_catch[i] = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            code.handler_class[i] = readClassRefOrNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        readAttributes(ATTR_CONTEXT_CODE, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   579
    void readBootstrapMethods(Class cls) throws IOException {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   580
        BootstrapMethodEntry[] bsms = new BootstrapMethodEntry[readUnsignedShort()];
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   581
        for (int i = 0; i < bsms.length; i++) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   582
            MethodHandleEntry bsmRef = (MethodHandleEntry) readRef(CONSTANT_MethodHandle);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   583
            Entry[] argRefs = new Entry[readUnsignedShort()];
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   584
            for (int j = 0; j < argRefs.length; j++) {
15526
84de8685a2d0 8003549: (pack200) assertion errors when processing lambda class files with IMethods
ksrini
parents: 12857
diff changeset
   585
                argRefs[j] = readRef(CONSTANT_LoadableValue);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   586
            }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   587
            bsms[i] = ConstantPool.getBootstrapMethodEntry(bsmRef, argRefs);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   588
        }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   589
        cls.setBootstrapMethods(Arrays.asList(bsms));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   590
    }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   591
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    void readInnerClasses(Class cls) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        int nc = readUnsignedShort();
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   594
        ArrayList<InnerClass> ics = new ArrayList<>(nc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        for (int i = 0; i < nc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            InnerClass ic =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                new InnerClass(readClassRef(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                               readClassRefOrNull(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                               (Utf8Entry)readRefOrNull(CONSTANT_Utf8),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                               readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            ics.add(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        cls.innerClasses = ics;  // set directly; do not use setInnerClasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        // (Later, ics may be transferred to the pkg.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   606
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   607
    static class ClassFormatException extends IOException {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
   608
        @java.io.Serial
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7802
diff changeset
   609
        private static final long serialVersionUID = -3564121733989501833L;
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7802
diff changeset
   610
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   611
        public ClassFormatException(String message) {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   612
            super(message);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   613
        }
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   614
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   615
        public ClassFormatException(String message, Throwable cause) {
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   616
            super(message, cause);
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   617
        }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   618
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
}