-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.go
718 lines (604 loc) · 16.6 KB
/
class.go
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package java
import (
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"math"
)
type ConstantPoolItem interface {
isConstantPoolItem()
String() string
}
type accessFlags uint16
const (
Public accessFlags = 0x0001
Static = 0x0008
Final = 0x0010
Super = 0x0020
Native = 0x0100
Interface = 0x0200
Abstract = 0x0400
Synthetic = 0x1000
Annotation = 0x2000
Enum = 0x4000
)
type Code struct {
maxStack uint16
maxLocals uint16
Instructions []byte
ExceptionHandlers []ExceptionHandler
}
type Class struct {
magic uint32
MinorVersion uint16
MajorVersion uint16
ConstantPoolItems []ConstantPoolItem
AccessFlags accessFlags
thisClass uint16
superClass uint16
interfaces []uint16
fields []field
methods []Method
initialised bool
}
type ExceptionHandler struct {
Start uint16
End uint16
Handler uint16
CatchType uint16
Class string
}
func parseCode(cr classDecoder, length uint32, method *Method) {
var c Code
c.maxStack = cr.u2()
c.maxLocals = cr.u2()
codeLength := cr.u4()
c.Instructions = make([]byte, codeLength)
for k := 0; k < len(c.Instructions); k++ {
c.Instructions[k] = cr.u1()
}
numExceptionHandlers := cr.u2()
c.ExceptionHandlers = make([]ExceptionHandler, numExceptionHandlers)
for i := 0; i < len(c.ExceptionHandlers); i++ {
c.ExceptionHandlers[i].Start = cr.u2()
c.ExceptionHandlers[i].End = cr.u2()
c.ExceptionHandlers[i].Handler = cr.u2()
catchType := cr.u2()
if catchType != 0 {
c.ExceptionHandlers[i].CatchType = catchType
info := method.class.ConstantPoolItems[catchType-1].(classInfo)
name := method.class.ConstantPoolItems[info.nameIndex-1].(utf8String)
c.ExceptionHandlers[i].Class = name.contents
}
}
for k := uint32(8) + codeLength + 2 + uint32(numExceptionHandlers)*8; k < length; k++ {
_ = cr.u1()
}
method.Code = c
}
type classDecoder struct {
reader io.Reader
err error
}
func (r classDecoder) u8() uint64 {
if r.err != nil {
return 0
}
var x uint64
r.err = binary.Read(r.reader, binary.BigEndian, &x)
return x
}
func (r classDecoder) u4() uint32 {
if r.err != nil {
return 0
}
var x uint32
r.err = binary.Read(r.reader, binary.BigEndian, &x)
return x
}
func (r classDecoder) u2() uint16 {
if r.err != nil {
return 0
}
var x uint16
r.err = binary.Read(r.reader, binary.BigEndian, &x)
return x
}
func (r classDecoder) u1() uint8 {
if r.err != nil {
return 0
}
var x uint8
r.err = binary.Read(r.reader, binary.BigEndian, &x)
return x
}
func newClassDecoder(r io.Reader) classDecoder {
cr := classDecoder{r, nil}
magic := cr.u4()
if magic != 0xCAFEBABE {
cr.err = errors.New("Bad magic number")
}
return cr
}
func parseConstantPool(c *Class, cr classDecoder, constantPoolCount uint16) []ConstantPoolItem {
items := make([]ConstantPoolItem, constantPoolCount)
for i := uint16(0); i < constantPoolCount; i++ {
tag := cr.u1()
switch tag {
case 1:
items[i] = parseUTF8String(c, cr)
case 3:
items[i] = parseIntConstant(c, cr)
case 4:
items[i] = parseFloatConstant(c, cr)
case 5:
items[i] = parseLongConstant(c, cr)
items[i+1] = WideConstantPart2{}
i++
case 6:
items[i] = parseDoubleConstant(c, cr)
items[i+1] = WideConstantPart2{}
i++
case 7:
items[i] = parseClassInfo(c, cr)
case 8:
items[i] = parseStringConstant(c, cr)
case 9:
items[i] = parseFieldRef(c, cr)
case 10:
items[i] = parseMethodRef(c, cr)
case 11:
items[i] = parseInterfaceMethodRef(c, cr)
case 12:
items[i] = parseNameAndType(c, cr)
case 15:
items[i] = parseMethodHandle(c, cr)
case 16:
items[i] = parseMethodType(c, cr)
case 18:
items[i] = parseInvokeDynamic(c, cr)
default:
log.Fatalf("Unknown tag %d\n", tag)
}
}
return items
}
func ParseClass(r io.Reader) (c Class, err error) {
cr := newClassDecoder(r)
c.MinorVersion = cr.u2() // minor version
c.MajorVersion = cr.u2() // major version
cpc := cr.u2()
constantPoolCount := cpc - 1
if cpc != 0 {
c.ConstantPoolItems = parseConstantPool(&c, cr, constantPoolCount)
}
c.AccessFlags = accessFlags(cr.u2())
c.thisClass = cr.u2()
c.superClass = cr.u2()
interfacesCount := cr.u2()
c.interfaces = make([]uint16, interfacesCount)
for i := uint16(0); i < interfacesCount; i++ {
c.interfaces[i] = cr.u2()
}
fieldsCount := cr.u2()
c.fields = make([]field, fieldsCount)
for i := uint16(0); i < fieldsCount; i++ {
c.fields[i].accessFlags = accessFlags(cr.u2())
c.fields[i].nameIndex = cr.u2()
c.fields[i].descriptorIndex = cr.u2()
attrCount := cr.u2()
for j := uint16(0); j < attrCount; j++ {
_ = cr.u2()
length := cr.u4()
for k := uint32(0); k < length; k++ {
_ = cr.u1() // throw away bytes
}
}
}
methodsCount := cr.u2()
c.methods = make([]Method, methodsCount)
for i := uint16(0); i < methodsCount; i++ {
c.methods[i].class = &c
c.methods[i].accessFlags = accessFlags(cr.u2())
c.methods[i].nameIndex = cr.u2()
c.methods[i].descriptorIndex = cr.u2()
var sig string
sig = c.ConstantPoolItems[c.methods[i].descriptorIndex-1].(utf8String).contents
c.methods[i].Signiture = parseSigniture(sig)
c.methods[i].RawSigniture = sig
attrCount := cr.u2()
for j := uint16(0); j < attrCount; j++ {
name := cr.u2()
length := cr.u4()
actualName := (c.ConstantPoolItems[name-1]).(utf8String)
if actualName.contents == "Code" {
parseCode(cr, length, &c.methods[i])
} else {
for k := uint32(0); k < length; k++ {
_ = cr.u1() // throw away bytes
}
}
}
}
attrCount := cr.u2()
for j := uint16(0); j < attrCount; j++ {
_ = cr.u2()
length := cr.u4()
for k := uint32(0); k < length; k++ {
_ = cr.u1() // throw away bytes
}
}
return c, cr.err
}
func (c *Class) hasMethodCalled(name string) bool {
for _, m := range c.methods {
n := c.ConstantPoolItems[m.nameIndex-1].(utf8String).contents
if n == name {
return true
}
}
return false
}
func (c *Class) resolveMethod(name string, descriptor string) *Method {
for i, m := range c.methods {
n := c.ConstantPoolItems[m.nameIndex-1].(utf8String).contents
d := c.ConstantPoolItems[m.descriptorIndex-1].(utf8String).contents
//log.Printf("Comparing %v::%v to %v::%v\n", name, descriptor, n, d)
if n == name && d == descriptor {
return &c.methods[i]
}
}
return nil
}
func (c *Class) getField(name string) *field {
for i, f := range c.fields {
n := c.ConstantPoolItems[f.nameIndex-1].(utf8String).contents
if n == name {
return &(c.fields[i])
}
}
panic(fmt.Sprintf("Could not find field called %v", name))
}
func (c *Class) Name() string {
info := c.ConstantPoolItems[c.thisClass-1].(classInfo)
name := c.ConstantPoolItems[info.nameIndex-1].(utf8String)
return name.contents
}
func (c *Class) getSuperName() string {
info := c.ConstantPoolItems[c.superClass-1].(classInfo)
name := c.ConstantPoolItems[info.nameIndex-1].(utf8String)
return name.contents
}
func (c *Class) getMethodRefAt(index uint16) methodRef {
return c.ConstantPoolItems[index-1].(methodRef)
}
func (c *Class) getInterfaceMethodRefAt(index uint16) interfaceMethodRef {
return c.ConstantPoolItems[index-1].(interfaceMethodRef)
}
func (c *Class) getFieldRefAt(index uint16) fieldRef {
return c.ConstantPoolItems[index-1].(fieldRef)
}
func (c *Class) getClassInfoAt(index uint16) classInfo {
return c.ConstantPoolItems[index-1].(classInfo)
}
func (c *Class) getConstantPoolItemAt(index uint16) ConstantPoolItem {
return c.ConstantPoolItems[index-1]
}
func (c *Class) getStringAt(index int) utf8String {
strRef := c.ConstantPoolItems[index].(stringConstant)
return c.ConstantPoolItems[strRef.utf8Index-1].(utf8String)
}
func (c *Class) getLongAt(index int) longConstant {
return c.ConstantPoolItems[index].(longConstant)
}
func (m methodRef) methodName() string {
nt := m.containingClass.ConstantPoolItems[m.nameAndTypeIndex-1].(nameAndType)
n := m.containingClass.ConstantPoolItems[nt.nameIndex-1].(utf8String).contents
return n
}
func (m methodRef) className() string {
ct := m.containingClass.ConstantPoolItems[m.classIndex-1].(classInfo)
c := m.containingClass.ConstantPoolItems[ct.nameIndex-1].(utf8String).contents
return c
}
func (m methodRef) methodType() string {
nt := m.containingClass.ConstantPoolItems[m.nameAndTypeIndex-1].(nameAndType)
t := m.containingClass.ConstantPoolItems[nt.descriptorIndex-1].(utf8String).contents
return t
}
func (m interfaceMethodRef) methodName() string {
nt := m.containingClass.ConstantPoolItems[m.nameAndTypeIndex-1].(nameAndType)
n := m.containingClass.ConstantPoolItems[nt.nameIndex-1].(utf8String).contents
return n
}
func (m interfaceMethodRef) methodType() string {
nt := m.containingClass.ConstantPoolItems[m.nameAndTypeIndex-1].(nameAndType)
t := m.containingClass.ConstantPoolItems[nt.descriptorIndex-1].(utf8String).contents
return t
}
func (m interfaceMethodRef) className() string {
ct := m.containingClass.ConstantPoolItems[m.classIndex-1].(classInfo)
c := m.containingClass.ConstantPoolItems[ct.nameIndex-1].(utf8String).contents
return c
}
func (ct classInfo) className() string {
c := ct.containingClass.ConstantPoolItems[ct.nameIndex-1].(utf8String).contents
return c
}
func (m fieldRef) fieldName() string {
nt := m.containingClass.ConstantPoolItems[m.nameAndTypeIndex-1].(nameAndType)
n := m.containingClass.ConstantPoolItems[nt.nameIndex-1].(utf8String).contents
return n
}
func (m fieldRef) fieldDescriptor() string {
nt := m.containingClass.ConstantPoolItems[m.nameAndTypeIndex-1].(nameAndType)
t := m.containingClass.ConstantPoolItems[nt.descriptorIndex-1].(utf8String).contents
return t
}
func (m fieldRef) className() string {
ct := m.containingClass.ConstantPoolItems[m.classIndex-1].(classInfo)
c := m.containingClass.ConstantPoolItems[ct.nameIndex-1].(utf8String).contents
return c
}
func parseSigniture(sig string) []string {
s := make([]string, 0)
className := false
for _, c := range sig {
//TODO: save the name of the class. Maybe
if className {
if c == ';' {
className = false
}
continue
}
switch c {
case '(', ')', '[':
break
case 'B':
s = append(s, "byte")
break
case 'C':
s = append(s, "char")
break
case 'D':
s = append(s, "double")
break
case 'F':
s = append(s, "float")
break
case 'I':
s = append(s, "int")
break
case 'J':
s = append(s, "long")
break
case 'S':
s = append(s, "short")
break
case 'Z':
s = append(s, "boolean")
break
case 'V':
s = append(s, "void")
break
case 'L':
s = append(s, "reference")
className = true
break
default:
log.Panicf("Can't parse signiture: %s", sig)
}
}
return s
}
type methodType struct {
descriptorIndex uint16
}
func (_ methodType) isConstantPoolItem() {}
func (n methodType) String() string {
return fmt.Sprintf("(MethodType)")
}
func parseMethodType(c *Class, cr classDecoder) ConstantPoolItem {
return methodType{cr.u2()}
}
type methodHandle struct {
referenceKind uint8
referenceIndex uint16
}
func (_ methodHandle) isConstantPoolItem() {}
func (n methodHandle) String() string {
return fmt.Sprintf("(MethodHandle)")
}
func parseMethodHandle(c *Class, cr classDecoder) ConstantPoolItem {
return methodHandle{cr.u1(), cr.u2()}
}
type invokeDynamic struct {
bootstrapMethodAttrIndex uint16
nameAndTypeIndex uint16
}
func (_ invokeDynamic) isConstantPoolItem() {}
func (n invokeDynamic) String() string {
return fmt.Sprintf("(InvokeDynamic) bootstrapMethodAttrIndex: %d, nameAndType: %d", n.bootstrapMethodAttrIndex, n.nameAndTypeIndex)
}
func parseInvokeDynamic(c *Class, cr classDecoder) ConstantPoolItem {
return invokeDynamic{cr.u2(), cr.u2()}
}
type nameAndType struct {
nameIndex uint16
descriptorIndex uint16
}
func (_ nameAndType) isConstantPoolItem() {}
func (n nameAndType) String() string {
return fmt.Sprintf("(NameAndType) name: %d, type: %d", n.nameIndex, n.descriptorIndex)
}
func parseNameAndType(c *Class, cr classDecoder) ConstantPoolItem {
nameIndex := cr.u2()
descriptorIndex := cr.u2()
return nameAndType{nameIndex, descriptorIndex}
}
type utf8String struct {
contents string
}
func (_ utf8String) isConstantPoolItem() {}
func (u utf8String) String() string {
return "(String) \"" + u.contents + "\""
}
func parseUTF8String(c *Class, cr classDecoder) ConstantPoolItem {
length := cr.u2()
bytes := make([]byte, length)
for i := uint16(0); i < length; i++ {
bytes[i] = cr.u1()
}
return utf8String{string(bytes)}
}
type classInfo struct {
containingClass *Class
nameIndex uint16
}
func (_ classInfo) isConstantPoolItem() {}
func (c classInfo) String() string {
return fmt.Sprintf("(ClassInfo) %d", c.nameIndex)
}
func parseClassInfo(c *Class, cr classDecoder) ConstantPoolItem {
nameIndex := cr.u2()
return classInfo{c, nameIndex}
}
type methodRef struct {
containingClass *Class
classIndex uint16
nameAndTypeIndex uint16
}
func (_ methodRef) isConstantPoolItem() {}
func (m methodRef) String() string {
return fmt.Sprintf("(MethodRef) class: %d, name: %d", m.classIndex, m.nameAndTypeIndex)
}
func parseMethodRef(c *Class, cr classDecoder) ConstantPoolItem {
classIndex := cr.u2()
nameAndTypeIndex := cr.u2()
return methodRef{c, classIndex, nameAndTypeIndex}
}
type interfaceMethodRef struct {
containingClass *Class
classIndex uint16
nameAndTypeIndex uint16
}
func (_ interfaceMethodRef) isConstantPoolItem() {}
func (i interfaceMethodRef) String() string {
return fmt.Sprintf("(InterfaceMethodRef) class: %d, name: %d", i.classIndex, i.nameAndTypeIndex)
}
func parseInterfaceMethodRef(c *Class, cr classDecoder) ConstantPoolItem {
classIndex := cr.u2()
nameAndTypeIndex := cr.u2()
return interfaceMethodRef{c, classIndex, nameAndTypeIndex}
}
type fieldRef struct {
containingClass *Class
classIndex uint16
nameAndTypeIndex uint16
}
func (_ fieldRef) isConstantPoolItem() {}
func (f fieldRef) String() string {
return fmt.Sprintf("(FieldRef) class: %d, name %d", f.classIndex, f.nameAndTypeIndex)
}
func parseFieldRef(c *Class, cr classDecoder) ConstantPoolItem {
classIndex := cr.u2()
nameAndTypeIndex := cr.u2()
return fieldRef{c, classIndex, nameAndTypeIndex}
}
type stringConstant struct {
utf8Index uint16
}
func (_ stringConstant) isConstantPoolItem() {}
func (s stringConstant) String() string {
return fmt.Sprintf("(StringConst) index: %d", s.utf8Index)
}
func parseStringConstant(c *Class, cr classDecoder) ConstantPoolItem {
utf8Index := cr.u2()
return stringConstant{utf8Index}
}
type intConstant struct {
value int32
}
func (_ intConstant) isConstantPoolItem() {}
func (i intConstant) String() string {
return fmt.Sprintf("(Int) %d", i.value)
}
func parseIntConstant(c *Class, cr classDecoder) ConstantPoolItem {
i := int32(cr.u4())
return intConstant{i}
}
type longConstant struct {
value int64
}
func (_ longConstant) isConstantPoolItem() {}
func (l longConstant) String() string {
return fmt.Sprintf("(Long) %d", l.value)
}
func parseLongConstant(c *Class, cr classDecoder) ConstantPoolItem {
long := int64(cr.u4()) << 32
long += int64(cr.u4())
return longConstant{long}
}
type WideConstantPart2 struct {
}
func (_ WideConstantPart2) isConstantPoolItem() {}
func (l WideConstantPart2) String() string {
return fmt.Sprintf("(Long Part 2)")
}
type floatConstant struct {
value float32
}
func (_ floatConstant) isConstantPoolItem() {}
func (f floatConstant) String() string {
return fmt.Sprintf("(Float) %f", f.value)
}
func parseFloatConstant(c *Class, cr classDecoder) ConstantPoolItem {
bits := cr.u4()
return floatConstant{math.Float32frombits(bits)}
}
type doubleConstant struct {
value float64
}
func (_ doubleConstant) isConstantPoolItem() {}
func (f doubleConstant) String() string {
return fmt.Sprintf("(Double) %v", f.value)
}
func parseDoubleConstant(c *Class, cr classDecoder) ConstantPoolItem {
bits := cr.u8()
return doubleConstant{math.Float64frombits(bits)}
}
type field struct {
accessFlags accessFlags
nameIndex uint16
descriptorIndex uint16
value javaValue
}
type Method struct {
class *Class
Signiture []string
RawSigniture string
accessFlags accessFlags
nameIndex uint16
descriptorIndex uint16
Code Code
}
func (m *Method) Name() string {
return m.class.ConstantPoolItems[m.nameIndex-1].(utf8String).contents
}
func (m *Method) Class() *Class {
return m.class
}
func (m *Method) Static() bool {
return m.accessFlags&Static != 0
}
func (m *Method) Native() bool {
return m.accessFlags&Native != 0
}
func (m *Method) numArgs() int {
return len(m.Signiture) - 1
}
func (m *Method) Sig() []string {
return m.Signiture
}