Files
craft-libs/crates/java-class/src/regressions/java_constant_pool_slots.rs
T
d118983bf9 Extract Java class reader and JVM opcode decoder (#2048)
* Port Java class source: Craft Anything

Source: storytold/craft-anything@f64ae3bf3c

* Port Java class source: Find class usage in multidimensional object arrays (R0.5) (#437)

Source: storytold/craft-anything@ffe15c9165

* Port Java class source: More format parsers keep to their declared lengths and refuse impossible values (#722)

Source: storytold/craft-anything@b9ca099903

* Port Java class source: Address the parser review findings from #722 (#766)

Source: storytold/craft-anything@eab6a0f01b

* Port Java class source: Read JVM method parameters containing parentheses in class names (R3.12) (#1342)

Source: storytold/craft-anything@fb4c437fe6

* Port Java class source: Reject Java constants that exceed the constant-pool slots (R3.12) (#1373)

Source: storytold/craft-anything@1c4f233f08

* Port Java class source: Validate JVM switch tables before allocating decoded pairs (R3.12) (#1503)

Source: storytold/craft-anything@b40c56ef5c

* Port Java class source: Reuse constant-pool names while reading Java attributes (R3.12) (#1549)

Source: storytold/craft-anything@76fcdaadb7

* Port Java class source: Trace JVM bootstrap dependencies at their bytecode use sites (R3.12) (#1648)

Source: storytold/craft-anything@c5e524dd08

* Port Java class source: Reject impossible JVM array allocation operands (R3.12) (#1675)

Source: storytold/craft-anything@f04d7456e0

* Port Java class source: Report truncated Java class attribute tables (R3.12)

Source: storytold/craft-anything@420552ff74

* Expose neutral Java class API and retain checked reader and synthetic fixtures

* Retain Java regression: java_array_operands

Source: storytold/craft-anything@f04d7456e0

* Retain Java regression: java_attribute_names

Source: storytold/craft-anything@76fcdaadb7

* Retain Java regression: java_constant_pool_slots

Source: storytold/craft-anything@1c4f233f08

* Retain Java regression: java_invoke_operands

Source: storytold/craft-anything@eab6a0f01b

* Retain Java regression: java_linkage_references

Source: storytold/craft-anything@c5e524dd08

* Retain Java regression: java_truncated_attributes

Source: storytold/craft-anything@420552ff74

* Retain Java regression: java_wide_opcodes

Source: storytold/craft-anything@b9ca099903

* Register Java class reader and retain source regression coverage

---------

Co-authored-by: Brandon Thomas <echelon@gmail.com>
Co-authored-by: Afloat <132809511+Afloat16@users.noreply.github.com>
Co-authored-by: BFlat <bombayapirat@gmail.com>
Co-authored-by: Afloat <ShuffleMonkey@163.com>
Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com>
2026-11-17 16:13:18 -05:00

54 lines
2.1 KiB
Rust

use crate::{FormatError, java_class};
fn class_with_pool(count: u16, pool: &[u8]) -> Vec<u8> {
let mut bytes = vec![0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 52];
bytes.extend_from_slice(&count.to_be_bytes());
bytes.extend_from_slice(pool);
bytes.extend_from_slice(&[0; 14]);
bytes
}
#[test]
fn a_wide_constant_needs_its_reserved_slot() {
for tag in [5, 6] {
let mut pool = vec![tag];
pool.extend_from_slice(&[0; 8]);
let bytes = class_with_pool(2, &pool);
assert!(matches!(java_class::parse(&bytes), Err(FormatError::Malformed(message)) if message.contains("second slot")));
}
}
#[test]
fn the_largest_pool_cannot_overflow_on_a_trailing_wide_constant() {
for tag in [5, 6] {
let mut pool = [3, 0, 0, 0, 0].repeat(usize::from(u16::MAX) - 2);
pool.push(tag);
pool.extend_from_slice(&[0; 8]);
let bytes = class_with_pool(u16::MAX, &pool);
assert!(matches!(java_class::parse(&bytes), Err(FormatError::Malformed(message)) if message.contains("second slot")));
}
}
#[test]
fn a_wide_constant_with_two_remaining_slots_is_retained() {
for tag in [5, 6] {
for count in [3, u16::MAX] {
let mut pool = [3, 0, 0, 0, 0].repeat(usize::from(count) - 3);
pool.push(tag);
pool.extend_from_slice(&[0; 8]);
let parsed = java_class::parse(&class_with_pool(count, &pool)).unwrap();
assert_eq!(parsed.pool.len(), usize::from(count));
assert!(matches!(parsed.pool.last(), Some(java_class::Const::Unusable)));
assert!(matches!(parsed.pool[usize::from(count) - 2], java_class::Const::Long(_) | java_class::Const::Double(_)));
}
}
}
#[test]
fn ordinary_final_constants_and_existing_classes_still_parse() {
let parsed = java_class::parse(&class_with_pool(2, &[3, 0, 0, 0, 7])).unwrap();
assert_eq!(parsed.pool, vec![java_class::Const::Unusable, java_class::Const::Integer(7)]);
let parsed = java_class::parse(&crate::test_support::java::hello_class()).unwrap();
assert!(!parsed.methods.is_empty());
}