This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconst-pool.cpp
More file actions
439 lines (310 loc) · 12.5 KB
/
const-pool.cpp
File metadata and controls
439 lines (310 loc) · 12.5 KB
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
#ifndef JDECOMPILER_CONST_POOL_CPP
#define JDECOMPILER_CONST_POOL_CPP
#include "jdecompiler-instance.cpp"
#include "primitive-to-string.cpp"
#define DEFINE_CONSTANT_NAME(name)\
static constexpr const char* CONSTANT_NAME = #name;\
virtual const char* getConstantName() const override {\
return CONSTANT_NAME;\
}
namespace jdecompiler {
struct Constant {
virtual ~Constant() {}
virtual const char* getConstantName() const = 0;
virtual uint8_t getPositions() const {
return 1;
}
};
struct InterConstant {
virtual const Constant* createConstant(const ConstantPool&) const = 0;
};
struct ConstantPool {
public:
const uint16_t size;
private:
const Constant** pool;
const InterConstant** interPool;
public:
ConstantPool(ClassInputStream&);
private:
template<typename C>
static inline constexpr void checkTemplate() {
static_assert(is_base_of<Constant, C>(), "template type C of method ConstantPool::get0 is not subclass of class Constant");
}
inline void checkIndex(uint16_t index) const {
if(index >= size)
throw ConstantPoolIndexOutOfBoundsException(index, size);
}
mutable stack<uint16_t> backtrace;
void initInterConstant(uint16_t index) const {
if(pool[index] != nullptr)
return;
if(backtrace.has(index)) {
string backtraceString;
for(const uint16_t backtraceIndex : backtrace)
backtraceString += "\n#" + to_string(backtraceIndex);
throw ConstantPoolInitializingException("Recursion detected while initializing a constant pool: " + backtraceString);
}
if(interPool[index] == nullptr)
throw ConstantPoolInitializingException("Cannot initialize constant #" + to_string(index) + ": inter constant is null");
pool[index] = interPool[index]->createConstant(*this);
}
template<class C>
const C* get0(uint16_t index) const {
checkTemplate<C>();
checkIndex(index);
const Constant* const constant = pool[index];
if(instanceof<const C*>(constant))
return static_cast<const C*>(constant);
throw InvalidConstantPoolReferenceException("Invalid constant pool reference " + hexWithPrefix<4>(index) +
": expected " + C::CONSTANT_NAME + ", got " + (constant == nullptr ? "null" : constant->getConstantName()));
//": expected " + typenameof(C) + ", got " + (constant == nullptr ? "null" : typenameof(*constant)));
}
public:
const Constant*& operator[](uint16_t index) const {
checkIndex(index);
return pool[index];
}
template<class C>
const C* getInter(uint16_t index) const {
initInterConstant(index);
return get0<C>(index);
}
inline const Utf8Constant& getInterUtf8Constant(uint16_t index) const {
return *getInter<Utf8Constant>(index);
}
template<class C>
const C* getNullable(uint16_t index) const {
return index == 0 ? nullptr : get0<C>(index);
}
template<class C>
inline const C* get(uint16_t index) const {
return get0<C>(index);
}
template<class C>
const C* getOrDefault(uint16_t index, function<const C*()> defaultValueGetter) const {
if(index == 0)
return defaultValueGetter();
return get0<C>(index);
}
inline const Utf8Constant& getUtf8Constant(uint16_t index) const {
return *get<Utf8Constant>(index);
}
};
struct Utf8Constant: Constant, string {
DEFINE_CONSTANT_NAME(Utf8);
Utf8Constant(const char* str, size_t length): string(str, length) {}
Utf8Constant(const char* str): string(str) {}
};
struct ConstValueConstant: Constant {
DEFINE_CONSTANT_NAME(ConstantValue);
virtual string toString(const ClassInfo&) const = 0;
virtual const Operation* toOperation() const = 0;
};
template<typename T>
struct NumberConstant: ConstValueConstant {
const T value;
NumberConstant(const T value): value(value) {}
virtual string toString(const ClassInfo&) const override {
return primitiveToString(value);
}
};
struct IntegerConstant: NumberConstant<jint> {
DEFINE_CONSTANT_NAME(Integer);
IntegerConstant(const jint value): NumberConstant(value) {};
virtual const Operation* toOperation() const override;
};
struct FloatConstant: NumberConstant<jfloat> {
DEFINE_CONSTANT_NAME(Float);
FloatConstant(const jfloat value): NumberConstant(value) {};
virtual const Operation* toOperation() const override;
};
struct LongConstant: NumberConstant<jlong> {
DEFINE_CONSTANT_NAME(Long);
LongConstant(const jlong value): NumberConstant(value) {};
virtual const Operation* toOperation() const override;
virtual uint8_t getPositions() const override {
return 2;
}
};
struct DoubleConstant: NumberConstant<jdouble> {
DEFINE_CONSTANT_NAME(Double);
DoubleConstant(const jdouble value): NumberConstant(value) {};
virtual const Operation* toOperation() const override;
virtual uint8_t getPositions() const override {
return 2;
}
};
struct ClassConstant: ConstValueConstant {
DEFINE_CONSTANT_NAME(Class);
const Utf8Constant& name;
ClassConstant(const Utf8Constant& name): name(name) {}
ClassConstant(const ConstantPool& constPool, uint16_t index): name(constPool.getInterUtf8Constant(index)) {}
virtual string toString(const ClassInfo&) const override;
virtual const Operation* toOperation() const override;
};
struct StringConstant: ConstValueConstant {
DEFINE_CONSTANT_NAME(String);
const Utf8Constant& value;
StringConstant(const Utf8Constant& value): value(value) {}
StringConstant(const ConstantPool& constPool, uint16_t index): value(constPool.getInterUtf8Constant(index)) {}
virtual string toString(const ClassInfo&) const override {
return stringToLiteral(value);
}
virtual const Operation* toOperation() const override;
};
struct NameAndTypeConstant: Constant {
DEFINE_CONSTANT_NAME(NameAndType);
const Utf8Constant & name, & descriptor;
NameAndTypeConstant(const ConstantPool& constPool, uint16_t nameIndex, uint16_t descriptorIndex):
name(constPool.getInterUtf8Constant(nameIndex)), descriptor(constPool.getInterUtf8Constant(descriptorIndex)) {}
};
struct ReferenceConstant: Constant {
DEFINE_CONSTANT_NAME(Reference);
const ClassConstant* const clazz;
const NameAndTypeConstant* const nameAndType;
ReferenceConstant(const ConstantPool& constPool, uint16_t classIndex, uint16_t nameAndTypeIndex):
clazz(constPool.getInter<ClassConstant>(classIndex)), nameAndType(constPool.getInter<NameAndTypeConstant>(nameAndTypeIndex)) {}
};
struct FieldrefConstant: ReferenceConstant {
DEFINE_CONSTANT_NAME(Fieldref);
FieldrefConstant(const ConstantPool& constPool, uint16_t classIndex, uint16_t nameAndTypeIndex):
ReferenceConstant(constPool, classIndex, nameAndTypeIndex) {}
};
struct MethodrefConstant: ReferenceConstant {
DEFINE_CONSTANT_NAME(Methodref);
MethodrefConstant(const ConstantPool& constPool, uint16_t classIndex, uint16_t nameAndTypeIndex):
ReferenceConstant(constPool, classIndex, nameAndTypeIndex) {}
};
struct InterfaceMethodrefConstant: MethodrefConstant {
DEFINE_CONSTANT_NAME(InterfaceMethodref);
InterfaceMethodrefConstant(const ConstantPool& constPool, uint16_t classIndex, uint16_t nameAndTypeIndex):
MethodrefConstant(constPool, classIndex, nameAndTypeIndex) {}
};
struct MethodHandleConstant: ConstValueConstant {
DEFINE_CONSTANT_NAME(MethodHandle);
public:
enum class ReferenceKind {
GETFIELD = 1, GETSTATIC, PUTFIELD, PUTSTATIC, INVOKEVIRTUAL, INVOKESTATIC, INVOKESPECIAL, NEWINVOKESPECIAL, INVOKEINTERFACE
};
enum class KindType { FIELD, METHOD };
const ReferenceKind referenceKind;
const KindType kindType;
const ReferenceConstant* const referenceConstant;
private:
static KindType getKindType(const ReferenceKind referenceKind) {
switch(referenceKind) {
case ReferenceKind::GETFIELD: case ReferenceKind::GETSTATIC: case ReferenceKind::PUTFIELD: case ReferenceKind::PUTSTATIC:
return KindType::FIELD;
default: return KindType::METHOD;
}
}
public:
MethodHandleConstant(const ConstantPool& constPool, uint8_t referenceKind, uint16_t referenceIndex):
referenceKind((ReferenceKind)referenceKind), kindType(getKindType((ReferenceKind)referenceKind)),
referenceConstant(constPool.getInter<ReferenceConstant>(referenceIndex)) {
if(referenceKind < 1 || referenceKind > 9)
throw IllegalStateException("referenceKind is " + to_string(referenceKind) + ", must be in the range 1 to 9");
}
virtual string toString(const ClassInfo&) const override {
return "#MethodHandle#";
}
virtual const Operation* toOperation() const override;
};
struct MethodTypeConstant: ConstValueConstant {
DEFINE_CONSTANT_NAME(MethodType);
const Utf8Constant& descriptor;
MethodTypeConstant(const Utf8Constant& descriptor): descriptor(descriptor) {}
MethodTypeConstant(const ConstantPool& constPool, uint16_t descriptorIndex):
descriptor(constPool.getInterUtf8Constant(descriptorIndex)) {}
virtual string toString(const ClassInfo&) const override;
virtual const Operation* toOperation() const override;
};
struct InvokeDynamicConstant: Constant {
DEFINE_CONSTANT_NAME(InvokeDynamic);
const uint16_t bootstrapMethodAttrIndex;
const NameAndTypeConstant* const nameAndType;
InvokeDynamicConstant(const ConstantPool& constPool, uint16_t bootstrapMethodAttrIndex, uint16_t nameAndTypeIndex):
bootstrapMethodAttrIndex(bootstrapMethodAttrIndex), nameAndType(constPool.getInter<NameAndTypeConstant>(nameAndTypeIndex)) {}
};
template<typename Const, typename... Args>
struct InterConstantImpl: InterConstant {
static_assert(is_base_of<Constant, Const>(), "Type Const must be subtype of type Constant");
const tuple<Args...> args;
InterConstantImpl(Args... args): args(args...) {}
template<size_t... indexes>
inline const Constant* createConstant0(const ConstantPool& constPool, const index_sequence<indexes...>&) const {
return new Const(constPool, get<indexes>(args)...);
}
virtual const Constant* createConstant(const ConstantPool& constPool) const override {
return createConstant0(constPool, index_sequence_for<Args...>());
}
};
ConstantPool::ConstantPool(ClassInputStream& instream): size(instream.readUShort()),
pool(new const Constant*[size]), interPool(new const InterConstant*[size]) {
for(uint16_t i = 0; i < size; i++) {
pool[i] = nullptr;
interPool[i] = nullptr;
}
for(uint16_t i = 1; i < size; i++) {
uint8_t tag = instream.readUByte();
switch(tag) {
case 1: {
uint16_t length = instream.readUShort();
const char* bytes = instream.readString(length);
pool[i] = new Utf8Constant(bytes, length);
delete[] bytes;
break;
}
case 3:
pool[i] = new IntegerConstant(instream.readInt());
break;
case 4:
pool[i] = new FloatConstant(instream.readFloat());
break;
case 5:
pool[i] = new LongConstant(instream.readLong());
i++; // Long and Double constants have historically held two positions in the pool
break;
case 6:
pool[i] = new DoubleConstant(instream.readDouble());
i++;
break;
case 7:
interPool[i] = new InterConstantImpl<ClassConstant, uint16_t>(instream.readUShort());
break;
case 8:
interPool[i] = new InterConstantImpl<StringConstant, uint16_t>(instream.readUShort());
break;
case 9:
interPool[i] = new InterConstantImpl<FieldrefConstant, uint16_t, uint16_t>(instream.readUShort(), instream.readUShort());
break;
case 10:
interPool[i] = new InterConstantImpl<MethodrefConstant, uint16_t, uint16_t>(instream.readUShort(), instream.readUShort());
break;
case 11:
interPool[i] = new InterConstantImpl<InterfaceMethodrefConstant, uint16_t, uint16_t>(instream.readUShort(), instream.readUShort());
break;
case 12:
interPool[i] = new InterConstantImpl<NameAndTypeConstant, uint16_t, uint16_t>(instream.readUShort(), instream.readUShort());
break;
case 15:
interPool[i] = new InterConstantImpl<MethodHandleConstant, uint16_t, uint16_t>(instream.readUByte(), instream.readUShort());
break;
case 16:
interPool[i] = new InterConstantImpl<MethodTypeConstant, uint16_t>(instream.readUShort());
break;
case 18:
interPool[i] = new InterConstantImpl<InvokeDynamicConstant, uint16_t, uint16_t>(instream.readUShort(), instream.readUShort());
break;
default:
throw ClassFormatError("Illegal constant type " + hexWithPrefix<2>(tag) + " at index #" + to_string(i) +
" at pos " + hexWithPrefix((uint32_t)instream.getPos()));
}
}
for(uint16_t i = 1; i < size; i += pool[i]->getPositions())
this->initInterConstant(i);
}
}
#undef DEFINE_CONSTANT_NAME
#endif