@@ -32,10 +32,10 @@ use std::{
3232 sync:: LazyLock ,
3333} ;
3434
35- // #[cfg(target_os = "linux")]
36- // use arbitrary_int::u4;
37- // #[cfg(target_os = "linux")]
38- // use bitbybit::bitfield;
35+ #[ cfg( target_os = "linux" ) ]
36+ use arbitrary_int:: u4;
37+ #[ cfg( target_os = "linux" ) ]
38+ use bitbybit:: bitfield;
3939#[ cfg( target_os = "linux" ) ]
4040use caps:: { CapSet , Capability } ;
4141#[ cfg( target_os = "linux" ) ]
@@ -641,19 +641,19 @@ impl IntelPTBuilder {
641641 }
642642}
643643
644- // // / Perf event config for `IntelPT`
645- // // /
646- // // / (This is almost mapped to `IA32_RTIT_CTL MSR` by perf)
647- // #[cfg(target_os = "linux")]
648- // #[bitfield(u64, default = 0)]
649- // struct PtConfig {
650- // /// Disable call return address compression. AKA DisRETC in Intel SDM.
651- // #[bit(11, rw)]
652- // noretcomp: bool,
653- // /// Indicates the frequency of PSB packets. AKA PSBFreq in Intel SDM.
654- // #[bits(24..=27, rw)]
655- // psb_period: u4,
656- // }
644+ /// Perf event config for `IntelPT`
645+ ///
646+ /// (This is almost mapped to `IA32_RTIT_CTL MSR` by perf)
647+ #[ cfg( target_os = "linux" ) ]
648+ #[ bitfield( u64 , default = 0 ) ]
649+ struct PtConfig {
650+ /// Disable call return address compression. AKA DisRETC in Intel SDM.
651+ #[ bit( 11 , rw) ]
652+ noretcomp : bool ,
653+ /// Indicates the frequency of PSB packets. AKA PSBFreq in Intel SDM.
654+ #[ bits( 24 ..=27 , rw) ]
655+ psb_period : u4 ,
656+ }
657657
658658/// Number of address filters available on the running CPU
659659#[ cfg( target_os = "linux" ) ]
@@ -815,12 +815,11 @@ fn new_perf_event_attr_intel_pt() -> Result<perf_event_attr, Error> {
815815 Ok ( t) => Ok ( * t) ,
816816 Err ( e) => Err ( Error :: unsupported ( e. clone ( ) ) ) ,
817817 } ?;
818- // let config = PtConfig::builder()
819- // .with_noretcomp(true)
820- // .with_psb_period(u4::new(0))
821- // .build()
822- // .raw_value;
823- let config = 0x08_00 ; // noretcomp
818+ let config = PtConfig :: builder ( )
819+ . with_noretcomp ( true )
820+ . with_psb_period ( u4:: new ( 0 ) )
821+ . build ( )
822+ . raw_value ;
824823
825824 let mut attr = perf_event_attr {
826825 size : size_of :: < perf_event_attr > ( ) as u32 ,
@@ -940,16 +939,16 @@ const fn wrap_aux_pointer(ptr: u64, perf_aux_buffer_size: usize) -> u64 {
940939
941940#[ cfg( test) ]
942941mod test {
943- // #[cfg(target_os = "linux")]
944- // use arbitrary_int::Number;
942+ #[ cfg( target_os = "linux" ) ]
943+ use arbitrary_int:: Number ;
945944 use static_assertions:: assert_eq_size;
946945
947946 use super :: * ;
948947
949948 // Only 64-bit systems are supported, ensure we can use usize and u64 interchangeably
950949 assert_eq_size ! ( usize , u64 ) ;
951950
952- /// Quick way to check if your machine is compatible with Intl PT's features used by libafl
951+ /// Quick way to check if your machine is compatible with Intel PT's features used by libafl
953952 ///
954953 /// Simply run `cargo test intel_pt_check_availability -- --show-output`
955954 #[ test]
@@ -979,52 +978,52 @@ mod test {
979978 . unwrap ( ) ;
980979 }
981980
982- // #[test]
983- // #[cfg(target_os = "linux")]
984- // fn intel_pt_pt_config_noretcomp_format() {
985- // let ptconfig_noretcomp = PtConfig::DEFAULT.with_noretcomp(true).raw_value;
986- // let path = format!("{PT_EVENT_PATH}/format/noretcomp");
987- // let s = fs::read_to_string(&path).expect("Failed to read Intel PT config noretcomp format");
988- // assert!(
989- // s.starts_with("config:"),
990- // "Unexpected Intel PT config noretcomp format"
991- // );
992- // let bit = s["config:".len()..]
993- // .trim()
994- // .parse::<u32>()
995- // .expect("Failed to parse Intel PT config noretcomp format");
996- // assert_eq!(
997- // ptconfig_noretcomp,
998- // 0b1 << bit,
999- // "Unexpected Intel PT config noretcomp format"
1000- // );
1001- // }
1002- //
1003- // #[test]
1004- // #[cfg(target_os = "linux")]
1005- // fn intel_pt_pt_config_psb_period_format() {
1006- // let ptconfig_psb_period = PtConfig::DEFAULT.with_psb_period(u4::MAX).raw_value;
1007- // let path = format!("{PT_EVENT_PATH}/format/psb_period");
1008- // let s =
1009- // fs::read_to_string(&path).expect("Failed to read Intel PT config psb_period format");
1010- // assert!(
1011- // s.starts_with("config:"),
1012- // "Unexpected Intel PT config psb_period format"
1013- // );
1014- // let from = s["config:".len().."config:".len() + 2]
1015- // .parse::<u32>()
1016- // .expect("Failed to parse Intel PT config psb_period format");
1017- // let to = s["config:".len() + 3..]
1018- // .trim()
1019- // .parse::<u32>()
1020- // .expect("Failed to parse Intel PT config psb_period format");
1021- // let mut format = 0;
1022- // for bit in from..=to {
1023- // format |= 0b1 << bit;
1024- // }
1025- // assert_eq!(
1026- // ptconfig_psb_period, format,
1027- // "Unexpected Intel PT config psb_period format"
1028- // );
1029- // }
981+ #[ test]
982+ #[ cfg( target_os = "linux" ) ]
983+ fn intel_pt_pt_config_noretcomp_format ( ) {
984+ let ptconfig_noretcomp = PtConfig :: DEFAULT . with_noretcomp ( true ) . raw_value ;
985+ let path = format ! ( "{PT_EVENT_PATH}/format/noretcomp" ) ;
986+ let s = fs:: read_to_string ( & path) . expect ( "Failed to read Intel PT config noretcomp format" ) ;
987+ assert ! (
988+ s. starts_with( "config:" ) ,
989+ "Unexpected Intel PT config noretcomp format"
990+ ) ;
991+ let bit = s[ "config:" . len ( ) ..]
992+ . trim ( )
993+ . parse :: < u32 > ( )
994+ . expect ( "Failed to parse Intel PT config noretcomp format" ) ;
995+ assert_eq ! (
996+ ptconfig_noretcomp,
997+ 0b1 << bit,
998+ "Unexpected Intel PT config noretcomp format"
999+ ) ;
1000+ }
1001+
1002+ #[ test]
1003+ #[ cfg( target_os = "linux" ) ]
1004+ fn intel_pt_pt_config_psb_period_format ( ) {
1005+ let ptconfig_psb_period = PtConfig :: DEFAULT . with_psb_period ( u4:: MAX ) . raw_value ;
1006+ let path = format ! ( "{PT_EVENT_PATH}/format/psb_period" ) ;
1007+ let s =
1008+ fs:: read_to_string ( & path) . expect ( "Failed to read Intel PT config psb_period format" ) ;
1009+ assert ! (
1010+ s. starts_with( "config:" ) ,
1011+ "Unexpected Intel PT config psb_period format"
1012+ ) ;
1013+ let from = s[ "config:" . len ( ) .."config:" . len ( ) + 2 ]
1014+ . parse :: < u32 > ( )
1015+ . expect ( "Failed to parse Intel PT config psb_period format" ) ;
1016+ let to = s[ "config:" . len ( ) + 3 ..]
1017+ . trim ( )
1018+ . parse :: < u32 > ( )
1019+ . expect ( "Failed to parse Intel PT config psb_period format" ) ;
1020+ let mut format = 0 ;
1021+ for bit in from..=to {
1022+ format |= 0b1 << bit;
1023+ }
1024+ assert_eq ! (
1025+ ptconfig_psb_period, format,
1026+ "Unexpected Intel PT config psb_period format"
1027+ ) ;
1028+ }
10301029}
0 commit comments