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
//! # FieldRVA Builder
//!
//! Provides a fluent API for building FieldRVA table entries that define Relative Virtual Addresses (RVAs)
//! for fields with initial data stored in the PE file. The FieldRVA table enables static field initialization,
//! constant data embedding, and global variable setup with pre-computed values.
//!
//! ## Overview
//!
//! The `FieldRVABuilder` enables creation of field RVA entries with:
//! - Field row index specification (required)
//! - RVA location for initial data (required)
//! - Validation of field row indices and RVA values
//! - Automatic metadata management
//!
//! ## Usage
//!
//! ```rust,ignore
//! # use dotscope::prelude::*;
//! # use std::path::Path;
//! # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
//! # let mut assembly = CilAssembly::new(view);
//!
//! // Create a field signature for static data
//! let field_sig = vec![0x06]; // Simple type signature
//!
//! // Create a field first
//! let field_ref = FieldBuilder::new()
//!     .name("StaticData")
//!     .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
//!     .signature(&field_sig)
//!     .build(&mut context)?;
//!
//! // Create a field RVA entry for static field initialization
//! let field_rva_ref = FieldRVABuilder::new()
//!     .field(field_ref.placeholder())
//!     .rva(0x2000) // RVA pointing to initial data
//!     .build(&mut context)?;
//! # Ok::<(), dotscope::Error>(())
//! ```
//!
//! ## Design
//!
//! The builder follows the established pattern with:
//! - **Validation**: Field row index and RVA are required and validated
//! - **Field Verification**: Ensures field row index is valid and non-zero
//! - **Automatic Management**: Metadata entries are created automatically
//! - **RVA Validation**: Ensures RVA values are non-zero and valid

use crate::{
    cilassembly::{ChangeRefRc, CilAssembly},
    metadata::{
        tables::{FieldRvaRaw, TableDataOwned, TableId},
        token::Token,
    },
    Error, Result,
};

/// Builder for creating FieldRVA table entries.
///
/// `FieldRVABuilder` provides a fluent API for creating entries in the FieldRVA
/// metadata table, which specifies Relative Virtual Addresses for fields that have
/// initial data stored in the PE file.
///
/// # Purpose
///
/// The FieldRVA table serves several key functions:
/// - **Static Field Initialization**: Pre-computed values for static fields
/// - **Constant Data**: Read-only data embedded directly in the PE file
/// - **Global Variables**: Module-level data with specific initial states
/// - **Interop Data**: Native data structures for P/Invoke and COM scenarios
/// - **Resource Embedding**: Binary resources accessible through field references
///
/// # Builder Pattern
///
/// The builder provides a fluent interface for constructing FieldRVA entries:
///
/// ```rust,ignore
/// # use dotscope::prelude::*;
/// # use std::path::Path;
/// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
/// # let mut assembly = CilAssembly::new(view);
/// # let field_ref = context.field_add(...)?;
///
/// let field_rva_ref = FieldRVABuilder::new()
///     .field(field_ref.placeholder())
///     .rva(0x2000)
///     .build(&mut context)?;
/// # Ok::<(), dotscope::Error>(())
/// ```
///
/// # Validation
///
/// The builder enforces the following constraints:
/// - **Field Required**: A field row index must be provided
/// - **Field Validation**: Field row index must be non-zero
/// - **RVA Required**: An RVA value must be provided
/// - **RVA Validation**: RVA values must be greater than 0
///
/// # Integration
///
/// FieldRVA entries integrate with other metadata structures:
/// - **Field**: References specific fields in the Field table
/// - **PE Sections**: RVAs point to data in specific PE file sections
/// - **Static Data**: Enables runtime access to pre-initialized field values
#[derive(Debug, Clone)]
pub struct FieldRVABuilder {
    /// The row index of the field with initial data
    field: Option<u32>,
    /// The RVA pointing to the field's initial data
    rva: Option<u32>,
}

impl Default for FieldRVABuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl FieldRVABuilder {
    /// Creates a new `FieldRVABuilder` instance.
    ///
    /// Returns a builder with all fields unset, ready for configuration
    /// through the fluent API methods.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use dotscope::prelude::*;
    /// let builder = FieldRVABuilder::new();
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            field: None,
            rva: None,
        }
    }

    /// Sets the field row index for the field with initial data.
    ///
    /// The field must be a valid Field table row index or placeholder that represents
    /// the field that has initial data stored at the specified RVA location.
    ///
    /// # Arguments
    ///
    /// * `field` - Row index or placeholder of the Field table entry
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use dotscope::prelude::*;
    /// # use std::path::Path;
    /// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
    /// # let mut assembly = CilAssembly::new(view);
    /// let field_sig = vec![0x06]; // Simple type signature
    /// let field_ref = FieldBuilder::new()
    ///     .name("StaticArray")
    ///     .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
    ///     .signature(&field_sig)
    ///     .build(&mut assembly)?;
    ///
    /// let builder = FieldRVABuilder::new()
    ///     .field(field_ref.placeholder());
    /// # Ok::<(), dotscope::Error>(())
    /// ```
    #[must_use]
    pub fn field(mut self, field: u32) -> Self {
        self.field = Some(field);
        self
    }

    /// Sets the RVA pointing to the field's initial data.
    ///
    /// The RVA (Relative Virtual Address) specifies the location within the PE file
    /// where the field's initial data is stored. This address is relative to the
    /// image base and must point to valid data.
    ///
    /// # Arguments
    ///
    /// * `rva` - The RVA value pointing to initial data
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use dotscope::prelude::*;
    /// let builder = FieldRVABuilder::new()
    ///     .rva(0x2000); // RVA pointing to initial data
    /// ```
    #[must_use]
    pub fn rva(mut self, rva: u32) -> Self {
        self.rva = Some(rva);
        self
    }

    /// Builds the FieldRVA entry and adds it to the assembly.
    ///
    /// This method validates all required fields, verifies the field row index is valid,
    /// validates the RVA value, creates the FieldRVA table entry, and returns a
    /// reference to the new entry.
    ///
    /// # Arguments
    ///
    /// * `assembly` - The assembly being modified
    ///
    /// # Returns
    ///
    /// Returns a reference to the newly created FieldRVA entry.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The field row index is not set
    /// - The field row index is 0
    /// - The RVA is not set
    /// - The RVA value is 0
    /// - There are issues adding the table row
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use dotscope::prelude::*;
    /// # use std::path::Path;
    /// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
    /// # let mut assembly = CilAssembly::new(view);
    /// # let field_ref = assembly.field_add(...)?;
    ///
    /// let field_rva_ref = FieldRVABuilder::new()
    ///     .field(field_ref.placeholder())
    ///     .rva(0x2000)
    ///     .build(&mut assembly)?;
    ///
    /// println!("Created FieldRVA entry");
    /// # Ok::<(), dotscope::Error>(())
    /// ```
    pub fn build(self, assembly: &mut CilAssembly) -> Result<ChangeRefRc> {
        let field = self.field.ok_or_else(|| {
            Error::ModificationInvalid("Field row index is required for FieldRVA".to_string())
        })?;

        let rva = self.rva.ok_or_else(|| {
            Error::ModificationInvalid("RVA is required for FieldRVA".to_string())
        })?;

        if field == 0 {
            return Err(Error::ModificationInvalid(
                "Field row index cannot be 0".to_string(),
            ));
        }

        if rva == 0 {
            return Err(Error::ModificationInvalid("RVA cannot be 0".to_string()));
        }

        let field_rva = FieldRvaRaw {
            rid: 0,
            token: Token::new(0),
            offset: 0,
            rva,
            field,
        };

        assembly.table_row_add(TableId::FieldRVA, TableDataOwned::FieldRVA(field_rva))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        cilassembly::ChangeRefKind,
        metadata::tables::{FieldAttributes, TableId},
        test::factories::table::assemblyref::get_test_assembly,
    };

    #[test]
    fn test_field_rva_builder_basic() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create a Field for testing
        let field_ref = crate::metadata::tables::FieldBuilder::new()
            .name("StaticData")
            .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
            .signature(&[0x06]) // Simple signature
            .build(&mut assembly)?;

        let rva_ref = FieldRVABuilder::new()
            .field(field_ref.placeholder())
            .rva(0x2000)
            .build(&mut assembly)?;

        // Verify the ref has the correct kind
        assert_eq!(rva_ref.kind(), ChangeRefKind::TableRow(TableId::FieldRVA));

        Ok(())
    }

    #[test]
    fn test_field_rva_builder_default() -> Result<()> {
        let builder = FieldRVABuilder::default();
        assert!(builder.field.is_none());
        assert!(builder.rva.is_none());
        Ok(())
    }

    #[test]
    fn test_field_rva_builder_missing_field() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        let result = FieldRVABuilder::new().rva(0x2000).build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Field row index is required"));

        Ok(())
    }

    #[test]
    fn test_field_rva_builder_missing_rva() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create a Field for testing
        let field_ref = crate::metadata::tables::FieldBuilder::new()
            .name("StaticData")
            .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
            .signature(&[0x06])
            .build(&mut assembly)?;

        let result = FieldRVABuilder::new()
            .field(field_ref.placeholder())
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("RVA is required"));

        Ok(())
    }

    #[test]
    fn test_field_rva_builder_zero_field() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Use a zero field row index (invalid)
        let result = FieldRVABuilder::new()
            .field(0)
            .rva(0x2000)
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Field row index cannot be 0"));

        Ok(())
    }

    #[test]
    fn test_field_rva_builder_zero_rva() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create a Field for testing
        let field_ref = crate::metadata::tables::FieldBuilder::new()
            .name("StaticData")
            .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
            .signature(&[0x06])
            .build(&mut assembly)?;

        let result = FieldRVABuilder::new()
            .field(field_ref.placeholder())
            .rva(0) // Zero RVA is invalid
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("RVA cannot be 0"));

        Ok(())
    }

    #[test]
    fn test_field_rva_builder_multiple_entries() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create Fields for testing
        let field1_ref = crate::metadata::tables::FieldBuilder::new()
            .name("StaticData1")
            .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
            .signature(&[0x06])
            .build(&mut assembly)?;

        let field2_ref = crate::metadata::tables::FieldBuilder::new()
            .name("StaticData2")
            .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
            .signature(&[0x06])
            .build(&mut assembly)?;

        let rva1_ref = FieldRVABuilder::new()
            .field(field1_ref.placeholder())
            .rva(0x2000)
            .build(&mut assembly)?;

        let rva2_ref = FieldRVABuilder::new()
            .field(field2_ref.placeholder())
            .rva(0x3000)
            .build(&mut assembly)?;

        // Verify refs are different
        assert!(!std::sync::Arc::ptr_eq(&rva1_ref, &rva2_ref));
        assert_eq!(rva1_ref.kind(), ChangeRefKind::TableRow(TableId::FieldRVA));
        assert_eq!(rva2_ref.kind(), ChangeRefKind::TableRow(TableId::FieldRVA));

        Ok(())
    }

    #[test]
    fn test_field_rva_builder_various_rva_values() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Test with different RVA values
        let test_rvas = [0x1000, 0x2000, 0x4000, 0x8000, 0x10000];

        for (i, &rva) in test_rvas.iter().enumerate() {
            let field_ref = crate::metadata::tables::FieldBuilder::new()
                .name(format!("StaticData{i}"))
                .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
                .signature(&[0x06])
                .build(&mut assembly)?;

            let rva_ref = FieldRVABuilder::new()
                .field(field_ref.placeholder())
                .rva(rva)
                .build(&mut assembly)?;

            assert_eq!(rva_ref.kind(), ChangeRefKind::TableRow(TableId::FieldRVA));
        }

        Ok(())
    }

    #[test]
    fn test_field_rva_builder_fluent_api() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create a Field for testing
        let field_ref = crate::metadata::tables::FieldBuilder::new()
            .name("FluentTestField")
            .flags(FieldAttributes::STATIC | FieldAttributes::PRIVATE)
            .signature(&[0x06])
            .build(&mut assembly)?;

        // Test fluent API chaining
        let rva_ref = FieldRVABuilder::new()
            .field(field_ref.placeholder())
            .rva(0x5000)
            .build(&mut assembly)?;

        assert_eq!(rva_ref.kind(), ChangeRefKind::TableRow(TableId::FieldRVA));

        Ok(())
    }

    #[test]
    fn test_field_rva_builder_clone() {
        let builder1 = FieldRVABuilder::new().field(1).rva(0x2000);
        let builder2 = builder1.clone();

        assert_eq!(builder1.field, builder2.field);
        assert_eq!(builder1.rva, builder2.rva);
    }

    #[test]
    fn test_field_rva_builder_debug() {
        let builder = FieldRVABuilder::new().field(1).rva(0x2000);
        let debug_str = format!("{builder:?}");
        assert!(debug_str.contains("FieldRVABuilder"));
    }
}