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
//! StandAloneSigBuilder for creating standalone signature specifications.
//!
//! This module provides [`crate::metadata::tables::standalonesig::StandAloneSigBuilder`] for creating StandAloneSig table entries
//! with a fluent API. Standalone signatures provide metadata signatures that are not
//! directly associated with specific methods, fields, or properties, supporting complex
//! scenarios like method pointers, local variables, and dynamic signature generation.

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

/// Builder for creating StandAloneSig metadata entries.
///
/// `StandAloneSigBuilder` provides a fluent API for creating StandAloneSig table entries
/// with validation and automatic blob management. Standalone signatures are used for
/// various metadata scenarios including method pointers, local variable declarations,
/// and CIL instruction operands that require signature information.
///
/// # Standalone Signature Model
///
/// .NET standalone signatures follow a flexible architecture:
/// - **Signature Blob**: Binary representation of type and calling convention information
/// - **Multiple Uses**: Same signature can be referenced from multiple contexts
/// - **Type Resolution**: Signatures contain encoded type references and specifications
/// - **Calling Conventions**: Method signatures include calling convention information
/// - **Local Variables**: Method local variable type declarations
/// - **Generic Support**: Generic type parameters and constraints
///
/// # Signature Types and Scenarios
///
/// Different signature patterns serve various metadata scenarios:
/// - **Method Signatures**: Function pointer signatures with calling conventions and parameters
/// - **Local Variable Signatures**: Method local variable type declarations for proper runtime allocation
/// - **Field Signatures**: Standalone field type specifications for dynamic scenarios
/// - **Generic Signatures**: Generic type and method instantiation signatures with type constraints
/// - **Delegate Signatures**: Delegate type definitions with invoke method signatures
/// - **CIL Instruction Support**: Signatures referenced by CIL instructions like `calli` and `ldftn`
///
/// # Signature Blob Format
///
/// Signatures are stored as binary blobs containing:
/// - **Calling Convention**: Method calling convention flags and type
/// - **Parameter Count**: Number of parameters for method signatures
/// - **Return Type**: Return type specification for method signatures
/// - **Parameter Types**: Type specifications for each parameter
/// - **Generic Information**: Generic parameter count and constraints
/// - **Local Variables**: Local variable types and initialization information
///
/// # 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);
///
/// // Create a method signature for a function pointer
/// let method_signature = vec![
///     0x00, // Calling convention: DEFAULT
///     0x02, // Parameter count: 2
///     0x01, // Return type: ELEMENT_TYPE_VOID
///     0x08, // Parameter 1: ELEMENT_TYPE_I4 (int32)
///     0x0E, // Parameter 2: ELEMENT_TYPE_STRING
/// ];
///
/// let method_sig_token = StandAloneSigBuilder::new()
///     .signature(&method_signature)
///     .build(&mut assembly)?;
///
/// // Create a local variable signature
/// let locals_signature = vec![
///     0x07, // ELEMENT_TYPE_LOCALVAR signature
///     0x03, // Local variable count: 3
///     0x08, // Local 0: ELEMENT_TYPE_I4 (int32)
///     0x0E, // Local 1: ELEMENT_TYPE_STRING
///     0x1C, // Local 2: ELEMENT_TYPE_OBJECT
/// ];
///
/// let locals_sig_token = StandAloneSigBuilder::new()
///     .signature(&locals_signature)
///     .build(&mut assembly)?;
///
/// // Create a complex generic method signature
/// let generic_method_signature = vec![
///     0x10, // Calling convention: GENERIC
///     0x01, // Generic parameter count: 1
///     0x02, // Parameter count: 2
///     0x13, // Return type: ELEMENT_TYPE_VAR (generic parameter 0)
///     0x00, // Generic parameter index: 0
///     0x13, // Parameter 1: ELEMENT_TYPE_VAR (generic parameter 0)
///     0x00, // Generic parameter index: 0
///     0x08, // Parameter 2: ELEMENT_TYPE_I4 (int32)
/// ];
///
/// let generic_sig_token = StandAloneSigBuilder::new()
///     .signature(&generic_method_signature)
///     .build(&mut assembly)?;
///
/// // Create a delegate signature with multiple parameters
/// let delegate_signature = vec![
///     0x00, // Calling convention: DEFAULT
///     0x04, // Parameter count: 4
///     0x08, // Return type: ELEMENT_TYPE_I4 (int32)
///     0x0E, // Parameter 1: ELEMENT_TYPE_STRING
///     0x08, // Parameter 2: ELEMENT_TYPE_I4 (int32)
///     0x1C, // Parameter 3: ELEMENT_TYPE_OBJECT
///     0x01, // Parameter 4: ELEMENT_TYPE_VOID pointer
/// ];
///
/// let delegate_sig_token = StandAloneSigBuilder::new()
///     .signature(&delegate_signature)
///     .build(&mut assembly)?;
/// # Ok::<(), dotscope::Error>(())
/// ```
pub struct StandAloneSigBuilder {
    signature: Option<Vec<u8>>,
}

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

impl StandAloneSigBuilder {
    /// Creates a new StandAloneSigBuilder.
    ///
    /// # Returns
    ///
    /// A new [`crate::metadata::tables::standalonesig::StandAloneSigBuilder`] instance ready for configuration.
    #[must_use]
    pub fn new() -> Self {
        Self { signature: None }
    }

    /// Sets the signature blob data.
    ///
    /// Specifies the binary signature data that defines the type information,
    /// calling conventions, and parameter details for this standalone signature.
    /// The signature blob format follows the ECMA-335 specification for
    /// signature encoding.
    ///
    /// # Arguments
    ///
    /// * `data` - The signature blob data as a byte slice
    ///
    /// # Returns
    ///
    /// The builder instance for method chaining.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use dotscope::metadata::tables::StandAloneSigBuilder;
    /// let builder = StandAloneSigBuilder::new()
    ///     .signature(&[0x00, 0x01, 0x01]); // Simple void method signature
    /// ```
    #[must_use]
    pub fn signature(mut self, data: &[u8]) -> Self {
        self.signature = Some(data.to_vec());
        self
    }

    /// Builds the StandAloneSig entry and adds it to the assembly.
    ///
    /// Validates all required fields, adds the signature to the blob heap,
    /// creates the StandAloneSigRaw structure, and adds it to the assembly's
    /// StandAloneSig table. Returns a token that can be used to reference
    /// this standalone signature.
    ///
    /// # Arguments
    ///
    /// * `assembly` - CilAssembly for heap and table management
    ///
    /// # Returns
    ///
    /// Returns a `Result<Token>` containing the token for the new StandAloneSig entry,
    /// or an error if validation fails or required fields are missing.
    ///
    /// # Errors
    ///
    /// This method returns an error if:
    /// - `signature` is not specified (required field)
    /// - The signature blob is empty or invalid
    /// - Blob heap operations fail
    /// - Table operations fail
    ///
    /// # 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 signature_data = vec![0x00, 0x01, 0x01]; // Simple method signature
    /// let token = StandAloneSigBuilder::new()
    ///     .signature(&signature_data)
    ///     .build(&mut assembly)?;
    /// # Ok::<(), dotscope::Error>(())
    /// ```
    pub fn build(self, assembly: &mut CilAssembly) -> Result<ChangeRefRc> {
        let signature_data = self
            .signature
            .ok_or_else(|| Error::ModificationInvalid("signature field is required".to_string()))?;

        if signature_data.is_empty() {
            return Err(Error::ModificationInvalid(
                "signature cannot be empty".to_string(),
            ));
        }

        let signature_index = assembly.blob_add(&signature_data)?.placeholder();

        let standalonesig_raw = StandAloneSigRaw {
            rid: 0,
            token: Token::new(0),
            offset: 0,
            signature: signature_index,
        };

        assembly.table_row_add(
            TableId::StandAloneSig,
            TableDataOwned::StandAloneSig(standalonesig_raw),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::factories::table::assemblyref::get_test_assembly;
    use std::sync::Arc;

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

        let signature = vec![0x00, 0x01, 0x01]; // Simple method signature: DEFAULT, 1 param, VOID
        let _change_ref = StandAloneSigBuilder::new()
            .signature(&signature)
            .build(&mut assembly)?;

        Ok(())
    }

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

        // Method signature: DEFAULT calling convention, 2 params, returns I4, params: I4, STRING
        let method_signature = vec![
            0x00, // Calling convention: DEFAULT
            0x02, // Parameter count: 2
            0x08, // Return type: ELEMENT_TYPE_I4 (int32)
            0x08, // Parameter 1: ELEMENT_TYPE_I4 (int32)
            0x0E, // Parameter 2: ELEMENT_TYPE_STRING
        ];

        let _change_ref = StandAloneSigBuilder::new()
            .signature(&method_signature)
            .build(&mut assembly)?;

        Ok(())
    }

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

        // Local variable signature: 3 locals of types I4, STRING, OBJECT
        let locals_signature = vec![
            0x07, // ELEMENT_TYPE_LOCALVAR signature
            0x03, // Local variable count: 3
            0x08, // Local 0: ELEMENT_TYPE_I4 (int32)
            0x0E, // Local 1: ELEMENT_TYPE_STRING
            0x1C, // Local 2: ELEMENT_TYPE_OBJECT
        ];

        let _change_ref = StandAloneSigBuilder::new()
            .signature(&locals_signature)
            .build(&mut assembly)?;

        Ok(())
    }

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

        // Generic method signature: GENERIC calling convention, 1 generic param, 2 params
        let generic_signature = vec![
            0x10, // Calling convention: GENERIC
            0x01, // Generic parameter count: 1
            0x02, // Parameter count: 2
            0x13, // Return type: ELEMENT_TYPE_VAR (generic parameter 0)
            0x00, // Generic parameter index: 0
            0x13, // Parameter 1: ELEMENT_TYPE_VAR (generic parameter 0)
            0x00, // Generic parameter index: 0
            0x08, // Parameter 2: ELEMENT_TYPE_I4 (int32)
        ];

        let _change_ref = StandAloneSigBuilder::new()
            .signature(&generic_signature)
            .build(&mut assembly)?;

        Ok(())
    }

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

        // Complex signature with arrays and pointers
        let complex_signature = vec![
            0x00, // Calling convention: DEFAULT
            0x03, // Parameter count: 3
            0x01, // Return type: ELEMENT_TYPE_VOID
            0x1D, // Parameter 1: ELEMENT_TYPE_SZARRAY (single-dimensional array)
            0x08, // Array element type: ELEMENT_TYPE_I4 (int32[])
            0x0F, // Parameter 2: ELEMENT_TYPE_PTR (pointer)
            0x01, // Pointer target: ELEMENT_TYPE_VOID (void*)
            0x1C, // Parameter 3: ELEMENT_TYPE_OBJECT
        ];

        let _change_ref = StandAloneSigBuilder::new()
            .signature(&complex_signature)
            .build(&mut assembly)?;

        Ok(())
    }

    #[test]
    fn test_standalonesig_builder_missing_signature() {
        let mut assembly = get_test_assembly().unwrap();

        let result = StandAloneSigBuilder::new().build(&mut assembly);

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("signature"));
    }

    #[test]
    fn test_standalonesig_builder_empty_signature() {
        let mut assembly = get_test_assembly().unwrap();

        let result = StandAloneSigBuilder::new()
            .signature(&[])
            .build(&mut assembly);

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("signature cannot be empty"));
    }

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

        // Test Default trait implementation
        let signature = vec![0x00, 0x00, 0x01]; // No-param void method
        let _change_ref = StandAloneSigBuilder::default()
            .signature(&signature)
            .build(&mut assembly)?;

        Ok(())
    }

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

        // Create multiple different signatures
        let sig1 = vec![0x00, 0x00, 0x01]; // No-param void method
        let sig2 = vec![0x00, 0x01, 0x08, 0x08]; // One I4 param, returns I4
        let sig3 = vec![0x07, 0x02, 0x08, 0x0E]; // Two locals: I4, STRING

        let ref1 = StandAloneSigBuilder::new()
            .signature(&sig1)
            .build(&mut assembly)?;

        let ref2 = StandAloneSigBuilder::new()
            .signature(&sig2)
            .build(&mut assembly)?;

        let ref3 = StandAloneSigBuilder::new()
            .signature(&sig3)
            .build(&mut assembly)?;

        // All change refs should be different
        assert!(!Arc::ptr_eq(&ref1, &ref2));
        assert!(!Arc::ptr_eq(&ref2, &ref3));
        assert!(!Arc::ptr_eq(&ref1, &ref3));

        Ok(())
    }
}