Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ edition = "2021"
uuid = ["dep:uuid"]

[dependencies]
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
thiserror = "1.0.61"
uuid = { version = "1.0.0", features = ["v5"], optional = true }
watto = { version = "0.1.0", features = ["writer", "strings"] }
Expand Down
2 changes: 1 addition & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct CacheError {
}

impl CacheError {
/// Returns the corresponding [`ErrorKind`] for this error.
/// Returns the corresponding [`CacheErrorKind`] for this error.
pub fn kind(&self) -> CacheErrorKind {
self.kind
}
Expand Down
13 changes: 5 additions & 8 deletions src/cache/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::Write;

use watto::{Pod, StringTable};

use crate::mapping::R8Header;
use crate::{ProguardMapping, ProguardRecord};

use super::{CacheError, CacheErrorKind};
Expand Down Expand Up @@ -194,15 +195,11 @@ impl<'data> ProguardCache<'data> {
let mut records = mapping.iter().filter_map(Result::ok).peekable();
while let Some(record) = records.next() {
match record {
ProguardRecord::Header {
key,
value: Some(file_name),
} => {
if key == "sourceFile" {
current_class.class.file_name_offset =
string_table.insert(file_name) as u32;
}
ProguardRecord::R8Header(R8Header::SourceFile { file_name }) => {
current_class.class.file_name_offset = string_table.insert(file_name) as u32;
}
ProguardRecord::Header { .. } => {}
ProguardRecord::R8Header(R8Header::Other) => {}
ProguardRecord::Class {
original,
obfuscated,
Expand Down
9 changes: 5 additions & 4 deletions src/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt::{Error as FmtError, Write};
use std::iter::FusedIterator;

use crate::java;
use crate::mapping::R8Header;
use crate::mapping::{ProguardMapping, ProguardRecord};
use crate::stacktrace::{self, StackFrame, StackTrace, Throwable};

Expand Down Expand Up @@ -236,11 +237,11 @@ impl<'s> ProguardMapper<'s> {
let mut records = mapping.iter().filter_map(Result::ok).peekable();
while let Some(record) = records.next() {
match record {
ProguardRecord::Header { key, value } => {
if key == "sourceFile" {
class.file_name = value;
}
ProguardRecord::R8Header(R8Header::SourceFile { file_name }) => {
class.file_name = Some(file_name);
}
ProguardRecord::Header { .. } => {}
ProguardRecord::R8Header(R8Header::Other) => {}
ProguardRecord::Class {
original,
obfuscated,
Expand Down
112 changes: 80 additions & 32 deletions src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::fmt;
use std::ops::Range;
use std::str;

use serde::Deserialize;

#[cfg(feature = "uuid")]
use uuid::Uuid;

Expand Down Expand Up @@ -283,7 +285,7 @@ impl<'s> Iterator for ProguardRecordIter<'s> {
/// Maps start/end lines of a minified file to original start/end lines.
///
/// All line mappings are 1-based and inclusive.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LineMapping {
/// Start Line, 1-based.
pub startline: usize,
Expand All @@ -295,8 +297,26 @@ pub struct LineMapping {
pub original_endline: Option<usize>,
}

/// An R8 header, as described in
/// <https://r8.googlesource.com/r8/+/refs/heads/main/doc/retrace.md#additional-information-appended-as-comments-to-the-file>.
///
/// The format is a line starting with `#` and followed by a JSON object.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(tag = "id", rename_all = "camelCase")]
pub enum R8Header<'s> {
/// A source file header, stating what source file a class originated from.
///
/// See <https://r8.googlesource.com/r8/+/refs/heads/main/doc/retrace.md#source-file>.
#[serde(rename_all = "camelCase")]
SourceFile { file_name: &'s str },

/// Catchall variant for headers we don't support.
#[serde(other)]
Other,
}

/// A Proguard Mapping Record.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ProguardRecord<'s> {
/// A Proguard Header.
Header {
Expand All @@ -305,6 +325,8 @@ pub enum ProguardRecord<'s> {
/// Optional value if the Header is a KV pair.
value: Option<&'s str>,
},
/// An R8 Header.
R8Header(R8Header<'s>),
/// A Class Mapping.
Class {
/// Original name of the class.
Expand Down Expand Up @@ -437,8 +459,15 @@ impl<'s> ProguardRecord<'s> {
fn parse_proguard_record(bytes: &[u8]) -> (Result<ProguardRecord, ParseError>, &[u8]) {
let bytes = consume_leading_newlines(bytes);

let result = if bytes.starts_with(b"#") {
parse_proguard_header(bytes)
let result = if let Some(bytes) = bytes.trim_ascii_start().strip_prefix(b"#") {
// ProGuard / R8 headers

let bytes = bytes.trim_ascii_start();
if bytes.starts_with(b"{") {
parse_r8_header(bytes)
} else {
parse_proguard_header(bytes)
}
} else if bytes.starts_with(b" ") {
parse_proguard_field_or_method(bytes)
} else {
Expand All @@ -460,38 +489,36 @@ fn parse_proguard_record(bytes: &[u8]) -> (Result<ProguardRecord, ParseError>, &
}
}

const SOURCE_FILE_PREFIX: &[u8; 32] = br#" {"id":"sourceFile","fileName":""#;

/// Parses a single Proguard Header from a Proguard File.
fn parse_proguard_header(bytes: &[u8]) -> Result<(ProguardRecord, &[u8]), ParseError> {
let bytes = parse_prefix(bytes, b"#")?;
// Note: the leading `#` has already been parsed.

if let Ok(bytes) = parse_prefix(bytes, SOURCE_FILE_PREFIX) {
let (value, bytes) = parse_until(bytes, |c| *c == b'"')?;
let bytes = parse_prefix(bytes, br#""}"#)?;
// Existing logic for `key: value` format
let (key, bytes) = parse_until(bytes, |c| *c == b':' || is_newline(c))?;

let record = ProguardRecord::Header {
key: "sourceFile",
value: Some(value),
};
let (value, bytes) = match parse_prefix(bytes, b":") {
Ok(bytes) => parse_until(bytes, is_newline).map(|(v, bytes)| (Some(v), bytes)),
Err(_) => Ok((None, bytes)),
}?;

Ok((record, consume_leading_newlines(bytes)))
} else {
// Existing logic for `key: value` format
let (key, bytes) = parse_until(bytes, |c| *c == b':' || is_newline(c))?;
let record = ProguardRecord::Header {
key: key.trim(),
value: value.map(|v| v.trim()),
};

let (value, bytes) = match parse_prefix(bytes, b":") {
Ok(bytes) => parse_until(bytes, is_newline).map(|(v, bytes)| (Some(v), bytes)),
Err(_) => Ok((None, bytes)),
}?;
Ok((record, consume_leading_newlines(bytes)))
}

let record = ProguardRecord::Header {
key: key.trim(),
value: value.map(|v| v.trim()),
};
fn parse_r8_header(bytes: &[u8]) -> Result<(ProguardRecord, &[u8]), ParseError> {
// Note: the leading `#` has already been parsed.

Ok((record, consume_leading_newlines(bytes)))
}
let (header, rest) = parse_until(bytes, is_newline)?;

let header = serde_json::from_str(header).unwrap();
Ok((
ProguardRecord::R8Header(header),
consume_leading_newlines(rest),
))
}

/// Parses a single Proguard Field or Method from a Proguard File.
Expand Down Expand Up @@ -764,10 +791,29 @@ mod tests {
let parsed = ProguardRecord::try_parse(bytes);
assert_eq!(
parsed,
Ok(ProguardRecord::Header {
key: "sourceFile",
value: Some("Foobar.kt")
})
Ok(ProguardRecord::R8Header(R8Header::SourceFile {
file_name: "Foobar.kt",
}))
);
}

#[test]
fn try_parse_r8_headers() {
let bytes = br#"# {"id":"foobar"}"#;
assert_eq!(
ProguardRecord::try_parse(bytes).unwrap(),
ProguardRecord::R8Header(R8Header::Other),
);

let bytes = br#" #{"id":"foobar"}"#;
assert_eq!(
ProguardRecord::try_parse(bytes).unwrap(),
ProguardRecord::R8Header(R8Header::Other),
);
let bytes = br#"# {"id":"foobar"}"#;
assert_eq!(
ProguardRecord::try_parse(bytes).unwrap(),
ProguardRecord::R8Header(R8Header::Other),
);
}

Expand Down Expand Up @@ -1005,6 +1051,7 @@ androidx.activity.OnBackPressedCallback -> c.a.b:
boolean mEnabled -> a
java.util.ArrayDeque mOnBackPressedCallbacks -> b
1:4:void onBackPressed():184:187 -> c
# {\"id\":\"com.android.tools.r8.synthesized\"}
androidx.activity.OnBackPressedCallback
-> c.a.b:
";
Expand Down Expand Up @@ -1057,6 +1104,7 @@ androidx.activity.OnBackPressedCallback
original_endline: Some(187),
}),
}),
Ok(ProguardRecord::R8Header(R8Header::Other)),
Err(ParseError {
line: b"androidx.activity.OnBackPressedCallback \n",
kind: ParseErrorKind::ParseError("line is not a valid proguard record"),
Expand Down
Loading