Skip to content

Commit 6d7f3f8

Browse files
authored
Bump ptcov to 0.1.0 (#3723)
* Bump ptcov to 0.1.0 * Update tests * Update libafl_qemu intelpt module * Update command executor intel pt fuzzer * Use ptcov default features * Update intel pt babyfuzzer
1 parent 28d996d commit 6d7f3f8

File tree

8 files changed

+49
-48
lines changed

8 files changed

+49
-48
lines changed

crates/libafl/src/executors/hooks/intel_pt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use crate::executors::hooks::ExecutorHook;
88

99
/// Hook to enable Intel Processor Trace (PT) tracing
1010
#[derive(Debug, TypedBuilder)]
11-
pub struct IntelPTHook<T> {
12-
intel_pt: IntelPT,
11+
pub struct IntelPTHook<'a, T> {
12+
intel_pt: IntelPT<'a>,
1313
map_ptr: *mut T,
1414
map_len: usize,
1515
}
1616

17-
impl<I, S, T> ExecutorHook<I, S> for IntelPTHook<T>
17+
impl<I, S, T> ExecutorHook<I, S> for IntelPTHook<'_, T>
1818
where
1919
S: Serialize,
2020
T: CoverageEntry,

crates/libafl_intelpt/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ libc = { workspace = true }
3232
log = { workspace = true }
3333
num_enum = { workspace = true, default-features = false }
3434
num-traits = { workspace = true, default-features = false }
35-
ptcov = { version = "0.0.6" }
35+
ptcov = { version = "0.1.0" }
3636
raw-cpuid = { version = "11.1.0" }
3737

3838
[target.'cfg(target_os = "linux" )'.dependencies]
3939
caps = { version = "0.5.5" }
4040
perf-event-open-sys = { version = "5.0.0" }
4141

42+
[target.'cfg(target_os = "windows" )'.dependencies]
43+
ptcov = { version = "0.1.0", features = ["retc"] }
44+
4245
[lints]
4346
workspace = true

crates/libafl_intelpt/src/linux.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub enum KvmPTMode {
8181

8282
/// Intel Processor Trace (PT)
8383
#[derive(Debug)]
84-
pub struct IntelPT {
84+
pub struct IntelPT<'a> {
8585
fd: OwnedFd,
8686
perf_buffer: *mut c_void,
8787
perf_aux_buffer: *mut c_void,
@@ -90,17 +90,17 @@ pub struct IntelPT {
9090
aux_head: *mut u64,
9191
aux_tail: *mut u64,
9292
previous_decode_head: u64,
93-
ptcov_decoder: PtCoverageDecoder,
93+
ptcov_decoder: PtCoverageDecoder<'a>,
9494
#[cfg(feature = "export_raw")]
9595
last_decode_trace: Vec<u8>,
9696
}
9797

98-
impl IntelPT {
98+
impl<'a> IntelPT<'a> {
9999
/// Create a default builder
100100
///
101101
/// Checkout [`IntelPTBuilder::default()`] for more details
102102
#[must_use]
103-
pub fn builder() -> IntelPTBuilder {
103+
pub fn builder() -> IntelPTBuilder<'a> {
104104
IntelPTBuilder::default()
105105
}
106106

@@ -313,7 +313,7 @@ impl IntelPT {
313313
}
314314
}
315315

316-
impl Drop for IntelPT {
316+
impl Drop for IntelPT<'_> {
317317
fn drop(&mut self) {
318318
unsafe {
319319
let ret = libc::munmap(self.perf_aux_buffer, self.perf_aux_buffer_size);
@@ -326,7 +326,7 @@ impl Drop for IntelPT {
326326

327327
/// Builder for [`IntelPT`]
328328
#[derive(Debug, Clone, PartialEq)]
329-
pub struct IntelPTBuilder {
329+
pub struct IntelPTBuilder<'a> {
330330
pid: Option<i32>,
331331
cpu: i32,
332332
exclude_kernel: bool,
@@ -335,10 +335,10 @@ pub struct IntelPTBuilder {
335335
perf_buffer_size: usize,
336336
perf_aux_buffer_size: usize,
337337
ip_filters: Vec<RangeInclusive<u64>>,
338-
images: Vec<PtImage>,
338+
images: &'a [PtImage<'a>],
339339
}
340340

341-
impl Default for IntelPTBuilder {
341+
impl Default for IntelPTBuilder<'_> {
342342
/// Create a default builder for [`IntelPT`]
343343
///
344344
/// The default configuration corresponds to:
@@ -354,7 +354,7 @@ impl Default for IntelPTBuilder {
354354
/// .unwrap()
355355
/// .perf_aux_buffer_size(16 * 1024 * 1024)
356356
/// .unwrap()
357-
/// .images(Vec::new())
357+
/// .images(&[])
358358
/// .ip_filters(Default::default());
359359
/// assert_eq!(builder, IntelPTBuilder::default());
360360
/// ```
@@ -368,14 +368,14 @@ impl Default for IntelPTBuilder {
368368
perf_buffer_size: 128 * PAGE_SIZE + PAGE_SIZE,
369369
perf_aux_buffer_size: 16 * 1024 * 1024,
370370
ip_filters: Vec::new(),
371-
images: Vec::new(),
371+
images: &[],
372372
}
373373
}
374374
}
375375

376-
impl IntelPTBuilder {
376+
impl<'a> IntelPTBuilder<'a> {
377377
/// Build the [`IntelPT`] struct
378-
pub fn build(self) -> Result<IntelPT, Error> {
378+
pub fn build(self) -> Result<IntelPT<'a>, Error> {
379379
self.check_config();
380380
let mut perf_event_attr = new_perf_event_attr_intel_pt()?;
381381
perf_event_attr.set_exclude_kernel(self.exclude_kernel.into());
@@ -435,8 +435,7 @@ impl IntelPTBuilder {
435435
.cpu(current_cpu())
436436
.filter_vmx_non_root(self.exclude_hv)
437437
.images(self.images)
438-
.build()
439-
.unwrap();
438+
.build();
440439

441440
let mut intel_pt = IntelPT {
442441
fd,
@@ -554,7 +553,7 @@ impl IntelPTBuilder {
554553
}
555554

556555
#[must_use]
557-
pub fn images(mut self, images: Vec<PtImage>) -> Self {
556+
pub fn images(mut self, images: &'a [PtImage<'_>]) -> Self {
558557
self.images = images;
559558
self
560559
}

crates/libafl_intelpt/tests/integration_tests_linux.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#![cfg(feature = "std")]
22
#![cfg(target_os = "linux")]
33

4+
extern crate alloc;
5+
use alloc::slice;
46
use core::arch::asm;
5-
use std::{
6-
fs::File,
7-
io::{Read, Seek, SeekFrom},
8-
process,
9-
};
7+
use std::process;
108

119
use libafl_intelpt::{IntelPT, availability};
1210
use nix::{
@@ -58,16 +56,13 @@ fn intel_pt_trace_fork() {
5856
let images = maps
5957
.iter()
6058
.filter(|map| map.is_exec() && map.filename().is_some() && map.inode != 0)
61-
.map(|map| {
62-
let mut file = File::open(map.filename().unwrap()).unwrap();
63-
let mut data = vec![0; map.size()];
64-
file.seek(SeekFrom::Start(map.offset as u64)).unwrap();
65-
file.read_exact(&mut data).unwrap();
66-
PtImage::new(data, map.start() as u64)
59+
.map(|pm| {
60+
let data = unsafe { slice::from_raw_parts(pm.start() as *const u8, pm.size()) };
61+
PtImage::new(data, pm.start() as u64)
6762
})
6863
.collect::<Vec<_>>();
6964

70-
let pt_builder = IntelPT::builder().pid(Some(pid.as_raw())).images(images);
65+
let pt_builder = IntelPT::builder().pid(Some(pid.as_raw())).images(&images);
7166
let mut pt = pt_builder.build().expect("Failed to create IntelPT");
7267
pt.enable_tracing().expect("Failed to enable tracing");
7368

crates/libafl_qemu/src/modules/systemmode/intel_pt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ use crate::{
1414
#[derive(Debug, TypedBuilder)]
1515
pub struct IntelPTModule<T = u8> {
1616
#[builder(setter(skip), default)]
17-
pt: Option<IntelPT>,
17+
pt: Option<IntelPT<'static>>,
1818
#[builder(default = IntelPTModule::default_pt_builder())]
19-
intel_pt_builder: IntelPTBuilder,
19+
intel_pt_builder: IntelPTBuilder<'static>,
2020
map_ptr: *mut T,
2121
map_len: usize,
2222
}
2323

2424
impl IntelPTModule {
2525
#[must_use]
26-
pub fn default_pt_builder() -> IntelPTBuilder {
26+
pub fn default_pt_builder() -> IntelPTBuilder<'static> {
2727
IntelPT::builder().exclude_kernel(false).exclude_hv(true)
2828
}
2929
}

fuzzers/binary_only/intel_pt_baby_fuzzer/src/main.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::{
2-
hint::black_box, num::NonZero, path::PathBuf, process, ptr::copy_nonoverlapping, time::Duration,
3-
};
1+
use std::{hint::black_box, num::NonZero, path::PathBuf, process, slice, time::Duration};
42

53
use libafl::{
64
corpus::{InMemoryCorpus, OnDiskCorpus},
@@ -92,10 +90,7 @@ pub fn main() {
9290
.iter()
9391
.filter_map(|pm| {
9492
if pm.is_exec() && pm.filename().is_some() && pm.inode != 0 {
95-
let mut data = vec![0; pm.size()];
96-
unsafe {
97-
copy_nonoverlapping(pm.start() as *const u8, data.as_mut_ptr(), data.len())
98-
}
93+
let data = unsafe { slice::from_raw_parts(pm.start() as *const u8, pm.size()) };
9994
Some(PtImage::new(data, pm.start() as u64))
10095
} else {
10196
None
@@ -104,7 +99,7 @@ pub fn main() {
10499
.collect::<Vec<_>>();
105100

106101
// Pass the executable memory to the code responsible for Intel PT trace decoding
107-
let pt = IntelPT::builder().images(images).build().unwrap();
102+
let pt = IntelPT::builder().images(&images).build().unwrap();
108103
// Intel PT hook that will handle the setup of Intel PT for each execution and fill the map
109104
let pt_hook = unsafe {
110105
IntelPTHook::builder()
@@ -114,8 +109,8 @@ pub fn main() {
114109
}
115110
.build();
116111

117-
type PTInProcessExecutor<'a, EM, H, I, OT, S, T, Z> =
118-
GenericInProcessExecutor<EM, H, &'a mut H, (IntelPTHook<T>, ()), I, OT, S, Z>;
112+
type PTInProcessExecutor<'a, 'b, EM, H, I, OT, S, T, Z> =
113+
GenericInProcessExecutor<EM, H, &'a mut H, (IntelPTHook<'b, T>, ()), I, OT, S, Z>;
119114
// Create the executor for an in-process function with just one observer
120115
let mut executor = PTInProcessExecutor::with_timeout_generic(
121116
tuple_list!(pt_hook),

fuzzers/binary_only/intel_pt_command_executor/Cargo.lock

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuzzers/binary_only/intel_pt_command_executor/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
125125
let mut data = vec![0; executable_segment.size() as usize];
126126
file.seek(SeekFrom::Start(executable_segment.file_range().0))?;
127127
file.read_exact(&mut data)?;
128-
let images = vec![PtImage::new(data, actual_virtual_address)];
128+
let images = vec![PtImage::new(&data, actual_virtual_address)];
129129

130130
let intel_pt = IntelPT::builder()
131131
.cpu(cpu.0)
132132
.inherit(true)
133133
.ip_filters(filters)
134-
.images(images)
134+
.images(&images)
135135
.build()?;
136136

137137
let hook = unsafe { IntelPTHook::builder().map_ptr(MAP_PTR).map_len(MAP_SIZE) }

0 commit comments

Comments
 (0)