-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecode.go
528 lines (438 loc) · 13.7 KB
/
bytecode.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
package bytecode
import (
"fmt"
"log"
"reflect"
)
type Buffer struct {
buffer []byte
}
func (b *Buffer) PushBytes(bytes ...byte) {
b.buffer = append(b.buffer, bytes...)
}
func (b *Buffer) PushUInt16(values ...uint16) {
for _, value := range values {
b.buffer = append(b.buffer, Int16ToBinary(value)...)
}
}
func (b *Buffer) PushUInt32(values ...uint32) {
for _, value := range values {
b.buffer = append(b.buffer, Int32ToBinary(value)...)
}
}
func (b *Buffer) PushUInt64(values ...uint64) {
for _, value := range values {
b.buffer = append(b.buffer, Int64ToBinary(value)...)
}
}
func (b *Buffer) PushFloat32(values ...float32) {
for _, value := range values {
b.buffer = append(b.buffer, Float32ToBinary(value)...)
}
}
func (b *Buffer) PushFloat64(values ...float64) {
for _, value := range values {
b.buffer = append(b.buffer, Float64ToBinary(value)...)
}
}
func (b *Buffer) Get() []byte {
return b.buffer
}
type Constant struct {
Tag byte
Info []byte
}
type ClassVisitor struct {
MinorVersion uint16
MajorVersion uint16
AccessFlags uint16
Name string
SuperClassName string
ConstantPoolSize uint16
ConstantPool []Constant
ConstantPoolCache map[string]uint16
InterfaceLength uint16
Interfaces []string
FieldLength uint16
Fields []*FieldVisitor
MethodLength uint16
Methods []*MethodVisitor
AttributeLength uint16
Attributes []byte
}
func NewClass(majorVersion uint16, name, superClass string, interfaces []string, accessFlags uint16) *ClassVisitor {
visitor := ClassVisitor{
MinorVersion: 0,
MajorVersion: majorVersion,
AccessFlags: accessFlags,
Name: name,
SuperClassName: superClass,
Interfaces: interfaces,
ConstantPoolSize: 1,
ConstantPoolCache: make(map[string]uint16),
}
if interfaces != nil {
visitor.InterfaceLength = uint16(len(interfaces))
}
return &visitor
}
func (v *ClassVisitor) PushStringConstant(constant string) uint16 {
position := v.PushUtf8Constant(constant)
return v.PushUInt16Constant(String, position)
}
func (v *ClassVisitor) PushClassConstant(constant string) uint16 {
position := v.PushUtf8Constant(constant)
return v.PushUInt16Constant(Class, position)
}
func (v *ClassVisitor) PushUtf8Constant(constant string) uint16 {
var bytes []byte
bytes = append(bytes, Int16ToBinary(uint16(len(constant)))...)
bytes = append(bytes, []byte(constant)...)
return v.PushConstant(Utf8, bytes...)
}
func (v *ClassVisitor) PushUInt16Constant(tag byte, constants ...uint16) uint16 {
var bytes []byte
for _, constant := range constants {
bytes = append(bytes, Int16ToBinary(constant)...)
}
return v.PushConstant(tag, bytes...)
}
func (v *ClassVisitor) PushUInt32Constant(tag byte, constants ...uint32) uint16 {
var bytes []byte
for _, constant := range constants {
bytes = append(bytes, Int32ToBinary(constant)...)
}
return v.PushConstant(tag, bytes...)
}
func (v *ClassVisitor) PushInt32Constant(tag byte, constants ...int) uint16 {
var bytes []byte
for _, constant := range constants {
bytes = append(bytes, SInt32ToBinary(constant)...)
}
return v.PushConstant(tag, bytes...)
}
func (v *ClassVisitor) PushUInt64Constant(tag byte, constants ...uint64) uint16 {
var bytes []byte
for _, constant := range constants {
bytes = append(bytes, Int64ToBinary(constant)...)
}
return v.PushConstant(tag, bytes...)
}
func (v *ClassVisitor) PushInt64Constant(tag byte, constants ...int64) uint16 {
var bytes []byte
for _, constant := range constants {
bytes = append(bytes, SInt64ToBinary(constant)...)
}
return v.PushConstant(tag, bytes...)
}
func (v *ClassVisitor) PushFloat32Constant(tag byte, constants ...float32) uint16 {
var bytes []byte
for _, constant := range constants {
bytes = append(bytes, Float32ToBinary(constant)...)
}
return v.PushConstant(tag, bytes...)
}
func (v *ClassVisitor) PushFloat64Constant(tag byte, constants ...float64) uint16 {
var bytes []byte
for _, constant := range constants {
bytes = append(bytes, Float64ToBinary(constant)...)
}
return v.PushConstant(tag, bytes...)
}
func (v *ClassVisitor) PushNameAndTypeConstant(name, descriptor string) uint16 {
namePosition := v.PushUtf8Constant(name)
descriptorPosition := v.PushUtf8Constant(descriptor)
return v.PushUInt16Constant(NameAndType, namePosition, descriptorPosition)
}
func (v *ClassVisitor) PushMethodRefConstant(class, name, descriptor string) uint16 {
classPosition := v.PushClassConstant(class)
natPosition := v.PushNameAndTypeConstant(name, descriptor)
return v.PushUInt16Constant(Methodref, classPosition, natPosition)
}
func (v *ClassVisitor) PushFieldRefConstant(class, name, descriptor string) uint16 {
classPosition := v.PushClassConstant(class)
natPosition := v.PushNameAndTypeConstant(name, descriptor)
return v.PushUInt16Constant(Fieldref, classPosition, natPosition)
}
func (v *ClassVisitor) PushConstant(tag byte, constant ...byte) uint16 {
base16 := fmt.Sprintf("%x", constant)
position, ok := v.ConstantPoolCache[base16]
if ok {
return position
}
data := Constant{
Tag: tag,
Info: constant,
}
v.ConstantPool = append(v.ConstantPool, data)
index := v.ConstantPoolSize
v.ConstantPoolSize++
v.ConstantPoolCache[base16] = index
return index
}
func (v *ClassVisitor) PushTypeConstant(constant interface{}) (uint16, error, bool) {
switch value := constant.(type) {
case int8:
return v.PushInt32Constant(Integer, int(value)), nil, false
case int16:
return v.PushInt32Constant(Integer, int(value)), nil, false
case int:
return v.PushInt32Constant(Integer, value), nil, false
case int32:
return v.PushInt32Constant(Integer, int(value)), nil, false
case int64:
return v.PushInt64Constant(Long, value), nil, true
case uint8:
return v.PushInt32Constant(Integer, int(value)), nil, false
case uint16:
return v.PushInt32Constant(Integer, int(value)), nil, false
case uint:
return v.PushInt32Constant(Integer, int(value)), nil, false
case uint32:
return v.PushInt32Constant(Integer, int(value)), nil, false
case uint64:
return v.PushInt64Constant(Long, int64(value)), nil, true
case float32:
return v.PushFloat32Constant(Float, value), nil, false
case float64:
return v.PushFloat64Constant(Double, value), nil, true
case string:
return v.PushStringConstant(value), nil, false
}
return 0xffff, fmt.Errorf("constant value of %s is incorrect", reflect.TypeOf(constant)), false
}
func (v *ClassVisitor) AddSourceFile(sourceFile string) {
namePosition := v.PushUtf8Constant("SourceFile")
sourceFilePositon := v.PushUtf8Constant(sourceFile)
buffer := Buffer{}
buffer.PushUInt16(namePosition)
buffer.PushUInt32(2)
buffer.PushUInt16(sourceFilePositon)
v.AttributeLength++
v.Attributes = append(v.Attributes, buffer.Get()...)
}
func (v *ClassVisitor) AsBytecode() []byte {
classPosition := v.PushClassConstant(v.Name)
superClassPosition := v.PushClassConstant(v.SuperClassName)
var interfaces []uint16
for _, interfaceName := range v.Interfaces {
interfaces = append(interfaces, v.PushClassConstant(interfaceName))
}
var methods []byte
for _, method := range v.Methods {
methods = append(methods, method.AsBytecode()...)
}
var fields []byte
for _, field := range v.Fields {
fields = append(fields, field.AsBytecode()...)
}
buffer := Buffer{}
buffer.PushUInt32(0xcafebabe)
buffer.PushUInt16(v.MinorVersion, v.MajorVersion)
buffer.PushUInt16(v.ConstantPoolSize)
for _, pool := range v.ConstantPool {
buffer.PushBytes(pool.Tag)
buffer.PushBytes(pool.Info...)
}
buffer.PushUInt16(v.AccessFlags)
buffer.PushUInt16(classPosition)
buffer.PushUInt16(superClassPosition)
buffer.PushUInt16(v.InterfaceLength)
if v.InterfaceLength > 0 {
buffer.PushUInt16(interfaces...)
}
buffer.PushUInt16(v.FieldLength)
buffer.PushBytes(fields...)
buffer.PushUInt16(v.MethodLength)
buffer.PushBytes(methods...)
buffer.PushUInt16(v.AttributeLength)
buffer.PushBytes(v.Attributes...)
return buffer.Get()
}
func (v *ClassVisitor) NewMethod(accessFlags uint16, name, descriptor string) *MethodVisitor {
visitor := MethodVisitor{
Class: v,
AccessFlags: accessFlags,
Name: name,
Descriptor: descriptor,
}
v.MethodLength++
v.Methods = append(v.Methods, &visitor)
visitor.MaxLocals += DescriptorToStackSize(descriptor)[0]
if accessFlags&AccStatic != AccStatic {
visitor.MaxLocals++
}
return &visitor
}
func (v *ClassVisitor) NewField(accessFlags uint16, name, descriptor string, constantValue interface{}) *FieldVisitor {
visitor := FieldVisitor{
Class: v,
AccessFlags: accessFlags,
Name: name,
Descriptor: descriptor,
Constant: constantValue,
}
v.FieldLength++
v.Fields = append(v.Fields, &visitor)
return &visitor
}
type FieldVisitor struct {
Class *ClassVisitor
AccessFlags uint16
Name string
Descriptor string
Constant interface{}
}
func (f *FieldVisitor) AsBytecode() []byte {
namePosition := f.Class.PushUtf8Constant(f.Name)
descriptorPosition := f.Class.PushUtf8Constant(f.Descriptor)
buffer := Buffer{}
buffer.PushUInt16(f.AccessFlags)
buffer.PushUInt16(namePosition, descriptorPosition)
if f.AccessFlags&AccFinal == AccFinal && f.Constant != nil {
constantValuePosition, err, _ := f.Class.PushTypeConstant(f.Constant)
if err != nil {
log.Fatal(err)
}
constantNamePosition := f.Class.PushUtf8Constant("ConstantValue")
buffer.PushUInt16(1)
// ConstantValue attribute
buffer.PushUInt16(constantNamePosition)
buffer.PushUInt32(2) // ConstantValue length
buffer.PushUInt16(constantValuePosition)
} else {
buffer.PushUInt16(0) // zero attributes
}
return buffer.Get()
}
type MethodVisitor struct {
Class *ClassVisitor
AccessFlags uint16
Name string
Descriptor string
MaxLocals uint16
MaxStack uint16
StackObserver uint16
InvokeDescriptors []string
InstructionLabels []*Label
Instructions []Instruction
InstructionData [][]byte
CurrentByte uint16
}
func (m *MethodVisitor) AddTypeInsn(insn Instruction, typeName string) {
index := m.Class.PushClassConstant(typeName)
m.AddInsn(insn, Int16ToBinary(index)...)
}
func (m *MethodVisitor) AddLdcInsn(object interface{}) {
index, err, wide := m.Class.PushTypeConstant(object)
if err != nil {
log.Fatal(err)
}
if wide {
m.AddInsn(Ldc2w, Int16ToBinary(index)...)
return
}
m.AddInsn(Ldc, byte(index))
}
func (m *MethodVisitor) AddVarInsn(insn Instruction, value ...uint16) {
if len(value) > 0 {
m.AddInsn(insn, Int16ToBinary(value[0])...)
} else {
m.AddInsn(insn)
}
m.MaxLocals++
}
func (m *MethodVisitor) NewLabel() *Label {
return &Label{0}
}
func (m *MethodVisitor) AddLabel(label *Label) {
label.ByteOffset = m.CurrentByte + 1
}
func (m *MethodVisitor) AddJumpInsn(insn Instruction, label *Label) {
m.AddInsn(insn)
m.InstructionLabels = append(m.InstructionLabels, label)
}
func (m *MethodVisitor) AddInt8Insn(insn Instruction, value int8) {
m.AddInsn(insn, byte(value))
}
func (m *MethodVisitor) AddInt16Insn(insn Instruction, value int16) {
m.AddInsn(insn, SInt16ToBinary(value)...)
}
func (m *MethodVisitor) AddInt32Insn(insn Instruction, value int) {
m.AddInsn(insn, SInt32ToBinary(value)...)
}
func (m *MethodVisitor) AddInt64Insn(insn Instruction, value int64) {
m.AddInsn(insn, SInt64ToBinary(value)...)
}
func (m *MethodVisitor) AddMethodInsn(insn Instruction, instance, name, descriptor string) {
methodRefPosition := m.Class.PushMethodRefConstant(instance, name, descriptor)
m.InvokeDescriptors = append(m.InvokeDescriptors, descriptor)
m.AddInsn(insn, Int16ToBinary(methodRefPosition)...)
}
func (m *MethodVisitor) AddFieldInsn(insn Instruction, instance, name, descriptor string) {
fieldRefPosition := m.Class.PushFieldRefConstant(instance, name, descriptor)
m.AddInsn(insn, Int16ToBinary(fieldRefPosition)...)
}
func (m *MethodVisitor) AddInsn(insn Instruction, data ...byte) {
m.Instructions = append(m.Instructions, insn)
m.InstructionData = append(m.InstructionData, data)
m.CurrentByte += uint16(1 + len(data)) // calculate current byte offset
}
func (m *MethodVisitor) Maxs(maxStack, maxLocals uint16) {
m.MaxStack = maxStack
m.MaxLocals = maxLocals
}
func (m *MethodVisitor) AsBytecode() []byte {
namePosition := m.Class.PushUtf8Constant(m.Name)
descriptorPosition := m.Class.PushUtf8Constant(m.Descriptor)
codePosition := m.Class.PushUtf8Constant("Code")
insnBuffer := Buffer{}
var stack uint16
invokeIndex := 0
labelIndex := 0
currentByteOffset := uint16(0)
for i, value := range m.Instructions {
data := m.InstructionData[i]
if value.StackIntakeFlag == FlagStackLabel {
label := m.InstructionLabels[labelIndex]
data = append(data, Int16ToBinary(label.ByteOffset-currentByteOffset)...)
labelIndex++
}
if value.StackIntakeFlag == FlagStackArgs {
args := DescriptorToStackSize(m.InvokeDescriptors[invokeIndex])
invokeIndex++
stack -= args[0] + value.StackIntake
if value.StackOutputFlag == FlagStackEmpty {
stack = args[1] // empty the stack and add output size
} else {
stack += args[1] // add output size
}
} else if value.StackOutputFlag == FlagStackEmpty {
stack = value.StackOutput
} else {
stack -= value.StackIntake
stack += value.StackOutput
}
if stack > m.MaxStack {
m.MaxStack = stack
}
insnBuffer.PushBytes(value.Opcode)
insnBuffer.PushBytes(data...)
currentByteOffset += uint16(1 + len(data))
}
buffer := Buffer{}
buffer.PushUInt16(m.AccessFlags)
buffer.PushUInt16(namePosition, descriptorPosition)
buffer.PushUInt16(1) // attribute count
// Code attribute
buffer.PushUInt16(codePosition)
buffer.PushUInt32(uint32(len(insnBuffer.Get()) + 12)) // code size
buffer.PushUInt16(m.MaxStack, m.MaxLocals)
buffer.PushUInt32(uint32(len(insnBuffer.Get()))) // insn size
buffer.PushBytes(insnBuffer.Get()...) // instructions
buffer.PushBytes(0, 0, 0, 0) // exceptions + attributes
return buffer.Get()
}
type Label struct {
ByteOffset uint16
}