Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Find class usage in multidimensional object arrays (R0.5) (#437)

Afloatffe15c916574
diff --git a/crates/formats/src/java_class.rs b/crates/formats/src/java_class.rs
index 969850d8..c946c3e5 100644
--- a/crates/formats/src/java_class.rs
+++ b/crates/formats/src/java_class.rs
@@ -790,7 +790,12 @@ pub fn references(cf: &ClassFile, owner: &str, method: Option<&str>, max: usize)
0xb2..=0xb5 if method.is_none() => cf.resolve_ref(cp).and_then(|r| (r.owner == owner).then(|| ("field", r.display()))),
0xbb | 0xbd | 0xc0 | 0xc1 | 0xc5 if method.is_none() => {
let name = class_name_at(&cf.pool, cp);
- (name.trim_start_matches('[') == owner).then_some(("type", internal_to_dotted(&name)))
+ let component = if name.starts_with('[') {
+ name.trim_start_matches('[').strip_prefix('L').and_then(|n| n.strip_suffix(';'))
+ } else {
+ Some(name.as_str())
+ };
+ (component == Some(owner)).then_some(("type", internal_to_dotted(&name)))
}
_ => None,
};
@@ -810,6 +815,84 @@ mod tests {
use super::*;
use craft_anything_testkit::java as fx;
+ /// Two valid static methods: a one-dimensional allocation and a two-dimensional one.
+ fn array_reference_class(component: &str, array: &str) -> ClassFile {
+ let utf8 = |s: &str| {
+ let mut bytes = vec![1];
+ bytes.extend_from_slice(&(s.len() as u16).to_be_bytes());
+ bytes.extend_from_slice(s.as_bytes());
+ bytes
+ };
+ let pool = [
+ utf8("sample/ArrayUses"),
+ vec![7, 0, 1],
+ utf8("java/lang/Object"),
+ vec![7, 0, 3],
+ utf8("Code"),
+ utf8("row"),
+ utf8(&format!("()[L{component};")),
+ utf8("grid"),
+ utf8(&format!("(){array}")),
+ utf8(component),
+ vec![7, 0, 10],
+ utf8(array),
+ vec![7, 0, 12],
+ ];
+ let mut bytes = vec![0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 52, 0, 14];
+ bytes.extend(pool.into_iter().flatten());
+ // public/super, this, super, no interfaces or fields, two methods.
+ bytes.extend_from_slice(&[0, 0x21, 0, 2, 0, 4, 0, 0, 0, 0, 0, 2]);
+ for (name, descriptor, code) in [(6u16, 7u16, vec![4, 0xbd, 0, 11, 0xb0]), (8, 9, vec![4, 4, 0xc5, 0, 13, 2, 0xb0])] {
+ for word in [9u16, name, descriptor, 1, 5] {
+ bytes.extend_from_slice(&word.to_be_bytes());
+ }
+ bytes.extend_from_slice(&(12u32 + code.len() as u32).to_be_bytes());
+ bytes.extend_from_slice(&[0, 2, 0, 0]); // max_stack, max_locals
+ bytes.extend_from_slice(&(code.len() as u32).to_be_bytes());
+ bytes.extend_from_slice(&code);
+ bytes.extend_from_slice(&[0, 0, 0, 0]); // exception table and nested attributes
+ }
+ bytes.extend_from_slice(&[0, 0]); // class attributes
+ let cf = parse(&bytes).unwrap();
+ assert!(cf.warnings.is_empty(), "{:?}", cf.warnings);
+ cf
+ }
+
+ #[test]
+ fn object_arrays_reference_the_component_class() {
+ let cf = array_reference_class("java/lang/String", "[[Ljava/lang/String;");
+ let (refs, more) = references(&cf, "java/lang/String", None, 10);
+ assert!(!more);
+ assert_eq!(refs.len(), 2, "{refs:?}");
+ assert_eq!((refs[0].instruction, refs[0].pc), ("anewarray", 1));
+ assert_eq!((refs[1].instruction, refs[1].pc), ("multianewarray", 2));
+ assert_eq!(refs[1].target, "java.lang.String[][]");
+ let (limited, more) = references(&cf, "java/lang/String", None, 1);
+ assert_eq!(limited, refs[..1]);
+ assert!(more);
+ assert_eq!(references(&cf, "java/lang/String", Some("grid"), 10), (Vec::new(), false));
+ assert_eq!(references(&cf, "java/lang/Integer", None, 10), (Vec::new(), false));
+ }
+
+ #[test]
+ fn array_components_keep_class_names_beginning_with_l() {
+ for component in ["Leaf", "LLeaf"] {
+ let cf = array_reference_class(component, &format!("[[L{component};"));
+ let (refs, more) = references(&cf, component, None, 10);
+ assert_eq!(refs.len(), 2, "{component}: {refs:?}");
+ assert!(!more);
+ }
+ }
+
+ #[test]
+ fn primitive_arrays_do_not_reference_a_class_named_after_the_descriptor() {
+ let cf = array_reference_class("I", "[[I");
+ let (refs, more) = references(&cf, "I", None, 10);
+ assert!(!more);
+ assert_eq!(refs.len(), 1, "{refs:?}");
+ assert_eq!(refs[0].instruction, "anewarray");
+ }
+
#[test]
fn parses_fixture() {
let data = fx::hello_class();
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index bdc93fa3..bf0288cc 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -167,6 +167,7 @@ Owner: open · Status: done (branch chore/r0.5-cleanups)
entry that starts past the end of the file is now skipped
- [x] `cargo xtask layers` also scans `apps/`
- [x] Engines in `KNOWN` without a `ToolPaths` override (tshark, objdump, rizin, java)
+- [x] Class-usage search includes the component class of multidimensional object arrays (#325).
- [x] Missing `database` icon for SQLite in the UI (`kind_icon`)
- [x] Deduplicate the five private `fn spec(` helpers in the tool modules
- [x] rustfmt drift on main (11 files)
diff --git a/docs/STATUS.md b/docs/STATUS.md
index a800eb3c..ef4abed0 100644
--- a/docs/STATUS.md
+++ b/docs/STATUS.md
@@ -12,6 +12,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi
| Apple app graphs | Selecting a macOS bundle recognises its root-relative `Contents/Info.plist` and retains its service/extension type; nested XPC bundles appear in `services` and as `xpc-service` graph nodes, including their executables when hosted in frameworks | Synthetic inventory tests, including parent-directory and flat bundle layouts, selected services/extensions, framework-hosted service executables, and bare-program controls. Ordinary selected directories with `Contents/Info.plist` need an Apple bundle suffix; archives retain layout-based recognition. Framework libraries and dylibs stay excluded from executables. Real app bundles were not checked for this correction |
| Chinese UI | Simplified (`zh-hans`) and Traditional (`zh-hant`) Chinese in Settings → General → Language; System chooses a supported OS language, otherwise English. Both catalogs cover UI controls and tool titles; translated titles are searchable. Raw tool data, AI text, logs, CLI/MCP and the separate browser viewer keep their original text. See [localisation](localization.md). | Catalog integrity and locale-resolution tests; `cargo xtask i18n-coverage`; isolated UI language-switch test. Platform visual checks are recorded separately. |
| Engine | 298 engine tools (314 with the AI and setup layers') across native, managed, Java, Android, Apple, JS/Electron, web, firmware, packets, databases, game archives and assets; see [tools.md](tools.md) and [tool-status.md](tool-status.md). A saved result can be narrowed without running again (`core_evidence_browse` keeps the rows matching given field values, cut to chosen fields) | A functional audit that ran every tool on real inputs on Windows, plus 600+ unit and integration tests |
+| Java class usage | `java_class_users_find` includes the component class of multidimensional object arrays; primitive-array descriptors do not match an unrelated class with the same descriptor letter | Synthetic class files parsed by the built-in reader: one- and two-dimensional allocations, class names beginning with `L`, primitive arrays, hit limits and method filters; formats tests. This correction was not checked with javac output or a JVM |
| Single-file apps and game files | `core_packed_files_list` and `core_packed_files_unpack` read Bun and Node.js single-file programs, .NET single-file apps, PyInstaller and Godot packs through one seam (`formats::packed`); `formats_bytecode_show` reads Python bytecode, `game_unity_code_names_show` Unity IL2CPP metadata, `formats_ai_weights_show` GGUF and safetensors headers. `app_folder_explain` says what an install folder is made with and what to run next | Unit tests with damaged files. Real files: a 279 MB Bun program, Node blobs, PyInstaller programs (Python 3.9, 3.14), .NET 8 single-file apps (macOS, Linux, Windows, compressed), Godot 3.6, 4.3 and 4.5 packs, 214 Python modules against CPython's own reader, a GGUF and a safetensors file, about 25 installed Mac apps. IL2CPP on synthetic files only |
| Program facts across builds | `native_library_calls_list` (delay-load too), `native_offered_symbols_list`, `native_publisher_show` (checks the file hash and the signature, not Windows trust), `app_builds_line_up` pairs functions across two builds and says how, `native_source_folder_write` writes pseudo-C to a folder and picks up where it stopped, `android_components_list` | Unit tests. Real PE, ELF and Mach-O files; two builds of a 276 MB program; a signed Microsoft DLL; Android components from one real APK, spot-checked |
| ARM32 value flow | A32 and Thumb calls define ARM scratch registers, and word results flow through `r0` into returns in `native_value_flow_show` | Synthetic instruction fixtures for leaf results and indirect calls in both modes, with AArch64 and x86-64 controls; disassembler tests. This correction was not checked on real ARM binaries or devices |