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
/// One usable JVM constant-pool entry.
#[derive(Clone, Debug, PartialEq)]
pub enum Constant {
/// A modified-UTF-8 string, represented without losing UTF-16 code units.
Utf8(CodeUnitString),
/// A signed `int` bit pattern.
Integer(u32),
/// An IEEE 754 single-precision bit pattern.
Float(u32),
/// A signed `long` bit pattern.
Long(u64),
/// An IEEE 754 double-precision bit pattern.
Double(u64),
/// A class or interface name index.
Class {
/// Index of the `Utf8` internal name.
name_index: u16,
},
/// A string contents index.
String {
/// Index of the `Utf8` string contents.
string_index: u16,
},
/// A field reference.
Fieldref {
/// Index of the declaring `Class`.
class_index: u16,
/// Index of the member `NameAndType`.
name_and_type_index: u16,
},
/// A class method reference.
Methodref {
/// Index of the declaring `Class`.
class_index: u16,
/// Index of the member `NameAndType`.
name_and_type_index: u16,
},
/// An interface method reference.
InterfaceMethodref {
/// Index of the declaring interface `Class`.
class_index: u16,
/// Index of the member `NameAndType`.
name_and_type_index: u16,
},
/// A name and descriptor pair.
NameAndType {
/// Index of the member-name `Utf8`.
name_index: u16,
/// Index of the descriptor `Utf8`.
descriptor_index: u16,
},
/// A direct method-handle reference.
MethodHandle {
/// JVM reference-kind discriminator from 1 through 9.
reference_kind: u8,
/// Index of the category selected by `reference_kind`.
reference_index: u16,
},
/// A method descriptor.
MethodType {
/// Index of the method-descriptor `Utf8`.
descriptor_index: u16,
},
/// A dynamically computed constant.
Dynamic {
/// Index into the class's `BootstrapMethods` attribute.
bootstrap_method_attr_index: u16,
/// Index of the constant's `NameAndType`.
name_and_type_index: u16,
},
/// A dynamically selected call site.
InvokeDynamic {
/// Index into the class's `BootstrapMethods` attribute.
bootstrap_method_attr_index: u16,
/// Index of the call site's `NameAndType`.
name_and_type_index: u16,
},
/// A module name.
Module {
/// Index of the module-name `Utf8`.
name_index: u16,
},
/// A package name.
Package {
/// Index of the package-name `Utf8`.
name_index: u16,
},
}
/// One physical constant-pool index, including indices that cannot be referenced.
#[derive(Clone, Debug, PartialEq)]
pub enum ConstantSlot {
/// The mandated, non-encoded index zero.
Reserved,
/// A usable entry encoded at this index.
Entry(Constant),
/// The explicit second slot occupied by the preceding `Long` or `Double`.
Unusable,
}
/// A stable constant-pool failure category.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ConstantPoolErrorKind {
/// The underlying bounded byte lane failed.
Bytes,
/// The pool uses an unassigned constant tag.
UnknownTag,
/// The entry is not admitted by the classfile major version.
Version,
/// A method-handle reference kind is outside 1 through 9.
ReferenceKind,
/// A referenced index is zero or outside the pool.
InvalidIndex,
/// A reference points at a reserved or unusable slot.
UnusableTarget,
/// A reference points at the wrong constant category.
WrongCategory,
/// An in-memory slot sequence cannot be encoded faithfully.
InvalidLayout,
}
/// A typed constant-pool failure located at the entry that caused it.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ConstantPoolError {
/// Stable machine-matchable failure category.
pub kind: ConstantPoolErrorKind,
/// Constant-pool index containing the invalid value, or the next index while decoding.
pub index: u16,
/// Referenced constant-pool index when the failure concerns a target.
pub target_index: Option<u16>,
/// Human-readable context.
pub message: String,
}
impl ConstantPoolError {
fn new(
kind: ConstantPoolErrorKind,
index: u16,
target: Option<u16>,
message: impl Into<String>,
) -> Self {
Self {
kind,
index,
target_index: target,
message: message.into(),
}
}
fn bytes(index: u16, error: ByteError) -> Self {
Self::new(ConstantPoolErrorKind::Bytes, index, None, error.to_string())
}
}
impl fmt::Display for ConstantPoolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} at constant-pool index {}", self.message, self.index)
}
}
impl std::error::Error for ConstantPoolError {}
/// An index-preserving constant pool whose `slots()[index]` is the physical JVM slot.
#[derive(Clone, Debug, PartialEq)]
pub struct ConstantPool {
slots: Vec<ConstantSlot>,
}
impl ConstantPool {
/// Decode the `constant_pool_count` and all following entries for `major_version`.
pub fn decode(
reader: &mut ByteReader<'_>,
major_version: u16,
) -> Result<Self, ConstantPoolError> {
let count = reader
.read_u2()
.map_err(|e| ConstantPoolError::bytes(0, e))?;
if count == 0 {
return Err(ConstantPoolError::new(
ConstantPoolErrorKind::InvalidLayout,
0,
None,
"constant_pool_count must include the reserved zero index",
));
}
reader
.preflight_allocation(usize::from(count))
.map_err(|e| ConstantPoolError::bytes(0, e))?;
let mut slots = Vec::with_capacity(usize::from(count));
slots.push(ConstantSlot::Reserved);
let mut index = 1u16;
while index < count {
let tag = reader
.read_u1()
.map_err(|e| ConstantPoolError::bytes(index, e))?;
let constant = decode_constant(reader, index, tag, major_version)?;
let two_slot = matches!(constant, Constant::Long(_) | Constant::Double(_));
slots.push(ConstantSlot::Entry(constant));
index += 1;
if two_slot {
if index >= count {
return Err(ConstantPoolError::new(
ConstantPoolErrorKind::InvalidLayout,
index - 1,
None,
"two-slot constant has no trailing unusable index",
));
}
slots.push(ConstantSlot::Unusable);
index += 1;
}
}
let pool = Self { slots };
pool.validate(major_version)?;
Ok(pool)
}
/// Borrow every physical slot. Index zero and two-slot holes remain explicit.
pub fn slots(&self) -> &[ConstantSlot] {
&self.slots
}
/// Return a usable entry or a typed located target error.
pub fn entry(
&self,
source_index: u16,
target_index: u16,
) -> Result<&Constant, ConstantPoolError> {
match self.slots.get(usize::from(target_index)) {
Some(ConstantSlot::Entry(value)) => Ok(value),
Some(ConstantSlot::Reserved | ConstantSlot::Unusable) => Err(ConstantPoolError::new(
ConstantPoolErrorKind::UnusableTarget,
source_index,
Some(target_index),
format!("index {source_index} points at unusable index {target_index}"),
)),
None => Err(ConstantPoolError::new(
ConstantPoolErrorKind::InvalidIndex,
source_index,
Some(target_index),
format!("index {source_index} points outside the pool at index {target_index}"),
)),
}
}
/// Validate layout, version bounds, reference indices, and target categories.
pub fn validate(&self, major_version: u16) -> Result<(), ConstantPoolError> {
if !matches!(self.slots.first(), Some(ConstantSlot::Reserved))
|| self.slots.len() > usize::from(u16::MAX)
{
return Err(ConstantPoolError::new(
ConstantPoolErrorKind::InvalidLayout,
0,
None,
"pool must begin with exactly one reserved slot and fit u16",
));
}
for (position, slot) in self.slots.iter().enumerate().skip(1) {
let index = position as u16;
match slot {
ConstantSlot::Reserved => {
return Err(ConstantPoolError::new(
ConstantPoolErrorKind::InvalidLayout,
index,
None,
"reserved slot is only legal at index zero",
));
}
ConstantSlot::Unusable => {
if !matches!(
self.slots.get(position - 1),
Some(ConstantSlot::Entry(Constant::Long(_) | Constant::Double(_)))
) {
return Err(ConstantPoolError::new(
ConstantPoolErrorKind::InvalidLayout,
index,
None,
"unusable slot must follow a long or double",
));
}
}
ConstantSlot::Entry(value) => {
if matches!(
self.slots.get(position.wrapping_add(1)),
Some(ConstantSlot::Unusable)
) != matches!(value, Constant::Long(_) | Constant::Double(_))
{
return Err(ConstantPoolError::new(
ConstantPoolErrorKind::InvalidLayout,
index,
None,
"long and double entries must own exactly one following unusable slot",
));
}
let minimum_major = match value {
Constant::MethodHandle { .. }
| Constant::MethodType { .. }
| Constant::InvokeDynamic { .. } => Some(51),
Constant::Module { .. } | Constant::Package { .. } => Some(53),
Constant::Dynamic { .. } => Some(55),
_ => None,
};
if let Some(minimum_major) = minimum_major
&& major_version < minimum_major
{
return Err(ConstantPoolError::new(
ConstantPoolErrorKind::Version,
index,
None,
format!(
"constant at index {index} requires classfile major version {minimum_major}"
),
));
}
validate_constant(self, index, value, major_version)?;
}
}
}
Ok(())
}
/// Encode the count and entries after revalidating the pool.
pub fn encode(
&self,
writer: &mut ByteWriter,
major_version: u16,
) -> Result<(), ConstantPoolError> {
self.validate(major_version)?;
writer
.write_u2(self.slots.len() as u16)
.map_err(|e| ConstantPoolError::bytes(0, e))?;
for (position, slot) in self.slots.iter().enumerate().skip(1) {
if let ConstantSlot::Entry(value) = slot {
encode_constant(writer, position as u16, value)?;
}
}
Ok(())
}
}