jdk/src/java.base/share/classes/com/sun/java/util/jar/pack/Code.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17422
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: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.java.util.jar.pack.Package.Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Modifier;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import java.util.Arrays;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import java.util.Collection;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    32
import static com.sun.java.util.jar.pack.Constants.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Represents a chunk of bytecodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    38
class Code extends Attribute.Holder {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    Class.Method m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public Code(Class.Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public Class.Method getMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public Class thisClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return m.thisClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public Package getPackage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return m.thisClass().getPackage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public ConstantPool.Entry[] getCPMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return m.getCPMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    59
    private static final ConstantPool.Entry[] noRefs = ConstantPool.noRefs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // The following fields are used directly by the ClassReader, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int max_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int max_locals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    ConstantPool.Entry handler_class[] = noRefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int handler_start[] = noInts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    int handler_end[] = noInts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    int handler_catch[] = noInts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    Fixups fixups;  // reference relocations, if any are required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    Object insnMap; // array of instruction boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    int getLength() { return bytes.length; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    int getMaxStack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return max_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    void setMaxStack(int ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        max_stack = ms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    int getMaxNALocals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int argsize = m.getArgumentSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return max_locals - argsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    void setMaxNALocals(int ml) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int argsize = m.getArgumentSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        max_locals = argsize + ml;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    int getHandlerCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        assert(handler_class.length == handler_start.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        assert(handler_class.length == handler_end.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        assert(handler_class.length == handler_catch.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return handler_class.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    void setHandlerCount(int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (h > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            handler_class = new ConstantPool.Entry[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            handler_start = new int[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            handler_end   = new int[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            handler_catch = new int[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // caller must fill these in ASAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    void setBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.bytes = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (fixups != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            fixups.setBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    void setInstructionMap(int[] insnMap, int mapLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        //int[] oldMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        //assert((oldMap = getInstructionMap()) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.insnMap = allocateInstructionMap(insnMap, mapLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        //assert(Arrays.equals(oldMap, getInstructionMap()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    void setInstructionMap(int[] insnMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        setInstructionMap(insnMap, insnMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    int[] getInstructionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return expandInstructionMap(getInsnMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   128
    void addFixups(Collection<Fixups.Fixup> moreFixups) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (fixups == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            fixups = new Fixups(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        assert(fixups.getBytes() == bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        fixups.addAll(moreFixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (fixups != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            fixups.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (fixups.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                fixups = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        super.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   145
    protected void visitRefs(int mode, Collection<ConstantPool.Entry> refs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int verbose = getPackage().verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            System.out.println("Reference scan "+this);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   149
        refs.addAll(Arrays.asList(handler_class));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (fixups != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            fixups.visitRefs(refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // References (to a local cpMap) are embedded in the bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            ConstantPool.Entry[] cpMap = getCPMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            for (Instruction i = instructionAt(0); i != null; i = i.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (verbose > 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    System.out.println(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                int cpref = i.getCPIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (cpref >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    refs.add(cpMap[cpref]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // Handle attribute list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        super.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // Since bytecodes are the single largest contributor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // package size, it's worth a little bit of trouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // to reduce the per-bytecode memory footprint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // In the current scheme, half of the bulk of these arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // due to bytes, and half to shorts.  (Ints are insignificant.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // Given an average of 1.8 bytes per instruction, this means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // instruction boundary arrays are about a 75% overhead--tolerable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // (By using bytes, we get 33% savings over just shorts and ints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // Using both bytes and shorts gives 66% savings over just ints.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static final boolean shrinkMaps = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private Object allocateInstructionMap(int[] insnMap, int mapLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int PClimit = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (shrinkMaps && PClimit <= Byte.MAX_VALUE - Byte.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            byte[] map = new byte[mapLen+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            for (int i = 0; i < mapLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                map[i] = (byte)(insnMap[i] + Byte.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            map[mapLen] = (byte)(PClimit + Byte.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else if (shrinkMaps && PClimit < Short.MAX_VALUE - Short.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            short[] map = new short[mapLen+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            for (int i = 0; i < mapLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                map[i] = (short)(insnMap[i] + Short.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            map[mapLen] = (short)(PClimit + Short.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   196
            int[] map = Arrays.copyOf(insnMap, mapLen + 1);
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   197
            map[mapLen] = PClimit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private int[] expandInstructionMap(Object map0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int[] imap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (map0 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            byte[] map = (byte[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            imap = new int[map.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for (int i = 0; i < imap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                imap[i] = map[i] - Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else if (map0 instanceof short[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            short[] map = (short[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            imap = new int[map.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            for (int i = 0; i < imap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                imap[i] = map[i] - Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            int[] map = (int[]) map0;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   217
            imap = Arrays.copyOfRange(map, 0, map.length - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return imap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    Object getInsnMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // Build a map of instruction boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (insnMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return insnMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int[] map = new int[getLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int fillp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (Instruction i = instructionAt(0); i != null; i = i.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            map[fillp++] = i.getPC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // Make it byte[], short[], or int[] according to the max BCI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        insnMap = allocateInstructionMap(map, fillp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        //assert(assertBCICodingsOK());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return insnMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /** Encode the given BCI as an instruction boundary number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *  For completeness, irregular (non-boundary) BCIs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *  encoded compactly immediately after the boundary numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *  This encoding is the identity mapping outside 0..length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *  and it is 1-1 everywhere.  All by itself this technique
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *  improved zipped rt.jar compression by 2.6%.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public int encodeBCI(int bci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (bci <= 0 || bci > getLength())  return bci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        Object map0 = getInsnMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int i, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (shrinkMaps && map0 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            byte[] map = (byte[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            i = Arrays.binarySearch(map, (byte)(bci + Byte.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else if (shrinkMaps && map0 instanceof short[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            short[] map = (short[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            i = Arrays.binarySearch(map, (short)(bci + Short.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int[] map = (int[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            len = map.length;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   260
            i = Arrays.binarySearch(map, bci);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        assert(i != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        assert(i != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        assert(i != len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        assert(i != -len-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return (i >= 0) ? i : len + bci - (-i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public int decodeBCI(int bciCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (bciCode <= 0 || bciCode > getLength())  return bciCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        Object map0 = getInsnMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int i, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // len == map.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // If bciCode < len, result is map[bciCode], the common and fast case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // Otherwise, let map[i] be the smallest map[*] larger than bci.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Then, required by the return statement of encodeBCI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        //   bciCode == len + bci - i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // Thus:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        //   bci-i == bciCode-len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        //   map[i]-adj-i == bciCode-len ; adj in (0..map[i]-map[i-1])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // We can solve this by searching for adjacent entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // map[i-1], map[i] such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        //   map[i-1]-(i-1) <= bciCode-len < map[i]-i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // This can be approximated by searching map[i] for bciCode and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // linear searching backward.  Given the right i, we then have:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        //   bci == bciCode-len + i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // This linear search is at its worst case for indexes in the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // of a large method, but it's not clear that this is a problem in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // practice, since BCIs are usually on instruction boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (shrinkMaps && map0 instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            byte[] map = (byte[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (bciCode < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                return map[bciCode] - Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            i = Arrays.binarySearch(map, (byte)(bciCode + Byte.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (i < 0)  i = -i-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            int key = bciCode-len + Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            for (;; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if (map[i-1]-(i-1) <= key)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else if (shrinkMaps && map0 instanceof short[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            short[] map = (short[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (bciCode < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                return map[bciCode] - Short.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            i = Arrays.binarySearch(map, (short)(bciCode + Short.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (i < 0)  i = -i-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            int key = bciCode-len + Short.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            for (;; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                if (map[i-1]-(i-1) <= key)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            int[] map = (int[]) map0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            len = map.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (bciCode < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                return map[bciCode];
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   316
            i = Arrays.binarySearch(map, bciCode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (i < 0)  i = -i-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            int key = bciCode-len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            for (;; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (map[i-1]-(i-1) <= key)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return bciCode-len + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public void finishRefs(ConstantPool.Index ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (fixups != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            fixups.finishRefs(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            fixups = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // Code attributes are finished in ClassWriter.writeAttributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    Instruction instructionAt(int pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return Instruction.at(bytes, pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    static boolean flagsRequireCode(int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // A method's flags force it to have a Code attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // if the flags are neither native nor abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return (flags & (Modifier.NATIVE | Modifier.ABSTRACT)) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return m+".Code";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /// Fetching values from my own array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public int getInt(int pc)    { return Instruction.getInt(bytes, pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public int getShort(int pc)  { return Instruction.getShort(bytes, pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public int getByte(int pc)   { return Instruction.getByte(bytes, pc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    void setInt(int pc, int x)   { Instruction.setInt(bytes, pc, x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    void setShort(int pc, int x) { Instruction.setShort(bytes, pc, x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    void setByte(int pc, int x)  { Instruction.setByte(bytes, pc, x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
/* TEST CODE ONLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private boolean assertBCICodingsOK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        int len = java.lang.reflect.Array.getLength(insnMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        int base = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (insnMap.getClass().getComponentType() == Byte.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            base = Byte.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (insnMap.getClass().getComponentType() == Short.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            base = Short.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        for (int i = -1, imax = getLength()+1; i <= imax; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            int bci = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            int enc = Math.min(-999, bci-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            int dec = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                enc = encodeBCI(bci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                dec = decodeBCI(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            } catch (RuntimeException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (dec == bci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                //System.out.println("BCI="+bci+(enc<len?"":"   ")+" enc="+enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                for (int q = 0; q <= 1; q++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    sb.append("bci "+(q==0?"map":"del")+"["+len+"] = {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        int mapi = ((Number)java.lang.reflect.Array.get(insnMap, j)).intValue() - base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        mapi -= j*q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        sb.append(" "+mapi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    sb.append(" }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    System.out.println("*** "+sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            System.out.println("*** BCI="+bci+" enc="+enc+" dec="+dec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
//*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}