Skip to content

Commit 8a3e15a

Browse files
committed
Ignore blocks in VMX root if exclude_hv is set
1 parent 81eaf36 commit 8a3e15a

File tree

4 files changed

+62
-40
lines changed

4 files changed

+62
-40
lines changed

crates/libafl_intelpt/src/decoder.rs

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,7 @@ where
103103
match self.decoder.resync() {
104104
Ok(s) => {
105105
self.status = s;
106-
if self.status.eos() {
107-
return Ok(());
108-
}
109-
110-
// If exclude_hv is set and we are in root VMX operation, continue resyncing
111-
if self.exclude_hv || matches!(self.vmx_non_root, Some(true)) {
112-
return Ok(());
113-
}
106+
return Ok(());
114107
}
115108
Err(e) => match e.code() {
116109
PtErrorCode::Eos => return Ok(()),
@@ -149,39 +142,62 @@ where
149142
self.handle_event()?;
150143
}
151144

152-
// If exclude_hv is set and we are in root VMX operation, bail out
153-
if self.exclude_hv && matches!(self.vmx_non_root, Some(false)) {
154-
return Ok(());
145+
let offset = self.decoder.offset().map_err(error_from_pt_error)?;
146+
if self.should_ignore_vmx_root() || offset <= self.trace_skip {
147+
self.ignore_block()?;
148+
} else {
149+
self.decode_block()?;
155150
}
156151

157-
match self.decoder.decode_next() {
158-
Ok((b, s)) => {
159-
self.status = s;
160-
let offset = self.decoder.offset().map_err(error_from_pt_error)?;
161-
if b.ninsn() > 0 && self.trace_skip < offset {
162-
let id = hash_64_fast(self.previous_block_end_ip) ^ hash_64_fast(b.ip());
163-
// SAFETY: the index is < map_len since the modulo operation is applied
164-
unsafe {
165-
let map_loc = self.map_ptr.add(id as usize % self.map_len);
166-
*map_loc = (*map_loc).saturating_add(&1u8.into());
167-
}
168-
self.previous_block_end_ip = b.end_ip();
169-
}
152+
if self.status.eos() {
153+
return Ok(());
154+
}
155+
}
156+
}
170157

171-
if self.status.eos() {
172-
return Ok(());
158+
fn decode_block(&mut self) -> Result<(), Error> {
159+
match self.decoder.decode_next() {
160+
Ok((b, s)) => {
161+
self.status = s;
162+
if b.ninsn() > 0 {
163+
let id = hash_64_fast(self.previous_block_end_ip) ^ hash_64_fast(b.ip());
164+
// SAFETY: the index is < map_len since the modulo operation is applied
165+
unsafe {
166+
let map_loc = self.map_ptr.add(id as usize % self.map_len);
167+
*map_loc = (*map_loc).saturating_add(&1u8.into());
173168
}
169+
self.previous_block_end_ip = b.end_ip();
174170
}
175-
Err(e) => {
176-
if e.code() != PtErrorCode::Eos {
177-
let offset = self.decoder.offset().map_err(error_from_pt_error)?;
178-
log::info!(
179-
"PT error in block next {e:?} trace offset {offset:x} last decoded block end {:x}",
180-
self.previous_block_end_ip
181-
);
182-
}
183-
return Err(error_from_pt_error(e));
171+
Ok(())
172+
}
173+
Err(e) => {
174+
if e.code() != PtErrorCode::Eos {
175+
let offset = self.decoder.offset().map_err(error_from_pt_error)?;
176+
log::info!(
177+
"PT error in block next {e:?} trace offset {offset:x} last decoded block end {:x}",
178+
self.previous_block_end_ip
179+
);
180+
}
181+
Err(error_from_pt_error(e))
182+
}
183+
}
184+
}
185+
186+
fn ignore_block(&mut self) -> Result<(), Error> {
187+
match self.decoder.decode_next() {
188+
Ok((_, s)) => {
189+
self.status = s;
190+
Ok(())
191+
}
192+
Err(e) => {
193+
if e.code() != PtErrorCode::Eos {
194+
let offset = self.decoder.offset().map_err(error_from_pt_error)?;
195+
log::trace!(
196+
"PT error in ignore block {e:?} trace offset {offset:x} last decoded block end {:x}",
197+
self.previous_block_end_ip
198+
);
184199
}
200+
Err(error_from_pt_error(e))
185201
}
186202
}
187203
}
@@ -200,4 +216,9 @@ where
200216
Err(e) => Err(Error::illegal_state(format!("PT error in event {e:?}"))),
201217
}
202218
}
219+
220+
/// Returns true if `exclude_hv` is set and we are in root VMX operation
221+
fn should_ignore_vmx_root(&self) -> bool {
222+
self.exclude_hv && matches!(self.vmx_non_root, Some(false))
223+
}
203224
}

crates/libafl_intelpt/src/linux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl Default for IntelPTBuilder {
365365
/// .pid(None)
366366
/// .all_cpus()
367367
/// .exclude_kernel(true)
368-
/// .exclude_hv(true)
368+
/// .exclude_hv(false)
369369
/// .inherit(false)
370370
/// .perf_buffer_size(128 * PAGE_SIZE + PAGE_SIZE)
371371
/// .unwrap()
@@ -379,7 +379,7 @@ impl Default for IntelPTBuilder {
379379
pid: None,
380380
cpu: -1,
381381
exclude_kernel: true,
382-
exclude_hv: true,
382+
exclude_hv: false,
383383
inherit: false,
384384
perf_buffer_size: 128 * PAGE_SIZE + PAGE_SIZE,
385385
perf_aux_buffer_size: 16 * 1024 * 1024,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct IntelPTModule<T = u8> {
3030

3131
impl IntelPTModule {
3232
pub fn default_pt_builder() -> IntelPTBuilder {
33-
IntelPT::builder().exclude_kernel(false)
33+
IntelPT::builder().exclude_kernel(false).exclude_hv(true)
3434
}
3535
}
3636

fuzzers/binary_only/intel_pt_command_executor/Cargo.lock

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

0 commit comments

Comments
 (0)