001: /**
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package com.uwyn.rife.asm.attrs;
030:
031: import java.util.ArrayList;
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Set;
036:
037: import com.uwyn.rife.asm.Attribute;
038: import com.uwyn.rife.asm.ByteVector;
039: import com.uwyn.rife.asm.ClassReader;
040: import com.uwyn.rife.asm.ClassWriter;
041: import com.uwyn.rife.asm.Label;
042:
043: /**
044: * StackMapAttribute is used by CDLC preverifier. Definition is given in
045: * appendix "CLDC Byte Code Typechecker Specification" from CDLC 1.1
046: * specification. <p> <i>Note that this implementation does not calculate
047: * StackMapFrame structures from the method bytecode. If method code is changed
048: * or generated from scratch, then developer is responsible to prepare a correct
049: * StackMapFrame structures.</i> <p> The format of the stack map in the class
050: * file is given below. In the following, <ul> <li>if the length of the
051: * method's byte code1 is 65535 or less, then <tt>uoffset</tt> represents the
052: * type u2; otherwise <tt>uoffset</tt> represents the type u4.</li> <li>If
053: * the maximum number of local variables for the method is 65535 or less, then
054: * <tt>ulocalvar</tt> represents the type u2; otherwise <tt>ulocalvar</tt>
055: * represents the type u4.</li> <li>If the maximum size of the operand stack
056: * is 65535 or less, then <tt>ustack</tt> represents the type u2; otherwise
057: * ustack represents the type u4.</li> </ul>
058: *
059: * <pre>
060: * stack_map { // attribute StackMap
061: * u2 attribute_name_index;
062: * u4 attribute_length
063: * uoffset number_of_entries;
064: * stack_map_frame entries[number_of_entries];
065: * }
066: * </pre>
067: *
068: * Each stack map frame has the following format:
069: *
070: * <pre>
071: * stack_map_frame {
072: * uoffset offset;
073: * ulocalvar number_of_locals;
074: * verification_type_info locals[number_of_locals];
075: * ustack number_of_stack_items;
076: * verification_type_info stack[number_of_stack_items];
077: * }
078: * </pre>
079: *
080: * The <tt>verification_type_info</tt> structure consists of a one-byte tag
081: * followed by zero or more bytes, giving more information about the tag. Each
082: * <tt>verification_type_info</tt> structure specifies the verification type
083: * of one or two locations.
084: *
085: * <pre>
086: * union verification_type_info {
087: * Top_variable_info;
088: * Integer_variable_info;
089: * Float_variable_info;
090: * Long_variable_info;
091: * Double_variable_info;
092: * Null_variable_info;
093: * UninitializedThis_variable_info;
094: * Object_variable_info;
095: * Uninitialized_variable_info;
096: * }
097: *
098: * Top_variable_info {
099: * u1 tag = ITEM_Top; // 0
100: * }
101: *
102: * Integer_variable_info {
103: * u1 tag = ITEM_Integer; // 1
104: * }
105: *
106: * Float_variable_info {
107: * u1 tag = ITEM_Float; // 2
108: * }
109: *
110: * Long_variable_info {
111: * u1 tag = ITEM_Long; // 4
112: * }
113: *
114: * Double_variable_info {
115: * u1 tag = ITEM_Double; // 3
116: * }
117: *
118: * Null_variable_info {
119: * u1 tag = ITEM_Null; // 5
120: * }
121: *
122: * UninitializedThis_variable_info {
123: * u1 tag = ITEM_UninitializedThis; // 6
124: * }
125: *
126: * Object_variable_info {
127: * u1 tag = ITEM_Object; // 7
128: * u2 cpool_index;
129: * }
130: *
131: * Uninitialized_variable_info {
132: * u1 tag = ITEM_Uninitialized // 8
133: * uoffset offset;
134: * }
135: * </pre>
136: *
137: * @see <a href="http://www.jcp.org/en/jsr/detail?id=139">JSR 139 : Connected
138: * Limited Device Configuration 1.1</a>
139: *
140: * @author Eugene Kuleshov
141: */
142: public class StackMapAttribute extends Attribute {
143:
144: static final int MAX_SIZE = 65535;
145:
146: /**
147: * A List of <code>StackMapFrame</code> instances.
148: */
149: public List frames = new ArrayList();
150:
151: public StackMapAttribute() {
152: super ("StackMap");
153: }
154:
155: public StackMapAttribute(List frames) {
156: this ();
157: this .frames = frames;
158: }
159:
160: public List getFrames() {
161: return frames;
162: }
163:
164: public StackMapFrame getFrame(Label label) {
165: for (int i = 0; i < frames.size(); i++) {
166: StackMapFrame frame = (StackMapFrame) frames.get(i);
167: if (frame.label == label) {
168: return frame;
169: }
170: }
171: return null;
172: }
173:
174: public boolean isUnknown() {
175: return false;
176: }
177:
178: public boolean isCodeAttribute() {
179: return true;
180: }
181:
182: protected Attribute read(ClassReader cr, int off, int len,
183: char[] buf, int codeOff, Label[] labels) {
184: StackMapAttribute attr = new StackMapAttribute();
185: // note that this is not the size of Code attribute
186: boolean isExtCodeSize = cr.readInt(codeOff + 4) > MAX_SIZE;
187: boolean isExtLocals = cr.readUnsignedShort(codeOff + 2) > MAX_SIZE;
188: boolean isExtStack = cr.readUnsignedShort(codeOff) > MAX_SIZE;
189:
190: int size = 0;
191: if (isExtCodeSize) {
192: size = cr.readInt(off);
193: off += 4;
194: } else {
195: size = cr.readUnsignedShort(off);
196: off += 2;
197: }
198: for (int i = 0; i < size; i++) {
199: int offset;
200: if (isExtCodeSize) {
201: offset = cr.readInt(off);
202: off += 4;
203: } else {
204: offset = cr.readUnsignedShort(off);
205: off += 2;
206: }
207:
208: Label label = getLabel(offset, labels);
209: List locals = new ArrayList();
210: List stack = new ArrayList();
211:
212: off = readTypeInfo(cr, off, locals, labels, buf,
213: isExtLocals, isExtCodeSize);
214: off = readTypeInfo(cr, off, stack, labels, buf, isExtStack,
215: isExtCodeSize);
216:
217: attr.frames.add(new StackMapFrame(label, locals, stack));
218: }
219: return attr;
220: }
221:
222: private int readTypeInfo(ClassReader cr, int off, List info,
223: Label[] labels, char[] buf, boolean isExt, boolean isExtCode) {
224: int n = 0;
225: if (isExt) {
226: n = cr.readInt(off);
227: off += 4;
228: } else {
229: n = cr.readUnsignedShort(off);
230: off += 2;
231: }
232: for (int j = 0; j < n; j++) {
233: int itemType = cr.readByte(off++);
234: StackMapType typeInfo = StackMapType.getTypeInfo(itemType);
235: info.add(typeInfo);
236: switch (itemType) {
237: case StackMapType.ITEM_Object: //
238: typeInfo.setObject(cr.readClass(off, buf));
239: off += 2;
240: break;
241: case StackMapType.ITEM_Uninitialized: //
242: int offset;
243: if (isExtCode) {
244: offset = cr.readInt(off);
245: off += 4;
246: } else {
247: offset = cr.readUnsignedShort(off);
248: off += 2;
249: }
250: typeInfo.setLabel(getLabel(offset, labels));
251: break;
252: }
253: }
254: return off;
255: }
256:
257: private void writeTypeInfo(ByteVector bv, ClassWriter cw,
258: List info, int max) {
259: if (max > StackMapAttribute.MAX_SIZE) {
260: bv.putInt(info.size());
261: } else {
262: bv.putShort(info.size());
263: }
264: for (int j = 0; j < info.size(); j++) {
265: StackMapType typeInfo = (StackMapType) info.get(j);
266: bv.putByte(typeInfo.getType());
267: switch (typeInfo.getType()) {
268: case StackMapType.ITEM_Object: //
269: bv.putShort(cw.newClass(typeInfo.getObject()));
270: break;
271:
272: case StackMapType.ITEM_Uninitialized: //
273: bv.putShort(typeInfo.getLabel().getOffset());
274: break;
275:
276: }
277: }
278: }
279:
280: private Label getLabel(int offset, Label[] labels) {
281: Label l = labels[offset];
282: if (l != null) {
283: return l;
284: }
285: return labels[offset] = new Label();
286: }
287:
288: protected ByteVector write(ClassWriter cw, byte[] code, int len,
289: int maxStack, int maxLocals) {
290: ByteVector bv = new ByteVector();
291: if (code != null && code.length > MAX_SIZE) { // TODO verify value
292: bv.putInt(frames.size());
293: } else {
294: bv.putShort(frames.size());
295: }
296: for (int i = 0; i < frames.size(); i++) {
297: writeFrame((StackMapFrame) frames.get(i), cw, maxStack,
298: maxLocals, bv);
299: }
300: return bv;
301: }
302:
303: protected Label[] getLabels() {
304: HashSet labels = new HashSet();
305: for (int i = 0; i < frames.size(); i++) {
306: getFrameLabels((StackMapFrame) frames.get(i), labels);
307: }
308: return (Label[]) labels.toArray(new Label[labels.size()]);
309: }
310:
311: private void writeFrame(StackMapFrame frame, ClassWriter cw,
312: int maxStack, int maxLocals, ByteVector bv) {
313: bv.putShort(frame.label.getOffset());
314: writeTypeInfo(bv, cw, frame.locals, maxLocals);
315: writeTypeInfo(bv, cw, frame.stack, maxStack);
316: }
317:
318: private void getFrameLabels(StackMapFrame frame, Set labels) {
319: labels.add(frame.label);
320: getTypeInfoLabels(labels, frame.locals);
321: getTypeInfoLabels(labels, frame.stack);
322: }
323:
324: private void getTypeInfoLabels(Set labels, List info) {
325: for (Iterator it = info.iterator(); it.hasNext();) {
326: StackMapType typeInfo = (StackMapType) it.next();
327: if (typeInfo.getType() == StackMapType.ITEM_Uninitialized) {
328: labels.add(typeInfo.getLabel());
329: }
330: }
331: }
332:
333: public String toString() {
334: StringBuffer sb = new StringBuffer("StackMap[");
335: for (int i = 0; i < frames.size(); i++) {
336: sb.append('\n').append('[').append(frames.get(i)).append(
337: ']');
338: }
339: sb.append("\n]");
340: return sb.toString();
341: }
342: }
|