ristretto_classfile 0.31.0

A library for reading, writing and verifying Java classfiles.
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
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
use crate::FieldType;
use crate::JAVA_6;
use crate::attributes::Attribute;
use crate::attributes::ExceptionTableEntry;
use crate::attributes::Instruction;
use crate::class_file::ClassFile;
use crate::constant::Constant;
use crate::method::Method;
use crate::method_access_flags::MethodAccessFlags;
use crate::verifiers::error::Result;
use crate::verifiers::error::VerifyError::{
    InvalidInstructionOffset, InvalidStackFrameOffset, VerificationError,
};
use ahash::AHashSet;
use std::io::Cursor;

/// Verify the `Code` attribute.
///
/// Note: After parsing, both branch instruction targets and `StackMapTable` frame offsets
/// are stored as **instruction indices**. This function verifies them as such.
///
/// # Errors
/// Returns `VerificationError` if the code attribute is invalid.
#[expect(clippy::too_many_lines)]
pub(crate) fn verify(
    class_file: &ClassFile<'_>,
    method: &Method,
    _max_stack: u16,
    max_locals: u16,
    code: &[Instruction],
    exception_table: &[ExceptionTableEntry],
    attributes: &[Attribute],
) -> Result<u16> {
    // Verify max_locals
    let descriptor_index = method.descriptor_index;
    if let Some(Constant::Utf8(descriptor)) = class_file.constant_pool.get(descriptor_index)
        && let Ok((parameters, _)) = FieldType::parse_method_descriptor(descriptor)
    {
        let mut required_locals = 0;
        if !method.access_flags.contains(MethodAccessFlags::STATIC) {
            required_locals += 1;
        }
        for param in parameters {
            required_locals += match param {
                FieldType::Base(
                    crate::base_type::BaseType::Double | crate::base_type::BaseType::Long,
                ) => 2,
                _ => 1,
            };
        }
        if max_locals < required_locals {
            return Err(VerificationError {
                context: "Code Attribute".to_string(),
                message: format!(
                    "max_locals ({max_locals}) is less than required locals ({required_locals}) for method parameters"
                ),
            });
        }
    }

    // Calculate code byte length (needed for return value)
    // We need to serialize to compute the actual byte length
    let mut cursor = Cursor::new(Vec::new());
    for instruction in code {
        instruction.to_bytes(&mut cursor)?;
    }
    let code_length = u16::try_from(cursor.position())?;

    let num_instructions = code.len();
    let num_inst_u16 = u16::try_from(num_instructions)?;

    // 1. Identify jump targets (these are instruction indices after parsing)
    let mut jump_target_indices = AHashSet::default();
    for instruction in code {
        match instruction {
            Instruction::Ifeq(target)
            | Instruction::Ifne(target)
            | Instruction::Iflt(target)
            | Instruction::Ifge(target)
            | Instruction::Ifgt(target)
            | Instruction::Ifle(target)
            | Instruction::If_icmpeq(target)
            | Instruction::If_icmpne(target)
            | Instruction::If_icmplt(target)
            | Instruction::If_icmpge(target)
            | Instruction::If_icmpgt(target)
            | Instruction::If_icmple(target)
            | Instruction::If_acmpeq(target)
            | Instruction::If_acmpne(target)
            | Instruction::Goto(target)
            | Instruction::Jsr(target)
            | Instruction::Ifnull(target)
            | Instruction::Ifnonnull(target) => {
                // target is an instruction index
                let target_index = *target;
                if target_index >= num_inst_u16 {
                    return Err(InvalidInstructionOffset(u32::from(target_index)));
                }
                jump_target_indices.insert(target_index);
            }
            Instruction::Goto_w(target) | Instruction::Jsr_w(target) => {
                // target is an instruction index
                let target_index = u16::try_from(*target)?;
                if target_index >= num_inst_u16 {
                    return Err(InvalidInstructionOffset(u32::from(target_index)));
                }
                jump_target_indices.insert(target_index);
            }
            Instruction::Tableswitch(switch) => {
                // After parsing, switch offsets are relative instruction offsets from current instruction
                let current_index = code
                    .iter()
                    .position(|i| std::ptr::eq(i, instruction))
                    .unwrap_or(0);
                let current_index_i32 = i32::try_from(current_index)?;
                let num_instr_i32 = i32::try_from(num_instructions)?;

                // Validate default target
                let default_target = current_index_i32 + switch.default;
                if default_target < 0 || default_target >= num_instr_i32 {
                    return Err(InvalidInstructionOffset(u32::try_from(default_target)?));
                }

                jump_target_indices.insert(u16::try_from(default_target)?);

                // Validate all jump table offsets
                for offset in &switch.offsets {
                    let target = current_index_i32 + offset;
                    if target < 0 || target >= num_instr_i32 {
                        return Err(InvalidInstructionOffset(u32::try_from(target)?));
                    }
                    jump_target_indices.insert(u16::try_from(target)?);
                }
            }
            Instruction::Lookupswitch(switch) => {
                // After parsing, switch offsets are relative instruction offsets from current instruction
                let current_index = code
                    .iter()
                    .position(|i| std::ptr::eq(i, instruction))
                    .unwrap_or(0);
                let current_index_i32 = i32::try_from(current_index)?;
                let num_instr_i32 = i32::try_from(num_instructions)?;

                // Validate default target
                let default_target = current_index_i32 + switch.default;
                if default_target < 0 || default_target >= num_instr_i32 {
                    return Err(InvalidInstructionOffset(u32::try_from(default_target)?));
                }
                jump_target_indices.insert(u16::try_from(default_target)?);

                // Validate all match-offset pairs
                for (_, offset) in &switch.pairs {
                    let target = current_index_i32 + offset;
                    if target < 0 || target >= num_instr_i32 {
                        return Err(InvalidInstructionOffset(u32::try_from(target)?));
                    }
                    jump_target_indices.insert(u16::try_from(target)?);
                }
            }
            _ => {}
        }
    }

    // Exception table handlers; already instruction indices after parsing
    for entry in exception_table {
        if entry.handler_pc >= num_inst_u16 {
            return Err(InvalidInstructionOffset(u32::from(entry.handler_pc)));
        }
        jump_target_indices.insert(entry.handler_pc);
    }

    // 2. Verify StackMapTable (frame offsets are instruction indices after parsing)
    let mut frame_instruction_indices = AHashSet::default();
    let mut has_stack_map = false;
    for attribute in attributes {
        if let Attribute::StackMapTable { frames, .. } = attribute {
            has_stack_map = true;
            let mut current_frame_index = -1i32;
            for (i, frame) in frames.iter().enumerate() {
                // After parsing, offset_delta is in instruction indices, not byte offsets
                let offset_delta = i32::from(frame.offset_delta());
                let frame_index = if i == 0 {
                    offset_delta
                } else {
                    current_frame_index + offset_delta + 1
                };

                if frame_index < 0 || frame_index >= i32::from(num_inst_u16) {
                    return Err(InvalidStackFrameOffset(u16::try_from(frame_index)?));
                }

                let frame_index_u16 = u16::try_from(frame_index)?;
                frame_instruction_indices.insert(frame_index_u16);
                current_frame_index = frame_index;
            }
        }
    }

    // Verify that all jump targets have a corresponding StackFrame (if StackMapTable is present and version >= 50)
    if class_file.version >= JAVA_6 {
        if !jump_target_indices.is_empty() && !has_stack_map {
            return Err(VerificationError {
                context: "Code Attribute".to_string(),
                message: "StackMapTable missing for class version >= 50.0".to_string(),
            });
        }

        for target_index in &jump_target_indices {
            if !frame_instruction_indices.contains(target_index) {
                return Err(VerificationError {
                    context: "Code Attribute".to_string(),
                    message: format!(
                        "Jump target at instruction index {target_index} does not have a corresponding StackMapFrame"
                    ),
                });
            }
        }
    }

    Ok(code_length)
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::attributes::StackFrame;
    use crate::version::Version;

    fn get_dummy_method() -> Method {
        Method {
            access_flags: MethodAccessFlags::PUBLIC,
            name_index: 0,
            descriptor_index: 0,
            attributes: vec![],
        }
    }

    #[test]
    fn test_valid_code_with_stack_map() {
        let class_file = ClassFile {
            version: Version::Java7 { minor: 0 },
            ..Default::default()
        };
        let method = get_dummy_method();
        // After parsing, Goto targets AND StackMapTable frame offsets are instruction indices
        let code = vec![
            Instruction::Nop,     // Index 0
            Instruction::Goto(3), // Index 1, jumps to instruction 3
            Instruction::Return,  // Index 2
            Instruction::Return,  // Index 3; target
        ];

        // StackMapTable frame at instruction index 3
        let stack_map_table = Attribute::StackMapTable {
            name_index: 0,
            frames: vec![
                // After parsing, frame_type is instruction index delta, not byte offset
                StackFrame::SameFrame { frame_type: 3 }, // First frame: instruction index 3
            ],
        };
        let attributes = vec![stack_map_table];

        let result = verify(&class_file, &method, 0, 10, &code, &[], &attributes);
        assert!(result.is_ok());
    }

    #[test]
    fn test_invalid_jump_target() {
        let class_file = ClassFile::default();
        let method = get_dummy_method();
        let code = vec![
            Instruction::Goto(10), // Jump to instruction index 10 (invalid; only 1 instruction)
        ];

        assert_eq!(
            Err(InvalidInstructionOffset(10)),
            verify(&class_file, &method, 0, 10, &code, &[], &[])
        );
    }

    #[test]
    fn test_missing_stack_map_frame() {
        let class_file = ClassFile {
            version: Version::Java7 { minor: 0 },
            ..Default::default()
        };
        let method = get_dummy_method();
        let code = vec![
            Instruction::Nop,     // Index 0
            Instruction::Goto(3), // Index 1, jumps to instruction 3
            Instruction::Return,  // Index 2
            Instruction::Return,  // Index 3; target
        ];
        // Missing StackMapTable

        match verify(&class_file, &method, 0, 10, &code, &[], &[]) {
            Err(VerificationError { message, .. }) => {
                assert!(message.contains("StackMapTable missing"));
            }
            _ => panic!("Expected VerificationError"),
        }
    }

    #[test]
    fn test_missing_frame_for_target() {
        let class_file = ClassFile {
            version: Version::Java7 { minor: 0 },
            ..Default::default()
        };
        let method = get_dummy_method();
        // Jump to instruction index 3
        let code = vec![
            Instruction::Nop,     // Index 0
            Instruction::Goto(3), // Index 1, jumps to instruction 3
            Instruction::Return,  // Index 2
            Instruction::Return,  // Index 3; target
        ];

        // StackMapTable has frame at instruction index 2, but we jump to instruction 3
        let stack_map_table = Attribute::StackMapTable {
            name_index: 0,
            frames: vec![
                StackFrame::SameFrame { frame_type: 2 }, // Frame at instruction index 2
            ],
        };
        let attributes = vec![stack_map_table];

        match verify(&class_file, &method, 0, 10, &code, &[], &attributes) {
            Err(VerificationError { message, .. }) => {
                assert!(message.contains(
                    "Jump target at instruction index 3 does not have a corresponding StackMapFrame"
                ));
            }
            _ => panic!("Expected VerificationError"),
        }
    }

    #[test]
    fn test_max_locals_error() {
        let mut constant_pool = crate::ConstantPool::default();
        let descriptor_index = constant_pool.add_utf8("(II)V").unwrap();
        let class_file = ClassFile {
            version: Version::Java7 { minor: 0 },
            constant_pool,
            ..Default::default()
        };
        let method = Method {
            access_flags: MethodAccessFlags::PUBLIC,
            name_index: 0,
            descriptor_index,
            attributes: vec![],
        };
        let code = vec![Instruction::Return];
        // Requires 3 locals (this + int + int)
        // max_locals = 2 -> Error

        match verify(&class_file, &method, 0, 2, &code, &[], &[]) {
            Err(VerificationError { message, .. }) => {
                assert!(message.contains("max_locals (2) is less than required locals (3)"));
            }
            _ => panic!("Expected VerificationError"),
        }
    }

    #[test]
    fn test_tableswitch_valid() {
        use crate::attributes::TableSwitch;
        let class_file = ClassFile::default();
        let method = get_dummy_method();
        // Tableswitch at index 0, with targets as relative offsets
        // low=0, high=2 means 3 offsets in the table
        let code = vec![
            Instruction::Tableswitch(Box::new(TableSwitch {
                default: 4, // Relative: 0 + 4 = 4
                low: 0,
                high: 2,
                offsets: vec![1, 2, 3], // Relative: 0+1=1, 0+2=2, 0+3=3
            })), // Index 0
            Instruction::Nop,    // Index 1; target
            Instruction::Nop,    // Index 2; target
            Instruction::Nop,    // Index 3; target
            Instruction::Return, // Index 4; default target
        ];

        let result = verify(&class_file, &method, 0, 10, &code, &[], &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_tableswitch_invalid_default() {
        use crate::attributes::TableSwitch;
        let class_file = ClassFile::default();
        let method = get_dummy_method();
        let code = vec![
            Instruction::Tableswitch(Box::new(TableSwitch {
                default: 100, // Invalid: 0 + 100 = 100 (out of bounds)
                low: 0,
                high: 0,
                offsets: vec![1],
            })),
            Instruction::Return,
        ];

        let result = verify(&class_file, &method, 0, 10, &code, &[], &[]);
        assert!(matches!(result, Err(InvalidInstructionOffset(100))));
    }

    #[test]
    fn test_tableswitch_invalid_offset() {
        use crate::attributes::TableSwitch;
        let class_file = ClassFile::default();
        let method = get_dummy_method();
        let code = vec![
            Instruction::Tableswitch(Box::new(TableSwitch {
                default: 1,
                low: 0,
                high: 0,
                offsets: vec![50], // Invalid: 0 + 50 = 50 (out of bounds)
            })),
            Instruction::Return,
        ];

        let result = verify(&class_file, &method, 0, 10, &code, &[], &[]);
        assert!(matches!(result, Err(InvalidInstructionOffset(50))));
    }

    #[test]
    fn test_lookupswitch_valid() {
        use crate::attributes::LookupSwitch;
        use indexmap::IndexMap;
        let class_file = ClassFile::default();
        let method = get_dummy_method();

        let mut pairs = IndexMap::new();
        pairs.insert(1, 1); // match 1 -> offset 1 (relative from instruction 0)
        pairs.insert(2, 2); // match 2 -> offset 2

        let code = vec![
            Instruction::Lookupswitch(Box::new(LookupSwitch {
                default: 3, // Relative: 0 + 3 = 3
                pairs,
            })), // Index 0
            Instruction::Nop,    // Index 1; target for match 1
            Instruction::Nop,    // Index 2; target for match 2
            Instruction::Return, // Index 3; default target
        ];

        let result = verify(&class_file, &method, 0, 10, &code, &[], &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_lookupswitch_invalid_default() {
        use crate::attributes::LookupSwitch;
        use indexmap::IndexMap;
        let class_file = ClassFile::default();
        let method = get_dummy_method();

        let code = vec![
            Instruction::Lookupswitch(Box::new(LookupSwitch {
                default: 100, // Invalid: 0 + 100 = 100 (out of bounds)
                pairs: IndexMap::new(),
            })),
            Instruction::Return,
        ];

        let result = verify(&class_file, &method, 0, 10, &code, &[], &[]);
        assert!(matches!(result, Err(InvalidInstructionOffset(100))));
    }

    #[test]
    fn test_lookupswitch_invalid_pair_offset() {
        use crate::attributes::LookupSwitch;
        use indexmap::IndexMap;
        let class_file = ClassFile::default();
        let method = get_dummy_method();

        let mut pairs = IndexMap::new();
        pairs.insert(1, 50); // Invalid: 0 + 50 = 50 (out of bounds)

        let code = vec![
            Instruction::Lookupswitch(Box::new(LookupSwitch { default: 1, pairs })),
            Instruction::Return,
        ];

        let result = verify(&class_file, &method, 0, 10, &code, &[], &[]);
        assert!(matches!(result, Err(InvalidInstructionOffset(50))));
    }
}