add coded item to generated dex

This commit is contained in:
Jean-Marie Mineau 2023-12-06 10:58:07 +01:00
parent 3468bc0463
commit 53d321c7fe
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
5 changed files with 797 additions and 35 deletions

View file

@ -2,6 +2,7 @@
use crate as androscalpel_serializer;
use crate::{Error, ReadSeek, Result, Serializable, Sleb128, Uleb128};
use log::debug;
use std::io::Write;
/// <https://source.android.com/docs/core/runtime/dex-format#code-item>
@ -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 {
}
/// <https://source.android.com/docs/core/runtime/dex-format#encoded-catch-handler>
#[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 {
}
/// <https://source.android.com/docs/core/runtime/dex-format#encoded-type-addr-pair>
#[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()
);
}
}