-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcp.go
336 lines (271 loc) · 6.68 KB
/
rcp.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
package main
import "fmt"
type RuntimeConstantPool struct {
infos []RuntimeConstantPoolInfo
bootstrapMethods []RBootstrapMethod
}
type RBootstrapMethod struct {
bootstrapMethodRef RMethodHandle
arguments []RuntimeConstantPoolInfo
}
type RuntimeConstantPoolInfo interface {
id() byte
}
const (
RClassID = 1
RFieldRefID = 2
RMethodRefID = 3
RStringID = 4
RIntegerID = 5
RFloatID = 6
RLongID = 7
RDoubleID = 8
RNameAndTypeMID = 9
RNameAndTypeFID = 10
RUtf8ID = 11
RMethodHandleID = 12
RMethodTypeID = 13
RDynamicID = 14
RModuleID = 15
RPackageID = 16
)
func (_ RClass) id() byte {
return RClassID
}
func (_ RFieldRef) id() byte {
return RFieldRefID
}
func (_ RMethodRef) id() byte {
return RMethodRefID
}
func (_ RString) id() byte {
return RStringID
}
func (_ RInteger) id() byte {
return RIntegerID
}
func (_ RFloat) id() byte {
return RFloatID
}
func (_ RLong) id() byte {
return RLongID
}
func (_ RDouble) id() byte {
return RDoubleID
}
func (_ RNameAndTypeM) id() byte {
return RNameAndTypeMID
}
func (_ RNameAndTypeF) id() byte {
return RNameAndTypeFID
}
func (_ RUtf8) id() byte {
return RUtf8ID
}
func (_ RMethodHandle) id() byte {
return RMethodHandleID
}
func (_ RMethodType) id() byte {
return RMethodTypeID
}
func (_ RDynamic) id() byte {
return RDynamicID
}
func (_ RModule) id() byte {
return RModuleID
}
func (_ RPackage) id() byte {
return RPackageID
}
type RClass struct {
RuntimeConstantPoolInfo
name string
}
type RFieldRef struct {
RuntimeConstantPoolInfo
className string
name string
descriptor Type
}
type RMethodRef struct {
RuntimeConstantPoolInfo
className string
name string
descriptor MethodDescriptor
}
type RString struct {
RuntimeConstantPoolInfo
value string
}
type RInteger struct {
RuntimeConstantPoolInfo
value int32
}
type RFloat struct {
RuntimeConstantPoolInfo
value float32
}
type RLong struct {
RuntimeConstantPoolInfo
value int64
}
type RDouble struct {
RuntimeConstantPoolInfo
value float64
}
type RNameAndTypeM struct {
RuntimeConstantPoolInfo
name string
descriptor MethodDescriptor
}
type RNameAndTypeF struct {
RuntimeConstantPoolInfo
name string
descriptor Type
}
type RUtf8 struct {
RuntimeConstantPoolInfo
value string
}
type RMethodHandle struct {
RuntimeConstantPoolInfo
referenceKind uint8
referenceIndex uint16
}
type RMethodType struct {
RuntimeConstantPoolInfo
descriptor Type
}
type RDynamic struct {
RuntimeConstantPoolInfo
bootstrapMethodAttrIndex uint16
name string
descriptor Type
}
type RModule struct {
RuntimeConstantPoolInfo
name string
}
type RPackage struct {
RuntimeConstantPoolInfo
name string
}
func Transform(class ClassFile) *RuntimeConstantPool {
ocp := class.constantPool
info := make([]RuntimeConstantPoolInfo, len(ocp))
for i := 1; i < len(info); i++ {
info[i] = transformInfo(ocp, ocp[i])
}
return &RuntimeConstantPool{
infos: info,
bootstrapMethods: transformBootstrapMethods(class, info),
}
}
func transformBootstrapMethods(class ClassFile, cp []RuntimeConstantPoolInfo) (bootstrapMethods []RBootstrapMethod) {
bootstrapMethods = make([]RBootstrapMethod, 0)
for _, attr := range class.attributes {
if bm, ok := attr.attrType.(*BootstrapMethods); ok {
for _, bm := range bm.bootstrapMethods {
bootstrapMethods = append(bootstrapMethods, RBootstrapMethod{
bootstrapMethodRef: cp[bm.bootstrapMethodRef].(RMethodHandle),
arguments: transformBootstrapArguments(cp, bm.bootstrapArguments),
})
}
return
}
}
return
}
func transformBootstrapArguments(cp []RuntimeConstantPoolInfo, bs []uint16) (arguments []RuntimeConstantPoolInfo) {
arguments = make([]RuntimeConstantPoolInfo, len(bs))
for i, b := range bs {
arguments[i] = cp[b]
}
return
}
func transformInfo(cp []ClassPoolInfo, info ClassPoolInfo) RuntimeConstantPoolInfo {
switch info := info.(type) {
case *Class:
return &RClass{
name: AsString(cp[info.nameIndex]),
}
case *FieldRef:
return &RFieldRef{
className: AsString(cp[info.classIndex]),
name: AsString(cp[(cp[info.nameAndTypeIndex]).(*NameAndType).nameIndex]),
descriptor: ReadType(CreateStringConsumer(AsString(cp[(cp[info.nameAndTypeIndex]).(*NameAndType).descriptorIndex]))),
}
case *MethodRef:
return &RMethodRef{
className: AsString(cp[info.classIndex]),
name: AsString(cp[(cp[info.nameAndTypeIndex]).(*NameAndType).nameIndex]),
descriptor: MakeMethodDescriptor(CreateStringConsumer(AsString(cp[(cp[info.nameAndTypeIndex]).(*NameAndType).descriptorIndex]))),
}
case *InterfaceMethodRef:
return &RMethodRef{
className: AsString(cp[info.classIndex]),
name: AsString(cp[(cp[info.nameAndTypeIndex]).(*NameAndType).nameIndex]),
descriptor: MakeMethodDescriptor(CreateStringConsumer(AsString(cp[(cp[info.nameAndTypeIndex]).(*NameAndType).descriptorIndex]))),
}
case *String:
return &RString{
value: AsString(cp[info.stringIndex]),
}
case *Integer:
return &RInteger{
value: int32(info.bytes),
}
case *Float:
return &RFloat{
value: float32(info.bytes), // TODO rework
}
case *Long:
return &RLong{
value: (int64(info.highBytes) << 32) | int64(info.lowBytes),
}
case *Double:
return &RDouble{
value: float64((int64(info.highBytes) << 32) | int64(info.lowBytes)), // TODO rework
}
case *NameAndType:
if (AsString(cp[info.descriptorIndex])[0]) == '(' {
return &RNameAndTypeM{
name: AsString(cp[info.nameIndex]),
descriptor: MakeMethodDescriptor(CreateStringConsumer(AsString(cp[info.descriptorIndex]))),
}
}
return &RNameAndTypeF{
name: AsString(cp[info.nameIndex]),
descriptor: ReadType(CreateStringConsumer(AsString(cp[info.descriptorIndex]))),
}
case *Utf8:
return &RUtf8{
value: string(info.bytes),
}
case *MethodHandle:
return &RMethodHandle{
referenceKind: info.referenceKind,
referenceIndex: info.referenceIndex,
}
case *MethodType:
return &RMethodType{
descriptor: ReadType(CreateStringConsumer(AsString(cp[info.descriptorIndex]))),
}
case *Dynamic:
return &RDynamic{
bootstrapMethodAttrIndex: info.bootstrapMethodAttrIndex,
name: AsString(cp[(cp[info.nameAndTypeIndex]).(*NameAndType).nameIndex]),
descriptor: ReadType(CreateStringConsumer(AsString(cp[(cp[info.nameAndTypeIndex]).(*NameAndType).descriptorIndex]))),
}
case *ModuleClassPoolInfo:
return &RModule{
name: AsString(cp[info.nameIndex]),
}
case *Package:
return &RPackage{
name: AsString(cp[info.nameIndex]),
}
default:
panic(fmt.Sprintf("Unknown constant pool info: %T", info))
}
}