Skip to main content

iptr_decoder/
utils.rs

1//! Utility functions for dealing with extracted values in PT packets.
2
3use crate::IpReconstructionPattern;
4
5/// Follow the `ip_reconstruction_pattern` to update the `last_ip`.
6///
7/// This function will return `true` if the `last_ip` is updated. When this function
8/// returns false, it means the target of FUP or TIP is out of context, according to
9/// the Intel manual.
10#[expect(
11    clippy::cast_sign_loss,
12    clippy::cast_possible_wrap,
13    clippy::enum_glob_use
14)]
15pub fn reconstruct_ip_and_update_last(
16    last_ip: &mut u64,
17    ip_reconstruction_pattern: IpReconstructionPattern,
18) -> bool {
19    use IpReconstructionPattern::*;
20    let ip = match ip_reconstruction_pattern {
21        OutOfContext => {
22            // `last_ip` is not updated
23            return false;
24        }
25        TwoBytesWithLastIp(payload) => (*last_ip & 0xFFFF_FFFF_FFFF_0000) | (payload as u64),
26        FourBytesWithLastIp(payload) => (*last_ip & 0xFFFF_FFFF_0000_0000) | (payload as u64),
27        SixBytesExtended(payload) => (((payload << 16) as i64) >> 16) as u64,
28        SixBytesWithLastIp(payload) => (*last_ip & 0xFFFF_0000_0000_0000) | (payload as u64),
29        EightBytes(payload) => payload,
30    };
31    *last_ip = ip;
32
33    true
34}