binaryninja/workflow/activity.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
use std::{
ffi::{c_void, CString},
ptr::NonNull,
};
use binaryninjacore_sys::*;
use serde_derive::{Deserialize, Serialize};
use crate::{
rc::{Ref, RefCountable},
string::{BnString, IntoCStr},
workflow::AnalysisContext,
};
// TODO: This needs to be made into a trait similar to that of `Command`.
/// An `Activity` represents a fundamental unit of work within a workflow. It encapsulates
/// a specific analysis step or action as a callback function, which is augmented by a configuration.
/// The configuration defines the activity's metadata, eligibility criteria, and execution semantics,
/// allowing it to seamlessly integrate into the workflow system.
///
/// ```
/// use binaryninja::workflow::{activity, Activity, AnalysisContext};
///
/// fn activity_callback(context: &AnalysisContext) {
/// // Perform custom analysis using data provided in the context.
/// }
///
/// let config = activity::Config::action(
/// "example.analysis.analyzeFunction",
/// "Analyze functions",
/// "This activity performs custom analysis on each function"
/// ).eligibility(activity::Eligibility::auto());
/// let activity = Activity::new_with_action(config, activity_callback);
///
/// // Register the activity in a `Workflow`.
/// ```
///
/// See [Activity Fundamentals](https://docs.binary.ninja/dev/workflows.html#activity-fundamentals) for more information.
#[repr(transparent)]
pub struct Activity {
pub(crate) handle: NonNull<BNActivity>,
}
impl Activity {
#[allow(unused)]
pub(crate) unsafe fn from_raw(handle: NonNull<BNActivity>) -> Self {
Self { handle }
}
pub(crate) unsafe fn ref_from_raw(handle: NonNull<BNActivity>) -> Ref<Self> {
Ref::new(Self { handle })
}
pub fn new(config: impl AsConfig) -> Ref<Self> {
unsafe extern "C" fn cb_action_nop(_: *mut c_void, _: *mut BNAnalysisContext) {}
let config = config.as_config();
let result =
unsafe { BNCreateActivity(config.as_ptr(), std::ptr::null_mut(), Some(cb_action_nop)) };
unsafe { Activity::ref_from_raw(NonNull::new(result).unwrap()) }
}
pub fn new_with_action<F>(config: impl AsConfig, mut action: F) -> Ref<Self>
where
F: FnMut(&AnalysisContext),
{
unsafe extern "C" fn cb_action<F: FnMut(&AnalysisContext)>(
ctxt: *mut c_void,
analysis: *mut BNAnalysisContext,
) {
let ctxt = &mut *(ctxt as *mut F);
if let Some(analysis) = NonNull::new(analysis) {
ctxt(&AnalysisContext::from_raw(analysis))
}
}
let config = config.as_config();
let result = unsafe {
BNCreateActivity(
config.as_ptr(),
&mut action as *mut F as *mut c_void,
Some(cb_action::<F>),
)
};
unsafe { Activity::ref_from_raw(NonNull::new(result).unwrap()) }
}
pub fn name(&self) -> String {
let result = unsafe { BNActivityGetName(self.handle.as_ptr()) };
assert!(!result.is_null());
unsafe { BnString::into_string(result) }
}
}
impl ToOwned for Activity {
type Owned = Ref<Self>;
fn to_owned(&self) -> Self::Owned {
unsafe { RefCountable::inc_ref(self) }
}
}
unsafe impl RefCountable for Activity {
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
Ref::new(Self {
handle: NonNull::new(BNNewActivityReference(handle.handle.as_ptr()))
.expect("valid handle"),
})
}
unsafe fn dec_ref(handle: &Self) {
BNFreeActivity(handle.handle.as_ptr());
}
}
pub trait AsConfig {
fn as_config(&self) -> CString;
}
impl AsConfig for &str {
fn as_config(&self) -> std::ffi::CString {
self.to_cstr()
}
}
/// The configuration for an `Activity`, defining its metadata, eligibility criteria, and execution semantics.
#[must_use]
#[derive(Deserialize, Serialize, Debug)]
pub struct Config {
/// A unique identifier for the activity.
pub name: String,
/// A human-readable title for the activity.
pub title: String,
/// A brief description of the activity's purpose and functionality.
pub description: String,
/// The role of the activity within the workflow, determining its behavior and interaction with other activities.
#[serde(default)]
pub role: Role,
/// Names by which this activity has previously been known.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub aliases: Vec<String>,
/// The conditions that determine when the activity should execute.
#[serde(default)]
pub eligibility: Eligibility,
#[serde(skip_serializing_if = "Option::is_none")]
pub dependencies: Option<Dependencies>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct Dependencies {
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub downstream: Vec<String>,
}
impl Config {
/// Creates a new instance with role [`Role::Action`] and the specified name, title, and description.
pub fn action(
name: impl Into<String>,
title: impl Into<String>,
description: impl Into<String>,
) -> Self {
Self {
name: name.into(),
title: title.into(),
description: description.into(),
role: Role::Action,
aliases: Vec::new(),
eligibility: Eligibility::default(),
dependencies: None,
}
}
/// Sets the [`aliases`](field@Config::aliases) field, which contains names by which this activity has previously been known.
pub fn aliases<I, S>(mut self, aliases: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.aliases = aliases.into_iter().map(|s| s.into()).collect();
self
}
/// Sets the [`eligibility`](field@Config::eligibility) field, which defines the conditions under which this activity is eligible for execution.
pub fn eligibility(mut self, eligibility: Eligibility) -> Self {
self.eligibility = eligibility;
self
}
/// Sets the [`dependencies`](field@Config::dependencies) field to specify dependencies that should be triggered after this activity completes.
pub fn downstream_dependencies<I, S>(mut self, dependencies: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.dependencies = Some(Dependencies {
downstream: dependencies.into_iter().map(|s| s.into()).collect(),
});
self
}
}
impl AsConfig for &Config {
fn as_config(&self) -> CString {
serde_json::to_string(self)
.expect("Failed to serialize Config")
.to_cstr()
}
}
impl AsConfig for Config {
fn as_config(&self) -> CString {
(&self).as_config()
}
}
/// Defines the behavior of the activity in the workflow.
///
/// NOTE: Activities with the subflow role are only permitted in module workflows.
/// Subflows are not supported within function workflows.
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
#[derive(Default)]
pub enum Role {
/// The default role; performs a specific task.
#[default]
Action,
/// Contains child activities and uses an eligibility handler to determine which child activities to execute.
/// This enables the ability to have a dynamic and reactive execution pipeline.
Selector,
/// Creates a new task context and asynchronously processes its workflow sub-graph on a new thread within
/// the workflow machine. The subflow executes asynchronously from the requestor, allowing the original
/// thread to return immediately. Within this context, multiple task actions can be enqueued, enabling
/// extensive parallel processing. After completing its workflow sub-graph, it enters a stall state,
/// waiting for all its asynchronous task actions to complete.
Subflow,
/// Asynchronously processes the workflow graph on a new thread within the workflow machine.
/// `Task` activities enable the pipeline to execute asynchronously from its requestor. `Task` activities
/// require a task context to be present; if no task context exists, they execute immediately in the
/// current thread.
Task,
Sequence,
Listener,
}
/// The conditions that determine when an activity should execute.
#[must_use]
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Eligibility {
/// An object that automatically generates a boolean control setting and corresponding predicate.
#[serde(skip_serializing_if = "Option::is_none")]
pub auto: Option<Auto>,
/// Indicates whether the activity should run only once across all file/analysis sessions.
/// Once the activity runs, its state is saved persistently, and it will not run again unless
/// explicitly reset. This is useful for activities that only need to be performed exactly once,
/// such as initial setup tasks.
#[serde(skip_serializing_if = "Option::is_none")]
pub run_once: Option<bool>,
/// Indicates whether the activity should run only once per session. Its state is not
/// persisted, so it will run again in a new session. This is useful for activities
/// that should be performed once per analysis session, such as initialization steps
/// specific to a particular execution context.
#[serde(skip_serializing_if = "Option::is_none")]
pub run_once_per_session: Option<bool>,
/// Indicates if a subflow is eligible for re-execution based on its eligibility logic.
#[serde(skip_serializing_if = "Option::is_none")]
pub continuation: Option<bool>,
/// Objects that define the condition that must be met for the activity to be eligible to run.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub predicates: Vec<Predicate>,
/// Logical operator that defines how multiple predicates are combined.
#[serde(skip_serializing_if = "Option::is_none")]
pub logical_operator: Option<PredicateLogicalOperator>,
}
impl Eligibility {
/// Creates a new instance without an automatically generated boolean control setting.
/// The activity is eligible to run by default.
pub fn without_setting() -> Self {
Eligibility {
auto: None,
run_once: None,
run_once_per_session: None,
continuation: None,
predicates: vec![],
logical_operator: None,
}
}
/// Creates a new instance with an automatically generated boolean control setting and corresponding predicate.
/// The setting is enabled by default.
pub fn auto() -> Self {
Eligibility {
auto: Some(Auto::new()),
run_once: None,
run_once_per_session: None,
continuation: None,
predicates: vec![],
logical_operator: None,
}
}
/// Creates a new instance with an automatically generated boolean control setting and corresponding predicate.
/// The setting has the value `value` by default.
pub fn auto_with_default(value: bool) -> Self {
Eligibility {
auto: Some(Auto::new().default(value)),
run_once: None,
run_once_per_session: None,
continuation: None,
predicates: vec![],
logical_operator: None,
}
}
/// Sets the [`run_once`](field@Eligibility::run_once) field, indicating whether the activity should run only once across all file/analysis sessions.
pub fn run_once(mut self, value: bool) -> Self {
self.run_once = Some(value);
self
}
/// Sets the [`run_once_per_session`](field@Eligibility::run_once_per_session) field, indicating whether the activity should run only once per session.
pub fn run_once_per_session(mut self, value: bool) -> Self {
self.run_once_per_session = Some(value);
self
}
/// Sets the [`continuation`](field@Eligibility::continuation) field, indicating whether a subflow is eligible for re-execution based on its eligibility logic.
pub fn continuation(mut self, value: bool) -> Self {
self.continuation = Some(value);
self
}
/// Sets the predicate that must be satisfied for the activity to be eligible to run.
pub fn predicate(mut self, predicate: impl Into<Predicate>) -> Self {
self.predicates = vec![predicate.into()];
self
}
/// Sets the predicates that must be satisfied for the activity to be eligible to run.
/// If multiple predicates are provided, they are combined using a logical OR.
pub fn matching_any_predicate(mut self, predicates: &[Predicate]) -> Self {
self.predicates = predicates.to_vec();
self.logical_operator = Some(PredicateLogicalOperator::Or);
self
}
/// Sets the predicates that must be satisfied for the activity to be eligible to run.
/// If multiple predicates are provided, they are combined using a logical AND.
pub fn matching_all_predicates(mut self, predicates: &[Predicate]) -> Self {
self.predicates = predicates.to_vec();
self.logical_operator = Some(PredicateLogicalOperator::And);
self
}
}
impl Default for Eligibility {
fn default() -> Self {
Self::auto()
}
}
/// Represents the request for an automatically generated boolean control setting and corresponding predicate.
#[must_use]
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct Auto {
/// The default value for the setting. If `None`, the setting is enabled by default.
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<bool>,
}
impl Auto {
/// Creates a new `Auto` instance that represents a setting that is enabled by default.
pub fn new() -> Self {
Self { default: None }
}
/// Sets the `default` value for the setting.
pub fn default(mut self, value: bool) -> Self {
self.default = Some(value);
self
}
}
/// A predicate that can be used to determine the eligibility of an activity.
///
/// See [`ViewType`] and [`Setting`] for specific predicates that can be used.
#[must_use]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Predicate {
#[serde(flatten)]
predicate_type: PredicateType,
operator: Operator,
value: serde_json::Value,
}
/// A predicate that checks the type of the [`BinaryView`](crate::binary_view::BinaryView).
#[must_use]
pub enum ViewType {
In(Vec<String>),
NotIn(Vec<String>),
}
impl ViewType {
/// Creates a new predicate that checks if the type of the [`BinaryView`](crate::binary_view::BinaryView)
/// _is_ in the provided list.
pub fn in_<I, S>(values: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
ViewType::In(values.into_iter().map(|s| s.as_ref().to_string()).collect())
}
/// Creates a new predicate that checks if the type of the [`BinaryView`](crate::binary_view::BinaryView)
/// _is not_ in the provided list.
pub fn not_in<I, S>(values: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
ViewType::NotIn(values.into_iter().map(|s| s.as_ref().to_string()).collect())
}
}
impl From<ViewType> for Predicate {
fn from(predicate: ViewType) -> Self {
match predicate {
ViewType::In(value) => Predicate {
predicate_type: PredicateType::ViewType,
operator: Operator::In,
value: serde_json::json!(value),
},
ViewType::NotIn(value) => Predicate {
predicate_type: PredicateType::ViewType,
operator: Operator::NotIn,
value: serde_json::json!(value),
},
}
}
}
/// A predicate that evaluates the value of a specific setting.
#[must_use]
pub struct Setting {
identifier: String,
operator: Operator,
value: serde_json::Value,
}
impl Setting {
/// Creates a new predicate that evaluates the value of a specific setting against `value` using `operator`.
pub fn new(
identifier: impl Into<String>,
operator: Operator,
value: impl serde::Serialize,
) -> Self {
Self {
identifier: identifier.into(),
operator,
value: serde_json::json!(value),
}
}
/// Creates a new predicate that checks if the value of the setting is equal to `value`.
pub fn eq(identifier: impl Into<String>, value: impl serde::Serialize) -> Self {
Self::new(identifier, Operator::Eq, value)
}
/// Creates a new predicate that checks if the value of the setting is not equal to `value`.
pub fn ne(identifier: impl Into<String>, value: impl serde::Serialize) -> Self {
Self::new(identifier, Operator::Ne, value)
}
/// Creates a new predicate that checks if the value of the setting is less than `value`.
pub fn lt(identifier: impl Into<String>, value: impl serde::Serialize) -> Self {
Self::new(identifier, Operator::Lt, value)
}
/// Creates a new predicate that checks if the value of the setting is less than or equal to `value`.
pub fn lte(identifier: impl Into<String>, value: impl serde::Serialize) -> Self {
Self::new(identifier, Operator::Lte, value)
}
/// Creates a new predicate that checks if the value of the setting is greater than `value`.
pub fn gt(identifier: impl Into<String>, value: impl serde::Serialize) -> Self {
Self::new(identifier, Operator::Gt, value)
}
/// Creates a new predicate that checks if the value of the setting is greater than or equal to `value`.
pub fn gte(identifier: impl Into<String>, value: impl serde::Serialize) -> Self {
Self::new(identifier, Operator::Gte, value)
}
/// Creates a new predicate that checks if the value of the setting is in the provided list.
pub fn in_(identifier: impl Into<String>, value: impl serde::Serialize) -> Self {
Self::new(identifier, Operator::In, value)
}
/// Creates a new predicate that checks if the value of the setting is not in the provided list.
pub fn not_in(identifier: impl Into<String>, value: impl serde::Serialize) -> Self {
Self::new(identifier, Operator::NotIn, value)
}
}
impl From<Setting> for Predicate {
fn from(setting: Setting) -> Self {
Predicate {
predicate_type: PredicateType::Setting {
identifier: setting.identifier,
},
operator: setting.operator,
value: setting.value,
}
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase", tag = "type")]
enum PredicateType {
Setting { identifier: String },
ViewType,
}
#[derive(Deserialize, Serialize, Debug, Copy, Clone)]
pub enum Operator {
#[serde(rename = "==")]
Eq,
#[serde(rename = "!=")]
Ne,
#[serde(rename = "<")]
Lt,
#[serde(rename = "<=")]
Lte,
#[serde(rename = ">")]
Gt,
#[serde(rename = ">=")]
Gte,
#[serde(rename = "in")]
In,
#[serde(rename = "not in")]
NotIn,
}
#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum PredicateLogicalOperator {
And,
Or,
}