Files
craft-libs/crates/java-class/src/regressions/java_linkage_references.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

98 lines
3.3 KiB
Rust

use crate::java_class::{parse, references};
/// A method with a chosen load or call, plus a dynamic call site and a dynamic constant with distinct bootstrap signatures.
fn class(code: &[u8]) -> Vec<u8> {
fn utf(s: &str) -> Vec<u8> {
let mut b = vec![1];
b.extend((s.len() as u16).to_be_bytes());
b.extend(s.bytes());
b
}
let pool = [
utf("Uses"),
vec![7, 0, 1],
utf("java/lang/Object"),
vec![7, 0, 3],
utf("run"),
utf("()V"),
utf("Code"),
utf("BootstrapMethods"),
utf("Target"),
vec![7, 0, 9],
utf("apply"),
vec![12, 0, 11, 0, 6],
vec![10, 0, 10, 0, 12],
vec![15, 6, 0, 13],
utf("Factory"),
vec![7, 0, 15],
utf("boot"),
vec![12, 0, 17, 0, 28],
vec![10, 0, 16, 0, 18],
vec![15, 6, 0, 19],
utf("([LTarget;)V"),
vec![16, 0, 21],
vec![18, 0, 0, 0, 12],
utf("value"),
utf("I"),
vec![12, 0, 24, 0, 25],
vec![17, 0, 1, 0, 26],
utf(
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/CallSite;",
),
utf("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)I"),
vec![12, 0, 17, 0, 29],
vec![10, 0, 16, 0, 30],
vec![15, 6, 0, 31],
];
let mut bytes = vec![0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 55];
bytes.extend((pool.len() as u16 + 1).to_be_bytes());
for e in pool {
bytes.extend(e);
}
bytes.extend([0, 0x21, 0, 2, 0, 4, 0, 0, 0, 0, 0, 1, 0, 9, 0, 5, 0, 6, 0, 1, 0, 7]);
bytes.extend(((12 + code.len()) as u32).to_be_bytes());
bytes.extend([0, 3, 0, 0]);
bytes.extend((code.len() as u32).to_be_bytes());
bytes.extend(code);
bytes.extend([0, 0, 0, 0]);
let boots = [0, 2, 0, 20, 0, 1, 0, 14, 0, 32, 0, 0];
bytes.extend([0, 1, 0, 8]);
bytes.extend((boots.len() as u32).to_be_bytes());
bytes.extend(boots);
bytes
}
fn kinds(code: &[u8], owner: &str, method: Option<&str>) -> Vec<&'static str> {
let cf = parse(&class(code)).unwrap();
let (hits, partial) = references(&cf, owner, method, 10);
assert!(!partial);
hits.into_iter().map(|r| r.kind).collect()
}
#[test]
fn class_literal_has_a_use_site() {
assert_eq!(kinds(&[0x12, 10, 0x57, 0xb1], "Target", None), ["type"]);
}
#[test]
fn literal_method_handle_finds_its_member() {
assert_eq!(kinds(&[0x12, 14, 0x57, 0xb1], "Target", Some("apply")), ["method-handle"]);
}
#[test]
fn method_type_mentions_object_array_components() {
assert_eq!(kinds(&[0x12, 22, 0x57, 0xb1], "Target", None), ["method-type"]);
}
#[test]
fn dynamic_call_exposes_factory_dependency() {
assert_eq!(kinds(&[0xba, 0, 23, 0, 0, 0xb1], "Factory", Some("boot")), ["bootstrap-method"]);
}
#[test]
fn dynamic_call_exposes_static_method_handle_argument() {
assert_eq!(kinds(&[0xba, 0, 23, 0, 0, 0xb1], "Target", Some("apply")), ["method-handle"]);
}
#[test]
fn dynamic_constant_exposes_bootstrap_dependency() {
assert_eq!(kinds(&[0x12, 27, 0x57, 0xb1], "Factory", Some("boot")), ["bootstrap-method"]);
}
#[test]
fn unused_bootstrap_does_not_introduce_a_reference() {
assert!(kinds(&[0xb1], "Factory", None).is_empty());
}