VirusTotal / yara-x

A rewrite of YARA in Rust.
https://virustotal.github.io/yara-x/
BSD 3-Clause "New" or "Revised" License
647 stars 49 forks source link

Rc<yara_x::types::map::Map>` cannot be sent between threads safely #172

Closed xvsfekcn closed 2 months ago

xvsfekcn commented 2 months ago

Here’s the translation of your text into English:

"I want to use Tokio and yara-x together to build a web server that provides malware scanning capabilities. How should I implement this? Are there any examples I can refer to? After using Box to work around the lifetime issues, I encountered the error: Rc cannot be sent between threads safely."

If you need further assistance or examples on how to implement this, feel free to ask! Some error: Rc<yara_x::types::array::Array> cannot be sent between threads safely, Rc<yara_x::types::structure::Struct> cannot be sent between threads safely

xvsfekcn commented 2 months ago

pub struct Yara { data_dir: PathBuf, // key = file_name rule_source: Arc<RwLock<HashMap<String, Vec>>>, // yara rules complie from rule_source rules: Option<&'static Rules>, // yara scanner scanner: Option<Arc<Mutex<Scanner<'static>>>>, }

xvsfekcn commented 2 months ago
    let rules = compiler.build();

    self.rules = Some(Box::leak(Box::new(rules)));

    let scanner =Box::new(Scanner::new(self.rules.unwrap()));

    self.scanner = Some(Arc::new(Mutex::new(*scanner)));
plusvic commented 2 months ago

The Scanner type can't be shared by multiple threads, but you can create one scanner per thread using the same Rules. I'm not very familiar with the Tokio framework, so I can't provide detailed advice.