-
Notifications
You must be signed in to change notification settings - Fork 2
/
classfile.py
executable file
·660 lines (430 loc) · 18.7 KB
/
classfile.py
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#! /usr/bin/env python
from io import IOBase
import logging
from logging import DEBUG, INFO
import struct
from bytes import u1, u2, u4
import jopcode
MAGIC = 0xcafebabe
MINOR_VERSION = 0x00
MAJOR_VERSION = 0x31
# access flags
ACC_PUBLIC = 0x0001 # class-applicable
ACC_PRIVATE = 0x0002
ACC_PROTECTED = 0x0004
ACC_STATIC = 0x0008
ACC_FINAL = 0x0010 #
ACC_SUPER = 0x0020 #
ACC_VOLATILE = 0x0040
ACC_TRANSIENT = 0x0080
ACC_INTERFACE = 0x0200 #
ACC_ABSTRACT = 0x0400 #
# Field descriptors
DESC_BYTE = 'B'
DESC_CHAR = 'C'
DESC_DOUBLE = 'D'
DESC_FLOAT = 'F'
DESC_INT = 'I'
DESC_LONG = 'J'
DESC_SHORT = 'S'
DESC_BOOLEAN = 'Z'
DESC_VOID = 'V'
# And this one is handy
DESC_STRING = 'Ljava/lang/String;'
# to start with, String and Integer constants will be implemented and that's
# all:
# constant tags
CONSTANT_Class = 7
CONSTANT_Fieldref = 9
CONSTANT_Methodref = 10
#CONSTANT_InterfaceMethodRef = 11
CONSTANT_String = 8
CONSTANT_Integer = 3
CONSTANT_Float = 4
CONSTANT_Long = 5
CONSTANT_Double = 6
CONSTANT_NameAndType = 12
CONSTANT_Utf8 = 1
ATTR_CODE = 'Code'
ATTR_CONSTANT_VALUE = 'ConstantValue'
ATTR_SOURCE_FILE = 'SourceFile'
ATTR_LINE_NUMBER_TABLE = 'LineNumberTable'
log = logging.getLogger('classfile')
log.setLevel(INFO)
# debugging
def getByteList(byteString):
return map(lambda x: '{0:#x}'.format(x), struct.unpack('BBBB', byteString))
# FIXME arrayDimension never gets used by the parser, remove?
def fieldDescriptor(classname, arrayDimension = 0):
ret = ''
for i in range(arrayDimension):
ret += '['
ret = 'L'
ret += classname.replace('.', '/')
ret += ';'
return arrayDescriptor(ret, arrayDimension)
def arrayDescriptor(typename, arrayDimension=0):
ret = typename
for i in range(arrayDimension):
ret = '[' + ret
return ret
# fieldTypes and returnType should be a list of either DESC_FOO or returns
# from fieldDescriptor
def methodDescriptor(returnType = DESC_VOID, fieldTypes = []):
ret = '('
for fieldType in fieldTypes:
ret += fieldType
ret += ')'
ret += returnType
return ret
def getPos(stream):
return '0x' + format(stream.tell(), 'x')
class ClassFormatException(Exception):
def __init__(self, name):
assert isinstance(name, basestring)
self.name = name
def __str__(self):
return self.name
class JavaClass(object):
def __init__(self, classname, superclass = 'java.lang.Object', access_flags = ACC_PUBLIC):
assert isinstance(classname, basestring)
if log.getEffectiveLevel() <= DEBUG:
log.debug('JavaClass (' + `classname` + ', ' + `superclass` + ', ' + `access_flags` + ')')
self.name = classname
self.constant_pool = []
self.this_class = self.classConstant(classname)
self.super_class = self.classConstant(superclass)
self.access_flags = access_flags;
self.interfaces = []
self.fields = []
self.methods = []
self.attributes = []
def setSourceFile(self, sourceFile):
for attr in self.attributes:
if isinstance(attr, SourceFile_attribute):
raise ClassFormatException('Attempted to set a SourceFile attribute on a class that already has one (' + attr.sourceFile + '): ' + sourceFile)
self.attributes.append(SourceFile_attribute(self, sourceFile))
# the next methods are to ensure reuse of constant values
def utf8Constant(self, value):
index = 0
if isinstance(value, unicode):
unicodeValue = value
else:
unicodeValue = unicode(value, 'utf-8')
for const in self.constant_pool:
if const.tag == CONSTANT_Utf8 and const.value == unicodeValue:
return index + 1
else:
index += 1
self.constant_pool.append(CONSTANT_Utf8_info(unicodeValue))
return index + 1
def integerConstant(self, value):
index = 0
integerValue = int(value)
for const in self.constant_pool:
if const.tag == CONSTANT_Integer and const.value == integerValue:
return index + 1
else:
index += 1
self.constant_pool.append(CONSTANT_Integer_info(integerValue))
return index + 1
def floatConstant(self, value):
index = 0
floatValue = float(value)
for const in self.constant_pool:
if const.tag == CONSTANT_Float and const.value == floatValue:
return index + 1
else:
index += 1
self.constant_pool.append(CONSTANT_Float_info(floatValue))
return index + 1
# FIXME refactor this and class and whatever to just switch on tag
def stringConstant(self, value):
utf8Index = self.utf8Constant(value)
index = 0
for const in self.constant_pool:
if const.tag == CONSTANT_String and const.value == utf8Index:
return index + 1
else:
index += 1
self.constant_pool.append(CONSTANT_String_info(utf8Index))
return index + 1
def classConstant(self, value):
utf8Index = self.utf8Constant(value.replace('.', '/'))
index = 0
for const in self.constant_pool:
if const.tag == CONSTANT_Class and const.value == utf8Index:
return index + 1
else:
index += 1
self.constant_pool.append(CONSTANT_Class_info(utf8Index))
return index + 1
def nameAndTypeConstant(self, name, typeDescriptor):
nameIndex = self.utf8Constant(name)
typeIndex = self.utf8Constant(typeDescriptor)
index = 0
for const in self.constant_pool:
if const.tag == CONSTANT_NameAndType and const.value == nameIndex and const.secondValue == typeIndex:
return index + 1
else:
index += 1
self.constant_pool.append(CONSTANT_NameAndType_info(nameIndex, typeIndex))
return index + 1
def methodConstant(self, className, methodName, methodSignature):
classIndex = self.classConstant(className)
nameAndTypeIndex = self.nameAndTypeConstant(methodName, methodSignature);
index = 0
for const in self.constant_pool:
if const.tag == CONSTANT_Methodref and const.value == classIndex and const.secondValue == nameAndTypeIndex:
return index + 1
else:
index += 1
self.constant_pool.append(CONSTANT_Methodref_info(classIndex, nameAndTypeIndex))
return index + 1
def fieldConstant(self, className, fieldName, typeDesc):
classIndex = self.classConstant(className)
nameAndTypeIndex = self.nameAndTypeConstant(fieldName, typeDesc);
index = 0
for const in self.constant_pool:
if const.tag == CONSTANT_Fieldref and const.value == classIndex and const.secondValue == nameAndTypeIndex:
return index + 1
else:
index += 1
self.constant_pool.append(CONSTANT_Fieldref_info(classIndex, nameAndTypeIndex))
return index + 1
# interface implementation
def implementedInterface(self, name):
index = self.classConstant(name)
if index not in self.interfaces:
self.interfaces.append(index)
def field(self, name, descriptor, access_flags = ACC_PUBLIC, attributes = []):
# FIXME check for name clashes!
f = field_info(self, name, descriptor, access_flags, attributes)
self.fields.append(f)
return f
def method(self, name, descriptor, access_flags, attributes):
# log.debug(`self` + '.method(' + `name` + ', ' + `descriptor` + ', ' + `access_flags` + ', ' + `attributes` + ')')
# TODO assert the code attribute is present
# FIXME check for clashes!
m = method_info(self, name, descriptor, access_flags, attributes)
self.methods.append(m)
return m
# stream output
def write(self, stream):
assert isinstance(stream, IOBase)
stream.write(u4(MAGIC))
stream.write(u2(MINOR_VERSION))
stream.write(u2(MAJOR_VERSION))
stream.write(u2(len(self.constant_pool) + 1))
for constant in self.constant_pool:
if log.getEffectiveLevel() <= DEBUG:
log.debug('writing constant ' + `constant` + ' at ' + getPos(stream))
constant.write(stream)
stream.write(u2(self.access_flags))
stream.write(u2(self.this_class))
stream.write(u2(self.super_class))
stream.write(u2(len(self.interfaces)))
for interface in self.interfaces:
stream.write(u2(interface))
stream.write(u2(len(self.fields)))
for field in self.fields:
if log.getEffectiveLevel() <= DEBUG:
log.debug('writing field ' + `field` + ' at ' + getPos(stream))
field.write(stream)
stream.write(u2(len(self.methods)))
for method in self.methods:
if log.getEffectiveLevel() <= DEBUG:
log.debug('writing method ' + `method` + ' at ' + getPos(stream))
method.write(stream)
stream.write(u2(len(self.attributes)))
for attribute in self.attributes:
if log.getEffectiveLevel() <= DEBUG:
log.debug('writing class attribute ' + `attribute` + ' at ' + getPos(stream))
attribute.write(stream)
class ConstantTableEntry(object):
def __init__(self, tag):
assert isinstance(tag, int)
self.tag = tag
self.value = None
self.bytes = []
def write(self, stream):
stream.write(u1(self.tag))
stream.write(self.bytes)
class CONSTANT_Utf8_info(ConstantTableEntry):
def __init__(self, value): # value is some Python string
assert isinstance(value, unicode)
ConstantTableEntry.__init__(self, CONSTANT_Utf8)
self.value = value
self.bytes = self.value.encode('utf-8')
def write(self, stream):
stream.write(u1(self.tag))
l = len(self.bytes)
stream.write(u2(l))
stream.write(self.bytes)
class CONSTANT_Integer_info(ConstantTableEntry):
def __init__(self, value):
assert isinstance(value, int)
ConstantTableEntry.__init__(self, CONSTANT_Integer)
# value is already an integer
self.value = value # int(value)
self.bytes = u4(value)
class CONSTANT_Float_info(ConstantTableEntry):
def __init__(self, value):
assert isinstance(value, float)
ConstantTableEntry.__init__(self, CONSTANT_Float)
self.value = value
# discovery: Python packs floats counter-endian to java
self.bytes = struct.pack('f', value)[-1::-1]
if log.getEffectiveLevel() <= DEBUG:
log.debug('CONSTANT_Float_info containing ' + `value` + ': ' + value.hex() +' , bytes ' + `getByteList(self.bytes)`)
# base class for the various magic string structures
class ConstantStringTableEntry(ConstantTableEntry):
def __init__(self, tag, index):
assert isinstance(index, int) and index >= 0
ConstantTableEntry.__init__(self, tag)
self.value = index
self.bytes = u2(index)
class CONSTANT_String_info(ConstantStringTableEntry):
def __init__(self, index):
ConstantStringTableEntry.__init__(self, CONSTANT_String, index)
class CONSTANT_Class_info(ConstantStringTableEntry):
def __init__(self, index):
ConstantStringTableEntry.__init__(self, CONSTANT_Class, index)
# fields, methods, interface methods
class ConstantReferencePairTableEntry(ConstantTableEntry):
def __init__(self, tag, index1, index2):
ConstantTableEntry.__init__(self, tag)
self.value = index1
self.secondValue = index2
self.bytes = u2(index1) + u2(index2)
class CONSTANT_NameAndType_info(ConstantReferencePairTableEntry):
def __init__(self, nameIndex, descriptorIndex):
ConstantReferencePairTableEntry.__init__(self, CONSTANT_NameAndType, nameIndex, descriptorIndex)
class CONSTANT_Fieldref_info(ConstantReferencePairTableEntry):
def __init__(self, classIndex, nameAndTypeIndex):
ConstantReferencePairTableEntry.__init__(self, CONSTANT_Fieldref, classIndex, nameAndTypeIndex)
class CONSTANT_Methodref_info(ConstantReferencePairTableEntry):
def __init__(self, classIndex, nameAndTypeIndex):
ConstantReferencePairTableEntry.__init__(self, CONSTANT_Methodref, classIndex, nameAndTypeIndex)
# attributes
# TODO wire this into class and utility methods
class attrib_info(object):
def __init__(self, owningClass, name, info):
# FIXME assert that info is a byte array
self.name_index = owningClass.utf8Constant(name)
self.info = info
def write(self, stream):
stream.write(u2(self.name_index))
stream.write(u4(len(self.info)))
stream.write(self.info)
SOURCE_FILE_ATTRIBUTE_LENGTH = 2
class SourceFile_attribute(object):
def __init__(self, owningClass, sourceFile):
self.sourceFile = sourceFile
self.attribute_name_index = owningClass.utf8Constant(ATTR_SOURCE_FILE)
self.sourcefile_index = owningClass.utf8Constant(sourceFile)
def write(self, stream):
stream.write(u2(self.attribute_name_index))
stream.write(u4(SOURCE_FILE_ATTRIBUTE_LENGTH))
stream.write(u2(self.sourcefile_index))
SIZE_OF_LINE_NUMBER_TABLE_ENTRY = 4 # bytes
class LineNumberTable_attribute(object):
# lineNumberTable is an iterable of (start_pc, line_number) pairs
def __init__(self, owningClass, lineNumberTable):
self.attribute_name_index = owningClass.utf8Constant(ATTR_LINE_NUMBER_TABLE)
self.line_number_table = lineNumberTable
self.size = (len(lineNumberTable) * SIZE_OF_LINE_NUMBER_TABLE_ENTRY) + 8
def write(self, stream):
stream.write(u2(self.attribute_name_index))
stream.write(u4(self.size - 6))
stream.write(u2(len(self.line_number_table)))
for entry in self.line_number_table:
log.info('line number pair ' + `entry`)
stream.write(u2(entry[0]))
stream.write(u2(entry[1]))
SIZE_OF_EXCEPTION_TABLE_ENTRY = 8
EMPTY_METHOD = u1(jopcode.getOperation('return').opcode)
class Code_attribute(object):
def __init__(self, owningClass, max_stack = 0, max_locals = 0, code = EMPTY_METHOD, exception_table = [], attributes = []):
if log.getEffectiveLevel() <= DEBUG:
log.debug('Code_attribute(' + `owningClass` + ', ' + `max_stack` + ', ' + `max_locals` + ', ' + `code` + ', ' + `exception_table` + ', ' + `attributes` + ')')
self.attribute_name_index = owningClass.utf8Constant(ATTR_CODE)
# Size of max_stack, max_locals, code_length, exception_table_length,
# attributes_count = 12.
# Size of attribute.name_index, length = 6
# attributes that go on a code attribute must now support size member
self.attribute_length = len(code) + SIZE_OF_EXCEPTION_TABLE_ENTRY * len (exception_table) + reduce(lambda x, y: x + y.size, attributes, 0) + 12
self.max_stack = max_stack
self.max_locals = max_locals
self.code = code
self.exception_table = exception_table
self.attributes = attributes
def write(self, stream):
if log.getEffectiveLevel() <= DEBUG:
log.debug('Writing ' + `self` + ' at ' + getPos(stream))
stream.write(u2(self.attribute_name_index))
stream.write(u4(self.attribute_length))
stream.write(u2(self.max_stack))
stream.write(u2(self.max_locals))
stream.write(u4(len(self.code)))
stream.write(self.code)
stream.write(u2(len(self.exception_table)))
for exception in self.exception_table:
exception.write(stream)
stream.write(u2(len(self.attributes)))
for attribute in self.attributes:
attribute.write(stream)
class ExceptionTableEntry(object):
def __init__(self, owningClass, startPc, endPc, exceptionClassName, handlerPc):
self.start_pc = startPc
self.end_pc = endPc
self.handler_pc = handlerPc
if exceptionClassName == None:
self.catch_type = 0
else:
self.catch_type = owningClass.classConstant(exceptionClassName)
def write(self, stream):
stream.write(u2(self.start_pc))
stream.write(u2(self.end_pc))
stream.write(u2(self.handler_pc))
stream.write(u2(self.catch_type))
# This is for static constant field initializers
# http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#1405
SIZE_OF_CONSTANT_ATTRIBUTE = 2
class ConstantValue_attribute(object):
# long float double integer or string constants only
# must specify type as it might not be obvious from the literal... or should it be?
# subclass for consistency's sake?
def __init__(self, owningClass, typeDesc, value):
if typeDesc == DESC_STRING:
self.constant_index = owningClass.stringConstant(value)
elif typeDesc == DESC_INT:
self.constant_index = owningClass.integerConstant(value)
elif typeDesc == DESC_FLOAT or typeDesc == DESC_LONG or typeDesc == DESC_DOUBLE:
raise ClassFormatException("NotImplementedYet")
else:
raise ClassFormatException("ConstantValue attributes must be of type String, Integer (or Long), or Float (or Double). Unsupported type " + `typeDesc`)
self.name_index = owningClass.utf8Constant(ATTR_CONSTANT_VALUE)
def write(self, stream):
stream.write(u2(self.name_index))
stream.write(u4(SIZE_OF_CONSTANT_ATTRIBUTE))
stream.write(u2(self.constant_index))
# fields
class field_info(object):
def __init__(self, owningClass, name, descriptor, access_flags, attributes):
if log.getEffectiveLevel() <= DEBUG:
log.debug('field_info(' + `owningClass` + ', ' + `name` + ', ' + `descriptor` + ', ' + `access_flags` + ', ' + `attributes` + ')')
self.name_index = owningClass.utf8Constant(name)
self.descriptor_index = owningClass.utf8Constant(descriptor)
self.access_flags = access_flags
self.attributes = attributes
def write(self, stream):
stream.write(u2(self.access_flags))
stream.write(u2(self.name_index))
stream.write(u2(self.descriptor_index))
stream.write(u2(len(self.attributes)))
for attrib in self.attributes:
attrib.write(stream)
# methods
class method_info(field_info):
None