From 435023079dd260622a2ed82424b1d2dc3830487d Mon Sep 17 00:00:00 2001 From: Vincent Thiberville Date: Sun, 22 Sep 2024 11:53:39 +0200 Subject: [PATCH] feat: add UnwindSafe traits to module private datas --- boreal/src/module/console.rs | 5 +++-- boreal/src/module/mod.rs | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/boreal/src/module/console.rs b/boreal/src/module/console.rs index bae257be..5987f5e9 100644 --- a/boreal/src/module/console.rs +++ b/boreal/src/module/console.rs @@ -1,4 +1,5 @@ use std::fmt::Write; +use std::panic::{RefUnwindSafe, UnwindSafe}; use std::{collections::HashMap, sync::Arc}; use super::{EvalContext, Module, ModuleData, ModuleDataMap, StaticValue, Type, Value}; @@ -9,7 +10,7 @@ pub struct Console { } /// Type of callback called when a message is logged. -pub type LogCallback = dyn Fn(String) + Send + Sync; +pub type LogCallback = dyn Fn(String) + Send + Sync + UnwindSafe + RefUnwindSafe; impl Module for Console { fn get_name(&self) -> &'static str { @@ -69,7 +70,7 @@ impl Console { #[must_use] pub fn with_callback(callback: T) -> Self where - T: Fn(String) + Send + Sync + 'static, + T: Fn(String) + Send + Sync + UnwindSafe + RefUnwindSafe + 'static, { Self { callback: Arc::new(callback), diff --git a/boreal/src/module/mod.rs b/boreal/src/module/mod.rs index ae1cbd7f..218f3f33 100644 --- a/boreal/src/module/mod.rs +++ b/boreal/src/module/mod.rs @@ -261,7 +261,7 @@ impl std::fmt::Debug for ModuleUserData { /// Object holding the data of each module. See [`ModuleData`]. pub struct ModuleDataMap<'scanner> { - private_data: HashMap>, + private_data: HashMap>, user_data: &'scanner ModuleUserData, } @@ -390,7 +390,7 @@ pub trait ModuleData: Module { /// Private Data to associate with the module. /// /// This is the data that the module can store and retrieve during a scan. - type PrivateData: Any + Send + Sync; + type PrivateData: Any + Send + Sync + UnwindSafe + RefUnwindSafe; /// Data that the user can provide to the module. /// @@ -417,17 +417,23 @@ impl<'scanner> ModuleDataMap<'scanner> { /// Retrieve the private data of a module. #[must_use] pub fn get(&self) -> Option<&T::PrivateData> { - self.private_data - .get(&TypeId::of::()) - .and_then(|v| v.downcast_ref()) + self.private_data.get(&TypeId::of::()).and_then(|v| { + // For some reason, having "dyn Any + Send + Sync + UnwindSafe + RefUnwindSafe" + // causes the rust compiler to fail to resolve `v.downcast_ref()`, so we need to + // explicit where this method comes from. + ::downcast_ref(&**v) + }) } /// Retrieve a mutable borrow on the private data of a module. #[must_use] pub fn get_mut(&mut self) -> Option<&mut T::PrivateData> { - self.private_data - .get_mut(&TypeId::of::()) - .and_then(|v| v.downcast_mut()) + self.private_data.get_mut(&TypeId::of::()).and_then(|v| { + // For some reason, having "dyn Any + Send + Sync + UnwindSafe + RefUnwindSafe" + // causes the rust compiler to fail to resolve `v.downcast_mut()`, so we need to + // explicit where this method comes from. + ::downcast_mut(&mut **v) + }) } /// Retrieve the user data of a module.