mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 16:24:38 +00:00
Merge pull request #2046 from storytold/feat/dex-reader
Add shared Dalvik DEX reader and bytecode decoder
This commit is contained in:
15 files changed
+2844
No files matched your search
@@ -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.
|
||||
|
||||
Generated
+9
@@ -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"
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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 }
|
||||
@@ -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<u8> {
|
||||
self.data.get(off).copied()
|
||||
}
|
||||
|
||||
fn arr<const N: usize>(&self, off: usize) -> Option<[u8; N]> {
|
||||
self.slice(off, N)?.try_into().ok()
|
||||
}
|
||||
|
||||
pub fn u16_le(&self, off: usize) -> Option<u16> {
|
||||
self.arr(off).map(u16::from_le_bytes)
|
||||
}
|
||||
|
||||
pub fn u32_le(&self, off: usize) -> Option<u32> {
|
||||
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<String> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -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<i32> },
|
||||
SparseSwitch { keys: Vec<i32>, targets: Vec<i32> },
|
||||
FillArrayData { element_width: u16, count: u32, data: Vec<u8> },
|
||||
}
|
||||
|
||||
/// 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<u32>,
|
||||
/// True for `/range` forms.
|
||||
pub range: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub literal: Option<i64>,
|
||||
/// Absolute branch or payload target, in code units.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub target: Option<i64>,
|
||||
pub index_kind: IndexKind,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub index: Option<u32>,
|
||||
/// Second index (the proto of `invoke-polymorphic`).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub index2: Option<u32>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub payload: Option<Payload>,
|
||||
}
|
||||
|
||||
/// 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<u32> {
|
||||
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<Insn> {
|
||||
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<Insn>, 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<Insn>, 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<u16> = 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<u16> = 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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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<u8>) {
|
||||
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<u8>) {
|
||||
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<u8> {
|
||||
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 }));
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
use crate::dex_ops::{self, Payload};
|
||||
|
||||
fn arrays(references: usize, bytes: usize) -> Vec<u16> {
|
||||
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::<Vec<_>>(), [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);
|
||||
}
|
||||
@@ -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 }]);
|
||||
}
|
||||
}
|
||||
@@ -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<u8>, x: u16) {
|
||||
v.extend_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
fn u32le(v: &mut Vec<u8>, x: u32) {
|
||||
v.extend_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
fn uleb(v: &mut Vec<u8>, 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<u8>, 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<u8>) {
|
||||
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<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CodeSpec {
|
||||
pub registers: u16,
|
||||
pub ins: u16,
|
||||
pub outs: u16,
|
||||
pub insns: Vec<u16>,
|
||||
pub tries: Vec<TrySpec>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MethodSpec {
|
||||
pub method: u32,
|
||||
pub access: u32,
|
||||
pub code: Option<CodeSpec>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ClassSpec {
|
||||
pub class: u32,
|
||||
pub access: u32,
|
||||
pub superclass: u32,
|
||||
pub interfaces: Vec<u32>,
|
||||
pub source_file: u32,
|
||||
pub annotations: Vec<u32>,
|
||||
pub static_fields: Vec<(u32, u32)>,
|
||||
pub instance_fields: Vec<(u32, u32)>,
|
||||
pub direct: Vec<MethodSpec>,
|
||||
pub virtuals: Vec<MethodSpec>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct DexBuilder {
|
||||
strings: Vec<String>,
|
||||
types: Vec<u32>,
|
||||
protos: Vec<(u32, u32, Vec<u32>)>,
|
||||
fields: Vec<(u32, u32, u32)>,
|
||||
methods: Vec<(u32, u32, u32)>,
|
||||
pub classes: Vec<ClassSpec>,
|
||||
}
|
||||
|
||||
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<u32> = 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<u8> {
|
||||
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<u8> = Vec::new();
|
||||
let at = |d: &Vec<u8>| (data_start + d.len()) as u32;
|
||||
// type lists
|
||||
let tl = |d: &mut Vec<u8>, 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<u32> = self.protos.iter().map(|p| tl(&mut d, &p.2)).collect();
|
||||
let class_ifaces: Vec<u32> = 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<u8>, 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<u32> = direct.iter().map(|m| code_off(&mut d, m)).collect();
|
||||
let virtual_offs: Vec<u32> = 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<u8>, 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<u8> {
|
||||
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;", "<init>", "V", &[]);
|
||||
let super_create = b.method("Landroid/app/Activity;", "onCreate", "V", &["Landroid/os/Bundle;"]);
|
||||
let init = b.method(MAIN, "<init>", "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<u16>| 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()
|
||||
}
|
||||
}
|
||||
@@ -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<usize> = (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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user