1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. Alternatively, the contents of this file may be used under
* the terms of the GNU Lesser General Public License Version 2.1 or later.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
package javassist.bytecode;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Map;
import javassist.CannotCompileException;
import javassist.bytecode.StackMapTable.InsertLocal;
import javassist.bytecode.StackMapTable.NewRemover;
import javassist.bytecode.StackMapTable.Shifter;
/**
* Another <code>stack_map</code> attribute defined in CLDC 1.1 for J2ME.
*
* <p>This is an entry in the attributes table of a Code attribute.
* It was introduced by J2ME CLDC 1.1 (JSR 139) for pre-verification.
*
* <p>According to the CLDC specification, the sizes of some fields are not 16bit
* but 32bit if the code size is more than 64K or the number of the local variables
* is more than 64K. However, for the J2ME CLDC technology, they are always 16bit.
* The implementation of the StackMap class assumes they are 16bit.
*
* @see MethodInfo#doPreverify
* @see StackMapTable
* @since 3.12
*/
public class StackMap extends AttributeInfo {
/**
* The name of this attribute <code>"StackMap"</code>.
*/
public static final String tag = "StackMap";
/**
* Constructs a <code>stack_map</code> attribute.
*/
StackMap(ConstPool cp, byte[] newInfo) {
super(cp, tag, newInfo);
}
StackMap(ConstPool cp, int name_id, DataInputStream in)
throws IOException
{
super(cp, name_id, in);
}
/**
* Returns <code>number_of_entries</code>.
*/
public int numOfEntries() {
return ByteArray.readU16bit(info, 0);
}
/**
* <code>Top_variable_info.tag</code>.
*/
public static final int TOP = 0;
/**
* <code>Integer_variable_info.tag</code>.
*/
public static final int INTEGER = 1;
/**
* <code>Float_variable_info.tag</code>.
*/
public static final int FLOAT = 2;
/**
* <code>Double_variable_info.tag</code>.
*/
public static final int DOUBLE = 3;
/**
* <code>Long_variable_info.tag</code>.
*/
public static final int LONG = 4;
/**
* <code>Null_variable_info.tag</code>.
*/
public static final int NULL = 5;
/**
* <code>UninitializedThis_variable_info.tag</code>.
*/
public static final int THIS = 6;
/**
* <code>Object_variable_info.tag</code>.
*/
public static final int OBJECT = 7;
/**
* <code>Uninitialized_variable_info.tag</code>.
*/
public static final int UNINIT = 8;
/**
* Makes a copy.
*/
public AttributeInfo copy(ConstPool newCp, Map classnames) {
Copier copier = new Copier(this, newCp, classnames);
copier.visit();
return copier.getStackMap();
}
/**
* A code walker for a StackMap attribute.
*/
public static class Walker {
byte[] info;
/**
* Constructs a walker.
*/
public Walker(StackMap sm) {
info = sm.get();
}
/**
* Visits each entry of the stack map frames.
*/
public void visit() {
int num = ByteArray.readU16bit(info, 0);
int pos = 2;
for (int i = 0; i < num; i++) {
int offset = ByteArray.readU16bit(info, pos);
int numLoc = ByteArray.readU16bit(info, pos + 2);
pos = locals(pos + 4, offset, numLoc);
int numStack = ByteArray.readU16bit(info, pos);
pos = stack(pos + 2, offset, numStack);
}
}
/**
* Invoked when <code>locals</code> of <code>stack_map_frame</code>
* is visited.
*/
public int locals(int pos, int offset, int num) {
return typeInfoArray(pos, offset, num, true);
}
/**
* Invoked when <code>stack</code> of <code>stack_map_frame</code>
* is visited.
*/
public int stack(int pos, int offset, int num) {
return typeInfoArray(pos, offset, num, false);
}
/**
* Invoked when an array of <code>verification_type_info</code> is
* visited.
*
* @param num the number of elements.
* @param isLocals true if this array is for <code>locals</code>.
* false if it is for <code>stack</code>.
*/
public int typeInfoArray(int pos, int offset, int num, boolean isLocals) {
for (int k = 0; k < num; k++)
pos = typeInfoArray2(k, pos);
return pos;
}
int typeInfoArray2(int k, int pos) {
byte tag = info[pos];
if (tag == OBJECT) {
int clazz = ByteArray.readU16bit(info, pos + 1);
objectVariable(pos, clazz);
pos += 3;
}
else if (tag == UNINIT) {
int offsetOfNew = ByteArray.readU16bit(info, pos + 1);
uninitialized(pos, offsetOfNew);
pos += 3;
}
else {
typeInfo(pos, tag);
pos++;
}
return pos;
}
/**
* Invoked when an element of <code>verification_type_info</code>
* (except <code>Object_variable_info</code> and
* <code>Uninitialized_variable_info</code>) is visited.
*/
public void typeInfo(int pos, byte tag) {}
/**
* Invoked when an element of type <code>Object_variable_info</code>
* is visited.
*/
public void objectVariable(int pos, int clazz) {}
/**
* Invoked when an element of type <code>Uninitialized_variable_info</code>
* is visited.
*/
public void uninitialized(int pos, int offset) {}
}
static class Copier extends Walker {
byte[] dest;
ConstPool srcCp, destCp;
Map classnames;
Copier(StackMap map, ConstPool newCp, Map classnames) {
super(map);
srcCp = map.getConstPool();
dest = new byte[info.length];
destCp = newCp;
this.classnames = classnames;
}
public void visit() {
int num = ByteArray.readU16bit(info, 0);
ByteArray.write16bit(num, dest, 0);
super.visit();
}
public int locals(int pos, int offset, int num) {
ByteArray.write16bit(offset, dest, pos - 4);
return super.locals(pos, offset, num);
}
public int typeInfoArray(int pos, int offset, int num, boolean isLocals) {
ByteArray.write16bit(num, dest, pos - 2);
return super.typeInfoArray(pos, offset, num, isLocals);
}
public void typeInfo(int pos, byte tag) {
dest[pos] = tag;
}
public void objectVariable(int pos, int clazz) {
dest[pos] = OBJECT;
int newClazz = srcCp.copy(clazz, destCp, classnames);
ByteArray.write16bit(newClazz, dest, pos + 1);
}
public void uninitialized(int pos, int offset) {
dest[pos] = UNINIT;
ByteArray.write16bit(offset, dest, pos + 1);
}
public StackMap getStackMap() {
return new StackMap(destCp, dest);
}
}
/**
* Updates this stack map table when a new local variable is inserted
* for a new parameter.
*
* @param index the index of the added local variable.
* @param tag the type tag of that local variable.
* It is available by <code>StackMapTable.typeTagOf(char)</code>.
* @param classInfo the index of the <code>CONSTANT_Class_info</code> structure
* in a constant pool table. This should be zero unless the tag
* is <code>ITEM_Object</code>.
*
* @see javassist.CtBehavior#addParameter(javassist.CtClass)
* @see StackMapTable#typeTagOf(char)
* @see ConstPool
*/
public void insertLocal(int index, int tag, int classInfo)
throws BadBytecode
{
byte[] data = new InsertLocal(this, index, tag, classInfo).doit();
this.set(data);
}
static class SimpleCopy extends Walker {
Writer writer;
SimpleCopy(StackMap map) {
super(map);
writer = new Writer();
}
byte[] doit() {
visit();
return writer.toByteArray();
}
public void visit() {
int num = ByteArray.readU16bit(info, 0);
writer.write16bit(num);
super.visit();
}
public int locals(int pos, int offset, int num) {
writer.write16bit(offset);
return super.locals(pos, offset, num);
}
public int typeInfoArray(int pos, int offset, int num, boolean isLocals) {
writer.write16bit(num);
return super.typeInfoArray(pos, offset, num, isLocals);
}
public void typeInfo(int pos, byte tag) {
writer.writeVerifyTypeInfo(tag, 0);
}
public void objectVariable(int pos, int clazz) {
writer.writeVerifyTypeInfo(OBJECT, clazz);
}
public void uninitialized(int pos, int offset) {
writer.writeVerifyTypeInfo(UNINIT, offset);
}
}
static class InsertLocal extends SimpleCopy {
private int varIndex;
private int varTag, varData;
InsertLocal(StackMap map, int varIndex, int varTag, int varData) {
super(map);
this.varIndex = varIndex;
this.varTag = varTag;
this.varData = varData;
}
public int typeInfoArray(int pos, int offset, int num, boolean isLocals) {
if (!isLocals || num < varIndex)
return super.typeInfoArray(pos, offset, num, isLocals);
writer.write16bit(num + 1);
for (int k = 0; k < num; k++) {
if (k == varIndex)
writeVarTypeInfo();
pos = typeInfoArray2(k, pos);
}
if (num == varIndex)
writeVarTypeInfo();
return pos;
}
private void writeVarTypeInfo() {
if (varTag == OBJECT)
writer.writeVerifyTypeInfo(OBJECT, varData);
else if (varTag == UNINIT)
writer.writeVerifyTypeInfo(UNINIT, varData);
else
writer.writeVerifyTypeInfo(varTag, 0);
}
}
void shiftPc(int where, int gapSize, boolean exclusive)
throws BadBytecode
{
new Shifter(this, where, gapSize, exclusive).visit();
}
static class Shifter extends Walker {
private int where, gap;
private boolean exclusive;
public Shifter(StackMap smt, int where, int gap, boolean exclusive) {
super(smt);
this.where = where;
this.gap = gap;
this.exclusive = exclusive;
}
public int locals(int pos, int offset, int num) {
if (exclusive ? where <= offset : where < offset)
ByteArray.write16bit(offset + gap, info, pos - 4);
return super.locals(pos, offset, num);
}
}
/**
* Undocumented method. Do not use; internal-use only.
*
* <p>This method is for javassist.convert.TransformNew.
* It is called to update the stack map when
* the NEW opcode (and the following DUP) is removed.
*
* @param where the position of the removed NEW opcode.
*/
public void removeNew(int where) throws CannotCompileException {
byte[] data = new NewRemover(this, where).doit();
this.set(data);
}
static class NewRemover extends SimpleCopy {
int posOfNew;
NewRemover(StackMap map, int where) {
super(map);
posOfNew = where;
}
public int stack(int pos, int offset, int num) {
return stackTypeInfoArray(pos, offset, num);
}
private int stackTypeInfoArray(int pos, int offset, int num) {
int p = pos;
int count = 0;
for (int k = 0; k < num; k++) {
byte tag = info[p];
if (tag == OBJECT)
p += 3;
else if (tag == UNINIT) {
int offsetOfNew = ByteArray.readU16bit(info, p + 1);
if (offsetOfNew == posOfNew)
count++;
p += 3;
}
else
p++;
}
writer.write16bit(num - count);
for (int k = 0; k < num; k++) {
byte tag = info[pos];
if (tag == OBJECT) {
int clazz = ByteArray.readU16bit(info, pos + 1);
objectVariable(pos, clazz);
pos += 3;
}
else if (tag == UNINIT) {
int offsetOfNew = ByteArray.readU16bit(info, pos + 1);
if (offsetOfNew != posOfNew)
uninitialized(pos, offsetOfNew);
pos += 3;
}
else {
typeInfo(pos, tag);
pos++;
}
}
return pos;
}
}
/**
* Prints this stack map.
*/
public void print(java.io.PrintWriter out) {
new Printer(this, out).print();
}
static class Printer extends Walker {
private java.io.PrintWriter writer;
public Printer(StackMap map, java.io.PrintWriter out) {
super(map);
writer = out;
}
public void print() {
int num = ByteArray.readU16bit(info, 0);
writer.println(num + " entries");
visit();
}
public int locals(int pos, int offset, int num) {
writer.println(" * offset " + offset);
return super.locals(pos, offset, num);
}
}
/**
* Internal use only.
*/
public static class Writer {
// see javassist.bytecode.stackmap.MapMaker
private ByteArrayOutputStream output;
/**
* Constructs a writer.
*/
public Writer() {
output = new ByteArrayOutputStream();
}
/**
* Converts the written data into a byte array.
*/
public byte[] toByteArray() {
return output.toByteArray();
}
/**
* Converts to a <code>StackMap</code> attribute.
*/
public StackMap toStackMap(ConstPool cp) {
return new StackMap(cp, output.toByteArray());
}
/**
* Writes a <code>union verification_type_info</code> value.
*
* @param data <code>cpool_index</code> or <code>offset</code>.
*/
public void writeVerifyTypeInfo(int tag, int data) {
output.write(tag);
if (tag == StackMap.OBJECT || tag == StackMap.UNINIT)
write16bit(data);
}
/**
* Writes a 16bit value.
*/
public void write16bit(int value) {
output.write((value >>> 8) & 0xff);
output.write(value & 0xff);
}
}
}
|