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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
//! Validation context types and implementations for the unified validation framework.
//!
//! This module provides context abstractions that allow validators to operate on different
//! types of metadata (raw vs owned) while maintaining a unified interface. The context
//! system supports both raw metadata validation (Stage 1) and owned metadata validation (Stage 2).
//!
//! # Architecture
//!
//! The validation system operates through two main context types:
//! - [`crate::metadata::validation::context::RawValidationContext`] - For raw metadata validation during assembly loading
//! - [`crate::metadata::validation::context::OwnedValidationContext`] - For owned metadata validation with resolved data structures
//!
//! Both contexts implement the [`crate::metadata::validation::context::ValidationContext`] trait,
//! providing common functionality while allowing stage-specific operations.
//!
//! # Key Components
//!
//! - [`crate::metadata::validation::context::ValidationContext`] - Base trait for all validation contexts
//! - [`crate::metadata::validation::context::RawValidationContext`] - Context for Stage 1 raw validation
//! - [`crate::metadata::validation::context::OwnedValidationContext`] - Context for Stage 2 owned validation
//! - [`crate::metadata::validation::context::ValidationStage`] - Enumeration of validation stages
//! - [`crate::metadata::validation::context::factory`] - Factory functions for creating contexts
//!
//! # Usage Examples
//!
//! ```rust,no_run
//! use dotscope::metadata::validation::{RawValidationContext, ValidationContext, ValidationConfig, ReferenceScanner};
//! use dotscope::metadata::cilassemblyview::CilAssemblyView;
//! use std::path::Path;
//! use rayon::ThreadPoolBuilder;
//!
//! # let path = Path::new("assembly.dll");
//! let view = CilAssemblyView::from_path(&path)?;
//! let scanner = ReferenceScanner::from_view(&view)?;
//! let config = ValidationConfig::production();
//! let thread_pool = ThreadPoolBuilder::new().build().unwrap();
//!
//! // Create raw validation context for loading
//! let context = RawValidationContext::new_for_loading(&view, &scanner, &config, &thread_pool);
//! assert!(context.is_loading_validation());
//! # Ok::<(), dotscope::Error>(())
//! ```
//!
//! # Thread Safety
//!
//! All types in this module are [`Send`] and [`Sync`] when their contained references are.
//! Contexts are typically short-lived and used within a single validation run.
//!
//! # Integration
//!
//! This module integrates with:
//! - [`crate::metadata::validation::engine`] - Uses contexts to execute validation
//! - [`crate::metadata::validation::traits`] - Validators receive contexts as parameters
//! - [`crate::metadata::validation::scanner`] - Provides shared reference scanning capabilities

use std::sync::{Arc, OnceLock};

use rustc_hash::FxHashMap;

use crate::metadata::token::Token;

use crate::{
    cilassembly::AssemblyChanges,
    metadata::{
        cilassemblyview::CilAssemblyView,
        cilobject::CilObject,
        method::Method,
        typesystem::{CilTypeRc, TypeSource},
        validation::{config::ValidationConfig, scanner::ReferenceScanner},
    },
};
use rayon::ThreadPool;

/// Validation stage indicator for context discrimination.
///
/// Represents the two validation stages in the dotscope validation system:
/// raw metadata validation and owned metadata validation.
///
/// # Examples
///
/// ```rust,no_run
/// use dotscope::metadata::validation::ValidationStage;
///
/// let stage = ValidationStage::Raw;
/// assert_eq!(stage, ValidationStage::Raw);
/// # Ok::<(), dotscope::Error>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationStage {
    /// Stage 1: Raw metadata validation using [`crate::metadata::cilassemblyview::CilAssemblyView`]
    Raw,
    /// Stage 2: Owned metadata validation using [`crate::metadata::cilobject::CilObject`]
    Owned,
}

/// Base trait for all validation contexts.
///
/// This trait provides common functionality that all validation contexts must implement,
/// regardless of the validation stage or data type being validated. It ensures consistent
/// access to validation configuration and shared resources.
///
/// # Usage Examples
///
/// ```rust,no_run
/// use dotscope::metadata::validation::{ValidationContext, ValidationStage, ValidationConfig};
///
/// fn check_context<T: ValidationContext>(context: &T) {
///     match context.validation_stage() {
///         ValidationStage::Raw => println!("Raw validation context"),
///         ValidationStage::Owned => println!("Owned validation context"),
///     }
/// }
/// # Ok::<(), dotscope::Error>(())
/// ```
///
/// # Thread Safety
///
/// Implementations are thread-safe when their contained references are thread-safe.
pub trait ValidationContext {
    /// Returns the validation stage this context represents.
    ///
    /// # Returns
    ///
    /// Returns a [`crate::metadata::validation::context::ValidationStage`] indicating whether
    /// this is a raw or owned validation context.
    fn validation_stage(&self) -> ValidationStage;

    /// Returns a reference to the shared reference scanner.
    ///
    /// The reference scanner is used for efficient cross-table reference validation
    /// and is shared across all validators in a validation run.
    ///
    /// # Returns
    ///
    /// Returns a reference to the [`crate::metadata::validation::scanner::ReferenceScanner`]
    /// for this validation context.
    fn reference_scanner(&self) -> &ReferenceScanner;

    /// Returns a reference to the validation configuration.
    ///
    /// # Returns
    ///
    /// Returns a reference to the [`crate::metadata::validation::config::ValidationConfig`]
    /// that controls validation behavior.
    fn config(&self) -> &ValidationConfig;
}

/// Context for Stage 1 (raw) validation.
///
/// This context is used when validating raw metadata through [`crate::metadata::cilassemblyview::CilAssemblyView`],
/// either during initial loading or when validating assembly modifications.
/// It supports both scenarios through the optional changes parameter.
///
/// # Usage Examples
///
/// ```rust,no_run
/// use dotscope::metadata::validation::{RawValidationContext, ValidationConfig, ReferenceScanner};
/// use dotscope::metadata::cilassemblyview::CilAssemblyView;
/// use std::path::Path;
/// use rayon::ThreadPoolBuilder;
///
/// # let path = Path::new("assembly.dll");
/// let view = CilAssemblyView::from_path(&path)?;
/// let scanner = ReferenceScanner::from_view(&view)?;
/// let config = ValidationConfig::minimal();
/// let thread_pool = ThreadPoolBuilder::new().build().unwrap();
///
/// // Create context for loading validation
/// let context = RawValidationContext::new_for_loading(&view, &scanner, &config, &thread_pool);
/// assert!(context.is_loading_validation());
/// # Ok::<(), dotscope::Error>(())
/// ```
///
/// # Thread Safety
///
/// This struct is [`Send`] and [`Sync`] when all contained references are thread-safe.
pub struct RawValidationContext<'a> {
    /// The assembly view containing raw metadata
    view: &'a CilAssemblyView,
    /// Optional assembly changes for modification validation
    changes: Option<&'a AssemblyChanges>,
    /// Shared reference scanner for efficient validation
    scanner: &'a ReferenceScanner,
    /// Validation configuration
    config: &'a ValidationConfig,
    /// Dedicated thread pool for this validation session
    thread_pool: &'a ThreadPool,
}

impl<'a> RawValidationContext<'a> {
    /// Creates a new raw validation context for loading validation.
    ///
    /// This constructor is used when validating a [`crate::metadata::cilassemblyview::CilAssemblyView`] during loading,
    /// without any modifications.
    ///
    /// # Arguments
    ///
    /// * `view` - The [`crate::metadata::cilassemblyview::CilAssemblyView`] to validate
    /// * `scanner` - Shared [`crate::metadata::validation::scanner::ReferenceScanner`] for cross-table validation
    /// * `config` - [`crate::metadata::validation::config::ValidationConfig`] controlling validation behavior
    ///
    /// # Returns
    ///
    /// Returns a new [`crate::metadata::validation::context::RawValidationContext`] configured for loading validation.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::validation::{RawValidationContext, ValidationConfig, ReferenceScanner};
    /// use dotscope::metadata::cilassemblyview::CilAssemblyView;
    /// use std::path::Path;
    /// use rayon::ThreadPoolBuilder;
    ///
    /// # let path = Path::new("assembly.dll");
    /// let view = CilAssemblyView::from_path(&path)?;
    /// let scanner = ReferenceScanner::from_view(&view)?;
    /// let config = ValidationConfig::production();
    /// let thread_pool = ThreadPoolBuilder::new().build().unwrap();
    ///
    /// let context = RawValidationContext::new_for_loading(&view, &scanner, &config, &thread_pool);
    /// # Ok::<(), dotscope::Error>(())
    /// ```
    #[must_use]
    pub fn new_for_loading(
        view: &'a CilAssemblyView,
        scanner: &'a ReferenceScanner,
        config: &'a ValidationConfig,
        thread_pool: &'a ThreadPool,
    ) -> Self {
        Self {
            view,
            changes: None,
            scanner,
            config,
            thread_pool,
        }
    }

    /// Creates a new raw validation context for modification validation.
    ///
    /// This constructor is used when validating assembly changes against
    /// an original [`crate::metadata::cilassemblyview::CilAssemblyView`].
    ///
    /// # Arguments
    ///
    /// * `view` - The original [`crate::metadata::cilassemblyview::CilAssemblyView`]
    /// * `changes` - The assembly changes to validate
    /// * `scanner` - Shared [`crate::metadata::validation::scanner::ReferenceScanner`]
    /// * `config` - [`crate::metadata::validation::config::ValidationConfig`] controlling validation
    ///
    /// # Returns
    ///
    /// Returns a new [`crate::metadata::validation::context::RawValidationContext`] configured for modification validation.
    pub fn new_for_modification(
        view: &'a CilAssemblyView,
        changes: &'a AssemblyChanges,
        scanner: &'a ReferenceScanner,
        config: &'a ValidationConfig,
        thread_pool: &'a ThreadPool,
    ) -> Self {
        Self {
            view,
            changes: Some(changes),
            scanner,
            config,
            thread_pool,
        }
    }

    /// Returns the assembly changes if this is a modification validation context.
    ///
    /// # Returns
    ///
    /// Returns `Some(&AssemblyChanges)` for modification validation,
    /// `None` for loading validation contexts.
    #[must_use]
    pub fn changes(&self) -> Option<&AssemblyChanges> {
        self.changes
    }

    /// Returns true if this context is for modification validation.
    ///
    /// # Returns
    ///
    /// Returns `true` if this context contains assembly changes, `false` otherwise.
    #[must_use]
    pub fn is_modification_validation(&self) -> bool {
        self.changes.is_some()
    }

    /// Returns true if this context is for loading validation.
    ///
    /// # Returns
    ///
    /// Returns `true` if this context is for loading validation, `false` otherwise.
    #[must_use]
    pub fn is_loading_validation(&self) -> bool {
        self.changes.is_none()
    }

    /// Returns a reference to the underlying [`crate::metadata::cilassemblyview::CilAssemblyView`].
    ///
    /// This provides access to raw metadata for raw validation.
    ///
    /// # Returns
    ///
    /// Returns a reference to the [`crate::metadata::cilassemblyview::CilAssemblyView`] being validated.
    #[must_use]
    pub fn assembly_view(&self) -> &CilAssemblyView {
        self.view
    }

    /// Returns a reference to the dedicated thread pool for this validation session.
    ///
    /// This thread pool should be used for all parallel operations within validators
    /// to avoid interference with other concurrent validation sessions.
    ///
    /// # Returns
    ///
    /// Returns a reference to the [`ThreadPool`] for this validation session.
    #[must_use]
    pub fn thread_pool(&self) -> &ThreadPool {
        self.thread_pool
    }
}

impl ValidationContext for RawValidationContext<'_> {
    fn validation_stage(&self) -> ValidationStage {
        ValidationStage::Raw
    }

    fn reference_scanner(&self) -> &ReferenceScanner {
        self.scanner
    }

    fn config(&self) -> &ValidationConfig {
        self.config
    }
}

/// Fast method-to-type mapping for efficient method ownership lookup.
///
/// This structure provides O(1) lookups for method-to-type relationships,
/// which are frequently needed during inheritance and method validation.
/// Building this mapping is expensive (iterates all types and methods),
/// so it's cached in the validation context and shared across validators.
///
/// # Thread Safety
///
/// This struct is [`Send`] and [`Sync`] as it contains only thread-safe types.
/// All contained data is immutable after construction.
pub struct MethodTypeMapping {
    /// Maps method address to the type address that owns it
    method_to_type: FxHashMap<usize, usize>,
    /// Maps type address to all method addresses it owns
    type_to_methods: FxHashMap<usize, Vec<usize>>,
    /// Maps method address to `Arc<Method>` for lookup
    address_to_method: FxHashMap<usize, Arc<Method>>,
}

impl MethodTypeMapping {
    /// Builds the method-to-type mapping for fast lookups using cross-assembly safe addresses.
    ///
    /// # Arguments
    ///
    /// * `all_types` - All types to build the mapping from (typically from `types().all_types()`)
    ///
    /// # Performance
    ///
    /// This is an O(n*m) operation where n is the number of types and m is the average
    /// number of methods per type. The result should be cached for reuse.
    #[must_use]
    pub fn new(all_types: Vec<CilTypeRc>) -> Self {
        let mut method_to_type = FxHashMap::default();
        let mut type_to_methods: FxHashMap<usize, Vec<usize>> = FxHashMap::default();
        let mut address_to_method = FxHashMap::default();

        for type_entry in all_types {
            let type_address = Arc::as_ptr(&type_entry) as usize;
            let mut type_methods = Vec::new();

            for (_, method_ref) in type_entry.methods.iter() {
                if let Some(method_rc) = method_ref.upgrade() {
                    let method_address = Arc::as_ptr(&method_rc) as usize;
                    method_to_type.insert(method_address, type_address);
                    address_to_method.insert(method_address, Arc::clone(&method_rc));
                    type_methods.push(method_address);
                }
            }

            if !type_methods.is_empty() {
                type_to_methods.insert(type_address, type_methods);
            }
        }

        Self {
            method_to_type,
            type_to_methods,
            address_to_method,
        }
    }

    /// Fast check if a method belongs to a specific type (O(1) lookup).
    ///
    /// # Arguments
    ///
    /// * `method_address` - The address of the method (obtained via `Arc::as_ptr`)
    /// * `type_address` - The address of the type (obtained via `Arc::as_ptr`)
    ///
    /// # Returns
    ///
    /// `true` if the method belongs to the type, `false` otherwise.
    #[must_use]
    pub fn method_belongs_to_type(&self, method_address: usize, type_address: usize) -> bool {
        self.method_to_type.get(&method_address) == Some(&type_address)
    }

    /// Get all methods for a specific type (O(1) lookup).
    ///
    /// # Arguments
    ///
    /// * `type_address` - The address of the type (obtained via `Arc::as_ptr`)
    ///
    /// # Returns
    ///
    /// Slice of method addresses belonging to the type, or empty slice if none.
    #[must_use]
    pub fn get_type_methods(&self, type_address: usize) -> &[usize] {
        self.type_to_methods
            .get(&type_address)
            .map_or(&[], Vec::as_slice)
    }

    /// Get method by address (O(1) lookup).
    ///
    /// # Arguments
    ///
    /// * `method_address` - The address of the method (obtained via `Arc::as_ptr`)
    ///
    /// # Returns
    ///
    /// Reference to the method if found, `None` otherwise.
    #[must_use]
    pub fn get_method(&self, method_address: usize) -> Option<&Arc<Method>> {
        self.address_to_method.get(&method_address)
    }

    /// Returns the total number of methods in the mapping.
    #[must_use]
    pub fn method_count(&self) -> usize {
        self.address_to_method.len()
    }

    /// Returns the total number of types in the mapping.
    #[must_use]
    pub fn type_count(&self) -> usize {
        self.type_to_methods.len()
    }
}

/// Lazy cache for expensive validation computations.
///
/// This structure holds cached data that is expensive to compute and may be
/// needed by multiple validators. Each field uses [`OnceLock`] to ensure:
/// - Data is computed at most once (on first access)
/// - Zero cost if a validator doesn't need that particular cache
/// - Thread-safe access without explicit locking after initialization
///
/// # Design Rationale
///
/// During validation, multiple validators need similar computed data:
/// - `inheritance.rs` calls `target_assembly_types()` 4 times
/// - `field.rs` calls `all_types()` 4 times
/// - `MethodTypeMapping` is rebuilt for each call to `validate_method_inheritance()`
/// - Interface relationship maps are built in both circularity validators
/// - Nested type relationship maps are built for nested type cycle detection
///
/// By caching these at the context level, we eliminate redundant computations
/// and can reduce validation time by 40-60% for complex assemblies.
///
/// # Thread Safety
///
/// All fields use [`OnceLock`] which provides thread-safe lazy initialization.
/// After initialization, all data is immutable and can be accessed concurrently.
#[derive(Default)]
pub struct ValidationCache {
    /// All types from target assembly (filtered by assembly identity).
    /// Used by: inheritance, circularity, dependency, method, ownership validators.
    target_types: OnceLock<Vec<CilTypeRc>>,

    /// All types including external references.
    /// Used by: field validator (4x), circularity validator.
    all_types: OnceLock<Vec<CilTypeRc>>,

    /// Method-to-type ownership mapping.
    /// Used by: inheritance validator (method override validation).
    /// Expensive to build: iterates all types and all methods.
    method_type_mapping: OnceLock<MethodTypeMapping>,

    /// Interface implementation relationships: type Arc pointer -> implemented interface Arc pointers.
    /// Used by: both circularity validators for cycle detection.
    /// Built from target assembly types, mapping each type to its directly implemented interfaces.
    /// Uses Arc pointers (as usize) instead of tokens to avoid token collisions in multi-assembly scenarios.
    interface_relationships: OnceLock<FxHashMap<usize, Vec<usize>>>,

    /// Nested type relationships: parent type token -> nested type tokens.
    /// Used by: types/circularity validator for nested type cycle detection.
    /// Built from all types, mapping each type to its direct nested types.
    nested_relationships: OnceLock<FxHashMap<Token, Vec<Token>>>,
}

impl ValidationCache {
    /// Creates a new empty validation cache.
    ///
    /// All cached values will be computed lazily on first access.
    #[must_use]
    pub fn new() -> Self {
        Self {
            target_types: OnceLock::new(),
            all_types: OnceLock::new(),
            method_type_mapping: OnceLock::new(),
            interface_relationships: OnceLock::new(),
            nested_relationships: OnceLock::new(),
        }
    }
}

/// Context for Stage 2 (owned) validation.
///
/// This context is used when validating owned metadata through `CilObject`,
/// which contains fully resolved type information and cross-references.
/// CilObject provides access to both raw and resolved metadata through its public API.
///
/// # Caching
///
/// The context maintains a `ValidationCache` that lazily computes expensive
/// data structures on first access. This eliminates redundant computations
/// across validators while ensuring unused caches have zero overhead.
///
/// Available cached data:
/// - `target_assembly_types()` - Types from the target assembly
/// - `all_types()` - All types including external references
/// - `method_type_mapping()` - Method-to-type ownership mappings
pub struct OwnedValidationContext<'a> {
    /// The CilObject containing both raw and resolved metadata
    object: &'a CilObject,
    /// Shared reference scanner for efficient validation
    scanner: &'a ReferenceScanner,
    /// Validation configuration
    config: &'a ValidationConfig,
    /// Lazy cache for expensive computations shared across validators
    cache: ValidationCache,
    /// Dedicated thread pool for this validation session
    thread_pool: &'a ThreadPool,
}

impl<'a> OwnedValidationContext<'a> {
    /// Creates a new owned validation context.
    ///
    /// # Arguments
    ///
    /// * `object` - The CilObject containing both raw and resolved metadata
    /// * `scanner` - Shared reference scanner
    /// * `config` - Validation configuration
    /// * `thread_pool` - Dedicated thread pool for parallel validation
    ///
    /// # Caching
    ///
    /// The context initializes an empty `ValidationCache` that will lazily
    /// compute expensive data structures on first access by any validator.
    pub fn new(
        object: &'a CilObject,
        scanner: &'a ReferenceScanner,
        config: &'a ValidationConfig,
        thread_pool: &'a ThreadPool,
    ) -> Self {
        Self {
            object,
            scanner,
            config,
            cache: ValidationCache::new(),
            thread_pool,
        }
    }

    /// Returns a reference to the CilObject.
    ///
    /// This provides access to both raw and fully resolved metadata including type registries,
    /// method maps, and other resolved structures through CilObject's public API.
    #[must_use]
    pub fn object(&self) -> &CilObject {
        self.object
    }

    /// Returns a reference to the dedicated thread pool for this validation session.
    ///
    /// This thread pool should be used for all parallel operations within validators
    /// to avoid interference with other concurrent validation sessions.
    ///
    /// # Returns
    ///
    /// Returns a reference to the [`ThreadPool`] for this validation session.
    #[must_use]
    pub fn thread_pool(&self) -> &ThreadPool {
        self.thread_pool
    }
}

impl OwnedValidationContext<'_> {
    /// Get types that belong to the assembly being validated (cached).
    ///
    /// This method returns only the types that should be validated for the current assembly,
    /// filtering out external assembly types that should not be subject to local validation rules.
    ///
    /// The result is cached on first access and shared across all validators.
    ///
    /// # Returns
    ///
    /// Cached reference to types from the target assembly that should be validated.
    ///
    /// # Performance
    ///
    /// First call: O(n) where n is the total number of types (filters by assembly)
    /// Subsequent calls: O(1) (returns cached reference)
    pub fn target_assembly_types(&self) -> &Vec<CilTypeRc> {
        self.cache.target_types.get_or_init(|| {
            if let Some(assembly_identity) = self.object.identity() {
                self.object
                    .types()
                    .types_from_source(&TypeSource::Assembly(assembly_identity))
            } else {
                // Fallback: if no assembly identity is available, return empty vec to avoid cross-assembly validation
                Vec::new()
            }
        })
    }

    /// Get all types including external references (cached).
    ///
    /// This method returns all types known to the type system, including types
    /// from external assemblies. This is needed by validators that must examine
    /// cross-assembly relationships (e.g., field type validation).
    ///
    /// The result is cached on first access and shared across all validators.
    ///
    /// # Returns
    ///
    /// Cached reference to all types in the type system.
    ///
    /// # Performance
    ///
    /// First call: O(n) where n is the total number of types
    /// Subsequent calls: O(1) (returns cached reference)
    pub fn all_types(&self) -> &Vec<CilTypeRc> {
        self.cache
            .all_types
            .get_or_init(|| self.object.types().all_types())
    }

    /// Get method-to-type ownership mapping (cached).
    ///
    /// This method returns a mapping structure that provides O(1) lookups for:
    /// - Which type owns a given method
    /// - All methods belonging to a given type
    /// - Method lookup by address
    ///
    /// The mapping is built from all types (not just target assembly types) to
    /// support cross-assembly method inheritance validation.
    ///
    /// The result is cached on first access and shared across all validators.
    ///
    /// # Returns
    ///
    /// Cached reference to the [`MethodTypeMapping`] structure.
    ///
    /// # Performance
    ///
    /// First call: O(n*m) where n is types and m is average methods per type
    /// Subsequent calls: O(1) (returns cached reference)
    ///
    /// # Usage
    ///
    /// ```rust,ignore
    /// let mapping = context.method_type_mapping();
    /// let type_addr = Arc::as_ptr(&some_type) as usize;
    /// for &method_addr in mapping.get_type_methods(type_addr) {
    ///     if let Some(method) = mapping.get_method(method_addr) {
    ///         // Use method...
    ///     }
    /// }
    /// ```
    pub fn method_type_mapping(&self) -> &MethodTypeMapping {
        self.cache
            .method_type_mapping
            .get_or_init(|| MethodTypeMapping::new(self.all_types().clone()))
    }

    /// Get interface implementation relationships (cached).
    ///
    /// This method returns a mapping from type tokens to the tokens of interfaces
    /// they implement. This is used by circularity validators to detect cycles
    /// in interface implementation chains.
    ///
    /// The result is cached on first access and shared across all validators.
    ///
    /// # Returns
    ///
    /// Cached reference to a FxHashMap mapping type Arc pointers (as usize) to implemented
    /// interface Arc pointers (as usize). Using Arc pointers instead of tokens avoids
    /// token collisions in multi-assembly scenarios where different types from different
    /// assemblies may share the same token value.
    ///
    /// # Performance
    ///
    /// First call: O(n*m) where n is types and m is average interfaces per type
    /// Subsequent calls: O(1) (returns cached reference)
    pub fn interface_relationships(&self) -> &FxHashMap<usize, Vec<usize>> {
        self.cache.interface_relationships.get_or_init(|| {
            let mut relationships = FxHashMap::default();
            for type_entry in self.all_types() {
                let type_ptr = Arc::as_ptr(type_entry) as usize;
                let mut implemented_interfaces = Vec::new();

                for (_, interface_ref) in type_entry.interfaces.iter() {
                    if let Some(interface_type) = interface_ref.upgrade() {
                        let interface_ptr = Arc::as_ptr(&interface_type) as usize;
                        implemented_interfaces.push(interface_ptr);
                    }
                }

                if !implemented_interfaces.is_empty() {
                    relationships.insert(type_ptr, implemented_interfaces);
                }
            }
            relationships
        })
    }

    /// Get nested type relationships (cached).
    ///
    /// This method returns a mapping from parent type tokens to the tokens of their
    /// nested types. This is used by circularity validators to detect cycles
    /// in nested type containment.
    ///
    /// The result is cached on first access and shared across all validators.
    ///
    /// # Returns
    ///
    /// Cached reference to a FxHashMap mapping type tokens to nested type tokens.
    ///
    /// # Performance
    ///
    /// First call: O(n*m) where n is types and m is average nested types per type
    /// Subsequent calls: O(1) (returns cached reference)
    pub fn nested_relationships(&self) -> &FxHashMap<Token, Vec<Token>> {
        self.cache.nested_relationships.get_or_init(|| {
            let mut relationships = FxHashMap::default();
            for type_entry in self.all_types() {
                let token = type_entry.token;
                let mut nested_tokens = Vec::new();
                for (_, nested_ref) in type_entry.nested_types.iter() {
                    if let Some(nested_type) = nested_ref.upgrade() {
                        nested_tokens.push(nested_type.token);
                    }
                }
                // Only insert if there are nested types to save memory
                if !nested_tokens.is_empty() {
                    relationships.insert(token, nested_tokens);
                }
            }
            relationships
        })
    }
}

impl ValidationContext for OwnedValidationContext<'_> {
    fn validation_stage(&self) -> ValidationStage {
        ValidationStage::Owned
    }

    fn reference_scanner(&self) -> &ReferenceScanner {
        self.scanner
    }

    fn config(&self) -> &ValidationConfig {
        self.config
    }
}

/// Factory functions for creating validation contexts.
pub mod factory {
    use super::{
        AssemblyChanges, CilAssemblyView, CilObject, OwnedValidationContext, RawValidationContext,
        ReferenceScanner, ValidationConfig,
    };
    use rayon::ThreadPool;

    /// Creates a raw validation context for loading validation.
    pub fn raw_loading_context<'a>(
        view: &'a CilAssemblyView,
        scanner: &'a ReferenceScanner,
        config: &'a ValidationConfig,
        thread_pool: &'a ThreadPool,
    ) -> RawValidationContext<'a> {
        RawValidationContext::new_for_loading(view, scanner, config, thread_pool)
    }

    /// Creates a raw validation context for modification validation.
    pub fn raw_modification_context<'a>(
        view: &'a CilAssemblyView,
        changes: &'a AssemblyChanges,
        scanner: &'a ReferenceScanner,
        config: &'a ValidationConfig,
        thread_pool: &'a ThreadPool,
    ) -> RawValidationContext<'a> {
        RawValidationContext::new_for_modification(view, changes, scanner, config, thread_pool)
    }

    /// Creates an owned validation context.
    pub fn owned_context<'a>(
        object: &'a CilObject,
        scanner: &'a ReferenceScanner,
        config: &'a ValidationConfig,
        thread_pool: &'a ThreadPool,
    ) -> OwnedValidationContext<'a> {
        OwnedValidationContext::new(object, scanner, config, thread_pool)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metadata::validation::config::ValidationConfig;
    use rayon::ThreadPoolBuilder;
    use std::path::PathBuf;

    #[test]
    fn test_raw_loading_context() {
        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 =
                RawValidationContext::new_for_loading(&view, &scanner, &config, &thread_pool);

            assert_eq!(context.validation_stage(), ValidationStage::Raw);
            assert!(context.is_loading_validation());
            assert!(!context.is_modification_validation());
            assert!(context.changes().is_none());
        }
    }

    #[test]
    fn test_raw_modification_context() {
        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 changes = AssemblyChanges::new();
            let thread_pool = ThreadPoolBuilder::new().num_threads(4).build().unwrap();

            let context = RawValidationContext::new_for_modification(
                &view,
                &changes,
                &scanner,
                &config,
                &thread_pool,
            );

            assert_eq!(context.validation_stage(), ValidationStage::Raw);
            assert!(!context.is_loading_validation());
            assert!(context.is_modification_validation());
            assert!(context.changes().is_some());
        }
    }

    #[test]
    fn test_factory_functions() {
        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 changes = AssemblyChanges::new();
            let thread_pool = ThreadPoolBuilder::new().num_threads(4).build().unwrap();

            let loading_context =
                factory::raw_loading_context(&view, &scanner, &config, &thread_pool);
            assert_eq!(loading_context.validation_stage(), ValidationStage::Raw);

            let modification_context =
                factory::raw_modification_context(&view, &changes, &scanner, &config, &thread_pool);
            assert_eq!(
                modification_context.validation_stage(),
                ValidationStage::Raw
            );
        }
    }
}