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 || frameType == Const.SAME_FRAME_EXTENDED) {
075            byteCodeOffset = dataInput.readUnsignedShort();
076        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
077            byteCodeOffset = dataInput.readUnsignedShort();
078            final int numberOfLocals = frameType - 251;
079            typesOfLocals = new StackMapType[numberOfLocals];
080            for (int i = 0; i < numberOfLocals; i++) {
081                typesOfLocals[i] = new StackMapType(dataInput, constantPool);
082            }
083        } else if (frameType == Const.FULL_FRAME) {
084            byteCodeOffset = dataInput.readUnsignedShort();
085            final int numberOfLocals = dataInput.readUnsignedShort();
086            typesOfLocals = new StackMapType[numberOfLocals];
087            for (int i = 0; i < numberOfLocals; i++) {
088                typesOfLocals[i] = new StackMapType(dataInput, constantPool);
089            }
090            final int numberOfStackItems = dataInput.readUnsignedShort();
091            typesOfStackItems = new StackMapType[numberOfStackItems];
092            for (int i = 0; i < numberOfStackItems; i++) {
093                typesOfStackItems[i] = new StackMapType(dataInput, constantPool);
094            }
095        } else {
096            /* Can't happen */
097            throw new ClassFormatException("Invalid frame type found while parsing stack map table: " + frameType);
098        }
099    }
100
101    /**
102     * DO NOT USE
103     *
104     * @param byteCodeOffset
105     * @param numberOfLocals NOT USED
106     * @param typesOfLocals array of {@link StackMapType}s of locals
107     * @param numberOfStackItems NOT USED
108     * @param typesOfStackItems array ot {@link StackMapType}s of stack items
109     * @param constantPool the constant pool
110     * @deprecated Since 6.0, use {@link #StackMapEntry(int, int, StackMapType[], StackMapType[], ConstantPool)} instead
111     */
112    @java.lang.Deprecated
113    public StackMapEntry(final int byteCodeOffset, final int numberOfLocals, final StackMapType[] typesOfLocals, final int numberOfStackItems,
114        final StackMapType[] typesOfStackItems, final ConstantPool constantPool) {
115        this.byteCodeOffset = byteCodeOffset;
116        this.typesOfLocals = typesOfLocals != null ? typesOfLocals : StackMapType.EMPTY_ARRAY;
117        this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : StackMapType.EMPTY_ARRAY;
118        this.constantPool = constantPool;
119        if (numberOfLocals < 0) {
120            throw new IllegalArgumentException("numberOfLocals < 0");
121        }
122        if (numberOfStackItems < 0) {
123            throw new IllegalArgumentException("numberOfStackItems < 0");
124        }
125    }
126
127    /**
128     * Create an instance
129     *
130     * @param tag the frameType to use
131     * @param byteCodeOffset
132     * @param typesOfLocals array of {@link StackMapType}s of locals
133     * @param typesOfStackItems array ot {@link StackMapType}s of stack items
134     * @param constantPool the constant pool
135     */
136    public StackMapEntry(final int tag, final int byteCodeOffset, final StackMapType[] typesOfLocals, final StackMapType[] typesOfStackItems,
137        final ConstantPool constantPool) {
138        this.frameType = tag;
139        this.byteCodeOffset = byteCodeOffset;
140        this.typesOfLocals = typesOfLocals != null ? typesOfLocals : StackMapType.EMPTY_ARRAY;
141        this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : StackMapType.EMPTY_ARRAY;
142        this.constantPool = constantPool;
143    }
144
145    /**
146     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
147     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
148     *
149     * @param v Visitor object
150     */
151    @Override
152    public void accept(final Visitor v) {
153        v.visitStackMapEntry(this);
154    }
155
156    /**
157     * @return deep copy of this object
158     */
159    public StackMapEntry copy() {
160        StackMapEntry e;
161        try {
162            e = (StackMapEntry) clone();
163        } catch (final CloneNotSupportedException ex) {
164            throw new UnsupportedOperationException("Clone Not Supported", ex);
165        }
166
167        e.typesOfLocals = new StackMapType[typesOfLocals.length];
168        Arrays.setAll(e.typesOfLocals, i -> typesOfLocals[i].copy());
169        e.typesOfStackItems = new StackMapType[typesOfStackItems.length];
170        Arrays.setAll(e.typesOfStackItems, i -> typesOfStackItems[i].copy());
171        return e;
172    }
173
174    /**
175     * Dump stack map entry
176     *
177     * @param file Output file stream
178     * @throws IOException if an I/O error occurs.
179     */
180    public void dump(final DataOutputStream file) throws IOException {
181        file.write(frameType);
182        if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
183            typesOfStackItems[0].dump(file);
184        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
185            file.writeShort(byteCodeOffset);
186            typesOfStackItems[0].dump(file);
187        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX || frameType == Const.SAME_FRAME_EXTENDED) {
188            file.writeShort(byteCodeOffset);
189        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
190            file.writeShort(byteCodeOffset);
191            for (final StackMapType type : typesOfLocals) {
192                type.dump(file);
193            }
194        } else if (frameType == Const.FULL_FRAME) {
195            file.writeShort(byteCodeOffset);
196            file.writeShort(typesOfLocals.length);
197            for (final StackMapType type : typesOfLocals) {
198                type.dump(file);
199            }
200            file.writeShort(typesOfStackItems.length);
201            for (final StackMapType type : typesOfStackItems) {
202                type.dump(file);
203            }
204        } else if (!(frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX)) {
205            /* Can't happen */
206            throw new ClassFormatException("Invalid Stack map table tag: " + frameType);
207        }
208    }
209
210    public int getByteCodeOffset() {
211        return byteCodeOffset;
212    }
213
214    /**
215     * @return Constant pool used by this object.
216     */
217    public ConstantPool getConstantPool() {
218        return constantPool;
219    }
220
221    public int getFrameType() {
222        return frameType;
223    }
224
225    /**
226     * Calculate stack map entry size
227     *
228     */
229    int getMapEntrySize() {
230        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
231            return 1;
232        }
233        if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
234            return 1 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
235        }
236        if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
237            return 3 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
238        }
239        if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX || frameType == Const.SAME_FRAME_EXTENDED) {
240            return 3;
241        }
242        if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
243            int len = 3;
244            for (final StackMapType typesOfLocal : typesOfLocals) {
245                len += typesOfLocal.hasIndex() ? 3 : 1;
246            }
247            return len;
248        }
249        if (frameType != Const.FULL_FRAME) {
250            throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
251        }
252        int len = 7;
253        for (final StackMapType typesOfLocal : typesOfLocals) {
254            len += typesOfLocal.hasIndex() ? 3 : 1;
255        }
256        for (final StackMapType typesOfStackItem : typesOfStackItems) {
257            len += typesOfStackItem.hasIndex() ? 3 : 1;
258        }
259        return len;
260    }
261
262    public int getNumberOfLocals() {
263        return typesOfLocals.length;
264    }
265
266    public int getNumberOfStackItems() {
267        return typesOfStackItems.length;
268    }
269
270    public StackMapType[] getTypesOfLocals() {
271        return typesOfLocals;
272    }
273
274    public StackMapType[] getTypesOfStackItems() {
275        return typesOfStackItems;
276    }
277
278    private boolean invalidFrameType(final int f) {
279        // @formatter:off
280        return f != Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED
281            && !(f >= Const.CHOP_FRAME && f <= Const.CHOP_FRAME_MAX)
282            && f != Const.SAME_FRAME_EXTENDED
283            && !(f >= Const.APPEND_FRAME && f <= Const.APPEND_FRAME_MAX)
284            && f != Const.FULL_FRAME;
285        // @formatter:on
286    }
287
288    public void setByteCodeOffset(final int newOffset) {
289        if (newOffset < 0 || newOffset > 32767) {
290            throw new IllegalArgumentException("Invalid StackMap offset: " + newOffset);
291        }
292
293        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
294            if (newOffset > Const.SAME_FRAME_MAX) {
295                frameType = Const.SAME_FRAME_EXTENDED;
296            } else {
297                frameType = newOffset;
298            }
299        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
300            if (newOffset > Const.SAME_FRAME_MAX) {
301                frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED;
302            } else {
303                frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME + newOffset;
304            }
305        } else if (invalidFrameType(frameType)) {
306            throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
307        }
308        byteCodeOffset = newOffset;
309    }
310
311    /**
312     * @param constantPool Constant pool to be used for this object.
313     */
314    public void setConstantPool(final ConstantPool constantPool) {
315        this.constantPool = constantPool;
316    }
317
318    public void setFrameType(final int ft) {
319        if (ft >= Const.SAME_FRAME && ft <= Const.SAME_FRAME_MAX) {
320            byteCodeOffset = ft - Const.SAME_FRAME;
321        } else if (ft >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && ft <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
322            byteCodeOffset = ft - Const.SAME_LOCALS_1_STACK_ITEM_FRAME;
323        } else if (invalidFrameType(ft)) {
324            throw new IllegalArgumentException("Invalid StackMap frameType");
325        }
326        frameType = ft;
327    }
328
329    /**
330     *
331     * @deprecated since 6.0
332     */
333    @java.lang.Deprecated
334    public void setNumberOfLocals(final int n) { // TODO unused
335    }
336
337    /**
338     *
339     * @deprecated since 6.0
340     */
341    @java.lang.Deprecated
342    public void setNumberOfStackItems(final int n) { // TODO unused
343    }
344
345    public void setTypesOfLocals(final StackMapType[] types) {
346        typesOfLocals = types != null ? types : StackMapType.EMPTY_ARRAY;
347    }
348
349    public void setTypesOfStackItems(final StackMapType[] types) {
350        typesOfStackItems = types != null ? types : StackMapType.EMPTY_ARRAY;
351    }
352
353    /**
354     * @return String representation.
355     */
356    @Override
357    public String toString() {
358        final StringBuilder buf = new StringBuilder(64);
359        buf.append("(");
360        if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
361            buf.append("SAME");
362        } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
363            buf.append("SAME_LOCALS_1_STACK");
364        } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
365            buf.append("SAME_LOCALS_1_STACK_EXTENDED");
366        } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
367            buf.append("CHOP ").append(String.valueOf(251 - frameType));
368        } else if (frameType == Const.SAME_FRAME_EXTENDED) {
369            buf.append("SAME_EXTENDED");
370        } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
371            buf.append("APPEND ").append(String.valueOf(frameType - 251));
372        } else if (frameType == Const.FULL_FRAME) {
373            buf.append("FULL");
374        } else {
375            buf.append("UNKNOWN (").append(frameType).append(")");
376        }
377        buf.append(", offset delta=").append(byteCodeOffset);
378        if (typesOfLocals.length > 0) {
379            buf.append(", locals={");
380            for (int i = 0; i < typesOfLocals.length; i++) {
381                buf.append(typesOfLocals[i]);
382                if (i < typesOfLocals.length - 1) {
383                    buf.append(", ");
384                }
385            }
386            buf.append("}");
387        }
388        if (typesOfStackItems.length > 0) {
389            buf.append(", stack items={");
390            for (int i = 0; i < typesOfStackItems.length; i++) {
391                buf.append(typesOfStackItems[i]);
392                if (i < typesOfStackItems.length - 1) {
393                    buf.append(", ");
394                }
395            }
396            buf.append("}");
397        }
398        buf.append(")");
399        return buf.toString();
400    }
401
402    /**
403     * Update the distance (as an offset delta) from this StackMap entry to the next. Note that this might cause the
404     * frame type to change. Note also that delta may be negative.
405     *
406     * @param delta offset delta
407     */
408    public void updateByteCodeOffset(final int delta) {
409        setByteCodeOffset(byteCodeOffset + delta);
410    }
411}