|
| 1 | +use std::{ |
| 2 | + fmt::Debug, |
| 3 | + ops::{Range, RangeInclusive}, |
| 4 | +}; |
| 5 | + |
| 6 | +use libafl::{HasMetadata, observers::ObserversTuple}; |
| 7 | +pub use libafl_intelpt::SectionInfo; |
| 8 | +use libafl_intelpt::{Image, IntelPT, IntelPTBuilder}; |
| 9 | +use libafl_qemu_sys::{CPUArchStatePtr, GuestAddr}; |
| 10 | +use num_traits::SaturatingAdd; |
| 11 | +use typed_builder::TypedBuilder; |
| 12 | + |
| 13 | +use crate::{ |
| 14 | + EmulatorModules, NewThreadHook, Qemu, QemuParams, |
| 15 | + modules::{AddressFilter, EmulatorModule, EmulatorModuleTuple, ExitKind}, |
| 16 | +}; |
| 17 | + |
| 18 | +#[derive(Debug, TypedBuilder)] |
| 19 | +pub struct IntelPTModule<T = u8> { |
| 20 | + #[builder(setter(skip), default)] |
| 21 | + pt: Option<IntelPT>, |
| 22 | + #[builder(default = IntelPTModule::default_pt_builder())] |
| 23 | + intel_pt_builder: IntelPTBuilder, |
| 24 | + #[builder(setter(transform = |sections: &[SectionInfo]| { |
| 25 | + let mut i = Image::new(None).unwrap(); |
| 26 | + i.add_files_cached(sections, None).unwrap(); |
| 27 | + i |
| 28 | + }))] |
| 29 | + image: Image, |
| 30 | + map_ptr: *mut T, |
| 31 | + map_len: usize, |
| 32 | +} |
| 33 | + |
| 34 | +impl IntelPTModule { |
| 35 | + pub fn default_pt_builder() -> IntelPTBuilder { |
| 36 | + IntelPT::builder().exclude_kernel(false) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<I, S, T> EmulatorModule<I, S> for IntelPTModule<T> |
| 41 | +where |
| 42 | + I: Unpin, |
| 43 | + S: Unpin + HasMetadata, |
| 44 | + T: SaturatingAdd + From<u8> + Debug + 'static, |
| 45 | +{ |
| 46 | + fn pre_qemu_init<ET>( |
| 47 | + &mut self, |
| 48 | + emulator_modules: &mut EmulatorModules<ET, I, S>, |
| 49 | + _qemu_params: &mut QemuParams, |
| 50 | + ) where |
| 51 | + ET: EmulatorModuleTuple<I, S>, |
| 52 | + { |
| 53 | + emulator_modules |
| 54 | + .thread_creation(NewThreadHook::Function(intel_pt_new_thread::<ET, I, S, T>)) |
| 55 | + .unwrap(); |
| 56 | + // fixme: consider implementing a clean emulator_modules.thread_teradown |
| 57 | + } |
| 58 | + |
| 59 | + fn pre_exec<ET>( |
| 60 | + &mut self, |
| 61 | + _qemu: Qemu, |
| 62 | + _emulator_modules: &mut EmulatorModules<ET, I, S>, |
| 63 | + _state: &mut S, |
| 64 | + _input: &I, |
| 65 | + ) where |
| 66 | + ET: EmulatorModuleTuple<I, S>, |
| 67 | + { |
| 68 | + let pt = self.pt.as_mut().expect("Intel PT module not initialized."); |
| 69 | + pt.enable_tracing().unwrap(); |
| 70 | + } |
| 71 | + |
| 72 | + fn post_exec<OT, ET>( |
| 73 | + &mut self, |
| 74 | + _qemu: Qemu, |
| 75 | + _emulator_modules: &mut EmulatorModules<ET, I, S>, |
| 76 | + _state: &mut S, |
| 77 | + _input: &I, |
| 78 | + _observers: &mut OT, |
| 79 | + _exit_kind: &mut ExitKind, |
| 80 | + ) where |
| 81 | + OT: ObserversTuple<I, S>, |
| 82 | + ET: EmulatorModuleTuple<I, S>, |
| 83 | + { |
| 84 | + let pt = self.pt.as_mut().expect("Intel PT module not initialized."); |
| 85 | + pt.disable_tracing().unwrap(); |
| 86 | + |
| 87 | + let _ = pt |
| 88 | + .decode_traces_into_map(&mut self.image, self.map_ptr, self.map_len) |
| 89 | + .inspect_err(|e| log::warn!("Intel PT trace decode failed: {e}")); |
| 90 | + |
| 91 | + #[cfg(feature = "intel_pt_export_raw")] |
| 92 | + { |
| 93 | + let _ = pt |
| 94 | + .dump_last_trace_to_file() |
| 95 | + .inspect_err(|e| log::warn!("Intel PT trace save to file failed: {e}")); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl<T> AddressFilter for IntelPTModule<T> |
| 101 | +where |
| 102 | + T: Debug + 'static, |
| 103 | +{ |
| 104 | + fn register(&mut self, address_range: &Range<GuestAddr>) { |
| 105 | + let pt = self.pt.as_mut().unwrap(); |
| 106 | + let mut filters = pt.ip_filters(); |
| 107 | + let range_inclusive = |
| 108 | + RangeInclusive::new(address_range.start as usize, address_range.end as usize - 1); |
| 109 | + filters.push(range_inclusive); |
| 110 | + pt.set_ip_filters(&filters).unwrap() |
| 111 | + } |
| 112 | + |
| 113 | + fn allowed(&self, address: &GuestAddr) -> bool { |
| 114 | + let pt = self.pt.as_ref().unwrap(); |
| 115 | + for f in pt.ip_filters() { |
| 116 | + if f.contains(&(*address as usize)) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + } |
| 120 | + false |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +pub fn intel_pt_new_thread<ET, I, S, T>( |
| 125 | + emulator_modules: &mut EmulatorModules<ET, I, S>, |
| 126 | + _state: Option<&mut S>, |
| 127 | + _env: CPUArchStatePtr, |
| 128 | + tid: u32, |
| 129 | +) -> bool |
| 130 | +where |
| 131 | + I: Unpin, |
| 132 | + S: HasMetadata + Unpin, |
| 133 | + ET: EmulatorModuleTuple<I, S>, |
| 134 | + T: Debug + 'static, |
| 135 | +{ |
| 136 | + let intel_pt_module = emulator_modules |
| 137 | + .modules_mut() |
| 138 | + .match_first_type_mut::<IntelPTModule<T>>() |
| 139 | + .unwrap(); |
| 140 | + |
| 141 | + if intel_pt_module.pt.is_some() { |
| 142 | + panic!("Intel PT module already initialized, only single core VMs are supported ATM."); |
| 143 | + } |
| 144 | + |
| 145 | + let pt = intel_pt_module |
| 146 | + .intel_pt_builder |
| 147 | + .clone() |
| 148 | + .pid(Some(tid as i32)) |
| 149 | + .build() |
| 150 | + .unwrap(); |
| 151 | + |
| 152 | + intel_pt_module.pt = Some(pt); |
| 153 | + |
| 154 | + true |
| 155 | +} |
0 commit comments