From 53d321c7fe07a2bb6ac71d3e07eaed2b557ffcd4 Mon Sep 17 00:00:00 2001 From: Jean-Marie Mineau Date: Wed, 6 Dec 2023 10:58:07 +0100 Subject: [PATCH] add coded item to generated dex --- androscalpel/src/apk.rs | 34 +- androscalpel/src/code.rs | 9 +- androscalpel/src/dex_writer.rs | 149 ++++- androscalpel_serializer/src/core/leb.rs | 7 +- androscalpel_serializer/src/items/code.rs | 633 +++++++++++++++++++++- 5 files changed, 797 insertions(+), 35 deletions(-) diff --git a/androscalpel/src/apk.rs b/androscalpel/src/apk.rs index 4677716..94338c8 100644 --- a/androscalpel/src/apk.rs +++ b/androscalpel/src/apk.rs @@ -693,28 +693,27 @@ impl Apk { handler_off, } in code_item.tries { - tries.push((start_addr, insn_count, handler_off)); - } - let mut handlers_aux = vec![]; - if let Some(EncodedCatchHandlerList { list }) = code_item.handlers { - for EncodedCatchHandler { + let EncodedCatchHandler { handlers, catch_all_addr, - } in list + } = code_item + .handlers + .as_ref() + .ok_or(anyhow!( + "Inconsistant code_item: found try blocks but no handler list" + ))? + .get_handler_at_offset(handler_off)?; + let catch_all_addr = catch_all_addr.map(|Uleb128(val)| val); + let mut handlers_ = vec![]; + for EncodedTypeAddrPair { + type_idx: Uleb128(type_idx), + addr: Uleb128(addr), + } in handlers { - let mut handlers_ = vec![]; - let catch_all_addr = catch_all_addr.map(|Uleb128(val)| val); - for EncodedTypeAddrPair { - type_idx: Uleb128(type_idx), - addr: Uleb128(addr), - } in handlers - { - handlers_.push((Self::get_id_type_from_idx(type_idx as usize, dex)?, addr)) - } - handlers_aux.push((handlers_, catch_all_addr)); + handlers_.push((Self::get_id_type_from_idx(*type_idx as usize, dex)?, *addr)) } + tries.push((start_addr, insn_count, (handlers_, catch_all_addr))); } - let handlers = handlers_aux; Ok(Code { registers_size: code_item.registers_size, ins_size: code_item.ins_size, @@ -722,7 +721,6 @@ impl Apk { debug_info, insns: code_item.insns, tries, - handlers, }) } diff --git a/androscalpel/src/code.rs b/androscalpel/src/code.rs index 3012944..112208d 100644 --- a/androscalpel/src/code.rs +++ b/androscalpel/src/code.rs @@ -38,12 +38,9 @@ pub struct Code { // TODO: maybe implement as custom OPcode to make me easy to modify? /// Try blocks #[pyo3(get, set)] - pub tries: Vec<(u32, u16, u16)>, + pub tries: Vec<(u32, u16, TmpHandlerType)>, // TODO: currently unusable, juste a mapping ty TryItem // TODO: maybe implement as custom OPcode to make me easy to modify? - /// The handlers associated to the tries blocks. - #[pyo3(get, set)] - pub handlers: Vec, } #[pymethods] @@ -66,7 +63,7 @@ impl Code { /// Return all strings referenced in the codes. pub fn get_all_strings(&self) -> HashSet { let mut strings = HashSet::new(); - for (list, _) in &self.handlers { + for (_, _, (list, _)) in &self.tries { for (ty, _) in list { strings.extend(ty.get_all_strings()); } @@ -77,7 +74,7 @@ impl Code { /// Return all types referenced in the codes. pub fn get_all_types(&self) -> HashSet { let mut types = HashSet::new(); - for (list, _) in &self.handlers { + for (_, _, (list, _)) in &self.tries { for (ty, _) in list { types.insert(ty.clone()); } diff --git a/androscalpel/src/dex_writer.rs b/androscalpel/src/dex_writer.rs index f99b3f8..71492c2 100644 --- a/androscalpel/src/dex_writer.rs +++ b/androscalpel/src/dex_writer.rs @@ -74,6 +74,8 @@ pub struct DexWriter { encoded_array_items: Vec, /// The method_handles section. method_handles: Vec, + /// The code_items sections. + code_items: Vec, /// The map_list map_list: MapList, } @@ -127,6 +129,7 @@ impl Default for DexWriter { map_list: MapList::default(), encoded_array_items: vec![], method_handles: vec![], + code_items: vec![], } } } @@ -328,7 +331,103 @@ impl DexWriter { Ok(()) } + /// Insert a code_item. + /// + /// # Warning + /// + /// This is currently a stub that probably serialize invalid references to data. + fn insert_code_item(&mut self, method_id: IdMethod, direct_methods: bool) -> Result<()> { + let code = if direct_methods { + self.class_defs + .get(&method_id.class_) + .unwrap() + .0 + .direct_methods + .get(&method_id) + .unwrap() + .code + .as_ref() + .unwrap() + } else { + self.class_defs + .get(&method_id.class_) + .unwrap() + .0 + .virtual_methods + .get(&method_id) + .unwrap() + .code + .as_ref() + .unwrap() + }; + let mut tries = vec![]; + let mut local_off = 0; + let mut handler_offsets = HashMap::new(); + let mut handler_list = EncodedCatchHandlerList { list: vec![] }; + for (start_addr, insn_count, (list, catch_all_addr)) in code.tries.iter().cloned() { + let mut handlers = vec![]; + for (ty, addr) in list { + let type_idx = Uleb128(*self.type_ids.get(&ty).ok_or(anyhow!( + "Could not found type {} captured by a try block in {} in the dex builder", + ty.__repr__(), + method_id.__repr__() + ))? as u32); + let addr = Uleb128(addr); + handlers.push(EncodedTypeAddrPair { type_idx, addr }); + } + let handler = EncodedCatchHandler { + handlers, + catch_all_addr: catch_all_addr.map(Uleb128), + }; + let handler_off = if let Some(handler_off) = handler_offsets.get(&handler).cloned() { + handler_off + } else { + let old_local_off = local_off; + local_off += handler.size() as u16; + handler_list.list.push(handler.clone()); + handler_offsets.insert(handler, old_local_off); + old_local_off + }; + + tries.push(TryItem { + start_addr, + insn_count, + handler_off, + }); + } + for try_ in tries.iter_mut() { + try_.handler_off += handler_list.size_field().size() as u16; + } + let handlers = if tries.is_empty() { + None + } else { + Some(handler_list) + }; + let item = CodeItem { + registers_size: code.registers_size, + ins_size: code.ins_size, + outs_size: code.outs_size, + debug_info_off: 0, // TODO: code.debug_info + insns: code.insns.clone(), + tries, + handlers, + }; + self.section_manager + .add_elt(Section::CodeItem, Some(item.size())); + self.code_items.push(item); + Ok(()) + } + /// Insert a class_data_item in the class_data section (in data). + /// + /// # Note + /// + /// code_item object are 4 bytes aligns, so code_item_off cannot be odd. + /// + /// To distinguish prelinked value (offset inside the code_item section) to actual values (offset + /// in the whole file or 0), their value is set to the actual value prelink value + 1. This allow + /// to distinguish the offset of the first code item (equal to zero before linking) and the value + /// 0 used to indicate an abscence of code. fn insert_class_data_item(&mut self, class_id: &IdType) -> Result<()> { let mut data = ClassDataItem::default(); let (class, _) = self.class_defs.get(class_id).unwrap(); @@ -379,6 +478,8 @@ impl DexWriter { direct_methods.sort(); let mut last_method_id = 0; for id in &direct_methods { + // &mut vs & of self and class make things difficult... + let (class, _) = self.class_defs.get(class_id).unwrap(); let idx = self.method_ids.get(id).ok_or(anyhow!( "Method {} (method of class {}) not found in dex builder", id.__repr__(), @@ -388,17 +489,27 @@ impl DexWriter { last_method_id = *idx; let access_flags = Uleb128(class.direct_methods.get(id).unwrap().get_raw_access_flags()); + // No if let because ownership gunfooterie + let code_off = if class.direct_methods.get(id).unwrap().code.is_some() { + let code_off = self.section_manager.get_size(Section::CodeItem); + self.insert_code_item(id.clone(), true)?; + Uleb128(code_off + 1) + } else { + Uleb128(0) + }; data.direct_methods.push(EncodedMethod { method_idx_diff, access_flags, - code_off: Uleb128(0), // TODO + code_off, // Will be relinked once the offset of the code item section is known }); } + let (class, _) = self.class_defs.get(class_id).unwrap(); let mut virtual_methods: Vec = class.virtual_methods.keys().cloned().collect(); virtual_methods.sort(); let mut last_method_id = 0; for id in &virtual_methods { + let (class, _) = self.class_defs.get(class_id).unwrap(); let idx = self.method_ids.get(id).ok_or(anyhow!( "Method {} (method of class {}) not found in dex builder", id.__repr__(), @@ -413,10 +524,18 @@ impl DexWriter { .unwrap() .get_raw_access_flags(), ); + // No if let because ownership gunfooterie + let code_off = if class.virtual_methods.get(id).unwrap().code.is_some() { + let code_off = self.section_manager.get_size(Section::CodeItem); + self.insert_code_item(id.clone(), false)?; + Uleb128(code_off + 1) + } else { + Uleb128(0) + }; data.virtual_methods.push(EncodedMethod { method_idx_diff, access_flags, - code_off: Uleb128(0), // TODO + code_off, // Will be relinked once the offset of the code item section is known }); } self.section_manager @@ -866,6 +985,24 @@ impl DexWriter { Ok(()) } + /// Link the offsets of code item in class_data_items. + fn link_code_item(&mut self) -> Result<()> { + debug!("Link the code_item entries in class_data_items"); + for data in &mut self.class_data_list { + for method in &mut data.direct_methods { + if method.code_off.0 != 0 { + method.code_off.0 += self.section_manager.get_offset(Section::CodeItem) - 1; + } + } + for method in &mut data.virtual_methods { + if method.code_off.0 != 0 { + method.code_off.0 += self.section_manager.get_offset(Section::CodeItem) - 1; + } + } + } + Ok(()) + } + fn write_dex_file(&mut self, writer: &mut dyn Write) -> Result<()> { // TODO: SPLIT THIS IN METHODS !!! self.section_manager.reset(); @@ -889,6 +1026,7 @@ impl DexWriter { self.link_type_list_occurences()?; self.link_class_data_occurences()?; self.link_static_values()?; + self.link_code_item()?; debug!("Serialize the dex file"); // TODO: compute checksum, hash, ect @@ -951,7 +1089,12 @@ impl DexWriter { for data in &self.class_data_list { data.serialize(writer)?; } - // TODO: CodeItem, + // CodeItem section + for code_item in &self.code_items { + // TODO: !!! fix the handler stub issue with handler offset that change when the + // type_idx change + code_item.serialize(writer)? + } for string in &self.string_data_list { string.serialize(writer)?; } diff --git a/androscalpel_serializer/src/core/leb.rs b/androscalpel_serializer/src/core/leb.rs index 822b4dc..eab6121 100644 --- a/androscalpel_serializer/src/core/leb.rs +++ b/androscalpel_serializer/src/core/leb.rs @@ -1,19 +1,20 @@ //! The implementation of serializable for LEB128 types. +use std::hash::Hash; use std::io::Write; use crate::{Error, ReadSeek, Result, Serializable}; /// Signed LEB128, variable-length -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Sleb128(pub i32); /// Unsigned LEB128, variable-length -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Uleb128(pub u32); /// Unsigned LEB128 plus 1, variable-length -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Uleb128p1(pub u32); impl Sleb128 { diff --git a/androscalpel_serializer/src/items/code.rs b/androscalpel_serializer/src/items/code.rs index c8afef6..78990a6 100644 --- a/androscalpel_serializer/src/items/code.rs +++ b/androscalpel_serializer/src/items/code.rs @@ -2,6 +2,7 @@ use crate as androscalpel_serializer; use crate::{Error, ReadSeek, Result, Serializable, Sleb128, Uleb128}; +use log::debug; use std::io::Write; /// @@ -50,14 +51,17 @@ impl CodeItem { "try_item in a code_item must be non overlapping and sorted from low to high address".into() )); } - max_addr += addr + (item.insn_count as u32); + max_addr = addr + (item.insn_count as u32); } - // Necessary? no in spec + /* + // Necessary? no in spec and apk in the wild seams to have try blocks spaming outside the + // insns array. if max_addr > self.insns.len() as u32 { return Err(Error::InconsistantStruct( "found try_item whose block span outside of the insns array".into(), )); } + */ for try_ in &self.tries { let handler = self @@ -196,7 +200,7 @@ impl EncodedCatchHandlerList { /// the value refered to by [`crate::TryItem`]`.handler_off`. pub fn get_handler_at_offset(&self, offset: u16) -> Result<&EncodedCatchHandler> { let offset = offset as usize; - let mut current_offset = 0; + let mut current_offset = self.size_field().size(); for handler in &self.list { if current_offset == offset { return Ok(handler); @@ -206,6 +210,16 @@ impl EncodedCatchHandlerList { break; } } + let mut current_offset = self.size_field().size(); + for handler in &self.list { + debug!( + "{:#?}: size {} at offset {}", + handler, + handler.size(), + current_offset + ); + current_offset += handler.size(); + } Err(Error::InconsistantStruct(format!( "Offset 0x{offset:x} does not match with the begining of a EncodedCatchHandler in this EncodedCatchHandlerList" ))) @@ -239,7 +253,7 @@ impl Serializable for EncodedCatchHandlerList { } /// -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct EncodedCatchHandler { // pub size: Sleb128, /// List of handler, one by type caught, in the order of the type tests @@ -314,7 +328,7 @@ impl Serializable for EncodedCatchHandler { } /// -#[derive(Serializable, Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Serializable, Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct EncodedTypeAddrPair { /// Index of the [`crate::TypeIdItem`] in `type_ids` pub type_idx: Uleb128, @@ -333,8 +347,61 @@ mod test { 0x05, 0x21, 0x00, 0x0a, 0x01, 0x38, 0x01, 0x03, 0x00, 0x0e, 0x00, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x01, 0x01, 0xe9, 0x46, 0x06, ]; + const CODE_ITEM_RAW_2: &[u8] = &[ + 0x09, 0x00, 0x04, 0x00, 0x03, 0x00, 0x07, 0x00, 0xd7, 0x28, 0x4f, 0x00, 0x19, 0x01, 0x00, + 0x00, 0x70, 0x10, 0x8b, 0x84, 0x05, 0x00, 0x22, 0x00, 0x2d, 0x24, 0x70, 0x10, 0x51, 0x87, + 0x00, 0x00, 0x5b, 0x50, 0x54, 0x06, 0x5b, 0x57, 0x58, 0x06, 0x22, 0x00, 0x84, 0x05, 0x70, + 0x10, 0x09, 0x1c, 0x00, 0x00, 0x5b, 0x50, 0x56, 0x06, 0x54, 0x77, 0xcc, 0x09, 0x71, 0x20, + 0xed, 0x18, 0x76, 0x00, 0x0c, 0x07, 0x5b, 0x57, 0x55, 0x06, 0x71, 0x10, 0xb9, 0x17, 0x06, + 0x00, 0x0c, 0x06, 0x5b, 0x56, 0x57, 0x06, 0x22, 0x06, 0x1e, 0x24, 0x70, 0x10, 0xa1, 0x86, + 0x06, 0x00, 0x54, 0x70, 0x01, 0x08, 0x1f, 0x00, 0x7a, 0x04, 0x6e, 0x10, 0x8e, 0x84, 0x00, + 0x00, 0x54, 0x00, 0x04, 0x08, 0x6e, 0x10, 0x2a, 0x04, 0x00, 0x00, 0x0c, 0x00, 0x71, 0x10, + 0xbc, 0x86, 0x00, 0x00, 0x0c, 0x00, 0x39, 0x08, 0x16, 0x00, 0x72, 0x10, 0x8d, 0x87, 0x00, + 0x00, 0x0c, 0x07, 0x72, 0x10, 0x6c, 0x87, 0x07, 0x00, 0x0a, 0x08, 0x38, 0x08, 0x58, 0x00, + 0x72, 0x10, 0x6d, 0x87, 0x07, 0x00, 0x0c, 0x08, 0x1f, 0x08, 0x87, 0x23, 0x6e, 0x20, 0xa5, + 0x86, 0x86, 0x00, 0x28, 0xf1, 0x6e, 0x10, 0xf3, 0x19, 0x08, 0x00, 0x0c, 0x01, 0x71, 0x30, + 0x73, 0x17, 0x17, 0x00, 0x0c, 0x07, 0x28, 0x02, 0x12, 0x07, 0x22, 0x01, 0x1e, 0x24, 0x70, + 0x10, 0xa1, 0x86, 0x01, 0x00, 0x72, 0x10, 0x8d, 0x87, 0x00, 0x00, 0x0c, 0x00, 0x72, 0x10, + 0x6c, 0x87, 0x00, 0x00, 0x0a, 0x02, 0x38, 0x02, 0x17, 0x00, 0x72, 0x10, 0x6d, 0x87, 0x00, + 0x00, 0x0c, 0x02, 0x1f, 0x02, 0x87, 0x23, 0x6e, 0x20, 0xb5, 0x84, 0x72, 0x00, 0x0a, 0x03, + 0x38, 0x03, 0x03, 0x00, 0x28, 0xee, 0x6e, 0x20, 0x8f, 0x16, 0x25, 0x00, 0x0c, 0x02, 0x6e, + 0x20, 0xa5, 0x86, 0x21, 0x00, 0x28, 0xe6, 0x6e, 0x20, 0xf2, 0x19, 0x18, 0x00, 0x0c, 0x07, + 0x72, 0x10, 0x8d, 0x87, 0x07, 0x00, 0x0c, 0x07, 0x72, 0x10, 0x6c, 0x87, 0x07, 0x00, 0x0a, + 0x08, 0x38, 0x08, 0x12, 0x00, 0x72, 0x10, 0x6d, 0x87, 0x07, 0x00, 0x0c, 0x08, 0x1f, 0x08, + 0xd4, 0x04, 0x1f, 0x08, 0x7d, 0x05, 0x72, 0x10, 0xf3, 0x1b, 0x08, 0x00, 0x0c, 0x08, 0x6e, + 0x20, 0xa5, 0x86, 0x86, 0x00, 0x28, 0xeb, 0x22, 0x07, 0x1e, 0x24, 0x70, 0x10, 0xa1, 0x86, + 0x07, 0x00, 0x6e, 0x10, 0xaf, 0x86, 0x06, 0x00, 0x0c, 0x06, 0x72, 0x10, 0x6c, 0x87, 0x06, + 0x00, 0x0a, 0x08, 0x38, 0x08, 0x57, 0x00, 0x72, 0x10, 0x6d, 0x87, 0x06, 0x00, 0x0c, 0x08, + 0x1f, 0x08, 0x87, 0x23, 0x1a, 0x00, 0xb5, 0x0d, 0x6e, 0x20, 0xb5, 0x84, 0x08, 0x00, 0x0a, + 0x00, 0x39, 0x00, 0x45, 0x00, 0x1a, 0x00, 0xd0, 0x0d, 0x6e, 0x20, 0xb5, 0x84, 0x08, 0x00, + 0x0a, 0x00, 0x38, 0x00, 0x03, 0x00, 0x28, 0x3b, 0x62, 0x00, 0xe2, 0x00, 0x1a, 0x01, 0x49, + 0xc0, 0x6e, 0x20, 0xb5, 0x84, 0x01, 0x00, 0x0a, 0x00, 0x38, 0x00, 0x03, 0x00, 0x28, 0x1a, + 0x54, 0x50, 0x55, 0x06, 0x6e, 0x20, 0xee, 0x18, 0x80, 0x00, 0x0c, 0x00, 0x62, 0x01, 0xb6, + 0x00, 0x6e, 0x20, 0xc4, 0x18, 0x10, 0x00, 0x0c, 0x00, 0x1f, 0x00, 0x9c, 0x2e, 0x12, 0x01, + 0x38, 0x00, 0x0f, 0x00, 0x21, 0x02, 0x01, 0x13, 0x35, 0x23, 0x0b, 0x00, 0x44, 0x04, 0x00, + 0x03, 0x39, 0x04, 0x04, 0x00, 0x12, 0x11, 0x28, 0x04, 0xd8, 0x03, 0x03, 0x01, 0x28, 0xf6, + 0x38, 0x01, 0xba, 0xff, 0x6e, 0x20, 0xa5, 0x86, 0x87, 0x00, 0x28, 0xb5, 0x0d, 0x06, 0x22, + 0x07, 0x1e, 0x05, 0x71, 0x10, 0xff, 0x48, 0x06, 0x00, 0x0c, 0x06, 0x70, 0x20, 0xc2, 0x1a, + 0x67, 0x00, 0x27, 0x07, 0x6e, 0x20, 0xa5, 0x86, 0x87, 0x00, 0x28, 0xa6, 0x5b, 0x57, 0x53, + 0x06, 0x0e, 0x00, 0x0d, 0x06, 0x22, 0x07, 0x47, 0x04, 0x70, 0x20, 0x84, 0x18, 0x67, 0x00, + 0x27, 0x07, 0x0d, 0x06, 0x22, 0x07, 0x1e, 0x05, 0x70, 0x20, 0xc2, 0x1a, 0x67, 0x00, 0x27, + 0x07, 0x0d, 0x06, 0x22, 0x07, 0x1e, 0x05, 0x71, 0x10, 0xff, 0x48, 0x06, 0x00, 0x0c, 0x06, + 0x70, 0x20, 0xc2, 0x1a, 0x67, 0x00, 0x27, 0x07, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x0c, + 0x00, 0x01, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x06, 0x00, 0x0a, 0x00, 0x33, 0x00, 0x00, 0x00, + 0x19, 0x00, 0x01, 0x00, 0x4d, 0x00, 0x00, 0x00, 0x08, 0x00, 0x17, 0x00, 0x57, 0x00, 0x00, + 0x00, 0x41, 0x00, 0x01, 0x00, 0xca, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x23, 0x00, 0x01, 0x01, + 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x04, 0x02, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, + 0x02, 0x03, 0xb4, 0x01, 0x80, 0x02, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02, 0x03, + 0xe9, 0x46, 0x56, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02, 0x01, 0xc7, 0x08, 0xee, + 0x01, + ]; const ENCODED_CATCH_HANDLER_LIST_1: &[u8] = &[0x01, 0x01, 0xe9, 0x46, 0x06]; + const ENCODED_CATCH_HANDLER_LIST_2: &[u8] = &[ + 0x04, 0x02, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02, 0x03, 0xb4, 0x01, 0x80, 0x02, + 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02, 0x03, 0xe9, 0x46, 0x56, 0xc7, 0x08, 0x8e, + 0x02, 0xd9, 0x09, 0x87, 0x02, 0x01, 0xc7, 0x08, 0xee, 0x01, + ]; #[test] fn test_deserialize_code_item() { @@ -365,6 +432,141 @@ mod test { }) } ); + assert_eq!( + CodeItem::deserialize_from_slice(CODE_ITEM_RAW_2).unwrap(), + CodeItem { + registers_size: 9, + ins_size: 4, + outs_size: 3, + debug_info_off: 5187799, + insns: vec![ + 0x1070, 0x848b, 0x0005, 0x0022, 0x242d, 0x1070, 0x8751, 0x0000, 0x505b, 0x0654, + 0x575b, 0x0658, 0x0022, 0x0584, 0x1070, 0x1c09, 0x0000, 0x505b, 0x0656, 0x7754, + 0x09cc, 0x2071, 0x18ed, 0x0076, 0x070c, 0x575b, 0x0655, 0x1071, 0x17b9, 0x0006, + 0x060c, 0x565b, 0x0657, 0x0622, 0x241e, 0x1070, 0x86a1, 0x0006, 0x7054, 0x0801, + 0x001f, 0x047a, 0x106e, 0x848e, 0x0000, 0x0054, 0x0804, 0x106e, 0x042a, 0x0000, + 0x000c, 0x1071, 0x86bc, 0x0000, 0x000c, 0x0839, 0x0016, 0x1072, 0x878d, 0x0000, + 0x070c, 0x1072, 0x876c, 0x0007, 0x080a, 0x0838, 0x0058, 0x1072, 0x876d, 0x0007, + 0x080c, 0x081f, 0x2387, 0x206e, 0x86a5, 0x0086, 0xf128, 0x106e, 0x19f3, 0x0008, + 0x010c, 0x3071, 0x1773, 0x0017, 0x070c, 0x0228, 0x0712, 0x0122, 0x241e, 0x1070, + 0x86a1, 0x0001, 0x1072, 0x878d, 0x0000, 0x000c, 0x1072, 0x876c, 0x0000, 0x020a, + 0x0238, 0x0017, 0x1072, 0x876d, 0x0000, 0x020c, 0x021f, 0x2387, 0x206e, 0x84b5, + 0x0072, 0x030a, 0x0338, 0x0003, 0xee28, 0x206e, 0x168f, 0x0025, 0x020c, 0x206e, + 0x86a5, 0x0021, 0xe628, 0x206e, 0x19f2, 0x0018, 0x070c, 0x1072, 0x878d, 0x0007, + 0x070c, 0x1072, 0x876c, 0x0007, 0x080a, 0x0838, 0x0012, 0x1072, 0x876d, 0x0007, + 0x080c, 0x081f, 0x04d4, 0x081f, 0x057d, 0x1072, 0x1bf3, 0x0008, 0x080c, 0x206e, + 0x86a5, 0x0086, 0xeb28, 0x0722, 0x241e, 0x1070, 0x86a1, 0x0007, 0x106e, 0x86af, + 0x0006, 0x060c, 0x1072, 0x876c, 0x0006, 0x080a, 0x0838, 0x0057, 0x1072, 0x876d, + 0x0006, 0x080c, 0x081f, 0x2387, 0x001a, 0x0db5, 0x206e, 0x84b5, 0x0008, 0x000a, + 0x0039, 0x0045, 0x001a, 0x0dd0, 0x206e, 0x84b5, 0x0008, 0x000a, 0x0038, 0x0003, + 0x3b28, 0x0062, 0x00e2, 0x011a, 0xc049, 0x206e, 0x84b5, 0x0001, 0x000a, 0x0038, + 0x0003, 0x1a28, 0x5054, 0x0655, 0x206e, 0x18ee, 0x0080, 0x000c, 0x0162, 0x00b6, + 0x206e, 0x18c4, 0x0010, 0x000c, 0x001f, 0x2e9c, 0x0112, 0x0038, 0x000f, 0x0221, + 0x1301, 0x2335, 0x000b, 0x0444, 0x0300, 0x0439, 0x0004, 0x1112, 0x0428, 0x03d8, + 0x0103, 0xf628, 0x0138, 0xffba, 0x206e, 0x86a5, 0x0087, 0xb528, 0x060d, 0x0722, + 0x051e, 0x1071, 0x48ff, 0x0006, 0x060c, 0x2070, 0x1ac2, 0x0067, 0x0727, 0x206e, + 0x86a5, 0x0087, 0xa628, 0x575b, 0x0653, 0x000e, 0x060d, 0x0722, 0x0447, 0x2070, + 0x1884, 0x0067, 0x0727, 0x060d, 0x0722, 0x051e, 0x2070, 0x1ac2, 0x0067, 0x0727, + 0x060d, 0x0722, 0x051e, 0x1071, 0x48ff, 0x0006, 0x060c, 0x2070, 0x1ac2, 0x0067, + 0x0727, + ], + tries: vec![ + TryItem { + start_addr: 33, + insn_count: 12, + handler_off: 1 + }, + TryItem { + start_addr: 45, + insn_count: 6, + handler_off: 10 + }, + TryItem { + start_addr: 51, + insn_count: 25, + handler_off: 1 + }, + TryItem { + start_addr: 77, + insn_count: 8, + handler_off: 23 + }, + TryItem { + start_addr: 87, + insn_count: 65, + handler_off: 1 + }, + TryItem { + start_addr: 202, + insn_count: 14, + handler_off: 35 + }, + TryItem { + start_addr: 257, + insn_count: 6, + handler_off: 1 + }, + ], + handlers: Some(EncodedCatchHandlerList { + list: vec![ + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(180), + addr: Uleb128(0x100), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(9065), + addr: Uleb128(0x56), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0xee), + }], + catch_all_addr: None, + }, + ], + }), + } + ); } #[test] @@ -381,5 +583,426 @@ mod test { }] } ); + + assert_eq!( + EncodedCatchHandlerList::deserialize_from_slice(ENCODED_CATCH_HANDLER_LIST_2).unwrap(), + EncodedCatchHandlerList { + list: vec![ + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(180), + addr: Uleb128(0x100), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(9065), + addr: Uleb128(0x56), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0xee), + }], + catch_all_addr: None, + }, + ], + } + ); + } + + #[test] + fn test_get_handler_at_offset() { + let list = EncodedCatchHandlerList { + list: vec![ + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(180), + addr: Uleb128(0x100), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(9065), + addr: Uleb128(0x56), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler { + handlers: vec![EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0xee), + }], + catch_all_addr: None, + }, + ], + }; + assert_eq!( + list.get_handler_at_offset(1).unwrap(), + &EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + } + ); + assert_eq!( + list.get_handler_at_offset(10).unwrap(), + &EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(180), + addr: Uleb128(0x100), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + ); + assert_eq!( + list.get_handler_at_offset(23).unwrap(), + &EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(9065), + addr: Uleb128(0x56), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + ); + assert_eq!( + list.get_handler_at_offset(35).unwrap(), + &EncodedCatchHandler { + handlers: vec![EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0xee), + }], + catch_all_addr: None, + }, + ); + } + + #[test] + fn test_encoded_catch_handler_size() { + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + } + .size(), + 9 + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(180), + addr: Uleb128(0x100), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + } + .size(), + 13 + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(9065), + addr: Uleb128(0x56), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + } + .size(), + 12 + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0xee), + },], + catch_all_addr: None, + } + .size(), + 5 + ); + } + + #[test] + fn test_encoded_catch_handler_serialize() { + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + } + .serialize_to_vec() + .unwrap(), + vec![0x02, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02,], + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(180), + addr: Uleb128(0x100), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + } + .serialize_to_vec() + .unwrap(), + vec![0x03, 0xb4, 0x01, 0x80, 0x02, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02,] + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(9065), + addr: Uleb128(0x56), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + } + .serialize_to_vec() + .unwrap(), + vec![0x03, 0xe9, 0x46, 0x56, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02,] + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0xee), + },], + catch_all_addr: None, + } + .serialize_to_vec() + .unwrap(), + vec![0x01, 0xc7, 0x08, 0xee, 0x01,] + ); + } + + #[test] + fn test_encoded_catch_handler_deserialize() { + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler::deserialize_from_slice(&[ + 0x02, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02, + ]) + .unwrap() + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(180), + addr: Uleb128(0x100), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler::deserialize_from_slice(&[ + 0x03, 0xb4, 0x01, 0x80, 0x02, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02, + ]) + .unwrap() + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![ + EncodedTypeAddrPair { + type_idx: Uleb128(9065), + addr: Uleb128(0x56), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0x10e), + }, + EncodedTypeAddrPair { + type_idx: Uleb128(1241), + addr: Uleb128(0x107), + }, + ], + catch_all_addr: None, + }, + EncodedCatchHandler::deserialize_from_slice(&[ + 0x03, 0xe9, 0x46, 0x56, 0xc7, 0x08, 0x8e, 0x02, 0xd9, 0x09, 0x87, 0x02, + ]) + .unwrap() + ); + assert_eq!( + EncodedCatchHandler { + handlers: vec![EncodedTypeAddrPair { + type_idx: Uleb128(1095), + addr: Uleb128(0xee), + },], + catch_all_addr: None, + }, + EncodedCatchHandler::deserialize_from_slice(&[0x01, 0xc7, 0x08, 0xee, 0x01,]).unwrap() + ); } }