add a rought implem of code

This commit is contained in:
Jean-Marie Mineau 2023-11-30 14:40:49 +01:00
parent 026b9ddd41
commit 80968c9bcf
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
7 changed files with 198 additions and 13 deletions

View file

@ -174,8 +174,8 @@ pub struct TryItem {
pub start_addr: u32,
/// Number of 16-bit code unit covered by the entry.
pub insn_count: u16,
/// **Offset in bytes** from the start of the `EncodedCatchHandlerList` to the
/// `EncodedCatchHandler` associated.
/// **Offset in bytes** from the start of the [`crate::EncodedCatchHandlerList`] to the
/// [`crate::EncodedCatchHandler`] associated.
pub handler_off: u16,
}
@ -193,7 +193,7 @@ impl EncodedCatchHandlerList {
/// Return a reference to the [`crate::EncodedCatchHandler`] located at `offset` bytes after
/// the begining of the [`crate::EncodedCatchHandlerList`]. Expected to be used to lookup
/// the value refered to by [`crate::TryItem.handler_off`].
/// 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;
@ -222,7 +222,7 @@ impl Serializable for EncodedCatchHandlerList {
}
fn deserialize(input: &mut dyn ReadSeek) -> Result<Self> {
let size = i32::deserialize(input)?;
let Uleb128(size) = Uleb128::deserialize(input)?;
let mut list = vec![];
for _ in 0..size {
list.push(EncodedCatchHandler::deserialize(input)?);
@ -321,3 +321,65 @@ pub struct EncodedTypeAddrPair {
/// Bytecode address of the exception handler
pub addr: Uleb128,
}
#[cfg(test)]
mod test {
use super::*;
const CODE_ITEM_RAW_1: &[u8] = &[
0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x3d, 0x25, 0x4f, 0x00, 0x15, 0x00, 0x00,
0x00, 0x54, 0x30, 0x6c, 0x01, 0x71, 0x10, 0xb1, 0x0c, 0x00, 0x00, 0x28, 0x0e, 0x0d, 0x00,
0x6e, 0x10, 0x26, 0x85, 0x00, 0x00, 0x0c, 0x01, 0x1a, 0x02, 0xcb, 0x15, 0x71, 0x20, 0xe3,
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 ENCODED_CATCH_HANDLER_LIST_1: &[u8] = &[0x01, 0x01, 0xe9, 0x46, 0x06];
#[test]
fn test_deserialize_code_item() {
assert_eq!(
CodeItem::deserialize_from_slice(CODE_ITEM_RAW_1).unwrap(),
CodeItem {
registers_size: 4,
ins_size: 1,
outs_size: 2,
debug_info_off: 5186877,
insns: vec![
0x3054, 0x16c, 0x1071, 0xcb1, 0x0, 0xe28, 0xd, 0x106e, 0x8526, 0x0, 0x10c,
0x21a, 0x15cb, 0x2071, 0x5e3, 0x21, 0x10a, 0x138, 0x3, 0xe, 0x27
],
tries: vec![TryItem {
start_addr: 0,
insn_count: 5,
handler_off: 1,
},],
handlers: Some(EncodedCatchHandlerList {
list: vec![EncodedCatchHandler {
handlers: vec![EncodedTypeAddrPair {
type_idx: Uleb128(9065),
addr: Uleb128(6)
}],
catch_all_addr: None,
}]
})
}
);
}
#[test]
fn test_deserialize_catch_handler_list() {
assert_eq!(
EncodedCatchHandlerList::deserialize_from_slice(ENCODED_CATCH_HANDLER_LIST_1).unwrap(),
EncodedCatchHandlerList {
list: vec![EncodedCatchHandler {
handlers: vec![EncodedTypeAddrPair {
type_idx: Uleb128(9065),
addr: Uleb128(6)
}],
catch_all_addr: None,
}]
}
);
}
}