mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 16:24:38 +00:00
fix(dex): report partial instruction decoding and encode fixture UTF-16
This commit is contained in:
1 parent
50fbf2bd0c
commit
6a18b99d6f
2 files changed
+53
-5
No files matched your search
+52
-3
@@ -691,7 +691,7 @@ pub struct Reference {
|
||||
const MAX_REFERENCE_BYTES: usize = 64 << 20;
|
||||
|
||||
/// Every instruction that refers to class `desc` (optionally only its methods named `method`).
|
||||
/// Returns a truncated prefix when the count or aggregate caller/target byte budget is exhausted.
|
||||
/// Reports truncation when instruction decoding is incomplete or the count or byte budget is exhausted.
|
||||
pub fn references(dex: &Dex<'_>, desc: &str, method: Option<&str>, max: usize) -> (Vec<Reference>, bool) {
|
||||
references_with_byte_limit(dex, desc, method, max, MAX_REFERENCE_BYTES)
|
||||
}
|
||||
@@ -699,11 +699,13 @@ pub fn references(dex: &Dex<'_>, desc: &str, method: Option<&str>, max: usize) -
|
||||
fn references_with_byte_limit(dex: &Dex<'_>, desc: &str, method: Option<&str>, max: usize, max_bytes: usize) -> (Vec<Reference>, 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);
|
||||
for m in cd.direct_methods.iter().chain(&cd.virtual_methods) {
|
||||
let Some(code) = dex.code(m.code_off) else { continue };
|
||||
let (insns, _) = crate::dex_ops::decode(&code.insns, 200_000);
|
||||
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 {
|
||||
@@ -735,7 +737,7 @@ fn references_with_byte_limit(dex: &Dex<'_>, desc: &str, method: Option<&str>, m
|
||||
}
|
||||
}
|
||||
}
|
||||
(out, false)
|
||||
(out, !complete)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -990,4 +992,51 @@ mod tests {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -176,8 +176,7 @@ pub mod android {
|
||||
for s in &self.strings {
|
||||
string_offs.push(at(&d));
|
||||
uleb(&mut d, s.encode_utf16().count() as u32);
|
||||
for ch in s.chars() {
|
||||
let c = ch as u32;
|
||||
for c in s.encode_utf16() {
|
||||
if c == 0 {
|
||||
d.extend_from_slice(&[0xc0, 0x80]);
|
||||
} else if c < 0x80 {
|
||||
|
||||
Reference in new issue
Block a user