From 382fe3176d66544146dea25b16db33ed590b7012 Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Tue, 10 Dec 2024 15:25:45 +0100 Subject: [PATCH] feat(capi+go): implement API for clearing profiling data. --- capi/include/yara_x.h | 13 ++++++++++++- capi/src/scanner.rs | 30 +++++++++++++++++++++++++++++- go/scanner.go | 19 ++++++++++++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/capi/include/yara_x.h b/capi/include/yara_x.h index d610d8fa..c184d0a2 100644 --- a/capi/include/yara_x.h +++ b/capi/include/yara_x.h @@ -711,7 +711,7 @@ enum YRX_RESULT yrx_scanner_set_global_float(struct YRX_SCANNER *scanner, // Iterates over the slowest N rules, calling the callback for each rule. // // Requires the `rules-profiling` feature, otherwise returns -// [`YRX_RESULT::NOT_SUPPORTED`] +// [`YRX_RESULT::NOT_SUPPORTED`]. // // See [`YRX_SLOWEST_RULES_CALLBACK`] for more details. enum YRX_RESULT yrx_scanner_iter_slowest_rules(struct YRX_SCANNER *scanner, @@ -719,4 +719,15 @@ enum YRX_RESULT yrx_scanner_iter_slowest_rules(struct YRX_SCANNER *scanner, YRX_SLOWEST_RULES_CALLBACK callback, void *user_data); +// Clears all accumulated profiling data. +// +// This resets the profiling data collected during rule execution across +// scanned files. Use this to start a new profiling session, ensuring the +// results reflect only the data gathered after this method is called. +// +// Requires the `rules-profiling` feature, otherwise returns +// [`YRX_RESULT::NOT_SUPPORTED`]. +// +enum YRX_RESULT yrx_scanner_clear_profiling_data(struct YRX_SCANNER *scanner); + #endif /* YARA_X */ diff --git a/capi/src/scanner.rs b/capi/src/scanner.rs index 7cfe439c..6f8e7eb1 100644 --- a/capi/src/scanner.rs +++ b/capi/src/scanner.rs @@ -325,7 +325,7 @@ pub type YRX_SLOWEST_RULES_CALLBACK = extern "C" fn( /// Iterates over the slowest N rules, calling the callback for each rule. /// /// Requires the `rules-profiling` feature, otherwise returns -/// [`YRX_RESULT::NOT_SUPPORTED`] +/// [`YRX_RESULT::NOT_SUPPORTED`]. /// /// See [`YRX_SLOWEST_RULES_CALLBACK`] for more details. #[no_mangle] @@ -362,3 +362,31 @@ pub unsafe extern "C" fn yrx_scanner_iter_slowest_rules( YRX_RESULT::SUCCESS } } + +/// Clears all accumulated profiling data. +/// +/// This resets the profiling data collected during rule execution across +/// scanned files. Use this to start a new profiling session, ensuring the +/// results reflect only the data gathered after this method is called. +/// +/// Requires the `rules-profiling` feature, otherwise returns +/// [`YRX_RESULT::NOT_SUPPORTED`]. +/// +#[no_mangle] +#[allow(unused_variables)] +pub unsafe extern "C" fn yrx_scanner_clear_profiling_data( + scanner: *mut YRX_SCANNER, +) -> YRX_RESULT { + #[cfg(not(feature = "rules-profiling"))] + return YRX_RESULT::NOT_SUPPORTED; + + #[cfg(feature = "rules-profiling")] + { + match scanner.as_mut() { + Some(s) => s.inner.clear_profiling_data(), + None => return YRX_RESULT::INVALID_ARGUMENT, + }; + + YRX_RESULT::SUCCESS + } +} diff --git a/go/scanner.go b/go/scanner.go index 08c842e7..2f42f78a 100644 --- a/go/scanner.go +++ b/go/scanner.go @@ -286,9 +286,9 @@ func slowestRulesCallback( // SlowestRules returns information about the slowest rules and how much // time they spent matching patterns and executing their conditions. // -// In order to use this function the YARA-X C library must be built with -// support for rules profiling, which is done by enabling the `rules-profiling` -// feature. Otherwise, calling this function will cause a panic. +// In order to use this function, the YARA-X C library must be built with +// support for rules profiling by enabling the `rules-profiling` feature. +// Otherwise, calling this function will cause a panic. func (s *Scanner) SlowestRules(n int) []ProfilingInfo { profilingInfo := make([]ProfilingInfo, 0) slowestRules := cgo.NewHandle(&profilingInfo) @@ -311,6 +311,19 @@ func (s *Scanner) SlowestRules(n int) []ProfilingInfo { return profilingInfo } +/// ClearProfilingData resets the profiling data collected during rule execution +/// across scanned files. Use it to start a new profiling session, ensuring the +/// results reflect only the data gathered after this method is called. +// +// In order to use this function, the YARA-X C library must be built with +// support for rules profiling by enabling the `rules-profiling` feature. +// Otherwise, calling this function will cause a panic. +func (s *Scanner) ClearProfilingData() { + if C.yrx_scanner_clear_profiling_data(s.cScanner) == C.NOT_SUPPORTED { + panic("ClearProfilingData requires that the YARA-X C library is built with the `rules-profiling` feature") + } +} + // Destroy destroys the scanner. // // Calling this method directly is not necessary, it will be invoked by the