sim-codec-classfile 0.1.0

Bounded, lossless Java Virtual Machine classfile codec for SIM
Documentation
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
/// A nested attribute retained in declaration order with an unresolved name index.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NestedAttribute {
    /// Constant-pool index naming the attribute.
    pub name_index: u16,
    /// Owner category in which this attribute was decoded.
    pub owner: NestedAttributeOwner,
    /// Zero-based order within its owner's table.
    pub order: usize,
    /// Body length declared by the attribute header.
    pub declared_length: u32,
    /// Exact attribute payload.
    pub bytes: Vec<u8>,
    /// Source range covering the complete header and body.
    pub origin: AttributeOrigin,
}
/// Legal owners for attributes nested inside structured attribute bodies.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NestedAttributeOwner {
    /// A `Code` attribute.
    Code,
    /// A record component.
    RecordComponent,
}

/// One `Code` exception-table row, with all offsets and the catch index retained verbatim.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CodeException {
    /// Inclusive bytecode start offset.
    pub start_pc: u16,
    /// Exclusive bytecode end offset.
    pub end_pc: u16,
    /// Handler bytecode offset.
    pub handler_pc: u16,
    /// Constant-pool class index, or zero for a catch-all handler.
    pub catch_type: u16,
}

/// The ordered, index-preserving body of a JVM `Code` attribute.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CodeAttribute {
    /// Declared operand-stack bound.
    pub max_stack: u16,
    /// Declared local-variable bound.
    pub max_locals: u16,
    /// Exact bytecode array; instruction decoding remains a separate operation.
    pub code: Vec<u8>,
    /// Exception handlers in classfile order.
    pub exception_table: Vec<CodeException>,
    /// Nested attributes in classfile order.
    pub attributes: Vec<NestedAttribute>,
}

impl CodeAttribute {
    /// Decode a complete `Code` payload, checking only its binary shape and allocation budget.
    pub fn decode(reader: &mut ByteReader<'_>) -> Result<Self, AttributeError> {
        let max_stack = reader.read_u2()?;
        let max_locals = reader.read_u2()?;
        let code_len = usize::try_from(reader.read_u4()?).map_err(|_| {
            error(
                AttributeErrorKind::CountOverflow,
                reader.offset(),
                "code length is not addressable",
            )
        })?;
        reader.preflight_allocation(code_len)?;
        let code = reader.take(code_len)?.to_vec();
        let exception_count = usize::from(reader.read_u2()?);
        reader.preflight_allocation(exception_count)?;
        let mut exception_table = Vec::with_capacity(exception_count);
        for _ in 0..exception_count {
            exception_table.push(CodeException {
                start_pc: reader.read_u2()?,
                end_pc: reader.read_u2()?,
                handler_pc: reader.read_u2()?,
                catch_type: reader.read_u2()?,
            });
        }
        validate_code_shape(code.len(), &exception_table)?;
        let attribute_count = usize::from(reader.read_u2()?);
        reader.preflight_allocation(attribute_count)?;
        let mut attributes = Vec::with_capacity(attribute_count);
        for order in 0..attribute_count {
            let start = reader.offset();
            let name_index = reader.read_u2()?;
            let declared_length = reader.read_u4()?;
            let length = usize::try_from(declared_length).map_err(|_| {
                error(
                    AttributeErrorKind::CountOverflow,
                    reader.offset(),
                    "nested attribute length is not addressable",
                )
            })?;
            reader.preflight_allocation(length)?;
            attributes.push(NestedAttribute {
                name_index,
                owner: NestedAttributeOwner::Code,
                order,
                declared_length,
                bytes: reader.take(length)?.to_vec(),
                origin: annotation_origin(start, reader),
            });
        }
        finish(reader)?;
        Ok(Self {
            max_stack,
            max_locals,
            code,
            exception_table,
            attributes,
        })
    }

    /// Encode the payload without resolving indices or performing bytecode verification.
    pub fn encode(&self, budget: usize) -> Result<Vec<u8>, AttributeError> {
        validate_code_shape(self.code.len(), &self.exception_table)?;
        let mut out = ByteWriter::new(budget);
        out.write_u2(self.max_stack)?;
        out.write_u2(self.max_locals)?;
        out.write_u4(
            u32::try_from(self.code.len())
                .map_err(|_| error(AttributeErrorKind::CountOverflow, 0, "code is too long"))?,
        )?;
        out.write_bytes(&self.code)?;
        out.write_u2(count(self.exception_table.len(), "exception handlers")?)?;
        for row in &self.exception_table {
            out.write_u2(row.start_pc)?;
            out.write_u2(row.end_pc)?;
            out.write_u2(row.handler_pc)?;
            out.write_u2(row.catch_type)?;
        }
        out.write_u2(count(self.attributes.len(), "nested attributes")?)?;
        for attribute in &self.attributes {
            if usize::try_from(attribute.declared_length).ok() != Some(attribute.bytes.len()) {
                return Err(error(
                    AttributeErrorKind::StaticConstraint,
                    attribute.origin.start,
                    "nested attribute declared length differs from retained bytes",
                ));
            }
            out.write_u2(attribute.name_index)?;
            out.write_u4(attribute.declared_length)?;
            out.write_bytes(&attribute.bytes)?;
        }
        Ok(out.into_bytes())
    }
}

fn validate_code_shape(
    code_length: usize,
    exceptions: &[CodeException],
) -> Result<(), AttributeError> {
    if !(1..=u16::MAX as usize).contains(&code_length) {
        return Err(error(
            AttributeErrorKind::StaticConstraint,
            0,
            format!("Code array length {code_length} is outside 1..=65535"),
        ));
    }
    for exception in exceptions {
        let start = usize::from(exception.start_pc);
        let end = usize::from(exception.end_pc);
        let handler = usize::from(exception.handler_pc);
        if start >= end || end > code_length || handler >= code_length {
            return Err(error(
                AttributeErrorKind::StaticConstraint,
                start,
                format!(
                    "exception range {start}..{end} with handler {handler} is outside Code length {code_length}"
                ),
            ));
        }
    }
    Ok(())
}

/// A verifier type exactly as represented by `verification_type_info`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum VerificationType {
    /// `Top_variable_info`.
    Top,
    /// `Integer_variable_info`.
    Integer,
    /// `Float_variable_info`.
    Float,
    /// `Double_variable_info`.
    Double,
    /// `Long_variable_info`.
    Long,
    /// `Null_variable_info`.
    Null,
    /// `UninitializedThis_variable_info`.
    UninitializedThis,
    /// `Object_variable_info`, retaining its constant-pool index.
    Object(u16),
    /// `Uninitialized_variable_info`, retaining its `new` instruction offset verbatim.
    Uninitialized(u16),
}

/// One compressed stack-map frame, retained without expansion.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StackMapFrame {
    /// Tags 0 through 63.
    Same {
        /// Encoded frame tag, which is also the offset delta.
        frame_type: u8,
    },
    /// Tags 64 through 127.
    SameLocalsOneStack {
        /// Encoded frame tag.
        frame_type: u8,
        /// Sole stack entry.
        stack: VerificationType,
    },
    /// Tag 247.
    SameLocalsOneStackExtended {
        /// Explicit offset delta.
        offset_delta: u16,
        /// Sole stack entry.
        stack: VerificationType,
    },
    /// Tags 248 through 250.
    Chop {
        /// Encoded frame tag, retaining the exact number of omitted locals.
        frame_type: u8,
        /// Explicit offset delta.
        offset_delta: u16,
    },
    /// Tag 251.
    SameExtended {
        /// Explicit offset delta.
        offset_delta: u16,
    },
    /// Tags 252 through 254.
    Append {
        /// Encoded frame tag, retaining the exact number of appended locals.
        frame_type: u8,
        /// Explicit offset delta.
        offset_delta: u16,
        /// Appended locals in encoded order.
        locals: Vec<VerificationType>,
    },
    /// Tag 255.
    Full {
        /// Explicit offset delta.
        offset_delta: u16,
        /// Complete locals in encoded order.
        locals: Vec<VerificationType>,
        /// Complete stack in encoded order.
        stack: Vec<VerificationType>,
    },
}

/// An ordered `StackMapTable` payload.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StackMapTableAttribute {
    /// Frames in their original compressed representation.
    pub frames: Vec<StackMapFrame>,
}

impl StackMapTableAttribute {
    /// Decode a complete stack-map payload without performing type-state verification.
    pub fn decode(reader: &mut ByteReader<'_>) -> Result<Self, AttributeError> {
        let n = usize::from(reader.read_u2()?);
        reader.preflight_allocation(n)?;
        let mut frames = Vec::with_capacity(n);
        for _ in 0..n {
            frames.push(decode_frame(reader)?);
        }
        finish(reader)?;
        Ok(Self { frames })
    }
    /// Encode frames in their retained compressed forms.
    pub fn encode(&self, budget: usize) -> Result<Vec<u8>, AttributeError> {
        let mut out = ByteWriter::new(budget);
        out.write_u2(count(self.frames.len(), "stack-map frames")?)?;
        for frame in &self.frames {
            encode_frame(frame, &mut out)?;
        }
        Ok(out.into_bytes())
    }
}

fn decode_type(reader: &mut ByteReader<'_>) -> Result<VerificationType, AttributeError> {
    let at = reader.offset();
    Ok(match reader.read_u1()? {
        0 => VerificationType::Top,
        1 => VerificationType::Integer,
        2 => VerificationType::Float,
        3 => VerificationType::Double,
        4 => VerificationType::Long,
        5 => VerificationType::Null,
        6 => VerificationType::UninitializedThis,
        7 => VerificationType::Object(reader.read_u2()?),
        8 => VerificationType::Uninitialized(reader.read_u2()?),
        tag => {
            return Err(error(
                AttributeErrorKind::ReservedTag,
                at,
                format!("reserved verification type tag {tag}"),
            ));
        }
    })
}

fn encode_type(value: VerificationType, out: &mut ByteWriter) -> Result<(), AttributeError> {
    let (tag, extra) = match value {
        VerificationType::Top => (0, None),
        VerificationType::Integer => (1, None),
        VerificationType::Float => (2, None),
        VerificationType::Double => (3, None),
        VerificationType::Long => (4, None),
        VerificationType::Null => (5, None),
        VerificationType::UninitializedThis => (6, None),
        VerificationType::Object(v) => (7, Some(v)),
        VerificationType::Uninitialized(v) => (8, Some(v)),
    };
    out.write_u1(tag)?;
    if let Some(v) = extra {
        out.write_u2(v)?;
    }
    Ok(())
}

fn decode_frame(r: &mut ByteReader<'_>) -> Result<StackMapFrame, AttributeError> {
    let at = r.offset();
    let tag = r.read_u1()?;
    Ok(match tag {
        0..=63 => StackMapFrame::Same { frame_type: tag },
        64..=127 => StackMapFrame::SameLocalsOneStack {
            frame_type: tag,
            stack: decode_type(r)?,
        },
        128..=246 => {
            return Err(error(
                AttributeErrorKind::ReservedTag,
                at,
                format!("reserved stack-map frame tag {tag}"),
            ));
        }
        247 => StackMapFrame::SameLocalsOneStackExtended {
            offset_delta: r.read_u2()?,
            stack: decode_type(r)?,
        },
        248..=250 => StackMapFrame::Chop {
            frame_type: tag,
            offset_delta: r.read_u2()?,
        },
        251 => StackMapFrame::SameExtended {
            offset_delta: r.read_u2()?,
        },
        252..=254 => {
            let offset_delta = r.read_u2()?;
            let mut locals = Vec::with_capacity(usize::from(tag - 251));
            for _ in 0..tag - 251 {
                locals.push(decode_type(r)?);
            }
            StackMapFrame::Append {
                frame_type: tag,
                offset_delta,
                locals,
            }
        }
        255 => {
            let offset_delta = r.read_u2()?;
            let nl = usize::from(r.read_u2()?);
            r.preflight_allocation(nl)?;
            let mut locals = Vec::with_capacity(nl);
            for _ in 0..nl {
                locals.push(decode_type(r)?);
            }
            let ns = usize::from(r.read_u2()?);
            r.preflight_allocation(ns)?;
            let mut stack = Vec::with_capacity(ns);
            for _ in 0..ns {
                stack.push(decode_type(r)?);
            }
            StackMapFrame::Full {
                offset_delta,
                locals,
                stack,
            }
        }
    })
}

fn encode_frame(f: &StackMapFrame, out: &mut ByteWriter) -> Result<(), AttributeError> {
    match f {
        StackMapFrame::Same { frame_type: t } if *t <= 63 => out.write_u1(*t)?,
        StackMapFrame::SameLocalsOneStack {
            frame_type: t,
            stack,
        } if (64..=127).contains(t) => {
            out.write_u1(*t)?;
            encode_type(*stack, out)?
        }
        StackMapFrame::SameLocalsOneStackExtended {
            offset_delta,
            stack,
        } => {
            out.write_u1(247)?;
            out.write_u2(*offset_delta)?;
            encode_type(*stack, out)?
        }
        StackMapFrame::Chop {
            frame_type: t,
            offset_delta,
        } if (248..=250).contains(t) => {
            out.write_u1(*t)?;
            out.write_u2(*offset_delta)?
        }
        StackMapFrame::SameExtended { offset_delta } => {
            out.write_u1(251)?;
            out.write_u2(*offset_delta)?
        }
        StackMapFrame::Append {
            frame_type: t,
            offset_delta,
            locals,
        } if (252..=254).contains(t) && locals.len() == usize::from(*t - 251) => {
            out.write_u1(*t)?;
            out.write_u2(*offset_delta)?;
            for v in locals {
                encode_type(*v, out)?
            }
        }
        StackMapFrame::Full {
            offset_delta,
            locals,
            stack,
        } => {
            out.write_u1(255)?;
            out.write_u2(*offset_delta)?;
            out.write_u2(count(locals.len(), "full-frame locals")?)?;
            for v in locals {
                encode_type(*v, out)?
            }
            out.write_u2(count(stack.len(), "full-frame stack entries")?)?;
            for v in stack {
                encode_type(*v, out)?
            }
        }
        _ => {
            return Err(error(
                AttributeErrorKind::ReservedTag,
                0,
                "frame variant contains a tag or arity outside its static format",
            ));
        }
    }
    Ok(())
}