001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017
018package org.apache.bcel.classfile;
019
020import java.io.DataInput;
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.util.Arrays;
024
025import org.apache.bcel.Const;
026
027/**
028 * This class represents a stack map entry recording the types of local variables and the of stack items at a given
029 * byte code offset. See CLDC specification 5.3.1.2.
030 *
031 * See also https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.4
032 *
033 * <pre>
034 * union stack_map_frame {
035 *   same_frame;
036 *   same_locals_1_stack_item_frame;
037 *   same_locals_1_stack_item_frame_extended;
038 *   chop_frame;
039 *   same_frame_extended;
040 *   append_frame;
041 *   full_frame;
042 * }
043 * </pre>
044 * @see StackMap
045 * @see StackMapType
046 */
047public final class StackMapEntry implements Node, Cloneable {
048
049    static final StackMapEntry[] EMPTY_ARRAY = {};
050
051    private int frameType;
052    private int byteCodeOffset;
053    private StackMapType[] typesOfLocals;
054    private StackMapType[] typesOfStackItems;
055    private ConstantPool constantPool;
056
057    /**
058     * Constructs object from input stream.
059     *
060     * @param dataInput Input stream
061     * @throws IOException if an I/O error occurs.
062     */
063    StackMapEntry(final DataInput dataInput, final ConstantPool constantPool) throws IOException {
064        this(dataInput.readByte() & 0xFF, -1, null, null, constantPool);
065
066        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
067            byteCodeOffset = frameType - Const.SAME_FRAME;
068        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
069            byteCodeOffset = frameType - Const.SAME_LOCALS_1_STACK_ITEM_FRAME;
070            typesOfStackItems = new StackMapType[] { new StackMapType(dataInput, constantPool) };
071        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
072            byteCodeOffset = dataInput.readUnsignedShort();
073            typesOfStackItems = new StackMapType[] { new StackMapType(dataInput, constantPool) };
074        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
075            byteCodeOffset = dataInput.readUnsignedShort();
076        } else if (frameType == Const.SAME_FRAME_EXTENDED) {
077            byteCodeOffset = dataInput.readUnsignedShort();
078        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
079            byteCodeOffset = dataInput.readUnsignedShort();
080            final int numberOfLocals = frameType - 251;
081            typesOfLocals = new StackMapType[numberOfLocals];
082            for (int i = 0; i < numberOfLocals; i++) {
083                typesOfLocals[i] = new StackMapType(dataInput, constantPool);
084            }
085        } else if (frameType == Const.FULL_FRAME) {
086            byteCodeOffset = dataInput.readUnsignedShort();
087            final int numberOfLocals = dataInput.readUnsignedShort();
088            typesOfLocals = new StackMapType[numberOfLocals];
089            for (int i = 0; i < numberOfLocals; i++) {
090                typesOfLocals[i] = new StackMapType(dataInput, constantPool);
091            }
092            final int numberOfStackItems = dataInput.readUnsignedShort();
093            typesOfStackItems = new StackMapType[numberOfStackItems];
094            for (int i = 0; i < numberOfStackItems; i++) {
095                typesOfStackItems[i] = new StackMapType(dataInput, constantPool);
096            }
097        } else {
098            /* Can't happen */
099            throw new ClassFormatException("Invalid frame type found while parsing stack map table: " + frameType);
100        }
101    }
102
103    /**
104     * DO NOT USE
105     *
106     * @param byteCodeOffset
107     * @param numberOfLocals NOT USED
108     * @param typesOfLocals array of {@link StackMapType}s of locals
109     * @param numberOfStackItems NOT USED
110     * @param typesOfStackItems array ot {@link StackMapType}s of stack items
111     * @param constantPool the constant pool
112     * @deprecated Since 6.0, use {@link #StackMapEntry(int, int, StackMapType[], StackMapType[], ConstantPool)} instead
113     */
114    @java.lang.Deprecated
115    public StackMapEntry(final int byteCodeOffset, final int numberOfLocals, final StackMapType[] typesOfLocals, final int numberOfStackItems,
116        final StackMapType[] typesOfStackItems, final ConstantPool constantPool) {
117        this.byteCodeOffset = byteCodeOffset;
118        this.typesOfLocals = typesOfLocals != null ? typesOfLocals : StackMapType.EMPTY_ARRAY;
119        this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : StackMapType.EMPTY_ARRAY;
120        this.constantPool = constantPool;
121        if (numberOfLocals < 0) {
122            throw new IllegalArgumentException("numberOfLocals < 0");
123        }
124        if (numberOfStackItems < 0) {
125            throw new IllegalArgumentException("numberOfStackItems < 0");
126        }
127    }
128
129    /**
130     * Create an instance
131     *
132     * @param tag the frameType to use
133     * @param byteCodeOffset
134     * @param typesOfLocals array of {@link StackMapType}s of locals
135     * @param typesOfStackItems array ot {@link StackMapType}s of stack items
136     * @param constantPool the constant pool
137     */
138    public StackMapEntry(final int tag, final int byteCodeOffset, final StackMapType[] typesOfLocals, final StackMapType[] typesOfStackItems,
139        final ConstantPool constantPool) {
140        this.frameType = tag;
141        this.byteCodeOffset = byteCodeOffset;
142        this.typesOfLocals = typesOfLocals != null ? typesOfLocals : StackMapType.EMPTY_ARRAY;
143        this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : StackMapType.EMPTY_ARRAY;
144        this.constantPool = constantPool;
145    }
146
147    /**
148     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
149     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
150     *
151     * @param v Visitor object
152     */
153    @Override
154    public void accept(final Visitor v) {
155        v.visitStackMapEntry(this);
156    }
157
158    /**
159     * @return deep copy of this object
160     */
161    public StackMapEntry copy() {
162        StackMapEntry e;
163        try {
164            e = (StackMapEntry) clone();
165        } catch (final CloneNotSupportedException ex) {
166            throw new UnsupportedOperationException("Clone Not Supported", ex);
167        }
168
169        e.typesOfLocals = new StackMapType[typesOfLocals.length];
170        Arrays.setAll(e.typesOfLocals, i -> typesOfLocals[i].copy());
171        e.typesOfStackItems = new StackMapType[typesOfStackItems.length];
172        Arrays.setAll(e.typesOfStackItems, i -> typesOfStackItems[i].copy());
173        return e;
174    }
175
176    /**
177     * Dump stack map entry
178     *
179     * @param file Output file stream
180     * @throws IOException if an I/O error occurs.
181     */
182    public void dump(final DataOutputStream file) throws IOException {
183        file.write(frameType);
184        if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
185            typesOfStackItems[0].dump(file);
186        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
187            file.writeShort(byteCodeOffset);
188            typesOfStackItems[0].dump(file);
189        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
190            file.writeShort(byteCodeOffset);
191        } else if (frameType == Const.SAME_FRAME_EXTENDED) {
192            file.writeShort(byteCodeOffset);
193        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
194            file.writeShort(byteCodeOffset);
195            for (final StackMapType type : typesOfLocals) {
196                type.dump(file);
197            }
198        } else if (frameType == Const.FULL_FRAME) {
199            file.writeShort(byteCodeOffset);
200            file.writeShort(typesOfLocals.length);
201            for (final StackMapType type : typesOfLocals) {
202                type.dump(file);
203            }
204            file.writeShort(typesOfStackItems.length);
205            for (final StackMapType type : typesOfStackItems) {
206                type.dump(file);
207            }
208        } else if (!(frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX)) {
209            /* Can't happen */
210            throw new ClassFormatException("Invalid Stack map table tag: " + frameType);
211        }
212    }
213
214    public int getByteCodeOffset() {
215        return byteCodeOffset;
216    }
217
218    /**
219     * @return Constant pool used by this object.
220     */
221    public ConstantPool getConstantPool() {
222        return constantPool;
223    }
224
225    public int getFrameType() {
226        return frameType;
227    }
228
229    /**
230     * Calculate stack map entry size
231     *
232     */
233    int getMapEntrySize() {
234        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
235            return 1;
236        }
237        if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
238            return 1 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
239        }
240        if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
241            return 3 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
242        }
243        if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX || frameType == Const.SAME_FRAME_EXTENDED) {
244            return 3;
245        }
246        if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
247            int len = 3;
248            for (final StackMapType typesOfLocal : typesOfLocals) {
249                len += typesOfLocal.hasIndex() ? 3 : 1;
250            }
251            return len;
252        }
253        if (frameType != Const.FULL_FRAME) {
254            throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
255        }
256        int len = 7;
257        for (final StackMapType typesOfLocal : typesOfLocals) {
258            len += typesOfLocal.hasIndex() ? 3 : 1;
259        }
260        for (final StackMapType typesOfStackItem : typesOfStackItems) {
261            len += typesOfStackItem.hasIndex() ? 3 : 1;
262        }
263        return len;
264    }
265
266    public int getNumberOfLocals() {
267        return typesOfLocals.length;
268    }
269
270    public int getNumberOfStackItems() {
271        return typesOfStackItems.length;
272    }
273
274    public StackMapType[] getTypesOfLocals() {
275        return typesOfLocals;
276    }
277
278    public StackMapType[] getTypesOfStackItems() {
279        return typesOfStackItems;
280    }
281
282    private boolean invalidFrameType(final int f) {
283        // @formatter:off
284        return f != Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED
285            && !(f >= Const.CHOP_FRAME && f <= Const.CHOP_FRAME_MAX)
286            && f != Const.SAME_FRAME_EXTENDED
287            && !(f >= Const.APPEND_FRAME && f <= Const.APPEND_FRAME_MAX)
288            && f != Const.FULL_FRAME;
289        // @formatter:on
290    }
291
292    public void setByteCodeOffset(final int newOffset) {
293        if (newOffset < 0 || newOffset > 32767) {
294            throw new IllegalArgumentException("Invalid StackMap offset: " + newOffset);
295        }
296
297        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
298            if (newOffset > Const.SAME_FRAME_MAX) {
299                frameType = Const.SAME_FRAME_EXTENDED;
300            } else {
301                frameType = newOffset;
302            }
303        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
304            if (newOffset > Const.SAME_FRAME_MAX) {
305                frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED;
306            } else {
307                frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME + newOffset;
308            }
309        } else if (invalidFrameType(frameType)) {
310            throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
311        }
312        byteCodeOffset = newOffset;
313    }
314
315    /**
316     * @param constantPool Constant pool to be used for this object.
317     */
318    public void setConstantPool(final ConstantPool constantPool) {
319        this.constantPool = constantPool;
320    }
321
322    public void setFrameType(final int ft) {
323        if (ft >= Const.SAME_FRAME && ft <= Const.SAME_FRAME_MAX) {
324            byteCodeOffset = ft - Const.SAME_FRAME;
325        } else if (ft >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && ft <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
326            byteCodeOffset = ft - Const.SAME_LOCALS_1_STACK_ITEM_FRAME;
327        } else if (invalidFrameType(ft)) {
328            throw new IllegalArgumentException("Invalid StackMap frameType");
329        }
330        frameType = ft;
331    }
332
333    /**
334     *
335     * @deprecated since 6.0
336     */
337    @java.lang.Deprecated
338    public void setNumberOfLocals(final int n) { // TODO unused
339    }
340
341    /**
342     *
343     * @deprecated since 6.0
344     */
345    @java.lang.Deprecated
346    public void setNumberOfStackItems(final int n) { // TODO unused
347    }
348
349    public void setTypesOfLocals(final StackMapType[] types) {
350        typesOfLocals = types != null ? types : StackMapType.EMPTY_ARRAY;
351    }
352
353    public void setTypesOfStackItems(final StackMapType[] types) {
354        typesOfStackItems = types != null ? types : StackMapType.EMPTY_ARRAY;
355    }
356
357    /**
358     * @return String representation.
359     */
360    @Override
361    public String toString() {
362        final StringBuilder buf = new StringBuilder(64);
363        buf.append("(");
364        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
365            buf.append("SAME");
366        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
367            buf.append("SAME_LOCALS_1_STACK");
368        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
369            buf.append("SAME_LOCALS_1_STACK_EXTENDED");
370        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
371            buf.append("CHOP ").append(String.valueOf(251 - frameType));
372        } else if (frameType == Const.SAME_FRAME_EXTENDED) {
373            buf.append("SAME_EXTENDED");
374        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
375            buf.append("APPEND ").append(String.valueOf(frameType - 251));
376        } else if (frameType == Const.FULL_FRAME) {
377            buf.append("FULL");
378        } else {
379            buf.append("UNKNOWN (").append(frameType).append(")");
380        }
381        buf.append(", offset delta=").append(byteCodeOffset);
382        if (typesOfLocals.length > 0) {
383            buf.append(", locals={");
384            for (int i = 0; i < typesOfLocals.length; i++) {
385                buf.append(typesOfLocals[i]);
386                if (i < typesOfLocals.length - 1) {
387                    buf.append(", ");
388                }
389            }
390            buf.append("}");
391        }
392        if (typesOfStackItems.length > 0) {
393            buf.append(", stack items={");
394            for (int i = 0; i < typesOfStackItems.length; i++) {
395                buf.append(typesOfStackItems[i]);
396                if (i < typesOfStackItems.length - 1) {
397                    buf.append(", ");
398                }
399            }
400            buf.append("}");
401        }
402        buf.append(")");
403        return buf.toString();
404    }
405
406    /**
407     * Update the distance (as an offset delta) from this StackMap entry to the next. Note that this might cause the
408     * frame type to change. Note also that delta may be negative.
409     *
410     * @param delta offset delta
411     */
412    public void updateByteCodeOffset(final int delta) {
413        setByteCodeOffset(byteCodeOffset + delta);
414    }
415}