diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 7ca726095..2b467131e 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -158,6 +158,10 @@ Sanitization, animation fitting and numeric output policy remain caller-owned. `crates/har` preserves Craft Anything `crates/formats/src/har.rs` from `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2` (MIT OR Apache-2.0). Brandon Thomas introduced the typed reader in `f64ae3bf3c8863a982af1304e06de20653803968`; BFlat added UTF-8 BOM compatibility in `cb0bb9d8fa4536ba44ef2c86f5ed759b879cc2d3`. Both source snapshots retain primary author attribution. Shared wiring, explicit byte admission and its tests are subsequent Dylan M. Taylor changes. Redaction and analysis remain app-owned. +## Dalvik DEX inspection + +`crates/dex` extracts Craft Anything `crates/formats/src/dex.rs` and `dex_ops.rs` at `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2` (MIT OR Apache-2.0). Five source-history ports preserve Brandon Thomas, BFlat and Afloat as primary authors. Five regression imports retain BFlat/Afloat authors and exact source revisions. The checked byte-reader/MUTF-8 subset and synthetic DEX fixture come from that app's `bytes.rs` and `testkit/src/android.rs`; deterministic noise/mutations come from its testkit. No application file I/O, APK logic or execution is imported. Shared neutral errors and workspace wiring are subsequent changes; existing per-file strings, member/handler and bytecode payload budgets are retained. + ## EVM bytecode inspection `crates/evm` extracts Craft Anything `crates/formats/src/evm.rs` at `a31e995858f4312e0f1d59a007112da191ef2d26` (MIT OR Apache-2.0). Eight separate source-history ports preserve Brandon Thomas, Afloat, RRFZ and NetterSilver as primary authors with immutable source references. Synthetic EVM fixtures and deterministic mutation helpers come from the same app source; regression files retain their source provenance. Neutral errors and workspace integration are subsequent changes. No code execution or transaction behavior is imported. diff --git a/Cargo.lock b/Cargo.lock index 104361feb..d6e7d3970 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1779,6 +1779,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-dex" +version = "0.1.0" +dependencies = [ + "craft-testkit", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-dialogs" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index c0b811a03..25342c02a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,7 @@ craft-aaf = { path = "crates/aaf" } craft-atomic = { path = "crates/atomic" } # The ArtCraft Launcher's app list for AI agents (storytold/craft-ai-integrations, crates/ai-agents). craft-ai-agents = { git = "https://github.com/storytold/craft-ai-integrations", rev = "35e0352666a117407d62230a6c6c2366f3e21a3b" } +craft-dex = { path = "crates/dex" } craft-xtask = { path = "crates/xtask" } craft-captions = { path = "crates/captions" } craft-cfb = { path = "crates/cfb" } diff --git a/crates/dex/Cargo.toml b/crates/dex/Cargo.toml new file mode 100644 index 000000000..fc7e35d1d --- /dev/null +++ b/crates/dex/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name="craft-dex" +description="Dalvik DEX reader and bytecode decoder with retained input bounds and partial-report semantics" +version.workspace=true +edition.workspace=true +license.workspace=true +rust-version.workspace=true +repository.workspace=true +[lints] +workspace=true +[dependencies] +serde={workspace=true} +thiserror={workspace=true} + +[dev-dependencies] +craft-testkit = { workspace = true } diff --git a/crates/dex/src/bytes.rs b/crates/dex/src/bytes.rs new file mode 100644 index 000000000..3e69ae4fd --- /dev/null +++ b/crates/dex/src/bytes.rs @@ -0,0 +1,85 @@ +//! Checked little/big-endian reads over untrusted bytes. Every read returns `None` when out of range. + +#[derive(Debug, Clone, Copy)] +pub struct Reader<'a> { + data: &'a [u8], +} + +impl<'a> Reader<'a> { + pub fn new(data: &'a [u8]) -> Self { + Reader { data } + } + + pub fn len(&self) -> usize { + self.data.len() + } + + pub fn is_empty(&self) -> bool { + self.data.is_empty() + } + + pub fn data(&self) -> &'a [u8] { + self.data + } + + pub fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> { + let end = off.checked_add(len)?; + self.data.get(off..end) + } + + pub fn u8(&self, off: usize) -> Option { + self.data.get(off).copied() + } + + fn arr(&self, off: usize) -> Option<[u8; N]> { + self.slice(off, N)?.try_into().ok() + } + + pub fn u16_le(&self, off: usize) -> Option { + self.arr(off).map(u16::from_le_bytes) + } + + pub fn u32_le(&self, off: usize) -> Option { + self.arr(off).map(u32::from_le_bytes) + } + + /// A NUL-terminated string starting at `off`, at most `max` bytes, lossily decoded. + pub fn cstr(&self, off: usize, max: usize) -> Option { + let rest = self.data.get(off..)?; + let rest = rest.get(..max.min(rest.len()))?; + let end = rest.iter().position(|&b| b == 0).unwrap_or(rest.len()); + Some(String::from_utf8_lossy(rest.get(..end)?).into_owned()) + } + + /// Unsigned LEB128 at `off`: (value, bytes consumed). At most 10 bytes. + pub fn uleb128(&self, off: usize) -> Option<(u64, usize)> { + let mut result = 0u64; + for i in 0..10usize { + let b = self.u8(off.checked_add(i)?)?; + let payload = u64::from(b & 0x7f); + let shift = 7 * i as u32; + if shift > 0 && payload.checked_shl(shift)? >> shift != payload { + return None; + } + result |= payload << shift; + if b & 0x80 == 0 { + return Some((result, i + 1)); + } + } + None + } +} + +/// Decode one modified UTF-8 code unit. Invalid bytes consume only themselves, +/// so a damaged sequence cannot hide valid text or borrow missing bytes. +pub(crate) fn mutf8_unit(bytes: &[u8]) -> (u16, usize) { + match bytes { + [a @ 0x00..=0x7f, ..] => (u16::from(*a), 1), + [0xc0, 0x80, ..] => (0, 2), + [a @ 0xc2..=0xdf, b @ 0x80..=0xbf, ..] => ((u16::from(*a & 0x1f) << 6) | u16::from(*b & 0x3f), 2), + [a @ 0xe0..=0xef, b @ 0x80..=0xbf, c @ 0x80..=0xbf, ..] if *a != 0xe0 || *b >= 0xa0 => { + ((u16::from(*a & 0x0f) << 12) | (u16::from(*b & 0x3f) << 6) | u16::from(*c & 0x3f), 3) + } + _ => (0xfffd, 1), + } +} diff --git a/crates/dex/src/dex.rs b/crates/dex/src/dex.rs new file mode 100644 index 000000000..8c0b25573 --- /dev/null +++ b/crates/dex/src/dex.rs @@ -0,0 +1,1154 @@ +//! Dalvik executables (DEX): header, string/type/proto/field/method ids, class definitions, +//! class data, code items with try/catch handlers, class annotations, a smali-like listing of +//! method bodies and a reference finder. Counts are checked against the file size, strings are +//! decoded from MUTF-8 under a total budget, and every read is bounds-checked. + +use crate::FormatError; +use crate::bytes::Reader; +use crate::dex_ops::{IndexKind, Insn, Payload}; +use serde::Serialize; + +pub const NO_INDEX: u32 = 0xffff_ffff; +/// Longest string decoded (UTF-16 units). +const MAX_STRING_UNITS: usize = 16 * 1024; +/// Total UTF-16 units decoded across all strings of one file. +const STRING_BUDGET: usize = 64 * 1024 * 1024; +/// Most members per class list. +const MAX_MEMBERS: usize = 65_536; +/// Most try blocks / handlers per method. +const MAX_TRIES: usize = 4096; +const MAX_HANDLERS: usize = 256; +/// Most handler descriptor bytes retained while reading one code item, however many try blocks +/// share a handler list. +const MAX_HANDLER_DESCRIPTOR_BYTES: usize = 1 << 20; + +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] +pub struct Header { + pub version: String, + pub checksum: String, + /// Adler-32 recomputed over the file. + pub checksum_ok: bool, + /// SHA-1 field as stored (not recomputed). + pub signature: String, + pub file_size: u32, + pub header_size: u32, + pub endian_tag: String, + pub string_ids: u32, + pub type_ids: u32, + pub proto_ids: u32, + pub field_ids: u32, + pub method_ids: u32, + pub class_defs: u32, + pub data_size: u32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct ProtoId { + shorty: u32, + ret: u32, + params_off: u32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct MemberId { + pub class_idx: u16, + /// Type (fields) or proto (methods). + pub type_or_proto: u16, + pub name_idx: u32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ClassDef { + pub class_idx: u32, + pub access: u32, + pub superclass_idx: u32, + pub interfaces_off: u32, + pub source_file_idx: u32, + pub annotations_off: u32, + pub class_data_off: u32, + pub static_values_off: u32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +pub struct EncodedField { + pub field_idx: u32, + pub access: u32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +pub struct EncodedMethod { + pub method_idx: u32, + pub access: u32, + pub code_off: u32, +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct ClassData { + pub static_fields: Vec, + pub instance_fields: Vec, + pub direct_methods: Vec, + pub virtual_methods: Vec, + pub complete: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Handler { + /// Exception type descriptor; `None` for catch-all. + pub exception: Option, + pub address: u32, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Try { + pub start: u32, + pub end: u32, + pub handlers: Vec, +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Code { + pub registers: u16, + pub ins: u16, + pub outs: u16, + pub insns: Vec, + pub tries: Vec, + pub debug_info_off: u32, +} + +/// A parsed DEX file. +#[derive(Debug, Clone)] +pub struct Dex<'a> { + data: &'a [u8], + pub header: Header, + pub strings: Vec, + types: Vec, + protos: Vec, + pub fields: Vec, + pub methods: Vec, + pub classes: Vec, + pub warnings: Vec, +} + +fn adler32(data: &[u8]) -> u32 { + let (mut a, mut b) = (1u32, 0u32); + for chunk in data.chunks(5552) { + for &x in chunk { + a += u32::from(x); + b += a; + } + a %= 65521; + b %= 65521; + } + (b << 16) | a +} + +/// Decode modified UTF-8 (as used by DEX and class files) to a String. +pub fn mutf8(r: &Reader<'_>, mut at: usize, max_units: usize) -> (String, usize) { + let mut units: Vec = Vec::new(); + let mut consumed = 0usize; + while units.len() < max_units { + let Some(b0) = r.u8(at) else { break }; + if b0 == 0 { + break; + } + let (u, n) = crate::bytes::mutf8_unit(r.data().get(at..).unwrap_or_default()); + units.push(u); + at = at.saturating_add(n); + consumed += n; + } + (String::from_utf16_lossy(&units), consumed) +} + +/// Signed 32-bit DEX LEB128 at `off`: (value, bytes), using at most five bytes. +pub fn sleb128(r: &Reader<'_>, off: usize) -> Option<(i64, usize)> { + let mut result = 0i64; + let mut shift = 0u32; + for i in 0..5usize { + let b = r.u8(off.checked_add(i)?)?; + // Four value bits remain in byte five; the other bits must extend the sign. + if i == 4 && !matches!(b, 0x00..=0x07 | 0x78..=0x7f) { + return None; + } + result |= i64::from(b & 0x7f).checked_shl(shift)?; + shift += 7; + if b & 0x80 == 0 { + if shift < 64 && b & 0x40 != 0 { + result |= -1i64 << shift; + } + return Some((result, i + 1)); + } + } + None +} + +/// `Lcom/x/Y;` → `com.x.Y`; `[I` → `int[]`. +pub fn java_name(desc: &str) -> String { + let dims = desc.chars().take_while(|&c| c == '[').count(); + let base = desc.get(dims..).unwrap_or(""); + let name = match base { + "V" => "void".to_string(), + "Z" => "boolean".to_string(), + "B" => "byte".to_string(), + "S" => "short".to_string(), + "C" => "char".to_string(), + "I" => "int".to_string(), + "J" => "long".to_string(), + "F" => "float".to_string(), + "D" => "double".to_string(), + b if b.starts_with('L') && b.ends_with(';') => b.get(1..b.len() - 1).unwrap_or("").replace('/', "."), + b => b.to_string(), + }; + format!("{name}{}", "[]".repeat(dims.min(255))) +} + +/// `com.x.Y` or `com/x/Y` or `Lcom/x/Y;` → `Lcom/x/Y;`. +pub fn to_descriptor(name: &str) -> String { + let n = name.trim(); + if n.starts_with('L') && n.ends_with(';') { n.to_string() } else { format!("L{};", n.replace('.', "/")) } +} + +/// Access flag names for a class, field or method. +pub fn access_flags(flags: u32, kind: &str) -> Vec<&'static str> { + let mut v = Vec::new(); + let table: &[(u32, &str)] = &[ + (0x1, "public"), + (0x2, "private"), + (0x4, "protected"), + (0x8, "static"), + (0x10, "final"), + (0x100, "native"), + (0x200, "interface"), + (0x400, "abstract"), + (0x800, "strictfp"), + (0x1000, "synthetic"), + (0x2000, "annotation"), + (0x4000, "enum"), + (0x10000, "constructor"), + (0x20000, "declared-synchronized"), + ]; + for (bit, name) in table { + if flags & bit != 0 { + v.push(*name); + } + } + match kind { + "method" => { + if flags & 0x20 != 0 { + v.push("synchronized"); + } + if flags & 0x40 != 0 { + v.push("bridge"); + } + if flags & 0x80 != 0 { + v.push("varargs"); + } + } + "field" => { + if flags & 0x40 != 0 { + v.push("volatile"); + } + if flags & 0x80 != 0 { + v.push("transient"); + } + } + _ => {} + } + v +} + +fn table(r: &Reader<'_>, count_off: usize, what: &str, entry: usize) -> Result<(usize, usize), FormatError> { + let n = r.u32_le(count_off).ok_or_else(|| FormatError::Truncated("header".into()))? as usize; + let off = r.u32_le(count_off + 4).ok_or_else(|| FormatError::Truncated("header".into()))? as usize; + if n == 0 { + return Ok((0, 0)); + } + let bytes = n.checked_mul(entry).ok_or_else(|| FormatError::Malformed(format!("{what} count overflows")))?; + if off.checked_add(bytes).is_none_or(|e| e > r.len()) { + return Err(FormatError::Malformed(format!("{n} {what} at offset {off} run past the end of the file"))); + } + Ok((n, off)) +} + +/// True when bytes start with a DEX magic. +pub fn is_dex(data: &[u8]) -> bool { + data.starts_with(b"dex\n") && data.len() >= 0x70 +} + +/// Parse a DEX file. +pub fn parse(data: &[u8]) -> Result, FormatError> { + let r = Reader::new(data); + if !data.starts_with(b"dex\n") { + return Err(FormatError::Malformed("not a DEX file (no dex magic)".into())); + } + if data.len() < 0x70 { + return Err(FormatError::Truncated("DEX header".into())); + } + let mut warnings = Vec::new(); + let version = r.cstr(4, 4).unwrap_or_default(); + let checksum = r.u32_le(8).unwrap_or(0); + let file_size = r.u32_le(32).unwrap_or(0); + let endian = r.u32_le(40).unwrap_or(0); + if endian != 0x1234_5678 { + return Err(FormatError::Unsupported(format!("byte-swapped DEX (endian tag {endian:#x})"))); + } + if file_size as usize != data.len() { + warnings.push(format!("header says {file_size} bytes, the file has {}", data.len())); + } + let sum_end = (file_size as usize).min(data.len()); + let checksum_ok = data.get(12..sum_end).is_some_and(|d| adler32(d) == checksum); + if !checksum_ok { + warnings.push("Adler-32 checksum does not match the content".into()); + } + let (ns, so) = table(&r, 56, "strings", 4)?; + let (nt, to) = table(&r, 64, "types", 4)?; + let (np, po) = table(&r, 72, "protos", 12)?; + let (nf, fo) = table(&r, 80, "fields", 8)?; + let (nm, mo) = table(&r, 88, "methods", 8)?; + let (nc, co) = table(&r, 96, "classes", 32)?; + let header = Header { + version, + checksum: format!("{checksum:08x}"), + checksum_ok, + signature: r.slice(12, 20).map(|b| b.iter().map(|x| format!("{x:02x}")).collect()).unwrap_or_default(), + file_size, + header_size: r.u32_le(36).unwrap_or(0), + endian_tag: format!("{endian:#x}"), + string_ids: ns as u32, + type_ids: nt as u32, + proto_ids: np as u32, + field_ids: nf as u32, + method_ids: nm as u32, + class_defs: nc as u32, + data_size: r.u32_le(104).unwrap_or(0), + }; + let mut strings = Vec::with_capacity(ns); + let mut budget = STRING_BUDGET; + let mut over_budget = false; + for i in 0..ns { + let off = r.u32_le(so + i * 4).unwrap_or(0) as usize; + let s = match r.uleb128(off) { + Some((len, n)) if budget > 0 => { + let want = (len as usize).min(MAX_STRING_UNITS).min(budget); + let (s, _) = mutf8(&r, off.saturating_add(n), want); + budget = budget.saturating_sub(want.max(1)); + s + } + Some(_) => { + over_budget = true; + format!("") + } + None => format!(""), + }; + strings.push(s); + } + if over_budget { + warnings.push("string decoding budget reached; later strings are shown as ".into()); + } + let types = (0..nt).map(|i| r.u32_le(to + i * 4).unwrap_or(NO_INDEX)).collect(); + let protos = (0..np) + .map(|i| { + let o = po + i * 12; + ProtoId { shorty: r.u32_le(o).unwrap_or(0), ret: r.u32_le(o + 4).unwrap_or(NO_INDEX), params_off: r.u32_le(o + 8).unwrap_or(0) } + }) + .collect(); + let member = |o: usize| MemberId { + class_idx: r.u16_le(o).unwrap_or(0), + type_or_proto: r.u16_le(o + 2).unwrap_or(0), + name_idx: r.u32_le(o + 4).unwrap_or(NO_INDEX), + }; + let fields = (0..nf).map(|i| member(fo + i * 8)).collect(); + let methods = (0..nm).map(|i| member(mo + i * 8)).collect(); + let classes = (0..nc) + .map(|i| { + let o = co + i * 32; + let g = |k: usize| r.u32_le(o + k * 4).unwrap_or(0); + ClassDef { + class_idx: g(0), + access: g(1), + superclass_idx: g(2), + interfaces_off: g(3), + source_file_idx: g(4), + annotations_off: g(5), + class_data_off: g(6), + static_values_off: g(7), + } + }) + .collect(); + Ok(Dex { data, header, strings, types, protos, fields, methods, classes, warnings }) +} + +impl<'a> Dex<'a> { + fn r(&self) -> Reader<'a> { + Reader::new(self.data) + } + + pub fn string(&self, i: u32) -> &str { + self.strings.get(i as usize).map(String::as_str).unwrap_or("") + } + + /// The descriptor of type `i` (`Lcom/x/Y;`). + pub fn type_desc(&self, i: u32) -> String { + match self.types.get(i as usize) { + Some(&s) => self.string(s).to_string(), + None => format!(""), + } + } + + fn type_list(&self, off: u32) -> Vec { + if off == 0 { + return Vec::new(); + } + let r = self.r(); + let n = r.u32_le(off as usize).unwrap_or(0) as usize; + (0..n.min(256)).filter_map(|i| r.u16_le(off as usize + 4 + i * 2).map(u32::from)).collect() + } + + /// `(Ljava/lang/String;I)V` + pub fn proto_desc(&self, i: u32) -> String { + match self.protos.get(i as usize) { + Some(p) => { + let params: String = self.type_list(p.params_off).into_iter().map(|t| self.type_desc(t)).collect(); + format!("({params}){}", self.type_desc(p.ret)) + } + None => format!(""), + } + } + + /// Parameter type descriptors and return type of proto `i`. + pub fn proto_parts(&self, i: u32) -> (Vec, String) { + match self.protos.get(i as usize) { + Some(p) => (self.type_list(p.params_off).into_iter().map(|t| self.type_desc(t)).collect(), self.type_desc(p.ret)), + None => (Vec::new(), "?".into()), + } + } + + pub fn shorty(&self, i: u32) -> &str { + self.protos.get(i as usize).map(|p| self.string(p.shorty)).unwrap_or("") + } + + /// `Lcls;->name:Ltype;` + pub fn field_ref(&self, i: u32) -> String { + match self.fields.get(i as usize) { + Some(f) => { + format!("{}->{}:{}", self.type_desc(u32::from(f.class_idx)), self.string(f.name_idx), self.type_desc(u32::from(f.type_or_proto))) + } + None => format!(""), + } + } + + /// `Lcls;->name(…)R` + pub fn method_ref(&self, i: u32) -> String { + match self.methods.get(i as usize) { + Some(m) => { + format!("{}->{}{}", self.type_desc(u32::from(m.class_idx)), self.string(m.name_idx), self.proto_desc(u32::from(m.type_or_proto))) + } + None => format!(""), + } + } + + pub fn class_name(&self, c: &ClassDef) -> String { + self.type_desc(c.class_idx) + } + + pub fn find_class(&self, name: &str) -> Option<&ClassDef> { + let want = to_descriptor(name); + self.classes.iter().find(|c| self.class_name(c) == want) + } + + pub fn interfaces(&self, c: &ClassDef) -> Vec { + self.type_list(c.interfaces_off).into_iter().map(|t| self.type_desc(t)).collect() + } + + /// Type descriptors of the class-level annotations. + pub fn annotations(&self, c: &ClassDef) -> Vec { + let r = self.r(); + let mut out = Vec::new(); + if c.annotations_off == 0 { + return out; + } + let Some(set_off) = r.u32_le(c.annotations_off as usize) else { return out }; + if set_off == 0 { + return out; + } + let n = r.u32_le(set_off as usize).unwrap_or(0) as usize; + for i in 0..n.min(256) { + let Some(item_at) = (set_off as usize).checked_add(4).and_then(|base| base.checked_add(i * 4)) else { break }; + let Some(item) = r.u32_le(item_at) else { break }; + // visibility u8, then encoded_annotation: uleb type_idx … + let (Some(vis), Some(annotation_at)) = (r.u8(item as usize), (item as usize).checked_add(1)) else { continue }; + if let Some((t, _)) = r.uleb128(annotation_at) { + let v = match vis { + 0 => "build", + 1 => "runtime", + 2 => "system", + _ => "?", + }; + out.push(format!("{} ({v})", self.type_desc(t as u32))); + } + } + out + } + + pub fn class_data(&self, c: &ClassDef) -> ClassData { + let mut cd = ClassData::default(); + if c.class_data_off == 0 { + cd.complete = true; + return cd; + } + let r = self.r(); + let mut pos = c.class_data_off as usize; + let next = |pos: &mut usize| -> Option { + let (v, n) = r.uleb128(*pos)?; + *pos += n; + u32::try_from(v).ok() + }; + let (Some(sf), Some(inf), Some(dm), Some(vm)) = (next(&mut pos), next(&mut pos), next(&mut pos), next(&mut pos)) else { return cd }; + let remaining = self.data.len().saturating_sub(pos); + let total: u64 = [sf, inf, dm, vm].iter().map(|&x| u64::from(x)).sum(); + if total > remaining as u64 || total > MAX_MEMBERS as u64 * 4 { + return cd; + } + for (count, is_method, slot) in [(sf, false, 0), (inf, false, 1), (dm, true, 2), (vm, true, 3)] { + let mut idx: u32 = 0; + for _ in 0..count.min(MAX_MEMBERS as u32) { + let Some(diff) = next(&mut pos) else { return cd }; + let Some(access) = next(&mut pos) else { return cd }; + idx = match idx.checked_add(diff) { + Some(v) => v, + None => return cd, + }; + if is_method { + let Some(code_off) = next(&mut pos) else { return cd }; + let m = EncodedMethod { method_idx: idx, access, code_off }; + if slot == 2 { + cd.direct_methods.push(m); + } else { + cd.virtual_methods.push(m); + } + } else { + let f = EncodedField { field_idx: idx, access }; + if slot == 0 { + cd.static_fields.push(f); + } else { + cd.instance_fields.push(f); + } + } + } + if count > MAX_MEMBERS as u32 { + return cd; + } + } + cd.complete = true; + cd + } + + /// The code item at `off`. + pub fn code(&self, off: u32) -> Option { + if off == 0 { + return None; + } + let r = self.r(); + let o = off as usize; + let registers = r.u16_le(o)?; + let ins = r.u16_le(o + 2)?; + let outs = r.u16_le(o + 4)?; + let tries_size = usize::from(r.u16_le(o + 6)?); + let debug_info_off = r.u32_le(o + 8)?; + let n = r.u32_le(o + 12)? as usize; + let bytes = r.slice(o + 16, n.checked_mul(2)?)?; + let insns: Vec = bytes.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect(); + let mut tries = Vec::new(); + let mut t_off = o + 16 + n * 2; + if tries_size > 0 && n % 2 == 1 { + t_off += 2; + } + let handlers_base = t_off + tries_size * 8; + let mut retained = 0usize; + 'tries: for i in 0..tries_size.min(MAX_TRIES) { + let at = t_off + i * 8; + let (Some(start), Some(count), Some(h_off)) = (r.u32_le(at), r.u16_le(at + 4), r.u16_le(at + 6)) else { break }; + let mut handlers = Vec::new(); + let mut p = handlers_base + usize::from(h_off); + if let Some((size, k)) = sleb128(&r, p) { + p += k; + for _ in 0..size.unsigned_abs().min(MAX_HANDLERS as u64) { + let Some((t, k1)) = r.uleb128(p) else { break }; + let Some((addr, k2)) = r.uleb128(p + k1) else { break }; + p += k1 + k2; + let desc = self.type_desc(t as u32); + let Some(next) = retained.checked_add(desc.len()) else { break 'tries }; + if next > MAX_HANDLER_DESCRIPTOR_BYTES { + break 'tries; + } + retained = next; + handlers.push(Handler { exception: Some(desc), address: addr as u32 }); + } + if size <= 0 + && handlers.len() as u64 == size.unsigned_abs() + && let Some((addr, _)) = r.uleb128(p) + { + handlers.push(Handler { exception: None, address: addr as u32 }); + } + } + tries.push(Try { start, end: start.saturating_add(u32::from(count)), handlers }); + } + Some(Code { registers, ins, outs, insns, tries, debug_info_off }) + } + + /// Name of method `i`. + pub fn method_name(&self, i: u32) -> &str { + self.methods.get(i as usize).map(|m| self.string(m.name_idx)).unwrap_or("") + } + + /// Java-like signature: `public static void log(java.lang.String)`. + pub fn method_signature(&self, m: &EncodedMethod) -> String { + let Some(id) = self.methods.get(m.method_idx as usize) else { return format!("", m.method_idx) }; + let (params, ret) = self.proto_parts(u32::from(id.type_or_proto)); + let flags = access_flags(m.access, "method").into_iter().filter(|f| *f != "constructor").collect::>().join(" "); + let params = params.iter().map(|p| java_name(p)).collect::>().join(", "); + format!("{}{}{} {}({params})", flags, if flags.is_empty() { "" } else { " " }, java_name(&ret), self.string(id.name_idx)).trim().to_string() + } + + /// Render one instruction smali-style. + pub fn format_insn(&self, i: &Insn) -> String { + let mut parts: Vec = Vec::new(); + if !i.regs.is_empty() { + let is_list = i.name.starts_with("invoke") || i.name.starts_with("filled-new-array"); + if i.range { + match (i.regs.first(), i.regs.last()) { + (Some(a), Some(b)) => parts.push(format!("{{v{a} .. v{b}}}")), + _ => parts.push("{}".into()), + } + } else if is_list { + parts.push(format!("{{{}}}", i.regs.iter().map(|r| format!("v{r}")).collect::>().join(", "))); + } else { + parts.extend(i.regs.iter().map(|r| format!("v{r}"))); + } + } else if i.range || i.name.starts_with("invoke") { + parts.push("{}".into()); + } + if let Some(l) = i.literal { + parts.push(if (-9..=9).contains(&l) { format!("{l}") } else { format!("{l:#x}") }); + } + if let Some(idx) = i.index { + parts.push(match i.index_kind { + IndexKind::String => format!("{:?}", self.string(idx)), + IndexKind::Type => self.type_desc(idx), + IndexKind::Field => self.field_ref(idx), + IndexKind::Method => self.method_ref(idx), + IndexKind::CallSite => format!("call_site@{idx}"), + IndexKind::MethodHandle => format!("method_handle@{idx}"), + IndexKind::Proto => self.proto_desc(idx), + IndexKind::None => format!("@{idx}"), + }); + } + if let Some(p) = i.index2 { + parts.push(self.proto_desc(p)); + } + if let Some(t) = i.target { + parts.push(format!(":{t:04x}")); + } + let mut s = if parts.is_empty() { i.name.to_string() } else { format!("{} {}", i.name, parts.join(", ")) }; + match (&i.payload, i.opcode) { + (Some(Payload::PackedSwitch { first_key, targets }), 0x2b) => { + let cases: Vec = targets + .iter() + .enumerate() + .take(32) + .map(|(k, t)| format!("{} -> :{:04x}", i64::from(*first_key) + k as i64, i64::from(i.offset) + i64::from(*t))) + .collect(); + s.push_str(&format!(" # {}", cases.join(", "))); + } + (Some(Payload::SparseSwitch { keys, targets }), 0x2c) => { + let cases: Vec = + keys.iter().zip(targets).take(32).map(|(k, t)| format!("{k} -> :{:04x}", i64::from(i.offset) + i64::from(*t))).collect(); + s.push_str(&format!(" # {}", cases.join(", "))); + } + (Some(Payload::FillArrayData { element_width, count, .. }), _) => s.push_str(&format!(" # {count} x {element_width}-byte elements")), + (Some(Payload::PackedSwitch { targets, .. }), 0) => s.push_str(&format!(" ({} cases)", targets.len())), + (Some(Payload::SparseSwitch { targets, .. }), 0) => s.push_str(&format!(" ({} cases)", targets.len())), + _ => {} + } + s + } +} + +/// One place code refers to a class, method or field. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Reference { + /// Caller method, `Lcls;->name(…)R`. + pub caller: String, + /// Code unit offset in the caller. + pub offset: u32, + pub instruction: &'static str, + /// method, field or type + pub kind: &'static str, + pub target: String, +} + +/// Maximum caller and target string bytes retained across a reference query. +/// A count limit alone cannot bound repeated, amplified method signatures. +const MAX_REFERENCE_BYTES: usize = 64 << 20; + +/// Every instruction that refers to class `desc` (optionally only its methods named `method`). +/// Reports truncation for incomplete class data, unreadable nonzero code offsets, incomplete +/// instruction decoding, or an exhausted count or byte budget. +pub fn references(dex: &Dex<'_>, desc: &str, method: Option<&str>, max: usize) -> (Vec, bool) { + references_with_byte_limit(dex, desc, method, max, MAX_REFERENCE_BYTES) +} + +fn references_with_byte_limit(dex: &Dex<'_>, desc: &str, method: Option<&str>, max: usize, max_bytes: usize) -> (Vec, bool) { + let mut bytes_left = max_bytes; + let mut out = Vec::new(); + let mut complete = true; + for c in &dex.classes { + let cd = dex.class_data(c); + complete &= cd.complete; + for m in cd.direct_methods.iter().chain(&cd.virtual_methods) { + let Some(code) = dex.code(m.code_off) else { + // Zero means no implementation (native/abstract), not damaged code. + // A nonzero unreadable body may hide references from this query. + complete &= m.code_off == 0; + continue; + }; + let (insns, decoded) = crate::dex_ops::decode(&code.insns, 200_000); + complete &= decoded; + for i in &insns { + let Some(idx) = i.index else { continue }; + let hit = match i.index_kind { + IndexKind::Method => dex.methods.get(idx as usize).and_then(|id| { + let cls = dex.type_desc(u32::from(id.class_idx)); + (cls == desc && method.is_none_or(|n| dex.string(id.name_idx) == n)).then(|| ("method", dex.method_ref(idx))) + }), + IndexKind::Field if method.is_none() => dex + .fields + .get(idx as usize) + .and_then(|id| (dex.type_desc(u32::from(id.class_idx)) == desc).then(|| ("field", dex.field_ref(idx)))), + IndexKind::Type if method.is_none() => { + let t = dex.type_desc(idx); + (t.trim_start_matches('[') == desc).then_some(("type", t)) + } + _ => None, + }; + if let Some((kind, target)) = hit { + if out.len() >= max { + return (out, true); + } + let caller = dex.method_ref(m.method_idx); + let Some(left) = bytes_left.checked_sub(caller.len()).and_then(|left| left.checked_sub(target.len())) else { + return (out, true); + }; + bytes_left = left; + out.push(Reference { caller, offset: i.offset, instruction: i.name, kind, target }); + } + } + } + } + (out, !complete) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_support::android as fx; + + #[test] + fn reference_queries_preserve_incomplete_decode_across_methods() { + for incomplete in [false, true] { + let mut builder = fx::DexBuilder::default(); + let class = builder.ty("LTest;"); + let first = builder.method("LTest;", "first", "V", &[]); + let second = builder.method("LTest;", "second", "V", &[]); + let mut first_code = vec![0x0071, second as u16, 0]; + if incomplete { + first_code.push(0x0071); // truncated invoke-static + } + let code = |insns| Some(fx::CodeSpec { insns, ..Default::default() }); + builder.classes.push(fx::ClassSpec { + class, + direct: vec![ + fx::MethodSpec { method: first, access: 0, code: code(first_code) }, + fx::MethodSpec { method: second, access: 0, code: code(vec![0x000e]) }, + ], + ..Default::default() + }); + let data = builder.build(); + let dex = parse(&data).unwrap(); + let (found, truncated) = references(&dex, "LTest;", Some("second"), 10); + assert_eq!(found.len(), 1); + assert_eq!(found[0].caller, "LTest;->first()V"); + assert_eq!(found[0].target, "LTest;->second()V"); + assert_eq!(truncated, incomplete); + let (absent, truncated) = references(&dex, "LAbsent;", None, 10); + assert!(absent.is_empty()); + assert_eq!(truncated, incomplete); + } + } + + #[test] + fn reference_queries_keep_class_data_and_code_parse_failures() { + let mut builder = fx::DexBuilder::default(); + let class = builder.ty("LTest;"); + let target = builder.method("LTest;", "target", "V", &[]); + let caller = builder.method("LTest;", "caller", "V", &[]); + builder.classes.push(fx::ClassSpec { + class, + direct: vec![ + fx::MethodSpec { method: target, access: 0, code: Some(fx::CodeSpec { insns: vec![0x000e], ..Default::default() }) }, + fx::MethodSpec { + method: caller, + access: 0, + code: Some(fx::CodeSpec { insns: vec![0x0071, target as u16, 0], ..Default::default() }), + }, + ], + ..Default::default() + }); + let bytes = builder.build(); + let mut dex = parse(&bytes).unwrap(); + let expected = references(&dex, "LTest;", Some("target"), 10); + assert_eq!(expected.0.len(), 1); + assert!(!expected.1); + + // A damaged class before a valid one must not hide the valid result or reset truncation. + let mut bad = dex.classes[0]; + bad.class_data_off = bytes.len() as u32; + dex.classes.insert(0, bad); + assert!(!dex.class_data(&dex.classes[0]).complete); + assert_eq!(references(&dex, "LTest;", Some("target"), 10), (expected.0.clone(), true)); + assert_eq!(references(&dex, "LAbsent;", None, 10), (Vec::new(), true)); + + // Locate the first method's code offset, after four counts and its index/access. + let original = parse(&bytes).unwrap(); + let reader = Reader::new(&bytes); + let mut at = original.classes[0].class_data_off as usize; + for _ in 0..6 { + at += reader.uleb128(at).unwrap().1; + } + let width = reader.uleb128(at).unwrap().1; + for missing in [false, true] { + let mut changed = bytes.clone(); + // Preserve the encoded width so the later valid method stays aligned. + changed[at..at + width].fill(if missing { 0xff } else { 0x80 }); + changed[at + width - 1] = if missing { 0x7f } else { 0 }; + let dex = parse(&changed).unwrap(); + let methods = dex.class_data(&dex.classes[0]); + assert!(methods.complete); + let offset = methods.direct_methods[0].code_off; + if missing { + assert!(offset as usize >= changed.len()); + } else { + assert_eq!(offset, 0); + } + assert!(dex.code(offset).is_none()); + assert_eq!(references(&dex, "LTest;", Some("target"), 10), (expected.0.clone(), missing)); + assert_eq!(references(&dex, "LAbsent;", None, 10), (Vec::new(), missing)); + } + } + + #[test] + fn fixture_strings_round_trip_utf16_surrogate_pairs() { + let mut builder = fx::DexBuilder::default(); + let text = "A\0é😀"; + let index = builder.string(text); + let data = builder.build(); + let dex = parse(&data).unwrap(); + assert_eq!(dex.string(index), text); + let offset = Reader::new(&data).u32_le(0x70).unwrap() as usize; + assert_eq!(&data[offset..offset + 13], &[5, 0x41, 0xc0, 0x80, 0xc3, 0xa9, 0xed, 0xa0, 0xbd, 0xed, 0xb8, 0x80, 0]); + } + + #[test] + fn parses_fixture() { + let data = fx::notes_dex(); + let d = parse(&data).unwrap(); + assert!(d.warnings.is_empty(), "{:?}", d.warnings); + assert!(d.header.checksum_ok); + assert_eq!(d.header.version, "035"); + assert_eq!(d.classes.len(), 2); + let main = d.find_class("com.example.notes.MainActivity").unwrap(); + assert_eq!(d.type_desc(main.superclass_idx), "Landroid/app/Activity;"); + assert_eq!(d.interfaces(main), vec!["Ljava/lang/Runnable;"]); + assert_eq!(d.string(main.source_file_idx), "MainActivity.java"); + assert_eq!(d.annotations(main), vec!["Ljava/lang/Deprecated; (runtime)"]); + let cd = d.class_data(main); + assert!(cd.complete); + assert_eq!(cd.static_fields.len(), 1); + assert_eq!(cd.instance_fields.len(), 1); + assert_eq!(cd.direct_methods.len(), 1); + assert_eq!(cd.virtual_methods.len(), 3); + let on_create = cd.virtual_methods.iter().find(|m| d.method_name(m.method_idx) == "onCreate").unwrap(); + assert_eq!(d.method_signature(on_create), "public void onCreate(android.os.Bundle)"); + let code = d.code(on_create.code_off).unwrap(); + let (ins, complete) = crate::dex_ops::decode(&code.insns, 1000); + assert!(complete); + let text: Vec = ins.iter().map(|i| d.format_insn(i)).collect(); + assert_eq!(text[0], "const-string v0, \"Hello, Craft!\""); + assert_eq!(text[1], "invoke-static {v0}, Lcom/example/notes/Util;->log(Ljava/lang/String;)V"); + let pick = cd.virtual_methods.iter().find(|m| d.method_name(m.method_idx) == "pick").unwrap(); + let pc = d.code(pick.code_off).unwrap(); + assert_eq!(pc.tries.len(), 1); + assert_eq!(pc.tries[0].handlers[0].exception.as_deref(), Some("Ljava/lang/RuntimeException;")); + assert_eq!(pc.tries[0].handlers[1].exception, None); + let (pins, _) = crate::dex_ops::decode(&pc.insns, 1000); + let sw = pins.iter().find(|i| i.name == "packed-switch").unwrap(); + assert!(d.format_insn(sw).contains("# 1 -> :"), "{}", d.format_insn(sw)); + let (refs, more) = references(&d, "Lcom/example/notes/Util;", Some("log"), 100); + assert!(!more); + assert_eq!(refs.len(), 2, "{refs:?}"); + assert_eq!(refs[0].caller, "Lcom/example/notes/MainActivity;->onCreate(Landroid/os/Bundle;)V"); + let (all, _) = references(&d, "Ljava/lang/System;", None, 100); + assert_eq!(all[0].kind, "field"); + } + + #[test] + fn helpers() { + assert_eq!(java_name("[[Ljava/lang/String;"), "java.lang.String[][]"); + assert_eq!(java_name("J"), "long"); + assert_eq!(to_descriptor("a.b.C"), "La/b/C;"); + assert_eq!(to_descriptor("La/b/C;"), "La/b/C;"); + assert_eq!(access_flags(0x10009, "method"), vec!["public", "static", "constructor"]); + let r = Reader::new(&[0x7f]); + assert_eq!(sleb128(&r, 0), Some((-1, 1))); + let s = [0xc0, 0x80, 0xe2, 0x82, 0xac, 0x41, 0]; + assert_eq!(mutf8(&Reader::new(&s), 0, 100).0, "\0\u{20ac}A"); + } + + #[test] + fn hostile() { + let data = fx::notes_dex(); + let walk = |d: &Dex<'_>| { + for c in &d.classes { + let cd = d.class_data(c); + let _ = d.annotations(c); + let _ = d.interfaces(c); + for m in cd.direct_methods.iter().chain(&cd.virtual_methods) { + let _ = d.method_signature(m); + if let Some(code) = d.code(m.code_off) { + let (ins, _) = crate::dex_ops::decode(&code.insns, 10_000); + for i in &ins { + let _ = d.format_insn(i); + } + } + } + } + let _ = references(d, "Lcom/example/notes/Util;", None, 100); + }; + for m in crate::test_support::mutations(&data) { + if let Ok(d) = parse(&m) { + walk(&d); + } + } + for len in 0..data.len() { + if let Ok(d) = parse(&data[..len]) { + walk(&d); + } + } + for seed in 0..64 { + let mut n = crate::test_support::noise(seed, 2048); + n[..8].copy_from_slice(b"dex\n035\0"); + n[40..44].copy_from_slice(&0x1234_5678u32.to_le_bytes()); + if let Ok(d) = parse(&n) { + walk(&d); + } + } + } + + fn bare_dex<'a>(data: &'a [u8], strings: Vec, types: Vec) -> Dex<'a> { + Dex { + data, + header: Header { + version: "035".into(), + checksum: String::new(), + checksum_ok: false, + signature: String::new(), + file_size: data.len() as u32, + header_size: 0x70, + endian_tag: String::new(), + string_ids: 0, + type_ids: 0, + proto_ids: 0, + field_ids: 0, + method_ids: 0, + class_defs: 0, + data_size: 0, + }, + strings, + types, + protos: Vec::new(), + fields: Vec::new(), + methods: Vec::new(), + classes: Vec::new(), + warnings: Vec::new(), + } + } + + #[test] + fn repeated_handler_lists_stop_at_the_descriptor_budget() { + let desc = format!("L{};", "a".repeat(16 * 1024)); + let mut data = vec![0u8; 4]; + data.extend_from_slice(&0u16.to_le_bytes()); + data.extend_from_slice(&0u16.to_le_bytes()); + data.extend_from_slice(&0u16.to_le_bytes()); + data.extend_from_slice(&(MAX_TRIES as u16).to_le_bytes()); + data.extend_from_slice(&0u32.to_le_bytes()); + data.extend_from_slice(&0u32.to_le_bytes()); + for _ in 0..MAX_TRIES { + data.extend_from_slice(&0u32.to_le_bytes()); + data.extend_from_slice(&1u16.to_le_bytes()); + data.extend_from_slice(&0u16.to_le_bytes()); + } + data.push(0x01); // sleb128: one typed handler + data.push(0x00); // type index 0 + data.push(0x00); // handler address + let d = bare_dex(&data, vec![desc.clone()], vec![0]); + let code = d.code(4).expect("code item"); + assert!(!code.tries.is_empty()); + assert!(code.tries.len() < MAX_TRIES, "read {} tries", code.tries.len()); + assert_eq!(code.tries[0].handlers[0].exception.as_deref(), Some(desc.as_str())); + let retained: usize = code.tries.iter().flat_map(|t| &t.handlers).filter_map(|h| h.exception.as_ref()).map(String::len).sum(); + assert!(retained <= MAX_HANDLER_DESCRIPTOR_BYTES, "{retained}"); + } + + #[test] + fn annotations_reject_an_unrepresentable_item_offset() { + let mut data = vec![0u8; 4]; + data.extend_from_slice(&8u32.to_le_bytes()); + data.extend_from_slice(&1u32.to_le_bytes()); + data.extend_from_slice(&u32::MAX.to_le_bytes()); + let d = bare_dex(&data, Vec::new(), Vec::new()); + let c = ClassDef { + class_idx: 0, + access: 0, + superclass_idx: 0, + interfaces_off: 0, + source_file_idx: 0, + annotations_off: 4, + class_data_off: 0, + static_values_off: 0, + }; + assert!(d.annotations(&c).is_empty()); + } + + #[test] + fn class_data_counts_do_not_overflow() { + let mut data = vec![0u8; 4]; + for mut x in [u32::MAX, 1, 0, 0] { + loop { + let b = (x & 0x7f) as u8; + x >>= 7; + if x == 0 { + data.push(b); + break; + } + data.push(b | 0x80); + } + } + let d = bare_dex(&data, Vec::new(), Vec::new()); + let c = ClassDef { + class_idx: 0, + access: 0, + superclass_idx: 0, + interfaces_off: 0, + source_file_idx: 0, + annotations_off: 0, + class_data_off: 4, + static_values_off: 0, + }; + assert!(!d.class_data(&c).complete); + } + + #[test] + fn references_bound_repeated_long_signatures_before_retaining_results() { + let mut data = vec![0u8; 578]; + data[4..8].copy_from_slice(&256u32.to_le_bytes()); + // 256 parameter entries all refer to the same long descriptor. + // One direct method at offset 520, with code at 544 (ULEB128 A0 04). + data[520..528].copy_from_slice(&[0, 0, 1, 0, 0, 0, 0xa0, 4]); + data[556..560].copy_from_slice(&9u32.to_le_bytes()); + for offset in [560, 566, 572] { + data[offset..offset + 2].copy_from_slice(&0x0071u16.to_le_bytes()); + } + let desc = format!("L{};", "x".repeat(256)); + let mut dex = bare_dex(&data, vec![desc.clone(), "run".into()], vec![0]); + dex.protos.push(ProtoId { shorty: 0, ret: 0, params_off: 4 }); + dex.methods.push(MemberId { class_idx: 0, type_or_proto: 0, name_idx: 1 }); + dex.classes.push(ClassDef { + class_idx: 0, + access: 0, + superclass_idx: NO_INDEX, + interfaces_off: 0, + source_file_idx: NO_INDEX, + annotations_off: 0, + class_data_off: 520, + static_values_off: 0, + }); + let signature = dex.method_ref(0); + assert!(signature.len() > 256 * 256); + let pair_bytes = signature.len() * 2; + let (all, truncated) = references_with_byte_limit(&dex, &desc, Some("run"), 100, pair_bytes * 3); + assert!(!truncated); + assert_eq!(all.len(), 3); + assert!(all.iter().all(|r| r.caller == signature && r.target == signature)); + let (bounded, truncated) = references_with_byte_limit(&dex, &desc, Some("run"), 100, pair_bytes * 2); + assert!(truncated); + assert_eq!(bounded, all[..2]); + assert_eq!(bounded.iter().map(|r| r.caller.len() + r.target.len()).sum::(), pair_bytes * 2); + let (below_one, truncated) = references_with_byte_limit(&dex, &desc, Some("run"), 100, pair_bytes - 1); + assert!(truncated); + assert!(below_one.is_empty()); + let (count_limited, truncated) = references_with_byte_limit(&dex, &desc, Some("run"), 1, usize::MAX); + assert!(truncated); + assert_eq!(count_limited, all[..1]); + let (zero, truncated) = references_with_byte_limit(&dex, &desc, Some("run"), 0, usize::MAX); + assert!(truncated); + assert!(zero.is_empty()); + assert_eq!(references(&dex, &desc, Some("run"), 100), (all, false)); + } + #[test] + fn references_report_incomplete_instruction_decoding() { + for (insns, incomplete) in [(vec![0x000e], false), (vec![0x0071], true), (vec![0; 200_001], true)] { + let mut b = fx::DexBuilder::default(); + let class = b.ty("Ltest;"); + let method = b.method("Ltest;", "run", "V", &[]); + b.classes.push(fx::ClassSpec { + class, + direct: vec![fx::MethodSpec { method, access: 0, code: Some(fx::CodeSpec { insns, ..Default::default() }) }], + ..Default::default() + }); + let bytes = b.build(); + let dex = parse(&bytes).unwrap(); + let (hits, truncated) = references(&dex, "Lmissing;", None, 100); + assert!(hits.is_empty()); + assert_eq!(truncated, incomplete); + } + } + + #[test] + fn references_keep_incompleteness_across_later_complete_methods() { + let mut b = fx::DexBuilder::default(); + let class = b.ty("Ltest;"); + let broken = b.method("Ltest;", "broken", "V", &[]); + let complete = b.method("Ltest;", "complete", "V", &[]); + b.classes.push(fx::ClassSpec { + class, + direct: vec![ + fx::MethodSpec { method: broken, access: 0, code: Some(fx::CodeSpec { insns: vec![0x0071], ..Default::default() }) }, + fx::MethodSpec { method: complete, access: 0, code: Some(fx::CodeSpec { insns: vec![0x000e], ..Default::default() }) }, + ], + ..Default::default() + }); + let bytes = b.build(); + let dex = parse(&bytes).unwrap(); + assert_eq!(references(&dex, "Lmissing;", None, 100), (Vec::new(), true)); + } + + #[test] + fn fixture_strings_round_trip_supplementary_and_null_characters() { + let mut b = fx::DexBuilder::default(); + let text = "a\0é😀z"; + let id = b.string(text); + let bytes = b.build(); + let dex = parse(&bytes).unwrap(); + assert_eq!(dex.string(id), text); + } +} diff --git a/crates/dex/src/dex_ops.rs b/crates/dex/src/dex_ops.rs new file mode 100644 index 000000000..3c27d4c5c --- /dev/null +++ b/crates/dex/src/dex_ops.rs @@ -0,0 +1,696 @@ +//! Dalvik bytecode: the opcode table (all 256 slots and three payload pseudo-instructions) and a +//! decoder for every instruction format. Pure decoding; index operands are resolved by the caller. + +use serde::Serialize; + +/// What an index operand refers to. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum IndexKind { + None, + String, + Type, + Field, + Method, + CallSite, + MethodHandle, + Proto, +} + +/// Instruction formats from the Dalvik specification (`10x`, `35c`, …). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +pub enum Fmt { + F10x, + F12x, + F11n, + F11x, + F10t, + F20t, + F22x, + F21t, + F21s, + F21h, + F21c, + F23x, + F22b, + F22t, + F22s, + F22c, + F30t, + F32x, + F31i, + F31t, + F31c, + F35c, + F3rc, + F45cc, + F4rcc, + F51l, +} + +impl Fmt { + /// Length in 16-bit code units. + pub fn units(self) -> usize { + use Fmt::*; + match self { + F10x | F12x | F11n | F11x | F10t => 1, + F20t | F22x | F21t | F21s | F21h | F21c | F23x | F22b | F22t | F22s | F22c => 2, + F30t | F32x | F31i | F31t | F31c | F35c | F3rc => 3, + F45cc | F4rcc => 4, + F51l => 5, + } + } +} + +/// One opcode slot. +#[derive(Debug, Clone, Copy)] +pub struct Op { + pub name: &'static str, + pub fmt: Fmt, + pub index: IndexKind, +} + +const fn op(name: &'static str, fmt: Fmt, index: IndexKind) -> Op { + Op { name, fmt, index } +} + +const UNUSED: Op = op("unused", Fmt::F10x, IndexKind::None); + +const UNOPS: [&str; 21] = [ + "neg-int", + "not-int", + "neg-long", + "not-long", + "neg-float", + "neg-double", + "int-to-long", + "int-to-float", + "int-to-double", + "long-to-int", + "long-to-float", + "long-to-double", + "float-to-int", + "float-to-long", + "float-to-double", + "double-to-int", + "double-to-long", + "double-to-float", + "int-to-byte", + "int-to-char", + "int-to-short", +]; + +const BINOPS: [&str; 32] = [ + "add-int", + "sub-int", + "mul-int", + "div-int", + "rem-int", + "and-int", + "or-int", + "xor-int", + "shl-int", + "shr-int", + "ushr-int", + "add-long", + "sub-long", + "mul-long", + "div-long", + "rem-long", + "and-long", + "or-long", + "xor-long", + "shl-long", + "shr-long", + "ushr-long", + "add-float", + "sub-float", + "mul-float", + "div-float", + "rem-float", + "add-double", + "sub-double", + "mul-double", + "div-double", + "rem-double", +]; + +const BINOPS_2ADDR: [&str; 32] = [ + "add-int/2addr", + "sub-int/2addr", + "mul-int/2addr", + "div-int/2addr", + "rem-int/2addr", + "and-int/2addr", + "or-int/2addr", + "xor-int/2addr", + "shl-int/2addr", + "shr-int/2addr", + "ushr-int/2addr", + "add-long/2addr", + "sub-long/2addr", + "mul-long/2addr", + "div-long/2addr", + "rem-long/2addr", + "and-long/2addr", + "or-long/2addr", + "xor-long/2addr", + "shl-long/2addr", + "shr-long/2addr", + "ushr-long/2addr", + "add-float/2addr", + "sub-float/2addr", + "mul-float/2addr", + "div-float/2addr", + "rem-float/2addr", + "add-double/2addr", + "sub-double/2addr", + "mul-double/2addr", + "div-double/2addr", + "rem-double/2addr", +]; + +const LIT16: [&str; 8] = + ["add-int/lit16", "rsub-int", "mul-int/lit16", "div-int/lit16", "rem-int/lit16", "and-int/lit16", "or-int/lit16", "xor-int/lit16"]; +const LIT8: [&str; 11] = [ + "add-int/lit8", + "rsub-int/lit8", + "mul-int/lit8", + "div-int/lit8", + "rem-int/lit8", + "and-int/lit8", + "or-int/lit8", + "xor-int/lit8", + "shl-int/lit8", + "shr-int/lit8", + "ushr-int/lit8", +]; +const CMP: [&str; 5] = ["cmpl-float", "cmpg-float", "cmpl-double", "cmpg-double", "cmp-long"]; +const IF2: [&str; 6] = ["if-eq", "if-ne", "if-lt", "if-ge", "if-gt", "if-le"]; +const IFZ: [&str; 6] = ["if-eqz", "if-nez", "if-ltz", "if-gez", "if-gtz", "if-lez"]; +const ARR: [&str; 14] = [ + "aget", + "aget-wide", + "aget-object", + "aget-boolean", + "aget-byte", + "aget-char", + "aget-short", + "aput", + "aput-wide", + "aput-object", + "aput-boolean", + "aput-byte", + "aput-char", + "aput-short", +]; +const IFIELD: [&str; 14] = [ + "iget", + "iget-wide", + "iget-object", + "iget-boolean", + "iget-byte", + "iget-char", + "iget-short", + "iput", + "iput-wide", + "iput-object", + "iput-boolean", + "iput-byte", + "iput-char", + "iput-short", +]; +const SFIELD: [&str; 14] = [ + "sget", + "sget-wide", + "sget-object", + "sget-boolean", + "sget-byte", + "sget-char", + "sget-short", + "sput", + "sput-wide", + "sput-object", + "sput-boolean", + "sput-byte", + "sput-char", + "sput-short", +]; +const INVOKE: [&str; 5] = ["invoke-virtual", "invoke-super", "invoke-direct", "invoke-static", "invoke-interface"]; +const INVOKE_RANGE: [&str; 5] = + ["invoke-virtual/range", "invoke-super/range", "invoke-direct/range", "invoke-static/range", "invoke-interface/range"]; + +fn pick(list: &[&'static str], i: u8, base: u8) -> &'static str { + list.get(usize::from(i.wrapping_sub(base))).copied().unwrap_or("unused") +} + +/// The opcode table entry for `b`. +pub fn opcode(b: u8) -> Op { + use Fmt::*; + use IndexKind as K; + match b { + 0x00 => op("nop", F10x, K::None), + 0x01 => op("move", F12x, K::None), + 0x02 => op("move/from16", F22x, K::None), + 0x03 => op("move/16", F32x, K::None), + 0x04 => op("move-wide", F12x, K::None), + 0x05 => op("move-wide/from16", F22x, K::None), + 0x06 => op("move-wide/16", F32x, K::None), + 0x07 => op("move-object", F12x, K::None), + 0x08 => op("move-object/from16", F22x, K::None), + 0x09 => op("move-object/16", F32x, K::None), + 0x0a => op("move-result", F11x, K::None), + 0x0b => op("move-result-wide", F11x, K::None), + 0x0c => op("move-result-object", F11x, K::None), + 0x0d => op("move-exception", F11x, K::None), + 0x0e => op("return-void", F10x, K::None), + 0x0f => op("return", F11x, K::None), + 0x10 => op("return-wide", F11x, K::None), + 0x11 => op("return-object", F11x, K::None), + 0x12 => op("const/4", F11n, K::None), + 0x13 => op("const/16", F21s, K::None), + 0x14 => op("const", F31i, K::None), + 0x15 => op("const/high16", F21h, K::None), + 0x16 => op("const-wide/16", F21s, K::None), + 0x17 => op("const-wide/32", F31i, K::None), + 0x18 => op("const-wide", F51l, K::None), + 0x19 => op("const-wide/high16", F21h, K::None), + 0x1a => op("const-string", F21c, K::String), + 0x1b => op("const-string/jumbo", F31c, K::String), + 0x1c => op("const-class", F21c, K::Type), + 0x1d => op("monitor-enter", F11x, K::None), + 0x1e => op("monitor-exit", F11x, K::None), + 0x1f => op("check-cast", F21c, K::Type), + 0x20 => op("instance-of", F22c, K::Type), + 0x21 => op("array-length", F12x, K::None), + 0x22 => op("new-instance", F21c, K::Type), + 0x23 => op("new-array", F22c, K::Type), + 0x24 => op("filled-new-array", F35c, K::Type), + 0x25 => op("filled-new-array/range", F3rc, K::Type), + 0x26 => op("fill-array-data", F31t, K::None), + 0x27 => op("throw", F11x, K::None), + 0x28 => op("goto", F10t, K::None), + 0x29 => op("goto/16", F20t, K::None), + 0x2a => op("goto/32", F30t, K::None), + 0x2b => op("packed-switch", F31t, K::None), + 0x2c => op("sparse-switch", F31t, K::None), + 0x2d..=0x31 => op(pick(&CMP, b, 0x2d), F23x, K::None), + 0x32..=0x37 => op(pick(&IF2, b, 0x32), F22t, K::None), + 0x38..=0x3d => op(pick(&IFZ, b, 0x38), F21t, K::None), + 0x44..=0x51 => op(pick(&ARR, b, 0x44), F23x, K::None), + 0x52..=0x5f => op(pick(&IFIELD, b, 0x52), F22c, K::Field), + 0x60..=0x6d => op(pick(&SFIELD, b, 0x60), F21c, K::Field), + 0x6e..=0x72 => op(pick(&INVOKE, b, 0x6e), F35c, K::Method), + 0x74..=0x78 => op(pick(&INVOKE_RANGE, b, 0x74), F3rc, K::Method), + 0x7b..=0x8f => op(pick(&UNOPS, b, 0x7b), F12x, K::None), + 0x90..=0xaf => op(pick(&BINOPS, b, 0x90), F23x, K::None), + 0xb0..=0xcf => op(pick(&BINOPS_2ADDR, b, 0xb0), F12x, K::None), + 0xd0..=0xd7 => op(pick(&LIT16, b, 0xd0), F22s, K::None), + 0xd8..=0xe2 => op(pick(&LIT8, b, 0xd8), F22b, K::None), + 0xfa => op("invoke-polymorphic", F45cc, K::Method), + 0xfb => op("invoke-polymorphic/range", F4rcc, K::Method), + 0xfc => op("invoke-custom", F35c, K::CallSite), + 0xfd => op("invoke-custom/range", F3rc, K::CallSite), + 0xfe => op("const-method-handle", F21c, K::MethodHandle), + 0xff => op("const-method-type", F21c, K::Proto), + _ => UNUSED, + } +} + +/// Switch and array payloads. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(tag = "kind", rename_all = "kebab-case")] +pub enum Payload { + PackedSwitch { first_key: i32, targets: Vec }, + SparseSwitch { keys: Vec, targets: Vec }, + FillArrayData { element_width: u16, count: u32, data: Vec }, +} + +/// One decoded instruction. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Insn { + /// Offset in 16-bit code units from the start of the method's code. + pub offset: u32, + pub opcode: u8, + pub name: &'static str, + /// Length in code units. + pub units: u32, + /// Registers in operand order (for ranges: every register in the range). + pub regs: Vec, + /// True for `/range` forms. + pub range: bool, + #[serde(skip_serializing_if = "Option::is_none")] + pub literal: Option, + /// Absolute branch or payload target, in code units. + #[serde(skip_serializing_if = "Option::is_none")] + pub target: Option, + pub index_kind: IndexKind, + #[serde(skip_serializing_if = "Option::is_none")] + pub index: Option, + /// Second index (the proto of `invoke-polymorphic`). + #[serde(skip_serializing_if = "Option::is_none")] + pub index2: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub payload: Option, +} + +/// Most entries decoded from one payload. +const MAX_PAYLOAD: usize = 65_536; +/// Maximum payload bytes retained by one decoded method, including copies attached to users. +pub const DEFAULT_MAX_PAYLOAD_BYTES: usize = 64 << 20; + +impl Payload { + fn retained_bytes(&self) -> usize { + match self { + Self::PackedSwitch { targets, .. } => targets.len().saturating_mul(4), + Self::SparseSwitch { keys, targets } => keys.len().saturating_add(targets.len()).saturating_mul(4), + Self::FillArrayData { data, .. } => data.len(), + } + } + + fn complete(&self) -> bool { + match self { + Self::FillArrayData { element_width, count, data } => u64::from(*element_width) * u64::from(*count) == data.len() as u64, + _ => true, + } + } +} + +fn u32_at(code: &[u16], i: usize) -> Option { + Some(u32::from(*code.get(i)?) | (u32::from(*code.get(i + 1)?) << 16)) +} + +/// Decode a payload pseudo-instruction at `pc`; returns it and its length in units. +fn payload(code: &[u16], pc: usize) -> Option<(Payload, usize)> { + let ident = *code.get(pc)?; + match ident { + 0x0100 => { + let size = usize::from(*code.get(pc + 1)?); + let first_key = u32_at(code, pc + 2)? as i32; + let mut targets = Vec::with_capacity(size.min(MAX_PAYLOAD)); + for i in 0..size.min(MAX_PAYLOAD) { + targets.push(u32_at(code, pc + 4 + i * 2)? as i32); + } + Some((Payload::PackedSwitch { first_key, targets }, size * 2 + 4)) + } + 0x0200 => { + let size = usize::from(*code.get(pc + 1)?); + let mut keys = Vec::with_capacity(size.min(MAX_PAYLOAD)); + let mut targets = Vec::with_capacity(size.min(MAX_PAYLOAD)); + for i in 0..size.min(MAX_PAYLOAD) { + keys.push(u32_at(code, pc + 2 + i * 2)? as i32); + targets.push(u32_at(code, pc + 2 + size * 2 + i * 2)? as i32); + } + Some((Payload::SparseSwitch { keys, targets }, size * 4 + 2)) + } + 0x0300 => { + let width = *code.get(pc + 1)?; + if !matches!(width, 1 | 2 | 4 | 8) { + return None; + } + let count = u32_at(code, pc + 2)?; + let bytes = usize::from(width).checked_mul(count as usize)?; + let units = bytes.checked_add(1)? / 2 + 4; + if pc.checked_add(units)? > code.len() { + return None; + } + let mut data = Vec::with_capacity(bytes.min(MAX_PAYLOAD)); + for i in 0..bytes.min(MAX_PAYLOAD) { + let u = *code.get(pc + 4 + i / 2)?; + data.push(if i % 2 == 0 { u as u8 } else { (u >> 8) as u8 }); + } + Some((Payload::FillArrayData { element_width: width, count, data }, units)) + } + _ => None, + } +} + +/// Decode one instruction at `pc`, or `None` when it runs past the end. +pub fn decode_one(code: &[u16], pc: usize) -> Option { + use Fmt::*; + let u0 = *code.get(pc)?; + let b = (u0 & 0xff) as u8; + let hi = (u0 >> 8) as u8; + if b == 0 && hi != 0 { + let (p, units) = payload(code, pc)?; + let name = match p { + Payload::PackedSwitch { .. } => "packed-switch-payload", + Payload::SparseSwitch { .. } => "sparse-switch-payload", + Payload::FillArrayData { .. } => "array-data-payload", + }; + return Some(Insn { + offset: pc as u32, + opcode: 0, + name, + units: units as u32, + regs: vec![], + range: false, + literal: None, + target: None, + index_kind: IndexKind::None, + index: None, + index2: None, + payload: Some(p), + }); + } + let o = opcode(b); + let n = o.fmt.units(); + if pc.checked_add(n)? > code.len() { + return None; + } + let u = |i: usize| code.get(pc + i).copied().unwrap_or(0); + let a4 = u32::from(hi & 0x0f); + let b4 = u32::from(hi >> 4); + let aa = u32::from(hi); + let rel = |d: i64| pc as i64 + d; + let mut insn = Insn { + offset: pc as u32, + opcode: b, + name: o.name, + units: n as u32, + regs: vec![], + range: false, + literal: None, + target: None, + index_kind: o.index, + index: None, + index2: None, + payload: None, + }; + match o.fmt { + F10x => {} + F12x => insn.regs = vec![a4, b4], + F11n => { + insn.regs = vec![a4]; + insn.literal = Some(i64::from((hi as i8) >> 4)); + } + F11x => insn.regs = vec![aa], + F10t => insn.target = Some(rel(i64::from(hi as i8))), + F20t => insn.target = Some(rel(i64::from(u(1) as i16))), + F22x => insn.regs = vec![aa, u32::from(u(1))], + F21t => { + insn.regs = vec![aa]; + insn.target = Some(rel(i64::from(u(1) as i16))); + } + F21s => { + insn.regs = vec![aa]; + insn.literal = Some(i64::from(u(1) as i16)); + } + F21h => { + insn.regs = vec![aa]; + let v = i64::from(u(1) as i16); + insn.literal = Some(if b == 0x19 { v << 48 } else { v << 16 }); + } + F21c => { + insn.regs = vec![aa]; + insn.index = Some(u32::from(u(1))); + } + F23x => insn.regs = vec![aa, u32::from(u(1) & 0xff), u32::from(u(1) >> 8)], + F22b => { + insn.regs = vec![aa, u32::from(u(1) & 0xff)]; + insn.literal = Some(i64::from((u(1) >> 8) as u8 as i8)); + } + F22t => { + insn.regs = vec![a4, b4]; + insn.target = Some(rel(i64::from(u(1) as i16))); + } + F22s => { + insn.regs = vec![a4, b4]; + insn.literal = Some(i64::from(u(1) as i16)); + } + F22c => { + insn.regs = vec![a4, b4]; + insn.index = Some(u32::from(u(1))); + } + F30t => insn.target = Some(rel(i64::from((u32::from(u(1)) | (u32::from(u(2)) << 16)) as i32))), + F32x => insn.regs = vec![u32::from(u(1)), u32::from(u(2))], + F31i => { + insn.regs = vec![aa]; + insn.literal = Some(i64::from((u32::from(u(1)) | (u32::from(u(2)) << 16)) as i32)); + } + F31t => { + insn.regs = vec![aa]; + insn.target = Some(rel(i64::from((u32::from(u(1)) | (u32::from(u(2)) << 16)) as i32))); + } + F31c => { + insn.regs = vec![aa]; + insn.index = Some(u32::from(u(1)) | (u32::from(u(2)) << 16)); + } + F35c | F45cc => { + if b4 > 5 { + return None; + } + let count = b4 as usize; + let g = a4; + let w = u(2); + let all = [u32::from(w & 0xf), u32::from((w >> 4) & 0xf), u32::from((w >> 8) & 0xf), u32::from(w >> 12), g]; + insn.regs = all.iter().take(count).copied().collect(); + insn.index = Some(u32::from(u(1))); + if o.fmt == F45cc { + insn.index2 = Some(u32::from(u(3))); + } + } + F3rc | F4rcc => { + let first = u32::from(u(2)); + let end = first.checked_add(aa)?; + if end > u32::from(u16::MAX) + 1 { + return None; + } + insn.regs = (first..end).collect(); + insn.range = true; + insn.index = Some(u32::from(u(1))); + if o.fmt == F4rcc { + insn.index2 = Some(u32::from(u(3))); + } + } + F51l => { + insn.regs = vec![aa]; + let v = u64::from(u(1)) | (u64::from(u(2)) << 16) | (u64::from(u(3)) << 32) | (u64::from(u(4)) << 48); + insn.literal = Some(v as i64); + } + } + Some(insn) +} + +/// Decode a whole method body, attaching each switch/array payload to the instruction that +/// references it. Stops at `max` instructions and caps all retained payload copies together. +pub fn decode(code: &[u16], max: usize) -> (Vec, bool) { + decode_with_payload_limit(code, max, DEFAULT_MAX_PAYLOAD_BYTES) +} + +/// Decode with an explicit aggregate payload byte budget. Payloads that do not fit are omitted; +/// their instruction metadata remains and the returned completeness flag is false. A source +/// payload and every attached copy consume the same budget, before the copy is allocated. +pub fn decode_with_payload_limit(code: &[u16], max: usize, max_payload_bytes: usize) -> (Vec, bool) { + let mut out = Vec::new(); + let mut pc = 0usize; + let mut complete = true; + let mut bytes_left = max_payload_bytes; + while pc < code.len() { + if out.len() >= max { + complete = false; + break; + } + match decode_one(code, pc) { + Some(mut i) => { + pc += (i.units as usize).max(1); + if let Some(p) = &i.payload { + complete &= p.complete(); + if let Some(left) = bytes_left.checked_sub(p.retained_bytes()) { + bytes_left = left; + } else { + i.payload = None; + complete = false; + } + } + out.push(i); + } + None => { + complete = false; + break; + } + } + } + // Attach payloads to their users (31t formats). + // The decoder visits offsets in order. Index their positions without copying any payload. + let payloads: Vec<(i64, usize)> = out.iter().enumerate().filter(|(_, i)| i.payload.is_some()).map(|(n, i)| (i64::from(i.offset), n)).collect(); + for n in 0..out.len() { + let Some(target) = out.get(n).filter(|i| matches!(i.opcode, 0x26 | 0x2b | 0x2c)).and_then(|i| i.target) else { continue }; + let Ok(position) = payloads.binary_search_by_key(&target, |(offset, _)| *offset) else { continue }; + let Some(payload) = payloads.get(position).and_then(|(_, index)| out.get(*index)).and_then(|i| i.payload.as_ref()) else { continue }; + let Some(left) = bytes_left.checked_sub(payload.retained_bytes()) else { + complete = false; + continue; + }; + bytes_left = left; + let copy = payload.clone(); + if let Some(instruction) = out.get_mut(n) { + instruction.payload = Some(copy); + } + } + (out, complete) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn table_is_complete() { + let mut named = 0; + for b in 0..=255u8 { + let o = opcode(b); + if o.name != "unused" { + named += 1; + } + } + // 256 slots minus the 0x3e-0x43, 0x73, 0x79-0x7a and 0xe3-0xf9 holes. + assert_eq!(named, 256 - 6 - 1 - 2 - 23); + assert_eq!(opcode(0x6e).name, "invoke-virtual"); + assert_eq!(opcode(0x8f).name, "int-to-short"); + assert_eq!(opcode(0xaf).name, "rem-double"); + assert_eq!(opcode(0xcf).name, "rem-double/2addr"); + assert_eq!(opcode(0xe2).name, "ushr-int/lit8"); + } + + #[test] + fn decodes_formats() { + // const/4 v1, #-1 ; invoke-static {v0, v1}, meth@5 ; packed-switch v0, +4 ; return-void + // payload: packed, size 2, first 10, targets 1, 2 + let code: Vec = vec![0xf112, 0x2071, 5, 0x0010, 0x002b, 4, 0, 0x000e, 0x0100, 2, 10, 0, 1, 0, 2, 0]; + let (ins, complete) = decode(&code, 100); + assert!(complete); + assert_eq!(ins[0].name, "const/4"); + assert_eq!(ins[0].literal, Some(-1)); + assert_eq!(ins[0].regs, vec![1]); + assert_eq!(ins[1].name, "invoke-static"); + assert_eq!(ins[1].regs, vec![0, 1]); + assert_eq!(ins[1].index, Some(5)); + assert_eq!(ins[2].name, "packed-switch"); + assert_eq!(ins[2].target, Some(8)); + assert_eq!(ins[2].payload, Some(Payload::PackedSwitch { first_key: 10, targets: vec![1, 2] })); + assert_eq!(ins[4].name, "packed-switch-payload"); + // const-wide v0, 0x1122334455667788 + let (w, _) = decode(&[0x0018, 0x7788, 0x5566, 0x3344, 0x1122], 10); + assert_eq!(w[0].literal, Some(0x1122_3344_5566_7788)); + // fill-array-data payload width 1 count 3 + let (f, _) = decode(&[0x0300, 1, 3, 0, 0x0201, 0x0003], 10); + assert_eq!(f[0].payload, Some(Payload::FillArrayData { element_width: 1, count: 3, data: vec![1, 2, 3] })); + // invoke-virtual/range {v4..v6} + let (r, _) = decode(&[0x0374, 9, 4], 10); + assert_eq!(r[0].regs, vec![4, 5, 6]); + assert!(r[0].range); + } + + #[test] + fn hostile() { + for seed in 0..200 { + let n = crate::test_support::noise(seed, 256); + let code: Vec = n.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect(); + let _ = decode(&code, 1000); + } + let _ = decode(&[0x0300, 0xffff, 0xffff, 0xffff], 10); + let _ = decode(&[0x0200, 0xffff], 10); + let _ = decode(&[0x0018], 10); + } +} diff --git a/crates/dex/src/lib.rs b/crates/dex/src/lib.rs new file mode 100644 index 000000000..97d28b7f4 --- /dev/null +++ b/crates/dex/src/lib.rs @@ -0,0 +1,42 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] +//! Dalvik DEX inspection and bytecode decoding. Never executes bytecode. +mod bytes; +/// Checked byte reader accepted by the signed-LEB compatibility helper. +pub use bytes::Reader; +pub mod dex; +pub mod dex_ops; +pub use dex::*; +#[cfg(test)] +mod test_support; +/// Parser admission failures; damaged substructures retain the original partial-report behavior. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum DexError { + #[error("the file ends early ({0})")] + Truncated(String), + #[error("the file is malformed ({0})")] + Malformed(String), + #[error("not supported: {0}")] + Unsupported(String), +} +use DexError as FormatError; + +#[cfg(test)] +#[path = "regressions/array_widths.rs"] +mod array_widths; + +#[cfg(test)] +#[path = "regressions/capped_table_boundaries.rs"] +mod capped_table_boundaries; + +#[cfg(test)] +#[path = "regressions/invoke_registers.rs"] +mod invoke_registers; + +#[cfg(test)] +#[path = "regressions/payload_budgets.rs"] +mod payload_budgets; + +#[cfg(test)] +#[path = "regressions/signed_leb128.rs"] +mod signed_leb128; diff --git a/crates/dex/src/regressions/array_widths.rs b/crates/dex/src/regressions/array_widths.rs new file mode 100644 index 000000000..37524b002 --- /dev/null +++ b/crates/dex/src/regressions/array_widths.rs @@ -0,0 +1,39 @@ +use crate::dex_ops::{Payload, decode, decode_one}; + +#[test] +fn zero_width_does_not_describe_a_huge_complete_array() { + let code = [0x0300, 0, 0xffff, 0xffff]; + assert!(decode_one(&code, 0).is_none()); + assert!(!decode(&code, 16).1); +} + +#[test] +fn unsupported_element_widths_are_refused_even_for_empty_arrays() { + for width in [0, 3, 5, 6, 7, 9, 16, u16::MAX] { + assert!(decode_one(&[0x0300, width, 0, 0], 0).is_none(), "width {width}"); + } +} + +#[test] +fn all_primitive_widths_and_odd_byte_padding_still_decode() { + for width in [1u16, 2, 4, 8] { + let count = 3u32; + let bytes = usize::from(width) * count as usize; + let mut code = vec![0x0300, width, count as u16, 0]; + code.resize(4 + bytes.div_ceil(2), 0x2211); + let instruction = decode_one(&code, 0).unwrap(); + assert_eq!(instruction.units as usize, code.len()); + match instruction.payload.unwrap() { + Payload::FillArrayData { element_width, count: decoded, data } => { + assert_eq!(element_width, width); + assert_eq!(decoded, count); + assert_eq!(data.len(), bytes); + assert_eq!(data[0], 0x11); + } + other => panic!("unexpected payload {other:?}"), + } + assert!(decode_one(&[0x0300, width, 0, 0], 0).is_some()); + code.pop(); + assert!(decode_one(&code, 0).is_none()); + } +} diff --git a/crates/dex/src/regressions/capped_table_boundaries.rs b/crates/dex/src/regressions/capped_table_boundaries.rs new file mode 100644 index 000000000..7fbb419f5 --- /dev/null +++ b/crates/dex/src/regressions/capped_table_boundaries.rs @@ -0,0 +1,114 @@ +use crate::dex::{self, ClassDef, Code}; + +const MEMBER_CAP: usize = 65_536; +const HANDLER_CAP: usize = 256; + +fn uleb(mut value: u32, bytes: &mut Vec) { + loop { + let byte = (value & 0x7f) as u8; + value >>= 7; + bytes.push(byte | if value == 0 { 0 } else { 0x80 }); + if value == 0 { + break; + } + } +} + +fn sleb(mut value: i32, bytes: &mut Vec) { + loop { + let byte = value as u8 & 0x7f; + value >>= 7; + let done = (value == 0 && byte & 0x40 == 0) || (value == -1 && byte & 0x40 != 0); + bytes.push(byte | if done { 0 } else { 0x80 }); + if done { + break; + } + } +} + +fn header() -> Vec { + let mut bytes = vec![0; 0x70]; + bytes[..8].copy_from_slice(b"dex\n035\0"); + bytes[36..40].copy_from_slice(&0x70u32.to_le_bytes()); + bytes[40..44].copy_from_slice(&0x1234_5678u32.to_le_bytes()); + bytes +} + +fn class_data(count: usize) -> dex::ClassData { + let mut bytes = header(); + for n in [count as u32, 0, 1, 0] { + uleb(n, &mut bytes); + } + bytes.extend_from_slice(&[0, 1].repeat(count)); + bytes.extend_from_slice(&[3, 9, 7]); + let len = bytes.len() as u32; + bytes[32..36].copy_from_slice(&len.to_le_bytes()); + let class = ClassDef { + class_idx: 0, + access: 0, + superclass_idx: 0, + interfaces_off: 0, + source_file_idx: 0, + annotations_off: 0, + class_data_off: 0x70, + static_values_off: 0, + }; + dex::parse(&bytes).unwrap().class_data(&class) +} + +fn code(size: i32, payload: &[u8]) -> Code { + let mut bytes = header(); + bytes.extend_from_slice(&[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 8, 0, 0, 0]); + bytes.extend_from_slice(&[0; 16]); + bytes.extend_from_slice(&[0, 0, 0, 0, 1, 0, 1, 0]); + bytes.push(1); + sleb(size, &mut bytes); + bytes.extend_from_slice(payload); + let len = bytes.len() as u32; + bytes[32..36].copy_from_slice(&len.to_le_bytes()); + dex::parse(&bytes).unwrap().code(0x70).unwrap() +} + +#[test] +fn capped_class_members_do_not_become_the_next_group() { + let data = class_data(MEMBER_CAP + 1); + assert_eq!(data.static_fields.len(), MEMBER_CAP); + assert!(data.direct_methods.is_empty()); + assert!(!data.complete); +} + +#[test] +fn an_exact_member_limit_keeps_the_following_group() { + let data = class_data(MEMBER_CAP); + assert_eq!(data.static_fields.len(), MEMBER_CAP); + assert!(data.complete); + assert_eq!(data.direct_methods, [dex::EncodedMethod { method_idx: 3, access: 9, code_off: 7 }]); +} + +#[test] +fn an_unread_typed_handler_is_not_a_catch_all() { + let mut payload = [0, 1].repeat(HANDLER_CAP + 1); + payload.push(7); + let parsed = code(-((HANDLER_CAP + 1) as i32), &payload); + assert_eq!(parsed.tries[0].handlers.len(), HANDLER_CAP); + assert!(parsed.tries[0].handlers.iter().all(|handler| handler.exception.is_some() && handler.address == 1)); +} + +#[test] +fn an_incomplete_typed_handler_cannot_supply_a_catch_all_address() { + let parsed = code(-2, &[0, 1, 0]); + assert_eq!(parsed.tries[0].handlers.len(), 1); + assert!(parsed.tries[0].handlers[0].exception.is_some()); + assert_eq!(parsed.tries[0].handlers[0].address, 1); +} + +#[test] +fn complete_typed_lists_keep_their_real_catch_all() { + for count in [0, 1, HANDLER_CAP] { + let mut payload = [0, 1].repeat(count); + payload.push(7); + let parsed = code(-(count as i32), &payload); + assert_eq!(parsed.tries[0].handlers.len(), count + 1); + assert_eq!(parsed.tries[0].handlers.last(), Some(&dex::Handler { exception: None, address: 7 })); + } +} diff --git a/crates/dex/src/regressions/invoke_registers.rs b/crates/dex/src/regressions/invoke_registers.rs new file mode 100644 index 000000000..c2c6faab6 --- /dev/null +++ b/crates/dex/src/regressions/invoke_registers.rs @@ -0,0 +1,33 @@ +use crate::dex_ops::{decode, decode_one}; + +#[test] +fn fixed_register_lists_cannot_claim_more_than_five_arguments() { + for op in [0x24u16, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0xfa, 0xfc] { + for count in 6..=15 { + let code = [(count << 12) | op, 1, 0x4321, 2]; + assert!(decode_one(&code, 0).is_none(), "opcode {op:#x}, count {count}"); + assert!(!decode(&code, 16).1); + } + } +} + +#[test] +fn range_arguments_cannot_leave_the_encoded_register_space() { + for op in [0x25u16, 0x74, 0x75, 0x76, 0x77, 0x78, 0xfb, 0xfd] { + assert!(decode_one(&[0x0200 | op, 1, u16::MAX, 2], 0).is_none(), "{op:#x}"); + assert!(decode_one(&[0xff00 | op, 1, u16::MAX - 1, 2], 0).is_none(), "{op:#x}"); + } +} + +#[test] +fn zero_and_boundary_register_lists_remain_valid() { + let fixed = decode_one(&[0x5571, 1, 0x4321], 0).unwrap(); + assert_eq!(fixed.regs, vec![1, 2, 3, 4, 5]); + assert!(decode_one(&[0x0071, 1, 0], 0).unwrap().regs.is_empty()); + let range = decode_one(&[0x0277, 1, u16::MAX - 1], 0).unwrap(); + assert_eq!(range.regs, vec![65534, 65535]); + assert!(decode_one(&[0x0077, 1, u16::MAX], 0).unwrap().regs.is_empty()); + let polymorphic = decode_one(&[0x01fb, 1, u16::MAX, 2], 0).unwrap(); + assert_eq!(polymorphic.regs, vec![65535]); + assert_eq!(polymorphic.index2, Some(2)); +} diff --git a/crates/dex/src/regressions/payload_budgets.rs b/crates/dex/src/regressions/payload_budgets.rs new file mode 100644 index 000000000..8680373a4 --- /dev/null +++ b/crates/dex/src/regressions/payload_budgets.rs @@ -0,0 +1,88 @@ +use crate::dex_ops::{self, Payload}; + +fn arrays(references: usize, bytes: usize) -> Vec { + let mut code = Vec::new(); + let offset = (references * 3).next_multiple_of(2); + for n in 0..references { + let relative = u32::try_from(offset - n * 3).unwrap(); + code.extend([0x0026, relative as u16, (relative >> 16) as u16]); + } + code.resize(offset, 0); + let count = u32::try_from(bytes).unwrap(); + code.extend([0x0300, 1, count as u16, (count >> 16) as u16]); + code.resize(code.len() + bytes.div_ceil(2), 0x5a5a); + code +} + +fn bytes(rows: &[dex_ops::Insn]) -> usize { + rows.iter() + .map(|i| match i.payload.as_ref() { + Some(Payload::FillArrayData { data, .. }) => data.len(), + Some(Payload::PackedSwitch { targets, .. }) => targets.len() * 4, + Some(Payload::SparseSwitch { keys, targets }) => (keys.len() + targets.len()) * 4, + None => 0, + }) + .sum() +} + +#[test] +fn shared_arrays_charge_the_source_and_each_attached_copy() { + let code = arrays(4, 8); + let (rows, complete) = dex_ops::decode_with_payload_limit(&code, 100, 24); + assert!(!complete); + assert_eq!(bytes(&rows), 24); + assert!(rows[0].payload.is_some() && rows[1].payload.is_some()); + assert!(rows[2].payload.is_none() && rows[3].payload.is_none()); + assert!(rows.last().unwrap().payload.is_some()); + let (full, complete) = dex_ops::decode_with_payload_limit(&code, 100, 40); + assert!(complete); + assert_eq!(bytes(&full), 40); +} + +#[test] +fn zero_budget_keeps_offsets_and_payload_instruction_metadata() { + let code = arrays(2, 8); + let (rows, complete) = dex_ops::decode_with_payload_limit(&code, 100, 0); + assert!(!complete); + assert_eq!(rows.iter().map(|i| i.offset).collect::>(), [0, 3, 6]); + assert_eq!(rows[2].name, "array-data-payload"); + assert_eq!(rows[2].units, 8); + assert!(rows.iter().all(|i| i.payload.is_none())); + assert!(dex_ops::decode_with_payload_limit(&[0x000e], 100, 0).1); +} + +#[test] +fn both_switch_tables_are_accounted_and_backward_references_work() { + // packed table: one target; sparse table: one key and one target; users follow the tables. + let code = [0x0100, 1, 7, 0, 1, 0, 0x0200, 1, 9, 0, 2, 0, 0x002b, 0xfff4, 0xffff, 0x002c, 0xfff7, 0xffff]; + let (rows, complete) = dex_ops::decode_with_payload_limit(&code, 100, 24); + assert!(complete); + assert_eq!(rows[2].payload, Some(Payload::PackedSwitch { first_key: 7, targets: vec![1] })); + assert_eq!(rows[3].payload, Some(Payload::SparseSwitch { keys: vec![9], targets: vec![2] })); + let (rows, complete) = dex_ops::decode_with_payload_limit(&code, 100, 16); + assert!(!complete); + assert!(rows[2].payload.is_some()); + assert!(rows[3].payload.is_none()); + assert_eq!(bytes(&rows), 16); +} + +#[test] +fn the_existing_per_array_cap_is_reported_as_partial() { + let (rows, complete) = dex_ops::decode(&arrays(1, 65_537), 100); + assert!(!complete); + assert_eq!(bytes(&rows), 2 * 65_536); + let (rows, complete) = dex_ops::decode(&arrays(1, 8), 1); + assert!(!complete); + assert_eq!(rows.len(), 1); + assert!(rows[0].payload.is_none()); +} + +#[test] +#[ignore = "memory regression: run separately with a 128 MiB address-space limit"] +fn repeated_array_references_stay_under_the_default_byte_budget() { + let code = arrays(2048, 65_536); + let (rows, complete) = dex_ops::decode(&code, 200_000); + assert!(!complete); + assert_eq!(rows.len(), 2049); + assert_eq!(bytes(&rows), dex_ops::DEFAULT_MAX_PAYLOAD_BYTES); +} diff --git a/crates/dex/src/regressions/signed_leb128.rs b/crates/dex/src/regressions/signed_leb128.rs new file mode 100644 index 000000000..f38fd8db6 --- /dev/null +++ b/crates/dex/src/regressions/signed_leb128.rs @@ -0,0 +1,94 @@ +use crate::bytes::Reader; +use crate::dex::{self, Code, Handler, sleb128}; + +#[test] +fn rejects_invalid_fifth_byte_sign_extensions() { + for prefix in [0x80, 0xff] { + for last in 0x08..=0x77 { + let mut encoded = vec![prefix; 4]; + encoded.push(last); + assert_eq!(sleb128(&Reader::new(&encoded), 0), None, "last byte {last:#x}"); + } + } +} + +#[test] +fn retains_signed_32_bit_limits_and_ordinary_values() { + assert_eq!(sleb128(&Reader::new(&[0xff, 0xff, 0xff, 0xff, 0x07]), 0), Some((i64::from(i32::MAX), 5))); + assert_eq!(sleb128(&Reader::new(&[0x80, 0x80, 0x80, 0x80, 0x78]), 0), Some((i64::from(i32::MIN), 5))); + assert_eq!(sleb128(&Reader::new(&[0x9b, 0xf1, 0x59]), 0), Some((-624485, 3))); + assert_eq!(sleb128(&Reader::new(&[0xe5, 0x8e, 0x26]), 0), Some((624485, 3))); + assert_eq!(sleb128(&Reader::new(&[0xff, 0x7f]), 0), Some((-1, 2))); + assert_eq!(sleb128(&Reader::new(&[0x80, 0]), 0), Some((0, 2))); + assert_eq!(sleb128(&Reader::new(&[0x55, 0x7f, 0x55]), 1), Some((-1, 1))); +} + +#[test] +fn retains_valid_five_byte_sign_extensions() { + assert_eq!(sleb128(&Reader::new(&[0x80, 0x80, 0x80, 0x80, 0]), 0), Some((0, 5))); + assert_eq!(sleb128(&Reader::new(&[0x81, 0x80, 0x80, 0x80, 0]), 0), Some((1, 5))); + assert_eq!(sleb128(&Reader::new(&[0xff, 0xff, 0xff, 0xff, 0x7f]), 0), Some((-1, 5))); +} + +#[test] +fn rejects_values_longer_than_five_bytes() { + for (prefix, last) in [(0x80, 0), (0xff, 0x7f)] { + for length in [6, 10] { + let mut encoded = vec![prefix; length - 1]; + encoded.push(last); + assert_eq!(sleb128(&Reader::new(&encoded), 0), None, "{encoded:x?}"); + } + } +} + +#[test] +fn rejects_truncated_values_and_out_of_range_offsets() { + assert_eq!(sleb128(&Reader::new(&[0x80; 5]), 0), None); + assert_eq!(sleb128(&Reader::new(&[0x80]), 0), None); + assert_eq!(sleb128(&Reader::new(&[]), 0), None); + assert_eq!(sleb128(&Reader::new(&[0]), usize::MAX), None); +} + +fn code_with_handler(size: &[u8], payload: &[u8]) -> Code { + let mut bytes = vec![0; 0x70]; + bytes[..8].copy_from_slice(b"dex\n035\0"); + bytes[36..40].copy_from_slice(&0x70u32.to_le_bytes()); + bytes[40..44].copy_from_slice(&0x1234_5678u32.to_le_bytes()); + // Eight nop instructions and one try entry pointing to the first encoded handler. + bytes.extend_from_slice(&[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 8, 0, 0, 0]); + bytes.extend_from_slice(&[0; 16]); + bytes.extend_from_slice(&[0, 0, 0, 0, 1, 0, 1, 0]); + bytes.push(1); + bytes.extend_from_slice(size); + bytes.extend_from_slice(payload); + let len = bytes.len() as u32; + bytes[32..36].copy_from_slice(&len.to_le_bytes()); + dex::parse(&bytes).unwrap().code(0x70).unwrap() +} + +#[test] +fn invalid_sizes_do_not_create_handlers() { + let sizes: &[&[u8]] = &[ + &[0x80, 0x80, 0x80, 0x80, 0x08], + &[0x80, 0x80, 0x80, 0x80, 0x77], + &[0x80, 0x80, 0x80, 0x80, 0x80, 0], + &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0], + ]; + for size in sizes { + // These bytes could be a type/address pair or a catch-all address if size is accepted. + let code = code_with_handler(size, &[0, 7]); + assert_eq!(code.tries.len(), 1); + assert!(code.tries[0].handlers.is_empty(), "invalid size {size:x?}"); + } +} + +#[test] +fn valid_catch_all_handlers_are_retained() { + for size in [&[0][..], &[0x80, 0x80, 0x80, 0x80, 0][..]] { + let code = code_with_handler(size, &[7]); + assert_eq!(code.tries.len(), 1); + assert_eq!(code.tries[0].start, 0); + assert_eq!(code.tries[0].end, 1); + assert_eq!(code.tries[0].handlers, vec![Handler { exception: None, address: 7 }]); + } +} diff --git a/crates/dex/src/test_support.rs b/crates/dex/src/test_support.rs new file mode 100644 index 000000000..772a9f0e4 --- /dev/null +++ b/crates/dex/src/test_support.rs @@ -0,0 +1,430 @@ +// Synthetic DEX fixture and deterministic mutations retained from Craft Anything. +pub use craft_testkit::sample_damage::{mutations, noise}; + +pub mod android { + pub const HELLO: &str = "Hello, Craft!"; + fn u16le(v: &mut Vec, x: u16) { + v.extend_from_slice(&x.to_le_bytes()); + } + fn u32le(v: &mut Vec, x: u32) { + v.extend_from_slice(&x.to_le_bytes()); + } + fn uleb(v: &mut Vec, mut x: u32) { + loop { + let b = (x & 0x7f) as u8; + x >>= 7; + if x == 0 { + v.push(b); + break; + } + v.push(b | 0x80); + } + } + fn sleb(v: &mut Vec, mut x: i32) { + loop { + let b = (x & 0x7f) as u8; + x >>= 7; + let done = (x == 0 && b & 0x40 == 0) || (x == -1 && b & 0x40 != 0); + v.push(if done { b } else { b | 0x80 }); + if done { + break; + } + } + } + fn align4(v: &mut Vec) { + while !v.len().is_multiple_of(4) { + v.push(0); + } + } + + #[derive(Debug, Clone, Default)] + pub struct TrySpec { + pub start: u32, + pub count: u16, + /// (type idx, handler address) + pub handlers: Vec<(u32, u32)>, + pub catch_all: Option, + } + + #[derive(Debug, Clone, Default)] + pub struct CodeSpec { + pub registers: u16, + pub ins: u16, + pub outs: u16, + pub insns: Vec, + pub tries: Vec, + } + + #[derive(Debug, Clone)] + pub struct MethodSpec { + pub method: u32, + pub access: u32, + pub code: Option, + } + + #[derive(Debug, Clone, Default)] + pub struct ClassSpec { + pub class: u32, + pub access: u32, + pub superclass: u32, + pub interfaces: Vec, + pub source_file: u32, + pub annotations: Vec, + pub static_fields: Vec<(u32, u32)>, + pub instance_fields: Vec<(u32, u32)>, + pub direct: Vec, + pub virtuals: Vec, + } + + #[derive(Debug, Clone, Default)] + pub struct DexBuilder { + strings: Vec, + types: Vec, + protos: Vec<(u32, u32, Vec)>, + fields: Vec<(u32, u32, u32)>, + methods: Vec<(u32, u32, u32)>, + pub classes: Vec, + } + + impl DexBuilder { + pub fn string(&mut self, s: &str) -> u32 { + if let Some(i) = self.strings.iter().position(|x| x == s) { + return i as u32; + } + self.strings.push(s.to_string()); + (self.strings.len() - 1) as u32 + } + + pub fn ty(&mut self, desc: &str) -> u32 { + let s = self.string(desc); + if let Some(i) = self.types.iter().position(|&x| x == s) { + return i as u32; + } + self.types.push(s); + (self.types.len() - 1) as u32 + } + + pub fn proto(&mut self, ret: &str, params: &[&str]) -> u32 { + let short = |d: &str| if d.starts_with('L') || d.starts_with('[') { 'L' } else { d.chars().next().unwrap() }; + let shorty: String = std::iter::once(short(ret)).chain(params.iter().map(|p| short(p))).collect(); + let s = self.string(&shorty); + let r = self.ty(ret); + let ps: Vec = params.iter().map(|p| self.ty(p)).collect(); + if let Some(i) = self.protos.iter().position(|p| p.1 == r && p.2 == ps) { + return i as u32; + } + self.protos.push((s, r, ps)); + (self.protos.len() - 1) as u32 + } + + pub fn field(&mut self, cls: &str, ty: &str, name: &str) -> u32 { + let c = self.ty(cls); + let t = self.ty(ty); + let n = self.string(name); + if let Some(i) = self.fields.iter().position(|&f| f == (c, t, n)) { + return i as u32; + } + self.fields.push((c, t, n)); + (self.fields.len() - 1) as u32 + } + + pub fn method(&mut self, cls: &str, name: &str, ret: &str, params: &[&str]) -> u32 { + let c = self.ty(cls); + let p = self.proto(ret, params); + let n = self.string(name); + if let Some(i) = self.methods.iter().position(|&m| m == (c, p, n)) { + return i as u32; + } + self.methods.push((c, p, n)); + (self.methods.len() - 1) as u32 + } + + pub fn build(self) -> Vec { + let ns = self.strings.len(); + let nt = self.types.len(); + let np = self.protos.len(); + let nf = self.fields.len(); + let nm = self.methods.len(); + let nc = self.classes.len(); + let string_ids = 0x70; + let type_ids = string_ids + ns * 4; + let proto_ids = type_ids + nt * 4; + let field_ids = proto_ids + np * 12; + let method_ids = field_ids + nf * 8; + let class_defs = method_ids + nm * 8; + let mut data_start = class_defs + nc * 32; + data_start = (data_start + 3) & !3; + let mut d: Vec = Vec::new(); + let at = |d: &Vec| (data_start + d.len()) as u32; + // type lists + let tl = |d: &mut Vec, list: &[u32]| -> u32 { + if list.is_empty() { + return 0; + } + align4(d); + let o = at(d); + u32le(d, list.len() as u32); + for &t in list { + u16le(d, t as u16); + } + o + }; + let proto_params: Vec = self.protos.iter().map(|p| tl(&mut d, &p.2)).collect(); + let class_ifaces: Vec = self.classes.iter().map(|c| tl(&mut d, &c.interfaces)).collect(); + // string data + let mut string_offs = Vec::new(); + for s in &self.strings { + string_offs.push(at(&d)); + uleb(&mut d, s.encode_utf16().count() as u32); + for c in s.encode_utf16() { + if c == 0 { + d.extend_from_slice(&[0xc0, 0x80]); + } else if c < 0x80 { + d.push(c as u8); + } else if c < 0x800 { + d.extend_from_slice(&[0xc0 | (c >> 6) as u8, 0x80 | (c & 0x3f) as u8]); + } else { + d.extend_from_slice(&[0xe0 | (c >> 12) as u8, 0x80 | ((c >> 6) & 0x3f) as u8, 0x80 | (c & 0x3f) as u8]); + } + } + d.push(0); + } + // code items, class data, annotations + let mut class_data_offs = Vec::new(); + let mut annot_offs = Vec::new(); + for c in &self.classes { + let code_off = |d: &mut Vec, m: &MethodSpec| -> u32 { + let Some(code) = &m.code else { return 0 }; + align4(d); + let o = at(d); + u16le(d, code.registers); + u16le(d, code.ins); + u16le(d, code.outs); + u16le(d, code.tries.len() as u16); + u32le(d, 0); + u32le(d, code.insns.len() as u32); + for &i in &code.insns { + u16le(d, i); + } + if !code.tries.is_empty() { + if code.insns.len() % 2 == 1 { + u16le(d, 0); + } + let mut list = Vec::new(); + uleb(&mut list, code.tries.len() as u32); + let mut offs = Vec::new(); + for t in &code.tries { + offs.push(list.len() as u16); + let n = t.handlers.len() as i32; + sleb(&mut list, if t.catch_all.is_some() { -n } else { n }); + for &(ty, addr) in &t.handlers { + uleb(&mut list, ty); + uleb(&mut list, addr); + } + if let Some(a) = t.catch_all { + uleb(&mut list, a); + } + } + for (t, off) in code.tries.iter().zip(offs) { + u32le(d, t.start); + u16le(d, t.count); + u16le(d, off); + } + d.extend_from_slice(&list); + } + o + }; + let mut direct = c.direct.clone(); + direct.sort_by_key(|m| m.method); + let mut virtuals = c.virtuals.clone(); + virtuals.sort_by_key(|m| m.method); + let direct_offs: Vec = direct.iter().map(|m| code_off(&mut d, m)).collect(); + let virtual_offs: Vec = virtuals.iter().map(|m| code_off(&mut d, m)).collect(); + let cd = at(&d); + let mut sf = c.static_fields.clone(); + sf.sort(); + let mut inf = c.instance_fields.clone(); + inf.sort(); + uleb(&mut d, sf.len() as u32); + uleb(&mut d, inf.len() as u32); + uleb(&mut d, direct.len() as u32); + uleb(&mut d, virtuals.len() as u32); + for list in [&sf, &inf] { + let mut prev = 0; + for &(f, a) in list.iter() { + uleb(&mut d, f - prev); + uleb(&mut d, a); + prev = f; + } + } + for (list, offs) in [(&direct, &direct_offs), (&virtuals, &virtual_offs)] { + let mut prev = 0; + for (m, &o) in list.iter().zip(offs.iter()) { + uleb(&mut d, m.method - prev); + uleb(&mut d, m.access); + uleb(&mut d, o); + prev = m.method; + } + } + class_data_offs.push(cd); + if c.annotations.is_empty() { + annot_offs.push(0); + } else { + let mut items = Vec::new(); + for &t in &c.annotations { + items.push(at(&d)); + d.push(1); // runtime visibility + uleb(&mut d, t); + uleb(&mut d, 0); + } + align4(&mut d); + let set_off = at(&d); + u32le(&mut d, items.len() as u32); + for i in items { + u32le(&mut d, i); + } + let dir = at(&d); + u32le(&mut d, set_off); + u32le(&mut d, 0); + u32le(&mut d, 0); + u32le(&mut d, 0); + annot_offs.push(dir); + } + } + align4(&mut d); + let file_size = data_start + d.len(); + let mut out = vec![0u8; data_start]; + out[..8].copy_from_slice(b"dex\n035\0"); + let put = |out: &mut Vec, off: usize, v: u32| out[off..off + 4].copy_from_slice(&v.to_le_bytes()); + put(&mut out, 32, file_size as u32); + put(&mut out, 36, 0x70); + put(&mut out, 40, 0x1234_5678); + for (i, (n, o)) in + [(ns, string_ids), (nt, type_ids), (np, proto_ids), (nf, field_ids), (nm, method_ids), (nc, class_defs)].iter().enumerate() + { + put(&mut out, 56 + i * 8, *n as u32); + put(&mut out, 60 + i * 8, if *n == 0 { 0 } else { *o as u32 }); + } + put(&mut out, 104, d.len() as u32); + put(&mut out, 108, data_start as u32); + for (i, o) in string_offs.iter().enumerate() { + put(&mut out, string_ids + i * 4, *o); + } + for (i, t) in self.types.iter().enumerate() { + put(&mut out, type_ids + i * 4, *t); + } + for (i, p) in self.protos.iter().enumerate() { + put(&mut out, proto_ids + i * 12, p.0); + put(&mut out, proto_ids + i * 12 + 4, p.1); + put(&mut out, proto_ids + i * 12 + 8, proto_params[i]); + } + for (i, (c, t, n)) in self.fields.iter().enumerate() { + out[field_ids + i * 8..field_ids + i * 8 + 2].copy_from_slice(&(*c as u16).to_le_bytes()); + out[field_ids + i * 8 + 2..field_ids + i * 8 + 4].copy_from_slice(&(*t as u16).to_le_bytes()); + put(&mut out, field_ids + i * 8 + 4, *n); + } + for (i, (c, p, n)) in self.methods.iter().enumerate() { + out[method_ids + i * 8..method_ids + i * 8 + 2].copy_from_slice(&(*c as u16).to_le_bytes()); + out[method_ids + i * 8 + 2..method_ids + i * 8 + 4].copy_from_slice(&(*p as u16).to_le_bytes()); + put(&mut out, method_ids + i * 8 + 4, *n); + } + for (i, c) in self.classes.iter().enumerate() { + let o = class_defs + i * 32; + for (k, v) in + [c.class, c.access, c.superclass, class_ifaces[i], c.source_file, annot_offs[i], class_data_offs[i], 0].iter().enumerate() + { + put(&mut out, o + k * 4, *v); + } + } + out.extend_from_slice(&d); + let sum = adler32(&out[12..]); + put(&mut out, 8, sum); + out + } + } + + fn adler32(data: &[u8]) -> u32 { + let (mut a, mut b) = (1u32, 0u32); + for &x in data { + a = (a + u32::from(x)) % 65521; + b = (b + a) % 65521; + } + (b << 16) | a + } + + const MAIN: &str = "Lcom/example/notes/MainActivity;"; + const UTIL: &str = "Lcom/example/notes/Util;"; + const STRING: &str = "Ljava/lang/String;"; + + /// `classes.dex` of the Notes app: `MainActivity` (constructor, `onCreate`, `run`, `pick` with a + /// packed switch inside a try/catch) and `Util` (two `log` overloads printing through + /// `System.out`). + pub fn notes_dex() -> Vec { + let mut b = DexBuilder::default(); + let activity = b.ty("Landroid/app/Activity;"); + let main = b.ty(MAIN); + let runnable = b.ty("Ljava/lang/Runnable;"); + let deprecated = b.ty("Ljava/lang/Deprecated;"); + let rte = b.ty("Ljava/lang/RuntimeException;"); + let object = b.ty("Ljava/lang/Object;"); + let util = b.ty(UTIL); + let tag = b.field(MAIN, STRING, "TAG"); + let count = b.field(MAIN, "I", "count"); + let out = b.field("Ljava/lang/System;", "Ljava/io/PrintStream;", "out"); + let super_init = b.method("Landroid/app/Activity;", "", "V", &[]); + let super_create = b.method("Landroid/app/Activity;", "onCreate", "V", &["Landroid/os/Bundle;"]); + let init = b.method(MAIN, "", "V", &[]); + let on_create = b.method(MAIN, "onCreate", "V", &["Landroid/os/Bundle;"]); + let run = b.method(MAIN, "run", "V", &[]); + let pick = b.method(MAIN, "pick", "I", &["I"]); + let log_s = b.method(UTIL, "log", "V", &[STRING]); + let log_i = b.method(UTIL, "log", "V", &["I"]); + let println = b.method("Ljava/io/PrintStream;", "println", "V", &[STRING]); + let hello = b.string(HELLO); + let main_src = b.string("MainActivity.java"); + let util_src = b.string("Util.java"); + let code = |registers, ins, outs, insns: Vec| Some(CodeSpec { registers, ins, outs, insns, tries: vec![] }); + b.classes.push(ClassSpec { + class: main, + access: 0x1, + superclass: activity, + interfaces: vec![runnable], + source_file: main_src, + annotations: vec![deprecated], + static_fields: vec![(tag, 0x1a)], + instance_fields: vec![(count, 0x2)], + direct: vec![MethodSpec { method: init, access: 0x10001, code: code(1, 1, 1, vec![0x1070, super_init as u16, 0x0000, 0x000e]) }], + virtuals: vec![ + MethodSpec { + method: on_create, + access: 0x1, + code: code(3, 2, 2, vec![0x001a, hello as u16, 0x1071, log_s as u16, 0x0000, 0x206f, super_create as u16, 0x0021, 0x000e]), + }, + MethodSpec { method: run, access: 0x1, code: code(1, 1, 1, vec![0x7012, 0x1071, log_i as u16, 0x0000, 0x000e]) }, + MethodSpec { + method: pick, + access: 0x1, + code: Some(CodeSpec { + registers: 3, + ins: 2, + outs: 0, + insns: vec![0x022b, 10, 0, 0x0012, 0x000f, 0x1012, 0x000f, 0x2012, 0x000f, 0x0000, 0x0100, 2, 1, 0, 5, 0, 7, 0], + tries: vec![TrySpec { start: 0, count: 3, handlers: vec![(rte, 3)], catch_all: Some(3) }], + }), + }, + ], + }); + b.classes.push(ClassSpec { + class: util, + access: 0x11, + superclass: object, + source_file: util_src, + direct: vec![ + MethodSpec { method: log_s, access: 0x9, code: code(2, 1, 2, vec![0x0062, out as u16, 0x206e, println as u16, 0x0010, 0x000e]) }, + MethodSpec { method: log_i, access: 0x9, code: code(1, 1, 0, vec![0x000e]) }, + ], + ..ClassSpec::default() + }); + b.build() + } +} diff --git a/crates/testkit/src/sample_damage.rs b/crates/testkit/src/sample_damage.rs index f0bdec729..b2fbeb757 100644 --- a/crates/testkit/src/sample_damage.rs +++ b/crates/testkit/src/sample_damage.rs @@ -72,3 +72,42 @@ mod tests { } } } + +#[cfg(test)] +mod dex_corpus_compatibility_tests { + use super::*; + + #[test] + fn noise_keeps_zero_wrapping_seed_and_draw_order() { + assert_eq!(noise(0, 8), [65, 65, 41, 37, 101, 1, 113, 13]); + assert_eq!(noise(1, 8), [173, 118, 54, 116, 236, 121, 207, 234]); + assert_eq!(noise(u64::MAX, 8), [44, 200, 255, 170, 173, 156, 79, 209]); + assert!(noise(1, 0).is_empty()); + } + + #[test] + fn mutation_order_and_empty_sample_are_stable() { + assert!(mutations(&[]).is_empty()); + assert_eq!(mutations(&[1, 2]), vec![vec![], vec![1], vec![173, 118], vec![255, 2], vec![1, 27], vec![1, 255]]); + } + + #[test] + fn stride_transition_preserves_every_original_case() { + for len in [1, 63, 64, 65, 127, 128, 129, 1024] { + let seed = vec![7; len]; + let step = (len / 64).max(1); + let cases = mutations(&seed); + let positions: Vec = (0..len).step_by(step).collect(); + assert_eq!(cases.len(), positions.len() * 3); + for (case, &end) in cases.iter().zip(&positions) { + assert_eq!(case.as_slice(), &seed[..end]); + } + for (pair, &offset) in cases[positions.len()..].as_chunks::<2>().0.iter().zip(&positions) { + assert_eq!(pair[0].len(), len); + let mut expected = seed.clone(); + expected[offset] = 255; + assert_eq!(pair[1], expected); + } + } + } +}