1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from struct import pack, unpack, calcsize
20 from collections import namedtuple
21 import re, zipfile, StringIO, os
22
23 import bytecode
24 from bytecode import SV, SVs
25
26 import jvm_generate
27
28
30 - def __init__(self, filename, raw=False) :
31 self.filename = filename
32
33 if raw == True :
34 self.__raw = filename
35 else :
36 fd = open( filename, "rb" )
37 self.__raw = fd.read()
38 fd.close()
39
40 self.zip = zipfile.ZipFile( StringIO.StringIO( self.__raw ) )
41
43 l = []
44 for i in self.zip.namelist() :
45 if ".class" in i :
46 l.append( (i, self.zip.read(i)) )
47
48 return l
49
50
53
54
55
56
58 return [ i for i in x ]
59
62
64 return (x[0] << 8) | x[1]
65
67 return [ (x & 0xFF00) >> 8, x & 0x00FF ]
68
70 v = ((x[0] << 8) | x[1])
71 if v > 0x7FFF :
72 v = (0x7FFF & v) - 0x8000
73
74 return v
75
77 val = x & 0xFFFF
78 return [ (val & 0xFF00) >> 8, val & 0x00FF ]
79
81 val = (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3]
82 if val > 0x7fffffff :
83 val = (0x7fffffff & val) - 0x80000000
84 return val
85
87 val = x & 0xFFFFFFFF
88 return [ (val & 0xFF000000) >> 24, (val & 0x00FF0000) >> 16, (val & 0x0000FF00) >> 8, val & 0x000000FF ]
89
91 return [ (x[0] << 8) | x[1], x[2] ]
92
95
98
99 FD = { "B" : "byte",
100 "C" : "char",
101 "D" : "double",
102 "F" : "float",
103 "I" : "int",
104 "J" : "long",
105 "S" : "short",
106 "Z" : "boolean",
107 "V" : "void",
108 }
109
136
138 r_buff = []
139 r_format = ">"
140
141 idx = idx + 1
142
143 n = 0
144 if idx % 4 :
145 n = 4 - (idx % 4)
146
147 for i in range(0, n) :
148 r_buff.append( "bytepad%d" % i )
149 r_format += "B"
150
151 r_buff.extend( [ "default", "low", "high" ] )
152 r_format += "LLL"
153
154 idx = 1 + n + 4
155
156 low = unpack('>L', raw_format[ idx : idx + 4 ])[0]
157 idx = idx + 4
158 high = unpack('>L', raw_format[ idx : idx + 4 ])[0]
159
160 for i in range(0, high - low + 1) :
161 r_buff.append( "offset%d" % i )
162 r_format += "L"
163
164 return specialSwitch, specialSwitch, r_buff, r_format, None
165
167 r_buff = []
168 r_format = ">"
169
170 idx = idx + 1
171
172 n = 0
173 if idx % 4 :
174 n = 4 - (idx % 4)
175
176 for i in range(0, n) :
177 r_buff.append( "bytepad%d" % i )
178 r_format += "B"
179
180 r_buff.extend( [ "default", "npairs" ] )
181 r_format += "LL"
182
183 idx = 1 + n + 4
184 for i in range(0, unpack('>L', raw_format[ idx : idx + 4 ])[0]) :
185 r_buff.extend( [ "match%d" % i, "offset%d" % i ] )
186 r_format += "LL"
187
188 return specialSwitch, specialSwitch, r_buff, r_format, None
189
190
191 JAVA_OPCODES = {
192 0x32 : [ "aaload" ],
193 0x53 : [ "aastore" ],
194 0x1 : [ "aconst_null" ],
195 0x19 : [ "aload", "index:B", special_F0, special_F0, None ],
196 0x2a : [ "aload_0" ],
197 0x2b : [ "aload_1" ],
198 0x2c : [ "aload_2" ],
199 0x2d : [ "aload_3" ],
200 0xbd : [ "anewarray", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_class" ],
201 0xb0 : [ "areturn" ],
202 0xbe : [ "arraylength" ],
203 0x3a : [ "astore", "index:B", special_F0, special_F0, None ],
204 0x4b : [ "astore_0" ],
205 0x4c : [ "astore_1" ],
206 0x4d : [ "astore_2" ],
207 0x4e : [ "astore_3" ],
208 0xbf : [ "athrow" ],
209 0x33 : [ "baload" ],
210 0x54 : [ "bastore" ],
211 0x10 : [ "bipush", "byte:B", special_F0, special_F0R, None ],
212 0x34 : [ "caload" ],
213 0x55 : [ "castore" ],
214 0xc0 : [ "checkcast", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, None ],
215 0x90 : [ "d2f" ],
216 0x8e : [ "d2i" ],
217 0x8f : [ "d2l" ],
218 0x63 : [ "dadd" ],
219 0x31 : [ "daload" ],
220 0x52 : [ "dastore" ],
221 0x98 : [ "dcmpg" ],
222 0x97 : [ "dcmpl" ],
223 0xe : [ "dconst_0" ],
224 0xf : [ "dconst_1" ],
225 0x6f : [ "ddiv" ],
226 0x18 : [ "dload", "index:1" ],
227 0x26 : [ "dload_0" ],
228 0x27 : [ "dload_1" ],
229 0x28 : [ "dload_2" ],
230 0x29 : [ "dload_3" ],
231 0x6b : [ "dmul" ],
232 0x77 : [ "dneg" ],
233 0x73 : [ "drem" ],
234 0xaf : [ "dreturn" ],
235 0x39 : [ "dstore", "index:B", special_F0, special_F0, None ],
236 0x47 : [ "dstore_0" ],
237 0x48 : [ "dstore_1" ],
238 0x49 : [ "dstore_2" ],
239 0x4a : [ "dstore_3" ],
240 0x67 : [ "dsub" ],
241 0x59 : [ "dup" ],
242 0x5a : [ "dup_x1" ],
243 0x5b : [ "dup_x2" ],
244 0x5c : [ "dup2" ],
245 0x5d : [ "dup2_x1" ],
246 0x5e : [ "dup2_x2" ],
247 0x8d : [ "f2d" ],
248 0x8b : [ "f2i" ],
249 0x8c : [ "f2l" ],
250 0x62 : [ "fadd" ],
251 0x30 : [ "faload" ],
252 0x51 : [ "fastore" ],
253 0x96 : [ "fcmpg" ],
254 0x95 : [ "fcmpl" ],
255 0xb : [ "fconst_0" ],
256 0xc : [ "fconst_1" ],
257 0xd : [ "fconst_2" ],
258 0x6e : [ "fdiv" ],
259 0x17 : [ "fload", "index:B", special_F0, special_F0, None ],
260 0x22 : [ "fload_0" ],
261 0x23 : [ "fload_1" ],
262 0x24 : [ "fload_2" ],
263 0x25 : [ "fload_3" ],
264 0x6a : [ "fmul" ],
265 0x76 : [ "fneg" ],
266 0x72 : [ "frem" ],
267 0xae : [ "freturn" ],
268 0x38 : [ "fstore", "index:B", special_F0, special_F0, None ],
269 0x43 : [ "fstore_0" ],
270 0x44 : [ "fstore_1" ],
271 0x45 : [ "fstore_2" ],
272 0x46 : [ "fstore_3" ],
273 0x66 : [ "fsub" ],
274 0xb4 : [ "getfield", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field" ],
275 0xb2 : [ "getstatic", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field", "get_field_index" ],
276 0xa7 : [ "goto", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
277 0xc8 : [ "goto_w", "branchbyte1:B branchbyte2:B branchbyte3:B branchbyte4:B", special_F3, special_F3R, None ],
278 0x91 : [ "i2b" ],
279 0x92 : [ "i2c" ],
280 0x87 : [ "i2d" ],
281 0x86 : [ "i2f" ],
282 0x85 : [ "i2l" ],
283 0x93 : [ "i2s" ],
284 0x60 : [ "iadd" ],
285 0x2e : [ "iaload" ],
286 0x7e : [ "iand" ],
287 0x4f : [ "iastore" ],
288 0x2 : [ "iconst_m1" ],
289 0x3 : [ "iconst_0" ],
290 0x4 : [ "iconst_1" ],
291 0x5 : [ "iconst_2" ],
292 0x6 : [ "iconst_3" ],
293 0x7 : [ "iconst_4" ],
294 0x8 : [ "iconst_5" ],
295 0x6c : [ "idiv" ],
296 0xa5 : [ "if_acmpeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
297 0xa6 : [ "if_acmpne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
298 0x9f : [ "if_icmpeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
299 0xa0 : [ "if_icmpne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
300 0xa1 : [ "if_icmplt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
301 0xa2 : [ "if_icmpge", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
302 0xa3 : [ "if_icmpgt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
303 0xa4 : [ "if_icmple", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
304 0x99 : [ "ifeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
305 0x9a : [ "ifne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
306 0x9b : [ "iflt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
307 0x9c : [ "ifge", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
308 0x9d : [ "ifgt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
309 0x9e : [ "ifle", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
310 0xc7 : [ "ifnonnull", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
311 0xc6 : [ "ifnull", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
312 0x84 : [ "iinc", "index:B const:B", special_F0, special_F0, None ],
313 0x15 : [ "iload", "index:B", special_F0, special_F0, None ],
314 0x1a : [ "iload_0" ],
315 0x1b : [ "iload_1" ],
316 0x1c : [ "iload_2" ],
317 0x1d : [ "iload_3" ],
318 0x68 : [ "imul" ],
319 0x74 : [ "ineg" ],
320 0xc1 : [ "instanceof", "indexbyte1:B indexbyte2:B", special_F2, special_F2R, None ],
321 0xb9 : [ "invokeinterface", "indexbyte1:B indexbyte2:B count:B null:B", special_F1, special_F1R, "get_interface", "get_interface_index" ],
322 0xb7 : [ "invokespecial", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ],
323 0xb8 : [ "invokestatic", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ],
324 0xb6 : [ "invokevirtual", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ],
325 0x80 : [ "ior" ],
326 0x70 : [ "irem" ],
327 0xac : [ "ireturn" ],
328 0x78 : [ "ishl" ],
329 0x7a : [ "ishr" ],
330 0x36 : [ "istore", "index:B", special_F0, special_F0, None ],
331 0x3b : [ "istore_0" ],
332 0x3c : [ "istore_1" ],
333 0x3d : [ "istore_2" ],
334 0x3e : [ "istore_3" ],
335 0x64 : [ "isub" ],
336 0x7c : [ "iushr" ],
337 0x82 : [ "ixor" ],
338 0xa8 : [ "jsr", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
339 0xc9 : [ "jsr_w", "branchbyte1:B branchbyte2:B branchbyte3:B branchbyte4:B", special_F3, special_F3R, None ],
340 0x8a : [ "l2d" ],
341 0x89 : [ "l2f" ],
342 0x88 : [ "l2i" ],
343 0x61 : [ "ladd" ],
344 0x2f : [ "laload" ],
345 0x7f : [ "land" ],
346 0x50 : [ "lastore" ],
347 0x94 : [ "lcmp" ],
348 0x9 : [ "lconst_0" ],
349 0xa : [ "lconst_1" ],
350 0x12 : [ "ldc", "index:B", special_F0, special_F0R, "get_value" ],
351 0x13 : [ "ldc_w", "indexbyte1:B indexbyte2:B", special_F2, special_F2R, None ],
352 0x14 : [ "ldc2_w", "indexbyte1:B indexbyte2:B", special_F2, special_F2R, None ],
353 0x6d : [ "ldiv" ],
354 0x16 : [ "lload", "index:B", special_F0, special_F0, None ],
355 0x1e : [ "lload_0" ],
356 0x1f : [ "lload_1" ],
357 0x20 : [ "lload_2" ],
358 0x21 : [ "lload_3" ],
359 0x69 : [ "lmul" ],
360 0x75 : [ "lneg" ],
361 0xab : [ "lookupswitch", LookupSwitch ],
362 0x81 : [ "lor" ],
363 0x71 : [ "lrem" ],
364 0xad : [ "lreturn" ],
365 0x79 : [ "lshl" ],
366 0x7b : [ "lshr" ],
367 0x37 : [ "lstore", "index:B", special_F0, special_F0, None ],
368 0x3f : [ "lstore_0" ],
369 0x40 : [ "lstore_1" ],
370 0x41 : [ "lstore_2" ],
371 0x42 : [ "lstore_3" ],
372 0x65 : [ "lsub" ],
373 0x7d : [ "lushr" ],
374 0x83 : [ "lxor" ],
375 0xc2 : [ "monitorenter" ],
376 0xc3 : [ "monitorexit" ],
377 0xc5 : [ "multianewarray", "indexbyte1:B indexbyte2:B dimensions:B", special_F4, special_F4R, None ],
378 0xbb : [ "new", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_class", "get_class_index2" ],
379 0xbc : [ "newarray", "atype:B", special_F0, special_F0, "get_array_type" ],
380 0x0 : [ "nop" ],
381 0x57 : [ "pop" ],
382 0x58 : [ "pop2" ],
383 0xb5 : [ "putfield", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field", "get_field_index" ],
384 0xb3 : [ "putstatic", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field", "get_field_index" ],
385 0xa9 : [ "ret", "index:B", special_F0, special_F0, None ],
386 0xb1 : [ "return" ],
387 0x35 : [ "saload" ],
388 0x56 : [ "sastore" ],
389 0x11 : [ "sipush", "byte1:B byte2:B", special_F1, special_F1R, None ],
390 0x5f : [ "swap" ],
391 0xaa : [ "tableswitch", TableSwitch ],
392 0xc4 : [ "wide" ],
393 }
394
395
396 INVERT_JAVA_OPCODES = dict([( JAVA_OPCODES[k][0], k ) for k in JAVA_OPCODES])
397
398
399 BRANCH_JVM_OPCODES = [ "goto", "goto_w", "if_acmpeq", "if_icmpeq", "if_icmpne", "if_icmplt", "if_icmpge", "if_icmpgt", "if_icmple", "ifeq", "ifne", "iflt", "ifge", "ifgt", "ifle", "ifnonnull", "ifnull", "jsr", "jsr_w" ]
400
401 BRANCH2_JVM_OPCODES = [ "goto", "goto.", "jsr", "jsr.", "if.", "return", ".return", "tableswitch", "lookupswitch" ]
402
403 MATH_JVM_OPCODES = { ".and" : '&',
404 ".add" : '+',
405 ".sub" : '-',
406 ".mul" : '*',
407 ".div" : '/',
408 ".shl" : '<<',
409 ".shr" : '>>',
410 ".xor" : '^',
411 ".or" : '|',
412 }
413
414 MATH_JVM_RE = []
415 for i in MATH_JVM_OPCODES :
416 MATH_JVM_RE.append( (re.compile( i ), MATH_JVM_OPCODES[i]) )
417
418 INVOKE_JVM_OPCODES = [ "invoke." ]
419 FIELD_READ_JVM_OPCODES = [ "get." ]
420 FIELD_WRITE_JVM_OPCODES = [ "put." ]
421
422 BREAK_JVM_OPCODES = [ "invoke.", "put.", ".store", "iinc", "pop", ".return", "if." ]
423
424 INTEGER_INSTRUCTIONS = [ "bipush", "sipush" ]
425
447
451
483
486
487 METHOD_INFO = [ '>HHHH', namedtuple("MethodInfo", "access_flags name_index descriptor_index attributes_count") ]
488 ATTRIBUTE_INFO = [ '>HL', namedtuple("AttributeInfo", "attribute_name_index attribute_length") ]
489 FIELD_INFO = [ '>HHHH', namedtuple("FieldInfo", "access_flags name_index descriptor_index attributes_count") ]
490 LINE_NUMBER_TABLE = [ '>HH', namedtuple("LineNumberTable", "start_pc line_number") ]
491 EXCEPTION_TABLE = [ '>HHHH', namedtuple("ExceptionTable", "start_pc end_pc handler_pc catch_type") ]
492 LOCAL_VARIABLE_TABLE = [ '>HHHHH', namedtuple("LocalVariableTable", "start_pc length name_index descriptor_index index") ]
493 LOCAL_VARIABLE_TYPE_TABLE = [ '>HHHHH', namedtuple("LocalVariableTypeTable", "start_pc length name_index signature_index index") ]
494
495 CODE_LOW_STRUCT = [ '>HHL', namedtuple( "LOW", "max_stack max_locals code_length" ) ]
496
497 ARRAY_TYPE = {
498 4 : "T_BOOLEAN",
499 5 : "T_CHAR",
500 6 : "T_FLOAT",
501 7 : "T_DOUBLE",
502 8 : "T_BYTE",
503 9 : "T_SHORT",
504 10 : "T_INT",
505 11 : "T_LONG",
506 }
507 INVERT_ARRAY_TYPE = dict([( ARRAY_TYPE[k][0], k ) for k in ARRAY_TYPE])
508
509
510 ACC_CLASS_FLAGS = {
511 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ],
512 0x0010 : [ "ACC_FINAL", "Declared final; no subclasses allowed." ],
513 0x0020 : [ "ACC_SUPER", "Treat superclass methods specially when invoked by the invokespecial instruction." ],
514 0x0200 : [ "ACC_INTERFACE", "Is an interface, not a class." ],
515 0x0400 : [ "ACC_ABSTRACT", "Declared abstract; may not be instantiated." ],
516 }
517 INVERT_ACC_CLASS_FLAGS = dict([( ACC_CLASS_FLAGS[k][0], k ) for k in ACC_CLASS_FLAGS])
518
519
520 ACC_FIELD_FLAGS = {
521 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ],
522 0x0002 : [ "ACC_PRIVATE", "Declared private; usable only within the defining class." ],
523 0x0004 : [ "ACC_PROTECTED", "Declared protected; may be accessed within subclasses." ],
524 0x0008 : [ "ACC_STATIC", "Declared static." ],
525 0x0010 : [ "ACC_FINAL", "Declared final; no further assignment after initialization." ],
526 0x0040 : [ "ACC_VOLATILE", "Declared volatile; cannot be cached." ],
527 0x0080 : [ "ACC_TRANSIENT", "Declared transient; not written or read by a persistent object manager." ],
528 }
529 INVERT_ACC_FIELD_FLAGS = dict([( ACC_FIELD_FLAGS[k][0], k ) for k in ACC_FIELD_FLAGS])
530
531
532 ACC_METHOD_FLAGS = {
533 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ],
534 0x0002 : [ "ACC_PRIVATE", "Declared private; accessible only within the defining class." ],
535 0x0004 : [ "ACC_PROTECTED", "Declared protected; may be accessed within subclasses." ],
536 0x0008 : [ "ACC_STATIC", "Declared static." ],
537 0x0010 : [ "ACC_FINAL", "Declared final; may not be overridden." ],
538 0x0020 : [ "ACC_SYNCHRONIZED", "Declared synchronized; invocation is wrapped in a monitor lock." ],
539 0x0100 : [ "ACC_NATIVE", "Declared native; implemented in a language other than Java." ],
540 0x0400 : [ "ACC_ABSTRACT", "Declared abstract; no implementation is provided." ],
541 0x0800 : [ "ACC_STRICT", "Declared strictfp; floating-point mode is FP-strict" ]
542 }
543 INVERT_ACC_METHOD_FLAGS = dict([( ACC_METHOD_FLAGS[k][0], k ) for k in ACC_METHOD_FLAGS])
544
546 """Generic class to manage constant info object"""
548 self.__tag = SV( '>B', buff.read_b(1) )
549
550 self.__bytes = None
551 self.__extra = 0
552
553 tag_value = self.__tag.get_value()
554 format = CONSTANT_INFO[ tag_value ][1]
555
556 self.__name = CONSTANT_INFO[ tag_value ][0]
557
558 self.format = SVs( format, CONSTANT_INFO[ tag_value ][2], buff.read( calcsize( format ) ) )
559
560
561 if tag_value == 1 :
562 self.__extra = self.format.get_value().length
563 self.__bytes = SVs( ">%ss" % self.format.get_value().length, namedtuple( CONSTANT_INFO[ tag_value ][0] + "_next", "bytes" ), buff.read( self.format.get_value().length ) )
564
567
570
573
578
581
586
592
594 - def __init__(self, class_manager, buff) :
596
598 return self.format.get_value().class_index
599
601 return self.format.get_value().name_and_type_index
602
604 - def __init__(self, class_manager, buff) :
606
608 return self.format.get_value().class_index
609
611 return self.format.get_value().name_and_type_index
612
614 - def __init__(self, class_manager, buff) :
616
618 return self.format.get_value().class_index
619
621 return self.format.get_value().name_and_type_index
622
624 - def __init__(self, class_manager, buff) :
626
628 return self.format.get_value().name_index
629
630 -class Utf8(CpInfo) :
631 - def __init__(self, class_manager, buff) :
633
635 - def __init__(self, class_manager, buff) :
637
639 - def __init__(self, class_manager, buff) :
641
643 - def __init__(self, class_manager, buff) :
645
646 -class Long(CpInfo) :
647 - def __init__(self, class_manager, buff) :
649
651 - def __init__(self, class_manager, buff) :
653
655 - def __init__(self, class_manager, buff) :
657
660
662 return self.format.get_value().name_index
663
665 return self.format.get_value().descriptor_index
666
682
683
684 CONSTANT_INFO = {
685 7 : [ "CONSTANT_Class", '>BH', namedtuple( "CONSTANT_Class_info", "tag name_index" ), Class ],
686 9 : [ "CONSTANT_Fieldref", '>BHH', namedtuple( "CONSTANT_Fieldref_info", "tag class_index name_and_type_index" ), FieldRef ],
687 10 : [ "CONSTANT_Methodref", '>BHH', namedtuple( "CONSTANT_Methodref_info", "tag class_index name_and_type_index" ), MethodRef ],
688 11 : [ "CONSTANT_InterfaceMethodref", '>BHH', namedtuple( "CONSTANT_InterfaceMethodref_info", "tag class_index name_and_type_index" ), InterfaceMethodRef ],
689 8 : [ "CONSTANT_String", '>BH', namedtuple( "CONSTANT_String_info", "tag string_index" ), String ],
690 3 : [ "CONSTANT_Integer", '>BL', namedtuple( "CONSTANT_Integer_info", "tag bytes" ), Integer ],
691 4 : [ "CONSTANT_Float", '>BL', namedtuple( "CONSTANT_Float_info", "tag bytes" ), Float ],
692 5 : [ "CONSTANT_Long", '>BLL', namedtuple( "CONSTANT_Long_info", "tag high_bytes low_bytes" ), Long ],
693 6 : [ "CONSTANT_Double", '>BLL', namedtuple( "CONSTANT_Long_info", "tag high_bytes low_bytes" ), Double ],
694 12 : [ "CONSTANT_NameAndType", '>BHH', namedtuple( "CONSTANT_NameAndType_info", "tag name_index descriptor_index" ), NameAndType ],
695 1 : [ "CONSTANT_Utf8", '>BH', namedtuple( "CONSTANT_Utf8_info", "tag length" ), Utf8 ]
696 }
697 INVERT_CONSTANT_INFO = dict([( CONSTANT_INFO[k][0], k ) for k in CONSTANT_INFO])
698 ITEM_Top = 0
699 ITEM_Integer = 1
700 ITEM_Float = 2
701 ITEM_Long = 4
702 ITEM_Double = 3
703 ITEM_Null = 5
704 ITEM_UninitializedThis = 6
705 ITEM_Object = 7
706 ITEM_Uninitialized = 8
707
708 VERIFICATION_TYPE_INFO = {
709 ITEM_Top : [ "Top_variable_info", '>B', namedtuple( "Top_variable_info", "tag" ) ],
710 ITEM_Integer : [ "Integer_variable_info", '>B', namedtuple( "Integer_variable_info", "tag" ) ],
711 ITEM_Float : [ "Float_variable_info", '>B', namedtuple( "Float_variable_info", "tag" ) ],
712 ITEM_Long : [ "Long_variable_info", '>B', namedtuple( "Long_variable_info", "tag" ) ],
713 ITEM_Double : [ "Double_variable_info", '>B', namedtuple( "Double_variable_info", "tag" ) ],
714 ITEM_Null : [ "Null_variable_info", '>B', namedtuple( "Null_variable_info", "tag" ) ],
715 ITEM_UninitializedThis : [ "UninitializedThis_variable_info", '>B', namedtuple( "UninitializedThis_variable_info", "tag" ) ],
716 ITEM_Object : [ "Object_variable_info", '>BH', namedtuple( "Object_variable_info", "tag cpool_index" ), [ ("cpool_index", "get_class") ] ],
717 ITEM_Uninitialized : [ "Uninitialized_variable_info", '>BH', namedtuple( "Uninitialized_variable_info", "tag offset" ) ],
718 }
719
721 """An object which represents a Field"""
722 - def __init__(self, class_manager, buff) :
723 self.__raw_buff = buff.read( calcsize( FIELD_INFO[0] ) )
724 self.format = SVs( FIELD_INFO[0], FIELD_INFO[1], self.__raw_buff )
725
726 self.__CM = class_manager
727 self.__attributes = []
728
729 for i in range(0, self.format.get_value().attributes_count) :
730 ai = AttributeInfo( self.__CM, buff )
731 self.__attributes.append( ai )
732
734 return self.__raw_buff + ''.join(x.get_raw() for x in self.__attributes)
735
737 val = 0
738 for i in self.__attributes :
739 val += i.length
740 return val + calcsize( FIELD_INFO[0] )
741
744
746 self.format.set_value( { "access_flags" : value } )
747
750
753
756
759
762
764 return self.__attributes
765
767 return self.format.get_value().name_index
768
770 return self.format.get_value().descriptor_index
771
776
778 """An object which represents a Method"""
779 - def __init__(self, class_manager, buff) :
780 self.format = SVs( METHOD_INFO[0], METHOD_INFO[1], buff.read( calcsize( METHOD_INFO[0] ) ) )
781
782 self.__CM = class_manager
783 self.__code = None
784 self.__attributes = []
785
786 for i in range(0, self.format.get_value().attributes_count) :
787 ai = AttributeInfo( self.__CM, buff )
788 self.__attributes.append( ai )
789
790 if ai.get_name() == "Code" :
791 self.__code = ai
792
795
797 val = 0
798 for i in self.__attributes :
799 val += i.length
800
801 return val + calcsize( METHOD_INFO[0] )
802
804 return self.__attributes
805
808
810 self.format.set_value( { "access_flags" : value } )
811
814
817
820
823
825 return self.format.get_value().name_index
826
828 return self.format.get_value().descriptor_index
829
832
835
837 self.format.set_value( { "name_index" : name_index } )
838
840 self.format.set_value( { "descriptor_index" : descriptor_index } )
841
844
846 self.__CM = cm
847 for i in self.__attributes :
848 i.set_cm( cm )
849
852
855
862
869
871 """Create a specific String constant by given the name index"""
872 - def __init__(self, class_manager, bytes) :
873 self.__string_index = class_manager.add_string( bytes )
874
880
882 """Create a specific Integer constant by given the name index"""
885
891
893 """Create a specific Class constant by given the name index"""
894 - def __init__(self, class_manager, name_index) :
895 self.__CM = class_manager
896
897 self.__name_index = name_index
898
904
906 """Create a specific NameAndType constant by given the name and the descriptor index"""
907 - def __init__(self, class_manager, name_index, descriptor_index) :
908 self.__CM = class_manager
909
910 self.__name_index = name_index
911 self.__descriptor_index = descriptor_index
912
914 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_NameAndType" ]
915 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__name_index, self.__descriptor_index )
916
917 return buff
918
920 """Create a specific FieldRef constant by given the class and the NameAndType index"""
921 - def __init__(self, class_manager, class_index, name_and_type_index) :
922 self.__CM = class_manager
923
924 self.__class_index = class_index
925 self.__name_and_type_index = name_and_type_index
926
928 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Fieldref" ]
929 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__class_index, self.__name_and_type_index )
930
931 return buff
932
934 """Create a specific MethodRef constant by given the class and the NameAndType index"""
935 - def __init__(self, class_manager, class_index, name_and_type_index) :
936 self.__CM = class_manager
937
938 self.__class_index = class_index
939 self.__name_and_type_index = name_and_type_index
940
942 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Methodref" ]
943 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__class_index, self.__name_and_type_index )
944
945 return buff
946
948 """Create a specific CodeAttributeInfo by given bytecodes (into an human readable format)"""
949 - def __init__(self, class_manager, codes) :
950 self.__CM = class_manager
951
952
953 self.__attribute_name_index = self.__CM.get_string_index( "Code" )
954 self.__attribute_length = 0
955
956
957
958 self.__max_stack = 1
959 self.__max_locals = 2
960 self.__code_length = 0
961
962
963
964 raw_buff = ""
965
966 for i in codes :
967 op_name = i[0]
968 op_value = INVERT_JAVA_OPCODES[ op_name ]
969 raw_buff += pack( '>B', op_value )
970
971 if len( JAVA_OPCODES[ op_value ] ) > 1 :
972 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value )
973 raw_buff += pack(r_format, *v_function( *i[1:] ) )
974
975 self.__code = JavaCode( self.__CM, raw_buff )
976 self.__code_length = len( raw_buff )
977
978
979
980
981 self.__exception_table_length = 0
982
983
984
985
986
987
988 self.__exception_table = []
989
990
991
992
993 self.__attributes_count = 0
994
995
996 self.__attributes = []
997
998
999
1000 self.__attribute_length = calcsize( ATTRIBUTE_INFO[0] ) + \
1001 calcsize( CODE_LOW_STRUCT[0] ) + \
1002 self.__code_length + \
1003 calcsize('>H') + \
1004 calcsize('>H')
1005
1007 return pack( ATTRIBUTE_INFO[0], self.__attribute_name_index, self.__attribute_length ) + \
1008 pack( CODE_LOW_STRUCT[0], self.__max_stack, self.__max_locals, self.__code_length ) + \
1009 self.__code.get_raw() + \
1010 pack( '>H', self.__exception_table_length ) + \
1011 ''.join( i.get_raw() for i in self.__exception_table ) + \
1012 pack( '>H', self.__attributes_count ) + \
1013 ''.join( i.get_raw() for i in self.__attributes )
1014
1015
1017 """Create a specific FieldInfo by given the name, the prototype of the "new" field"""
1018 - def __init__(self, class_manager, name, proto) :
1019 self.__CM = class_manager
1020
1021 access_flags_value = proto[0]
1022 type_value = proto[1]
1023
1024 self.__access_flags = INVERT_ACC_FIELD_FLAGS[ access_flags_value ]
1025 self.__name_index = self.__CM.get_string_index( name )
1026 if self.__name_index == -1 :
1027 self.__name_index = self.__CM.add_string( name )
1028 else :
1029 bytecode.Exit("field %s is already present ...." % name)
1030
1031 self.__descriptor_index = self.__CM.add_string( type_value )
1032
1033 self.__attributes = []
1034
1036 buff = pack( FIELD_INFO[0], self.__access_flags, self.__name_index, self.__descriptor_index, len(self.__attributes) )
1037
1038 for i in self.__attributes :
1039 buff += i.get_raw()
1040
1041 return buff
1042
1043
1045 """Create a specific MethodInfo by given the name, the prototype and the code (into an human readable format) of the "new" method"""
1046 - def __init__(self, class_manager, name, proto, codes) :
1047 self.__CM = class_manager
1048
1049 access_flags_value = proto[0]
1050 return_value = proto[1]
1051 arguments_value = proto[2]
1052
1053 self.__access_flags = INVERT_ACC_METHOD_FLAGS[ access_flags_value ]
1054
1055 self.__name_index = self.__CM.get_string_index( name )
1056 if self.__name_index == -1 :
1057 self.__name_index = self.__CM.add_string( name )
1058
1059 proto_final = "(" + arguments_value + ")" + return_value
1060 self.__descriptor_index = self.__CM.add_string( proto_final )
1061
1062 self.__attributes = []
1063
1064 self.__attributes.append( CreateCodeAttributeInfo( self.__CM, codes ) )
1065
1067 buff = pack( METHOD_INFO[0], self.__access_flags, self.__name_index, self.__descriptor_index, len(self.__attributes) )
1068
1069 for i in self.__attributes :
1070 buff += i.get_raw()
1071
1072 return buff
1073
1075 """JBC manages each bytecode with the value, name, raw buffer and special functions"""
1076
1077 - def __init__(self, class_manager, op_name, raw_buff, special=None) :
1078 self.__CM = class_manager
1079 self.__op_name = op_name
1080 self.__raw_buff = raw_buff
1081
1082 self.__special = special
1083 self.__special_value = None
1084
1085 self._load()
1086
1088 if self.__special != None :
1089 ntuple = namedtuple( self.__op_name, self.__special[2] )
1090 x = ntuple._make( unpack( self.__special[3], self.__raw_buff[1:] ) )
1091
1092 if self.__special[4] == None :
1093 self.__special_value = self.__special[0]( x )
1094 else :
1095 self.__special_value = getattr(self.__CM, self.__special[4])( self.__special[0]( x ) )
1096
1097 - def reload(self, raw_buff) :
1098 """Reload the bytecode with a new raw buffer"""
1099 self.__raw_buff = raw_buff
1100 self._load()
1101
1104
1106 """Return the length of the bytecode"""
1107 return len( self.__raw_buff )
1108
1110 """Return the current raw buffer of the bytecode"""
1111 return self.__raw_buff
1112
1114 """Return the name of the bytecode"""
1115 return self.__op_name
1116
1118 """Return the operands of the bytecode"""
1119 if isinstance( self.__special_value, list ):
1120 if len(self.__special_value) == 1 :
1121 return self.__special_value[0]
1122 return self.__special_value
1123
1126
1127 - def adjust_r(self, pos, pos_modif, len_modif) :
1128 """Adjust the bytecode (if necessary (in this cas the bytecode is a branch bytecode)) when a bytecode has been removed"""
1129
1130
1131 if pos > pos_modif :
1132 if (self.__special_value + pos) < (pos_modif) :
1133
1134 self.__special_value += len_modif
1135
1136 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
1137
1138 elif pos < pos_modif :
1139 if (self.__special_value + pos) > (pos_modif) :
1140
1141 self.__special_value -= len_modif
1142
1143 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
1144
1145 - def adjust_i(self, pos, pos_modif, len_modif) :
1146 """Adjust the bytecode (if necessary (in this cas the bytecode is a branch bytecode)) when a bytecode has been inserted"""
1147
1148
1149 if pos > pos_modif :
1150 if (self.__special_value + pos) < (pos_modif) :
1151
1152 self.__special_value -= len_modif
1153
1154 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
1155
1156 elif pos < pos_modif :
1157 if (self.__special_value + pos) > (pos_modif) :
1158
1159 self.__special_value += len_modif
1160
1161 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
1162
1164 buff = ""
1165 if self.__special_value == None :
1166 buff += self.__op_name
1167 else :
1168 if self.__op_name in BRANCH_JVM_OPCODES :
1169 buff += "%s %s %s" % (self.__op_name, self.__special_value, self.__special_value + pos)
1170 else :
1171 buff += "%s %s" % (self.__op_name, self.__special_value)
1172
1173 return buff
1174
1175 - def show(self, pos) :
1176 """Show the bytecode at a specific position
1177
1178 pos - the position into the bytecodes (integer)
1179 """
1180 print self.show_buff( pos ),
1181
1182
1184 """JavaCode manages a list of bytecode to a specific method, by decoding a raw buffer and transform each bytecode into a JBC object"""
1185 - def __init__(self, class_manager, buff) :
1186 self.__CM = class_manager
1187
1188 self.__raw_buff = buff
1189 self.__bytecodes = []
1190 self.__maps = []
1191 self.__branches = []
1192
1193 i = 0
1194 while i < len(self.__raw_buff) :
1195 op_value = unpack( '>B', self.__raw_buff[i])[0]
1196 if op_value in JAVA_OPCODES :
1197 if len( JAVA_OPCODES[ op_value ] ) >= 2 :
1198
1199 if isinstance(JAVA_OPCODES[ op_value ][1], str) == True :
1200 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value )
1201
1202 else :
1203 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_VARIABLE( i, op_value, self.__raw_buff[ i : ] )
1204
1205 len_format = calcsize(r_format)
1206 raw_buff = self.__raw_buff[ i : i + 1 + len_format ]
1207
1208 jbc = JBC( class_manager, JAVA_OPCODES[ op_value ][0], raw_buff, ( r_function, v_function, r_buff, r_format, f_function ) )
1209 self.__bytecodes.append( jbc )
1210
1211 i += len_format
1212 else :
1213 self.__bytecodes.append( JBC( class_manager, JAVA_OPCODES[ op_value ][0], self.__raw_buff[ i ] ) )
1214 else :
1215 bytecode.Exit( "op_value 0x%x is unknown" % op_value )
1216
1217 i += 1
1218
1219
1220 idx = 0
1221 nb = 0
1222 for i in self.__bytecodes :
1223 self.__maps.append( idx )
1224
1225 if i.get_name() in BRANCH_JVM_OPCODES :
1226 self.__branches.append( nb )
1227
1228 idx += i.get_length()
1229 nb += 1
1230
1232 methods = []
1233 for i in self.__bytecodes :
1234 if "invoke" in i.get_name() :
1235 operands = i.get_operands()
1236 methods.append( operands )
1237 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
1238 raw_buff = pack( '>B', op_value )
1239
1240 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value )
1241
1242 new_class_index = self.__CM.create_class( operands[0] )
1243 new_name_and_type_index = self.__CM.create_name_and_type( operands[1], operands[2] )
1244
1245 self.__CM.create_method_ref( new_class_index, new_name_and_type_index )
1246
1247 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *operands[0:] )
1248 if value == -1 :
1249 bytecode.Exit( "Unable to found method " + str(operands) )
1250
1251 raw_buff += pack(r_format, *v_function( value ) )
1252
1253 i.reload( raw_buff )
1254
1255 elif "anewarray" in i.get_name() :
1256 operands = i.get_operands()
1257 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
1258 raw_buff = pack( '>B', op_value )
1259
1260 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value )
1261
1262 new_class_index = self.__CM.create_class( operands )
1263
1264 raw_buff += pack(r_format, *v_function( new_class_index ) )
1265
1266 i.reload( raw_buff )
1267
1268 elif "getstatic" == i.get_name() :
1269 operands = i.get_operands()
1270 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
1271 raw_buff = pack( '>B', op_value )
1272
1273 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value )
1274
1275 new_class_index = self.__CM.create_class( operands[0] )
1276 new_name_and_type_index = self.__CM.create_name_and_type( operands[1], operands[2] )
1277
1278 self.__CM.create_field_ref( new_class_index, new_name_and_type_index )
1279
1280
1281 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *operands[1:] )
1282 if value == -1 :
1283 bytecode.Exit( "Unable to found method " + str(operands) )
1284
1285 raw_buff += pack(r_format, *v_function( value ) )
1286
1287 i.reload( raw_buff )
1288
1289 elif "ldc" == i.get_name() :
1290 operands = i.get_operands()
1291 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
1292 raw_buff = pack( '>B', op_value )
1293
1294 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value )
1295
1296 if operands[0] != "CONSTANT_Integer" and operands[0] != "CONSTANT_String" :
1297 bytecode.Exit( "...." )
1298
1299 if operands[0] == "CONSTANT_Integer" :
1300 new_int_index = self.__CM.create_integer( operands[1] )
1301 raw_buff += pack(r_format, *v_function( new_int_index ) )
1302
1303 elif operands[0] == "CONSTANT_String" :
1304 new_string_index = self.__CM.create_string( operands[1] )
1305
1306 raw_buff += pack(r_format, *v_function( new_string_index ) )
1307
1308 i.reload( raw_buff )
1309
1310 elif "new" == i.get_name() :
1311 operands = i.get_operands()
1312 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
1313 raw_buff = pack( '>B', op_value )
1314
1315 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value )
1316
1317 new_class_index = self.__CM.create_class( operands )
1318
1319 raw_buff += pack(r_format, *v_function( new_class_index ) )
1320
1321 i.reload( raw_buff )
1322
1323 return methods
1324
1326 """
1327 Return all bytecodes
1328
1329 @rtype : L{list}
1330 """
1331 return self.__bytecodes
1332
1334 return ''.join(x.get_raw() for x in self.__bytecodes)
1335
1337 """
1338 Display the code like a disassembler
1339 """
1340 nb = 0
1341 for i in self.__bytecodes :
1342 print nb, self.__maps[nb],
1343 i.show( self.__maps[nb] )
1344 print
1345 nb += 1
1346
1348 """
1349 Display the code like a disassembler but with instructions' links
1350 """
1351 bytecode.PrettyShow( m_a.basic_blocks.gets() )
1352
1354 """
1355 Return the relative idx by given an offset in the code
1356
1357 @param idx : an offset in the code
1358
1359 @rtype : the relative index in the code, it's the position in the list of a bytecode
1360 """
1361 n = 0
1362 x = 0
1363 for i in self.__bytecodes :
1364
1365 if n == idx :
1366 return x
1367 n += i.get_length()
1368 x += 1
1369 return -1
1370
1372 """
1373 Return a specific bytecode at an index
1374
1375 @param : the index of a bytecode
1376
1377 @rtype : L{JBC}
1378 """
1379 return self.__bytecodes[ idx ]
1380
1382 """
1383 Remove bytecode at a specific index
1384
1385 @param idx : the index to remove the bytecode
1386
1387 @rtype : the length of the removed bytecode
1388 """
1389 val = self.__bytecodes[idx]
1390 val_m = self.__maps[idx]
1391
1392
1393 if idx in self.__branches :
1394 self.__branches.remove( idx )
1395
1396
1397 for i in self.__branches :
1398 self.__bytecodes[i].adjust_r( self.__maps[i], val_m, val.get_length() )
1399
1400
1401 self.__maps.pop(idx)
1402 self.__bytecodes.pop(idx)
1403
1404
1405 self._adjust_maps( val_m, val.get_length() * -1 )
1406 self._adjust_branches( idx, -1 )
1407
1408 return val.get_length()
1409
1411 nb = 0
1412 for i in self.__maps :
1413 if i > val :
1414 self.__maps[ nb ] = i + size
1415 nb = nb + 1
1416
1418 nb = 0
1419 x = 0
1420 for i in self.__maps :
1421 if i == val :
1422 x+=1
1423
1424 if x == 2 :
1425 self.__maps[ nb ] = i + size
1426
1427 if i > val :
1428 self.__maps[ nb ] = i + size
1429 nb = nb + 1
1430
1432 nb = 0
1433 for i in self.__branches :
1434 if i > val :
1435 self.__branches[ nb ] = i + size
1436 nb += 1
1437
1439 """
1440 Insert bytecode at a specific index
1441
1442 @param idx : the index to insert the bytecode
1443 @param bytecode : a list which represent the bytecode
1444
1445 @rtype : the length of the inserted bytecode
1446 """
1447
1448 op_name = byte_code[0]
1449 op_value = INVERT_JAVA_OPCODES[ op_name ]
1450 raw_buff = pack( '>B', op_value )
1451
1452 new_jbc = None
1453
1454
1455 if len( JAVA_OPCODES[ op_value ] ) > 1 :
1456
1457
1458 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value )
1459
1460
1461 if len( JAVA_OPCODES[ op_value ] ) == 6 :
1462
1463 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *byte_code[1:] )
1464 if value == -1 :
1465 bytecode.Exit( "Unable to found " + str(byte_code[1:]) )
1466
1467 raw_buff += pack(r_format, *v_function( value ) )
1468 else :
1469 raw_buff += pack(r_format, *v_function( *byte_code[1:] ) )
1470
1471 new_jbc = JBC(self.__CM, op_name, raw_buff, ( r_function, v_function, r_buff, r_format, f_function ) )
1472 else :
1473 new_jbc = JBC(self.__CM, op_name, raw_buff)
1474
1475
1476 val_m = self.__maps[ idx ]
1477 for i in self.__branches :
1478 self.__bytecodes[i].adjust_i( self.__maps[i], val_m, new_jbc.get_length() )
1479
1480
1481
1482 self.__bytecodes.insert( idx, new_jbc )
1483 self.__maps.insert( idx, val_m )
1484 self._adjust_maps_i( val_m, new_jbc.get_length() )
1485
1486 self._adjust_branches( idx, 1 )
1487
1488
1489 if new_jbc.get_name() in BRANCH_JVM_OPCODES :
1490 self.__branches.append( idx )
1491
1492
1493
1494
1495
1496
1497 return len(raw_buff)
1498
1500 """
1501 Remplace bytecode at a specific index by another bytecode (remplace = remove + insert)
1502
1503 @param idx : the index to insert the bytecode
1504 @param bytecode : a list which represent the bytecode
1505
1506 @rtype : the length of the inserted bytecode
1507 """
1508 self.remove_at(idx) * (-1)
1509 size = self.insert_at(idx, bytecode)
1510
1511 return size
1512
1514 self.__CM = cm
1515 for i in self.__bytecodes :
1516 i.set_cm( cm )
1517
1520 self.__attributes = []
1521
1523 return self.__attributes
1524
1527
1529 - def __init__(self, class_manager, buff) :
1530 self.__CM = class_manager
1531
1532 super(CodeAttribute, self).__init__()
1533
1534
1535
1536
1537
1538
1539
1540 self.low_struct = SVs( CODE_LOW_STRUCT[0], CODE_LOW_STRUCT[1], buff.read( calcsize(CODE_LOW_STRUCT[0]) ) )
1541
1542 self.__code = JavaCode( class_manager, buff.read( self.low_struct.get_value().code_length ) )
1543
1544
1545 self.exception_table_length = SV( '>H', buff.read(2) )
1546
1547
1548
1549
1550
1551
1552 self.__exception_table = []
1553 for i in range(0, self.exception_table_length.get_value()) :
1554 et = SVs( EXCEPTION_TABLE[0], EXCEPTION_TABLE[1], buff.read( calcsize(EXCEPTION_TABLE[0]) ) )
1555 self.__exception_table.append( et )
1556
1557
1558 self.attributes_count = SV( '>H', buff.read(2) )
1559
1560
1561 self.__attributes = []
1562 for i in range(0, self.attributes_count.get_value()) :
1563 ai = AttributeInfo( self.__CM, buff )
1564 self.__attributes.append( ai )
1565
1567 return self.__attributes
1568
1570 return self.__exception_table
1571
1579
1581 return self.low_struct.get_value().code_length
1582
1583
1585 return self.low_struct.get_value().max_stack
1586
1588 return self.low_struct.get_value().max_locals
1589
1595
1598
1599
1601 print "!" * 70
1602 print self.low_struct.get_value()
1603 bytecode._Print( "ATTRIBUTES_COUNT", self.attributes_count.get_value() )
1604 for i in self.__attributes :
1605 i.show()
1606 print "!" * 70
1607
1609 print "!" * 70
1610 print self.low_struct.get_value()
1611
1613 bytecode._Print( "ATTRIBUTES_COUNT", self.attributes_count.get_value() )
1614 for i in self.__attributes :
1615 i.show()
1616 print "!" * 70
1617
1622
1627
1630
1636
1641
1643 i = 0
1644 while i < len(l_idx) :
1645 self.remove_at( l_idx[i] )
1646
1647 j = i + 1
1648 while j < len(l_idx) :
1649 if l_idx[j] > l_idx[i] :
1650 l_idx[j] -= 1
1651
1652 j += 1
1653
1654 i += 1
1655
1657
1658
1659 total_size = 0
1660 for i in l_bc :
1661 size = self.insert_at( idx, i )
1662 idx += 1
1663 total_size += size
1664 return total_size
1665
1672
1675
1677 return self.__code.get_at(idx)
1678
1680 return [ self.__code.get_at(i) for i in l_idx ]
1681
1683 self.__CM = cm
1684 for i in self.__attributes :
1685 i.set_cm( cm )
1686 self.__code.set_cm( cm )
1687
1691
1700
1703
1705 print self.sourcefile_index
1706
1724
1728
1730 return self.__line_number_table
1731
1733 bytecode._Print("LINE_NUMBER_TABLE_LENGTH", self.line_number_table_length.get_value())
1734 for x in self.__line_number_table :
1735 print "\t", x.get_value()
1736
1739
1759
1763
1765 print "LocalVariableTable", self.local_variable_table_length.get_value()
1766 for x in self.local_variable_table :
1767 print x.get_value()
1768
1788
1790 return self.local_variable_type_table_length.get_value_buff() + \
1791 ''.join(x.get_value_buff() for x in self.local_variable_type_table)
1792
1794 print "LocalVariableTypeTable", self.local_variable_type_table_length.get_value()
1795 for x in self.local_variable_type_table :
1796 print x.get_value()
1797
1806
1808 return self.debug_extension
1809
1811 print "SourceDebugExtension", self.debug_extension.get_value()
1812
1824
1836
1845
1848
1850 print "Signature", self.signature_index.get_value()
1851
1865
1867 return self.num_annotations.get_value_buff() + \
1868 ''.join(x.get_raw() for x in self.annotations)
1869
1871 print "RuntimeVisibleAnnotations", self.num_annotations.get_value()
1872 for i in self.annotations :
1873 i.show()
1874
1877 print "RuntimeInvisibleAnnotations", self.num_annotations.get_value()
1878 for i in self.annotations :
1879 i.show()
1880
1897
1899 return self.num_parameters.get_value_buff() + \
1900 ''.join(x.get_raw() for x in self.parameter_annotations)
1901
1903 print "RuntimeVisibleParameterAnnotations", self.num_parameters.get_value()
1904 for i in self.parameter_annotations :
1905 i.show()
1906
1909 print "RuntimeVisibleParameterAnnotations", self.num_annotations.get_value()
1910 for i in self.parameter_annotations :
1911 i.show()
1912
1915
1916
1917 self.num_annotations = SV( '>H', buff.read(2) )
1918 self.annotations = []
1919
1920 for i in range(0, self.num_annotations.get_value()) :
1921 self.annotations = Annotation( cm, buff )
1922
1923
1925 return self.num_annotations.get_value_buff() + \
1926 ''.join(x.get_raw() for x in self.annotations)
1927
1929 print "ParameterAnnotation", self.num_annotations.get_value()
1930 for i in self.annotations :
1931 i.show()
1932
1942
1944 return self.default_value.get_raw()
1945
1947 print "AnnotationDefault"
1948 self.default_value.show()
1949
1952
1953
1954
1955
1956
1957 self.type_index = SV( '>H', buff.read(2) )
1958 self.num_element_value_pairs = SV( '>H', buff.read(2) )
1959
1960 self.element_value_pairs = []
1961
1962 for i in range(0, self.num_element_value_pairs.get_value()) :
1963 self.element_value_pairs.append( ElementValuePair(cm, buff) )
1964
1968
1970 print "Annotation", self.type_index.get_value(), self.num_element_value_pairs.get_value()
1971 for i in self.element_value_pairs :
1972 i.show()
1973
1974
1977
1978
1979 self.element_name_index = SV( '>H', buff.read(2) )
1980 self.value = ElementValue(cm, buff)
1981
1985
1987 print "ElementValuePair", self.element_name_index.get_value()
1988 self.value.show()
1989
1990 ENUM_CONST_VALUE = [ '>HH', namedtuple("EnumConstValue", "type_name_index const_name_index") ]
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007 self.tag = SV( '>B', buff.read(1) )
2008
2009 tag = chr( self.tag.get_value() )
2010 if tag == 'B' or tag == 'C' or tag == 'D' or tag == 'F' or tag == 'I' or tag == 'J' or tag == 'S' or tag == 'Z' or tag == 's' :
2011 self.value = SV( '>H', buff.read(2) )
2012 elif tag == 'e' :
2013 self.value = SVs( ENUM_CONST_VALUE[0], ENUM_CONST_VALUE[1], buff.read( calcsize(ENUM_CONST_VALUE[0]) ) )
2014 elif tag == 'c' :
2015 self.value = SV( '>H', buff.read(2) )
2016 elif tag == '@' :
2017 self.value = Annotation( cm, buff )
2018 elif tag == '[' :
2019 self.value = ArrayValue( cm, buff )
2020 else :
2021 bytecode.Exit( "tag %c not in VERIFICATION_TYPE_INFO" % self.tag.get_value() )
2022
2028
2030 print "ElementValue", self.tag.get_value()
2031 if isinstance(self.value, SV) or isinstance(self.value, SVs) :
2032 print self.value.get_value()
2033 else :
2034 self.value.show()
2035
2038
2039
2040 self.num_values = SV( '>H', buff.read(2) )
2041
2042 self.values = []
2043 for i in range(0, self.num_values.get_value()) :
2044 self.values.append( ElementValue(cm, buff) )
2045
2049
2051 print "ArrayValue", self.num_values.get_value()
2052 for i in self.values :
2053 i.show()
2054
2057 super(ExceptionsAttribute, self).__init__()
2058
2059
2060
2061
2062
2063 self.number_of_exceptions = SV( '>H', buff.read(2) )
2064
2065 self.__exception_index_table = []
2066 for i in range(0, self.number_of_exceptions.get_value()) :
2067 self.__exception_index_table.append( SV( '>H', buff.read(2) ) )
2068
2071
2073 return self.__exception_index_table
2074
2076 print "Exceptions", self.number_of_exceptions.get_value()
2077 for i in self.__exception_index_table :
2078 print "\t", i
2079
2081 - def __init__(self, class_manager, buff) :
2090
2093
2095 general_format = self.format.get_value()
2096 if len( VERIFICATION_TYPE_INFO[ general_format.tag ] ) > 3 :
2097 print general_format,
2098 for (i,j) in VERIFICATION_TYPE_INFO[ general_format.tag ][3] :
2099 print getattr(self.__CM, j)( getattr(general_format, i) )
2100 else :
2101 print general_format
2102
2115
2118
2120 - def __init__(self, class_manager, buff) :
2121 self.__CM = class_manager
2122
2123
2124
2125 self.frame_type = SV( '>B', buff.read(1) )
2126 self.offset_delta = SV( '>H', buff.read(2) )
2127 self.number_of_locals = SV( '>H', buff.read(2) )
2128
2129
2130 self.__locals = []
2131 for i in range(0, self.number_of_locals.get_value()) :
2132 self.__locals.append( VerificationTypeInfo( self.__CM, buff ) )
2133
2134
2135 self.number_of_stack_items = SV( '>H', buff.read(2) )
2136
2137 self.__stack = []
2138 for i in range(0, self.number_of_stack_items.get_value()) :
2139 self.__stack.append( VerificationTypeInfo( self.__CM, buff ) )
2140
2142 return self.__locals
2143
2151
2166
2170
2172 self.__CM = cm
2173 for i in self.__locals :
2174 i.set_cm( cm )
2175
2178
2179
2180 self.frame_type = SV( '>B', buff.read(1) )
2181 self.offset_delta = SV( '>H', buff.read(2) )
2182
2185
2191
2194
2197
2200
2201 self.frame_type = SV( '>B', buff.read(1) )
2202
2205
2210
2213
2216
2218 - def __init__(self, class_manager, buff) :
2219 self.__CM = class_manager
2220
2221
2222 self.frame_type = SV( '>B', buff.read(1) )
2223 self.stack = VerificationTypeInfo( self.__CM, buff )
2224
2226 print "#" * 60
2227 bytecode._Print("\tSAME_LOCALS_1_STACK_ITEM_FRAME", self.frame_type.get_value())
2228 self.stack.show()
2229 print "#" * 60
2230
2233
2236
2239
2241 - def __init__(self, class_manager, buff) :
2242 self.__CM = class_manager
2243
2244
2245
2246 self.frame_type = SV( '>B', buff.read(1) )
2247 self.offset_delta = SV( '>H', buff.read(2) )
2248 self.stack = VerificationTypeInfo( self.__CM, buff )
2249
2252
2255
2258
2265
2268
2269
2270 self.frame_type = SV( '>B', buff.read(1) )
2271 self.offset_delta = SV( '>H', buff.read(2) )
2272
2275
2278
2281
2287
2289 - def __init__(self, class_manager, buff) :
2290 self.__CM = class_manager
2291
2292
2293 self.frame_type = SV( '>B', buff.read(1) )
2294 self.offset_delta = SV( '>H', buff.read(2) )
2295
2296
2297 self.__locals = []
2298 k = self.frame_type.get_value() - 251
2299 for i in range(0, k) :
2300 self.__locals.append( VerificationTypeInfo( self.__CM, buff ) )
2301
2303 return self.__locals
2304
2314
2319
2323
2325 self.__CM = cm
2326 for i in self.__locals :
2327 i.set_cm( cm )
2328
2330 - def __init__(self, class_manager, buff) :
2331 self.__CM = class_manager
2332
2333 super(StackMapTableAttribute, self).__init__()
2334
2335
2336
2337
2338 self.number_of_entries = SV( '>H', buff.read(2) )
2339
2340
2341 self.__entries = []
2342 for i in range(0, self.number_of_entries.get_value()) :
2343 frame_type = SV( '>B', buff.read_b(1) ).get_value()
2344
2345 if frame_type >= 0 and frame_type <= 63 :
2346 self.__entries.append( SameFrame( buff ) )
2347 elif frame_type >= 64 and frame_type <= 127 :
2348 self.__entries.append( SameLocals1StackItemFrame( self.__CM, buff ) )
2349 elif frame_type == 247 :
2350 self.__entries.append( SameLocals1StackItemFrameExtended( self.__CM, buff ) )
2351 elif frame_type >= 248 and frame_type <= 250 :
2352 self.__entries.append( ChopFrame( buff ) )
2353 elif frame_type == 251 :
2354 self.__entries.append( SameFrameExtended( buff ) )
2355 elif frame_type >= 252 and frame_type <= 254 :
2356 self.__entries.append( AppendFrame( self.__CM, buff ) )
2357 elif frame_type == 255 :
2358 self.__entries.append( FullFrame( self.__CM, buff ) )
2359 else :
2360 bytecode.Exit( "Frame type %d is unknown" % frame_type )
2361
2363 return self.__entries
2364
2371
2373 return self.number_of_entries.get_value_buff() + \
2374 ''.join(x.get_raw() for x in self.__entries )
2375
2380
2384
2386 self.__CM = cm
2387 for i in self.__entries :
2388 i.set_cm( cm )
2389
2391 - def __init__(self, class_manager, buff) :
2392 INNER_CLASSES_FORMAT = [ ">HHHH", "inner_class_info_index outer_class_info_index inner_name_index inner_class_access_flags" ]
2393
2394 self.__CM = class_manager
2395
2396 self.__raw_buff = buff.read( calcsize( INNER_CLASSES_FORMAT[0] ) )
2397
2398 self.format = SVs( INNER_CLASSES_FORMAT[0], namedtuple( "InnerClassesFormat", INNER_CLASSES_FORMAT[1] ), self.__raw_buff )
2399
2402
2405
2408
2410 - def __init__(self, class_manager, buff) :
2411 self.__CM = class_manager
2412
2413 super(InnerClassesAttribute, self).__init__()
2414
2415
2416
2417
2418 self.number_of_classes = SV( '>H', buff.read(2) )
2419
2420
2421
2422
2423
2424
2425 self.__classes = []
2426
2427 for i in range(0, self.number_of_classes.get_value()) :
2428 self.__classes.append( InnerClassesDesc( self.__CM, buff ) )
2429
2431 return self.__classes
2432
2434 print self.number_of_classes
2435 for i in self.__classes :
2436 i.show()
2437
2439 self.__CM = cm
2440 for i in self.__classes :
2441 i.set_cm( cm )
2442
2444 return self.number_of_classes.get_value_buff() + \
2445 ''.join(x.get_raw() for x in self.__classes)
2446
2448 - def __init__(self, class_manager, buff) :
2457
2459 print self.constantvalue_index
2460
2463
2466
2468 - def __init__(self, class_manager, buff) :
2469 ENCLOSING_METHOD_FORMAT = [ '>HH', "class_index method_index" ]
2470
2471 self.__CM = class_manager
2472
2473 super(EnclosingMethodAttribute, self).__init__()
2474
2475
2476
2477
2478
2479
2480 self.__raw_buff = buff.read( calcsize( ENCLOSING_METHOD_FORMAT[0] ) )
2481 self.format = SVs( ENCLOSING_METHOD_FORMAT[0], namedtuple( "EnclosingMethodFormat", ENCLOSING_METHOD_FORMAT[1] ), self.__raw_buff )
2482
2485
2488
2491
2492 ATTRIBUTE_INFO_DESCR = {
2493 "Code" : CodeAttribute,
2494 "Deprecated" : DeprecatedAttribute,
2495 "SourceFile" : SourceFileAttribute,
2496 "Exceptions" : ExceptionsAttribute,
2497 "LineNumberTable" : LineNumberTableAttribute,
2498 "LocalVariableTable" : LocalVariableTableAttribute,
2499 "LocalVariableTypeTable" : LocalVariableTypeTableAttribute,
2500 "StackMapTable" : StackMapTableAttribute,
2501 "InnerClasses" : InnerClassesAttribute,
2502 "ConstantValue" : ConstantValueAttribute,
2503 "EnclosingMethod" : EnclosingMethodAttribute,
2504 "Signature" : SignatureAttribute,
2505 "Synthetic" : SyntheticAttribute,
2506 "SourceDebugExtension" : SourceDebugExtensionAttribute,
2507 "RuntimeVisibleAnnotations" : RuntimeVisibleAnnotationsAttribute,
2508 "RuntimeInvisibleAnnotations" : RuntimeInvisibleAnnotationsAttribute,
2509 "RuntimeVisibleParameterAnnotations" : RuntimeVisibleParameterAnnotationsAttribute,
2510 "RuntimeInvisibleParameterAnnotations" : RuntimeInvisibleParameterAnnotationsAttribute,
2511 "AnnotationDefault" : AnnotationDefaultAttribute,
2512 }
2513
2515 """AttributeInfo manages each attribute info (Code, SourceFile ....)"""
2516 - def __init__(self, class_manager, buff) :
2527
2529 """Return the specific attribute info"""
2530 return self._info
2531
2533 """Return the name of the attribute"""
2534 return self.__name
2535
2543
2545 return self.format.get_value().attribute_name_index
2546
2548 self.format.set_value( { "attribute_name_index" : value } )
2549
2551 self.format.set_value( { "attribute_length" : value } )
2552
2555
2558
2560 self.__CM = cm
2561 self._info.set_cm( cm )
2562
2564 print self.format, self.__name
2565 if self._info != None :
2566 self._info.show()
2567
2569 print self.format, self.__name
2570 if self._info != None :
2571 if isinstance(self._info, CodeAttribute) :
2572 self._info.pretty_show(m_a)
2573 else :
2574 self._info.show()
2575
2577 """ClassManager can be used by all classes to get more information"""
2578 - def __init__(self, constant_pool, constant_pool_count) :
2579 self.constant_pool = constant_pool
2580 self.constant_pool_count = constant_pool_count
2581
2582 self.__this_class = None
2583
2598
2600 return self.constant_pool[ idx - 1]
2601
2613
2616
2628
2630 idx = 1
2631 for i in self.constant_pool :
2632 res = self.get_method( idx )
2633 if res != [] :
2634 m_class_name, m_name, m_descriptor = res
2635 if m_class_name == class_name and m_name == name and m_descriptor == descriptor :
2636 return idx
2637 idx += 1
2638
2639 return -1
2640
2652
2654 idx = 1
2655 for i in self.constant_pool :
2656 res = self.get_field( idx )
2657 if res != [] :
2658 _, m_name, m_descriptor = res
2659 if m_name == name and m_descriptor == descriptor :
2660 return idx
2661 idx += 1
2662
2668
2671
2673 idx = 1
2674 for i in self.constant_pool :
2675 if i.get_name() == "CONSTANT_Utf8" :
2676 if i.get_bytes() == name :
2677 return idx
2678 idx += 1
2679 return -1
2680
2682 idx = 1
2683 for i in self.constant_pool :
2684 if i.get_name() == "CONSTANT_Integer" :
2685 if i.get_format().get_value().bytes == value :
2686 return idx
2687 idx += 1
2688 return -1
2689
2691 idx = 1
2692 for i in self.constant_pool :
2693 if i.get_name() == "CONSTANT_String" :
2694 if self.get_string( i.get_format().get_value().string_index ) == value :
2695 return idx
2696 idx += 1
2697 return -1
2698
2700 idx = 1
2701 for i in self.constant_pool :
2702 if i.get_name() == "CONSTANT_NameAndType" :
2703 value = i.get_format().get_value()
2704 if value.name_index == name_method_index and value.descriptor_index == descriptor_method_index :
2705 return idx
2706 idx += 1
2707 return -1
2708
2710 idx = 1
2711 for i in self.constant_pool :
2712 if i.get_name() == "CONSTANT_Class" :
2713 value = i.get_format().get_value()
2714 if value.name_index == name_index :
2715 return idx
2716 idx += 1
2717 return -1
2718
2720 idx = 1
2721 for i in self.constant_pool :
2722 if i.get_name() == "CONSTANT_Methodref" :
2723 value = i.get_format().get_value()
2724 if value.class_index == new_class_index and value.name_and_type_index == new_name_and_type_index :
2725 return idx
2726 idx += 1
2727 return -1
2728
2730 idx = 1
2731 for i in self.constant_pool :
2732 if i.get_name() == "CONSTANT_Fieldref" :
2733 value = i.get_format().get_value()
2734 if value.class_index == new_class_index and value.name_and_type_index == new_name_and_type_index :
2735 return idx
2736 idx += 1
2737 return -1
2738
2740 idx = 1
2741 for i in self.constant_pool :
2742 res = self.get_method( idx )
2743 if res != [] :
2744 _, name, _ = res
2745 if name == method_name :
2746 return i.get_class_index()
2747 idx += 1
2748 return -1
2749
2751 idx = 1
2752 for i in self.constant_pool :
2753 res = self.get_class( idx )
2754 if res != [] :
2755 name = res[0]
2756 if name == class_name :
2757 return idx
2758 idx += 1
2759 return -1
2760
2762 l = []
2763 for i in self.constant_pool :
2764 if i.get_name() == "CONSTANT_Fieldref" :
2765 l.append( i )
2766 return l
2767
2769 l = []
2770 for i in self.constant_pool :
2771 if i.get_name() == "CONSTANT_Methodref" :
2772 l.append( i )
2773 return l
2774
2776 if self.constant_pool[idx - 1].get_name() == "CONSTANT_Utf8" :
2777 return self.constant_pool[idx - 1].get_bytes()
2778 return None
2779
2781 if self.constant_pool[idx - 1].get_name() == "CONSTANT_Utf8" :
2782 self.constant_pool[idx - 1].set_bytes( name )
2783 else :
2784 bytecode.Exit( "invalid index %d to set string %s" % (idx, name) )
2785
2799
2801 self.__this_class = this_class
2802
2805
2808
2810 self.constant_pool.append( elem )
2811 self.constant_pool_count.set_value( self.constant_pool_count.get_value() + 1 )
2812
2814 return self.constant_pool_count.get_value()
2815
2819
2827
2833
2836
2844
2852
2860
2869
2877
2878
3425