diff --git a/crates/java-class/src/java_class.rs b/crates/java-class/src/java_class.rs index 5b44236da0..0bd1853dd0 100644 --- a/crates/java-class/src/java_class.rs +++ b/crates/java-class/src/java_class.rs @@ -11,6 +11,7 @@ use crate::bytes::Reader; use crate::java_ops::{self, Insn}; use crate::{FormatError, descriptor as dex}; use serde::Serialize; +use std::collections::HashMap; /// Most constant-pool entries, fields, methods, attributes or interfaces we read from one file. const MAX_ITEMS: usize = 1 << 20; @@ -351,15 +352,17 @@ fn utf8_of(pool: &[Const], idx: u16) -> &str { } /// Copies constant-pool text into the parse result within `MAX_COPIED_TEXT` bytes in total. -struct Names<'p> { +pub(crate) struct Names<'p> { pool: &'p [Const], left: usize, exhausted: bool, + /// The length of each text `keep` has built, by kind and pool index. + built: HashMap<(&'static str, u16), usize>, } impl<'p> Names<'p> { - fn new(pool: &'p [Const]) -> Self { - Names { pool, left: MAX_COPIED_TEXT, exhausted: false } + pub(crate) fn new(pool: &'p [Const]) -> Self { + Names { pool, left: MAX_COPIED_TEXT, exhausted: false, built: HashMap::new() } } /// Charge `len` bytes; false once the budget cannot cover them. @@ -376,8 +379,15 @@ impl<'p> Names<'p> { } } - /// Keep `text` when the budget covers it, otherwise the placeholder ``. - fn keep(&mut self, text: String, kind: &str, idx: u16) -> String { + /// `render(pool, idx)` when the budget covers it, otherwise the placeholder ``. + /// The length of each rendered text is kept by kind and index, so a text over the budget is + /// built at most once however many entries name it. + pub(crate) fn keep(&mut self, kind: &'static str, idx: u16, render: fn(&[Const], u16) -> String) -> String { + if let Some(&len) = self.built.get(&(kind, idx)) { + return if self.admit(len) { render(self.pool, idx) } else { format!("<{kind}#{idx}>") }; + } + let text = render(self.pool, idx); + self.built.insert((kind, idx), text.len()); if self.admit(text.len()) { text } else { format!("<{kind}#{idx}>") } } @@ -590,7 +600,7 @@ fn parse_members(c: &mut Cursor<'_>, names: &mut Names<'_>, warnings: &mut Vec m.code = parse_code(a.info, names), "ConstantValue" => { if let Some(i) = Reader::new(a.info).u16_be(0) { - m.constant_value = Some(names.keep(const_display(pool, i), "const", i)); + m.constant_value = Some(names.keep("const", i, const_display)); } } "Exceptions" => m.exceptions = parse_exceptions_attr(a.info, names), @@ -672,7 +682,7 @@ fn parse_annotation_types(info: &[u8], names: &mut Names<'_>, visibility: &str) for _ in 0..n.min(MAX_ITEMS) { match read_annotation(&mut c, names.pool, 0) { Some(idx) => { - let t = names.keep(field_type(utf8_of(names.pool, idx)), "utf8", idx); + let t = names.keep("utf8", idx, |pool, idx| field_type(utf8_of(pool, idx))); out.push(format!("{t} ({visibility})")); } None => break, diff --git a/crates/java-class/src/regressions/java_copied_names.rs b/crates/java-class/src/regressions/java_copied_names.rs index 2fcee6499d..8ee60e0ccf 100644 --- a/crates/java-class/src/regressions/java_copied_names.rs +++ b/crates/java-class/src/regressions/java_copied_names.rs @@ -1,4 +1,5 @@ -use crate::java_class::{self, ClassFile, MAX_COPIED_TEXT}; +use crate::java_class::{self, ClassFile, Const, MAX_COPIED_TEXT, Names}; +use std::cell::Cell; const BIG: usize = u16::MAX as usize; // Each repeat names the 65,535-byte Utf8 at least once, so 1,100 repeats exceed the limit. @@ -12,6 +13,7 @@ enum Scope { Throws, Catches, InnerClasses, + Constants, } fn u16b(out: &mut Vec, n: u16) { @@ -31,19 +33,20 @@ fn attr(out: &mut Vec, name: u16, info: &[u8]) { } // Every repeated entry names pool entry 1 (a 65,535-byte Utf8) directly or through the -// `Class` at entry 2. The file stays well under 100 KiB. +// `Class` at entry 2 or the `String` at entry 11. The file stays well under 100 KiB. fn fixture(scope: Scope) -> Vec { let mut out = vec![0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 52]; - u16b(&mut out, 10); + u16b(&mut out, 12); utf8(&mut out, &"A".repeat(BIG)); // 1 out.extend_from_slice(&[7, 0, 1]); // 2 utf8(&mut out, "java/lang/Object"); // 3 out.extend_from_slice(&[7, 0, 3]); // 4 utf8(&mut out, "Sample"); // 5 out.extend_from_slice(&[7, 0, 5]); // 6 - for text in ["Exceptions", "InnerClasses", "Code"] { - utf8(&mut out, text); // 7..9 + for text in ["Exceptions", "InnerClasses", "Code", "ConstantValue"] { + utf8(&mut out, text); // 7..10 } + out.extend_from_slice(&[8, 0, 1]); // 11 for word in [0x0021, 6, 4] { u16b(&mut out, word); } @@ -65,6 +68,15 @@ fn fixture(scope: Scope) -> Vec { u16b(&mut out, word); } } + } else if matches!((scope, table), (Scope::Constants, Scope::Fields)) { + // Fields named `Sample` whose `ConstantValue` is the string at entry 11. + u16b(&mut out, REPEATS); + for _ in 0..REPEATS { + for word in [0x0018, 5, 5, 1] { + u16b(&mut out, word); + } + attr(&mut out, 10, &[0, 11]); + } } else if matches!((scope, table), (Scope::Throws | Scope::Catches, Scope::Methods)) { u16b(&mut out, 1); for word in [0x0009, 5, 5, 1] { @@ -131,6 +143,7 @@ fn names(parsed: &ClassFile, scope: Scope) -> Vec<&str> { out.extend(row.name.as_deref()); } } + Scope::Constants => out.extend(parsed.fields.iter().filter_map(|m| m.constant_value.as_deref())), } out } @@ -143,9 +156,11 @@ fn check(scope: Scope) { let per_entry = if matches!(scope, Scope::Fields | Scope::Methods | Scope::InnerClasses) { 2 } else { 1 }; // Every entry is still listed; the budget replaces names, not rows. assert_eq!(names.len(), usize::from(REPEATS) * per_entry, "{scope:?}"); - let full = names.iter().filter(|n| n.len() == BIG).count(); - assert!(full >= 1, "{scope:?}: the first names are copied in full"); - assert!(full * BIG <= MAX_COPIED_TEXT, "{scope:?}: {} bytes of names copied", full * BIG); + // A string constant is shown quoted, two bytes longer than its Utf8. + let full: Vec = names.iter().map(|n| n.len()).filter(|&n| n >= BIG).collect(); + assert!(!full.is_empty(), "{scope:?}: the first names are copied in full"); + let copied: usize = full.iter().sum(); + assert!(copied <= MAX_COPIED_TEXT, "{scope:?}: {copied} bytes of names copied"); assert!(names.iter().any(|n| n.starts_with('<') && n.ends_with('>')), "{scope:?}: names past the budget are placeholders"); assert!(parsed.warnings.iter().any(|w| w.contains("pool indices")), "{scope:?}: {:?}", parsed.warnings); assert_eq!(parsed.this_class, "Sample"); @@ -181,3 +196,33 @@ fn repeated_caught_type_names_stay_within_the_copy_budget() { fn repeated_inner_class_names_stay_within_the_copy_budget() { check(Scope::InnerClasses); } + +#[test] +fn repeated_constant_values_stay_within_the_copy_budget() { + check(Scope::Constants); +} + +thread_local! { + static RENDERS: Cell = const { Cell::new(0) }; +} + +fn counted_render(pool: &[Const], idx: u16) -> String { + RENDERS.with(|n| n.set(n.get() + 1)); + match pool.get(usize::from(idx)) { + Some(Const::Utf8(s)) => format!("{s:?}"), + _ => String::new(), + } +} + +#[test] +fn text_over_the_budget_is_rendered_once() { + let pool = vec![Const::Unusable, Const::Utf8("A".repeat(BIG))]; + let mut names = Names::new(&pool); + RENDERS.with(|n| n.set(0)); + let kept: Vec = (0..2 * MAX_COPIED_TEXT / BIG).map(|_| names.keep("const", 1, counted_render)).collect(); + let copied = kept.iter().filter(|t| t.len() == BIG + 2).count(); + assert!(copied >= 1 && copied < kept.len(), "{copied} of {} copied", kept.len()); + assert_eq!(kept.last().map(String::as_str), Some("")); + // Only the copied texts are rendered; the refusals reuse the length kept from the first one. + assert_eq!(RENDERS.with(Cell::get), copied); +}