serialize part of the debug info item

This commit is contained in:
Jean-Marie Mineau 2024-02-15 12:15:45 +01:00
parent 6637745cdf
commit 0b8dce9266
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
4 changed files with 32 additions and 13 deletions

View file

@ -35,11 +35,8 @@ impl Apk {
let class = self.get_class_from_dex_file(class, &dex)?;
self.classes.insert(class.descriptor.clone(), class);
}
self.not_referenced_strings.extend(
dex.get_not_resolved_strings()?
.into_iter()
.map(|string| DexString(string)),
);
self.not_referenced_strings
.extend(dex.get_not_resolved_strings()?.into_iter().map(DexString));
Ok(())
}
@ -2203,9 +2200,13 @@ impl Apk {
None
};
let debug_info = if let Some(debug_info) = debug_info {
debug_info.serialize_to_vec()?
let mut cursor = std::io::Cursor::new(vec![]);
debug_info
.bytecode
.serialize(&mut cursor, DbgBytecode::EndSequence)?;
(debug_info.line_start.0, cursor.into_inner())
} else {
vec![]
(0, vec![])
};
let mut labels: HashMap<usize, String> = HashMap::new();
let mut tries = HashMap::new();

View file

@ -33,7 +33,7 @@ pub struct Code {
// TODO: implement
/// The debug info
#[pyo3(get)]
pub debug_info: Vec<u8>, // Should be stripped, copying like this just don't work
pub debug_info: (u32, Vec<u8>), // Should be stripped, copying like this just don't work
/// The names of the parameters if given
#[pyo3(get)]
pub parameter_names: Option<Vec<Option<DexString>>>,
@ -81,7 +81,7 @@ impl Code {
outs_size,
insns,
parameter_names,
debug_info: vec![],
debug_info: (0, vec![]),
}
}

View file

@ -1308,13 +1308,32 @@ impl DexWriter {
try_.handler_off += handlers.size_field().size() as u16;
}
let debug_info_off = if code.debug_info.is_empty() {
let debug_info_off = if code.debug_info.1.is_empty() && code.parameter_names.is_none() {
0
} else {
let debug_info_off = self
.section_manager
.get_aligned_size(Section::DebugInfoItem);
let item = DebugInfoItem::deserialize_from_slice(&code.debug_info)?;
let mut cursor = Cursor::new(code.debug_info.1);
let mut item = DebugInfoItem {
line_start: Uleb128(code.debug_info.0),
parameter_names: vec![],
bytecode: Vec::<DbgBytecode>::deserialize(&mut cursor, DbgBytecode::EndSequence)?,
};
if let Some(parameter_names) = code.parameter_names {
for name in &parameter_names {
if let Some(name) = name {
item.parameter_names
.push(Uleb128p1(*self.strings.get(name).ok_or(anyhow!(
"String {} (name of param of {}) not found",
name.__str__(),
method_id.__repr__()
))? as u32));
} else {
item.parameter_names.push(NO_INDEX);
}
}
}
self.section_manager
.add_elt(Section::DebugInfoItem, Some(item.size()));
self.debug_info_items.push(item);
@ -2600,7 +2619,6 @@ impl DexWriter {
set.serialize(&mut buffer)?;
}
// CodeItem section
println!("Actual code offset: 0x{:x}", buffer.position());
self.check_section_offset(&buffer, Section::CodeItem);
for code_item in &self.code_items {
Self::fix_section_alignement(&mut buffer, Section::CodeItem)?;

View file

@ -15,7 +15,7 @@ pub struct DexFileReader<'a> {
header: HeaderItem,
string_ids: Vec<StringIdItem>,
/// If `string_was_resolved[string_idx]` is true, the string was resolved at some point.
/// This alows us to get the strings that are in a dex file but not used by its
/// This allows us to get the strings that are in a dex file but not used by its
/// classes. (Yes, they are some, looking at you `~~D8{"backend":"dex","compilation-mode":
/// "release","has-checksums":false,"min-api":24,"version":"8.2.42"}`)
string_was_resolved: Vec<AtomicBool>,