dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
Documentation
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
//! Validator trait definitions for the unified validation framework.
//!
//! This module defines the core traits that all validators must implement. The trait system
//! supports both raw validation (Stage 1) and owned validation (Stage 2) while providing
//! a unified interface for the validation engine.
//!
//! # Architecture
//!
//! The validation system uses two main trait hierarchies:
//! - [`crate::metadata::validation::traits::RawValidator`] - For Stage 1 validation on raw metadata
//! - [`crate::metadata::validation::traits::OwnedValidator`] - For Stage 2 validation on resolved data
//!
//! Both traits provide priority-based execution ordering and conditional execution through
//! the `should_run()` method, allowing validators to adapt to different validation contexts.
//!
//! # Key Components
//!
//! - [`crate::metadata::validation::traits::RawValidator`] - Trait for raw metadata validation
//! - [`crate::metadata::validation::traits::OwnedValidator`] - Trait for owned metadata validation
//! - [`crate::metadata::validation::traits::ValidatorCollection`] - Helper trait for managing validator collections
//! - [`crate::raw_validators`] - Macro for creating raw validator collections
//! - [`crate::owned_validators`] - Macro for creating owned validator collections
//!
//! # Usage Examples
//!
//! ```rust,no_run
//! use dotscope::metadata::validation::{RawValidator, OwnedValidator, RawValidationContext, OwnedValidationContext};
//! use dotscope::Result;
//!
//! struct ExampleRawValidator;
//!
//! impl RawValidator for ExampleRawValidator {
//!     fn validate_raw(&self, context: &RawValidationContext) -> Result<()> {
//!         // Perform raw validation
//!         Ok(())
//!     }
//!
//!     fn name(&self) -> &'static str {
//!         "ExampleRawValidator"
//!     }
//!
//!     fn priority(&self) -> u32 {
//!         150  // Higher priority than default
//!     }
//! }
//! # Ok::<(), dotscope::Error>(())
//! ```
//!
//! # Thread Safety
//!
//! All validator traits require [`Send`] + [`Sync`] implementations to support parallel
//! execution in the validation engine. This ensures validators can be safely executed
//! across multiple threads.
//!
//! # Integration
//!
//! This module integrates with:
//! - [`crate::metadata::validation::engine`] - Uses traits to execute validators
//! - [`crate::metadata::validation::context`] - Provides context types for validator methods
//! - [`crate::metadata::validation::validators`] - Contains concrete validator implementations

use crate::{
    metadata::validation::context::{OwnedValidationContext, RawValidationContext},
    Result,
};

/// Trait for validators that operate on raw metadata (Stage 1).
///
/// Raw validators are responsible for validating basic structural integrity,
/// schema compliance, and modification validity. They work with [`crate::metadata::cilassemblyview::CilAssemblyView`]
/// and optionally assembly changes for modification validation.
///
/// Raw validators support two use cases:
/// 1. **Loading validation** - Validate [`crate::metadata::cilassemblyview::CilAssemblyView`] structure during loading
/// 2. **Modification validation** - Validate assembly changes against original assembly
///
/// # Thread Safety
///
/// All raw validators must be [`Send`] + [`Sync`] to support parallel execution in the
/// validation engine.
///
/// # Usage Examples
///
/// ```rust,no_run
/// use dotscope::metadata::validation::{RawValidator, RawValidationContext};
/// use dotscope::Result;
///
/// struct MyRawValidator;
///
/// impl RawValidator for MyRawValidator {
///     fn validate_raw(&self, context: &RawValidationContext) -> Result<()> {
///         if context.is_modification_validation() {
///             // Validate changes
///             if let Some(_changes) = context.changes() {
///                 // Perform modification validation
///             }
///         } else {
///             // Validate raw assembly structure
///             let _view = context.assembly_view();
///             // Perform loading validation
///         }
///         Ok(())
///     }
///
///     fn name(&self) -> &'static str {
///         "MyRawValidator"
///     }
/// }
/// # Ok::<(), dotscope::Error>(())
/// ```
pub trait RawValidator: Send + Sync {
    /// Validates raw metadata in the provided context.
    ///
    /// This method is called by the validation engine to perform raw validation.
    /// The context provides access to the assembly view, optional changes,
    /// reference scanner, and configuration.
    ///
    /// # Arguments
    ///
    /// * `context` - The [`crate::metadata::validation::context::RawValidationContext`] containing all necessary data
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if validation passes, or an error describing the validation failure.
    ///
    /// # Errors
    ///
    /// Should return validation-specific errors from the [`crate::Error`] enum,
    /// such as `ValidationRawFailed` or domain-specific validation errors.
    fn validate_raw(&self, context: &RawValidationContext) -> Result<()>;

    /// Returns the name of this validator for error reporting and logging.
    ///
    /// The name should be a static string that uniquely identifies this validator
    /// within the raw validation stage.
    fn name(&self) -> &'static str;

    /// Returns the priority of this validator for execution ordering.
    ///
    /// Validators with higher priority values are executed first. This allows
    /// critical validators (like schema validation) to run before more complex
    /// validators that depend on basic structural integrity.
    ///
    /// Default priority is 100 (medium priority).
    fn priority(&self) -> u32 {
        100
    }

    /// Returns whether this validator should run for the given context.
    ///
    /// This allows validators to selectively enable themselves based on the
    /// validation context (e.g., only run for modification validation).
    ///
    /// Default implementation returns `true` (always run).
    fn should_run(&self, _context: &RawValidationContext) -> bool {
        true
    }
}

/// Trait for validators that operate on owned metadata (Stage 2).
///
/// Owned validators are responsible for validating semantic correctness,
/// type system consistency, and cross-reference integrity. They work with
/// fully resolved [`crate::metadata::cilobject::CilObject`] while maintaining access to raw metadata
/// through the validation context.
///
/// # Thread Safety
///
/// All owned validators must be [`Send`] + [`Sync`] to support parallel execution in the
/// validation engine.
///
/// # Usage Examples
///
/// ```rust,no_run
/// use dotscope::metadata::validation::{OwnedValidator, OwnedValidationContext};
/// use dotscope::Result;
///
/// struct MyOwnedValidator;
///
/// impl OwnedValidator for MyOwnedValidator {
///     fn validate_owned(&self, context: &OwnedValidationContext) -> Result<()> {
///         let object = context.object();
///         let types = object.types();
///
///         // Validate type system consistency
///         for type_entry in types.all_types() {
///             // Perform validation on each type
///             let _name = &type_entry.name;
///         }
///
///         Ok(())
///     }
///
///     fn name(&self) -> &'static str {
///         "MyOwnedValidator"
///     }
/// }
/// # Ok::<(), dotscope::Error>(())
/// ```
pub trait OwnedValidator: Send + Sync {
    /// Validates owned metadata in the provided context.
    ///
    /// This method is called by the validation engine to perform owned validation.
    /// The context provides access to both raw assembly view and resolved object data,
    /// along with the reference scanner and configuration.
    ///
    /// # Arguments
    ///
    /// * `context` - The [`crate::metadata::validation::context::OwnedValidationContext`] containing all necessary data
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if validation passes, or an error describing the validation failure.
    ///
    /// # Errors
    ///
    /// Should return validation-specific errors from the [`crate::Error`] enum,
    /// such as `ValidationOwnedFailed` or domain-specific validation errors.
    fn validate_owned(&self, context: &OwnedValidationContext) -> Result<()>;

    /// Returns the name of this validator for error reporting and logging.
    ///
    /// The name should be a static string that uniquely identifies this validator
    /// within the owned validation stage.
    fn name(&self) -> &'static str;

    /// Returns the priority of this validator for execution ordering.
    ///
    /// Validators with higher priority values are executed first. This allows
    /// fundamental validators (like token validation) to run before more complex
    /// validators that depend on basic consistency.
    ///
    /// Default priority is 100 (medium priority).
    fn priority(&self) -> u32 {
        100
    }

    /// Returns whether this validator should run for the given context.
    ///
    /// This allows validators to selectively enable themselves based on the
    /// validation context or configuration.
    ///
    /// Default implementation returns `true` (always run).
    fn should_run(&self, _context: &OwnedValidationContext) -> bool {
        true
    }
}

/// Helper trait for creating validator collections with type erasure.
///
/// This trait provides utilities for building collections of validators with automatic
/// priority-based sorting and type erasure through [`Box`] wrappers.
///
/// # Usage Examples
///
/// ```rust,no_run
/// use dotscope::metadata::validation::{ValidatorCollection, RawValidator, RawValidationContext};
/// use dotscope::Result;
///
/// struct TestValidator;
/// impl RawValidator for TestValidator {
///     fn validate_raw(&self, _context: &RawValidationContext) -> Result<()> { Ok(()) }
///     fn name(&self) -> &'static str { "TestValidator" }
/// }
///
/// let mut validators: Vec<Box<dyn RawValidator>> = Vec::new();
/// let validators = validators
///     .add_validator(Box::new(TestValidator))
///     .sort_by_priority();
/// # Ok::<(), dotscope::Error>(())
/// ```
pub trait ValidatorCollection<V> {
    /// Adds a validator to the collection.
    ///
    /// # Arguments
    ///
    /// * `validator` - The validator to add to the collection
    ///
    /// # Returns
    ///
    /// Returns the updated collection with the validator added.
    #[must_use]
    fn add_validator(self, validator: V) -> Self;

    /// Sorts validators by priority (highest first).
    ///
    /// Validators with higher priority values are placed first in the collection,
    /// ensuring they execute before lower-priority validators.
    ///
    /// # Returns
    ///
    /// Returns the collection sorted by validator priority in descending order.
    #[must_use]
    fn sort_by_priority(self) -> Self;
}

impl ValidatorCollection<Box<dyn RawValidator>> for Vec<Box<dyn RawValidator>> {
    fn add_validator(mut self, validator: Box<dyn RawValidator>) -> Self {
        self.push(validator);
        self
    }

    fn sort_by_priority(mut self) -> Self {
        self.sort_by_key(|validator| std::cmp::Reverse(validator.priority()));
        self
    }
}

impl ValidatorCollection<Box<dyn OwnedValidator>> for Vec<Box<dyn OwnedValidator>> {
    fn add_validator(mut self, validator: Box<dyn OwnedValidator>) -> Self {
        self.push(validator);
        self
    }

    fn sort_by_priority(mut self) -> Self {
        self.sort_by_key(|validator| std::cmp::Reverse(validator.priority()));
        self
    }
}

/// Convenience macros for creating validator collections.
#[macro_export]
macro_rules! raw_validators {
    ($($validator:expr),* $(,)?) => {
        {
            use $crate::metadata::validation::traits::ValidatorCollection;
            Vec::<Box<dyn $crate::metadata::validation::traits::RawValidator>>::new()
                $(
                    .add_validator(Box::new($validator))
                )*
                .sort_by_priority()
        }
    };
}

/// Creates a collection of owned validators with automatic priority sorting.
///
/// This macro simplifies the creation of validator collections by automatically
/// boxing validators and sorting them by priority (highest first).
///
/// # Examples
///
/// ```rust,ignore
/// use crate::owned_validators;
///
/// let validators = owned_validators![
///     TokenValidator::new(),
///     SemanticValidator::new(),
///     MethodValidator::new(),
/// ];
/// ```
#[macro_export]
macro_rules! owned_validators {
    ($($validator:expr),* $(,)?) => {
        {
            use $crate::metadata::validation::traits::ValidatorCollection;
            Vec::<Box<dyn $crate::metadata::validation::traits::OwnedValidator>>::new()
                $(
                    .add_validator(Box::new($validator))
                )*
                .sort_by_priority()
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metadata::{
        cilassemblyview::CilAssemblyView,
        validation::{config::ValidationConfig, context::factory, scanner::ReferenceScanner},
    };
    use rayon::ThreadPoolBuilder;
    use std::path::PathBuf;

    struct TestRawValidator {
        name: &'static str,
        priority: u32,
    }

    impl RawValidator for TestRawValidator {
        fn validate_raw(&self, _context: &RawValidationContext) -> Result<()> {
            Ok(())
        }

        fn name(&self) -> &'static str {
            self.name
        }

        fn priority(&self) -> u32 {
            self.priority
        }
    }

    struct TestOwnedValidator {
        name: &'static str,
        priority: u32,
    }

    impl OwnedValidator for TestOwnedValidator {
        fn validate_owned(&self, _context: &OwnedValidationContext) -> Result<()> {
            Ok(())
        }

        fn name(&self) -> &'static str {
            self.name
        }

        fn priority(&self) -> u32 {
            self.priority
        }
    }

    #[test]
    fn test_raw_validator_trait() {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/samples/WindowsBase.dll");
        if let Ok(view) = CilAssemblyView::from_path(&path) {
            let scanner = ReferenceScanner::from_view(&view).unwrap();
            let config = ValidationConfig::minimal();
            let thread_pool = ThreadPoolBuilder::new().num_threads(4).build().unwrap();
            let context = factory::raw_loading_context(&view, &scanner, &config, &thread_pool);

            let validator = TestRawValidator {
                name: "TestValidator",
                priority: 150,
            };

            assert_eq!(validator.name(), "TestValidator");
            assert_eq!(validator.priority(), 150);
            assert!(validator.should_run(&context));
            assert!(validator.validate_raw(&context).is_ok());
        }
    }

    #[test]
    fn test_validator_collection_sorting() {
        let validators = raw_validators![
            TestRawValidator {
                name: "Low",
                priority: 50
            },
            TestRawValidator {
                name: "High",
                priority: 200
            },
            TestRawValidator {
                name: "Medium",
                priority: 100
            },
        ];

        assert_eq!(validators[0].name(), "High");
        assert_eq!(validators[1].name(), "Medium");
        assert_eq!(validators[2].name(), "Low");
    }

    #[test]
    fn test_validator_macros() {
        let raw_validators = raw_validators![
            TestRawValidator {
                name: "Test1",
                priority: 100
            },
            TestRawValidator {
                name: "Test2",
                priority: 200
            },
        ];

        assert_eq!(raw_validators.len(), 2);
        assert_eq!(raw_validators[0].name(), "Test2"); // Higher priority first

        let owned_validators = owned_validators![TestOwnedValidator {
            name: "Test1",
            priority: 100
        },];

        assert_eq!(owned_validators.len(), 1);
        assert_eq!(owned_validators[0].name(), "Test1");
    }
}