From cd68557b4c86a85cbcea44b27d5013b0afa8d1ab Mon Sep 17 00:00:00 2001 From: Jason White Date: Tue, 1 Mar 2022 15:50:23 -0800 Subject: [PATCH] Fix bug in check_for_xen_pmi_bug Summary: The `count` wasn't getting initialized properly, so it was possible in theory that the check could have been failing. I noticed this while attempting to convert this chunk of `llvm_asm!()` to the new `asm!()` syntax. Also added documentation explaining what this code is doing because it's not easy to read. Reviewed By: johnhurt Differential Revision: D34542806 fbshipit-source-id: 0f79b9d36757756a1cf76e98830f9cc76f06336c --- reverie-ptrace/src/validation.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/reverie-ptrace/src/validation.rs b/reverie-ptrace/src/validation.rs index dc539be..d78f037 100644 --- a/reverie-ptrace/src/validation.rs +++ b/reverie-ptrace/src/validation.rs @@ -438,7 +438,8 @@ fn check_for_kvm_in_txcp_bug() -> Result<(), PmuValidationError> { } fn check_for_xen_pmi_bug(precise_ip: bool) -> Result<(), PmuValidationError> { - let mut count: i32; + #[allow(unused_assignments)] + let mut count: i32 = -1; let mut attr = ticks_attr(precise_ip); attr.__bindgen_anon_1.sample_period = NUM_BRANCHES - 1; let fd = start_counter(0, -1, &mut attr, None)?; @@ -468,6 +469,31 @@ fn check_for_xen_pmi_bug(precise_ip: bool) -> Result<(), PmuValidationError> { #[allow(deprecated)] unsafe { + // The following asm block does this: + // ``` + // let ret = syscall!(sys_ioctl, raw_fd, _PERF_EVENT_IOC_ENABLE, 0); + // if ret >= -4095 as u64 { return; } + // let ret = syscall!(SYS_ioctl, raw_fd, _PERF_EVENT_IOC_RESET, 0); + // // From this point on, all conditional branches count! + // if ret >= -4095 as u64 { return; } + // // Reset the counter period to the desired value. + // let ret = syscall!(SYS_ioctl, raw_fd, _PERF_EVENT_IOC_PERIOD, attr.sample_period); + // if ret >= -4095 as u64 { return; } + // let mut iterations = NUM_BRANCHES - 2; + // loop { + // iterations -= 1; + // accumulator *= 7; + // accumulator += 2; + // accumulator &= 0xffffff; + // if iterations == 0 { + // break; + // } + // } + // + // let ret = syscall!(SYS_ioctl, raw_fd, _PERF_EVENT_IOC_DISABLE, 0); + // if ret >= -4095 as u64 { return; } + // count = 0; + // ``` llvm_asm!( " mov $2, %rax; @@ -548,7 +574,7 @@ fn check_for_xen_pmi_bug(precise_ip: bool) -> Result<(), PmuValidationError> { ))); } - let has_xen_pmi_bug = (count > 0 && count as u64 > NUM_BRANCHES) || count == -1; + let has_xen_pmi_bug = count as u64 > NUM_BRANCHES || count == -1; if has_xen_pmi_bug { Err(PmuValidationError::IntelXenPmiBugDetected)