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
//! Factory methods for raw modification operation validation testing.
//!
//! Contains helper methods migrated from raw modification operation validation source files
//! for creating test assemblies with various operation validation scenarios.

use crate::{
    cilassembly::{AssemblyChanges, CilAssembly, Operation, TableModifications, TableOperation},
    metadata::{
        tables::{CodedIndex, CodedIndexType, TableDataOwned, TableId, TypeDefRaw},
        token::Token,
    },
    test::{get_testfile_wb, TestAssembly},
    Error, Result,
};
/// Test factory for RawOperationValidator following the golden pattern.
///
/// Creates test assemblies covering basic operation validation scenarios.
/// Note: This validator primarily uses direct corruption testing rather than file-based tests.
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn raw_operation_validator_file_factory() -> Result<Vec<TestAssembly>> {
    let mut assemblies = Vec::new();

    // 1. Clean test assembly (should pass all operation validation when no modifications)
    if let Some(clean_path) = get_testfile_wb() {
        assemblies.push(TestAssembly::new(clean_path, true));
    }

    // Note: File-based corruption testing is complex for modification validators
    // since they only run during modification validation contexts. Instead,
    // we use the direct corruption test (test_raw_operation_validator_direct_corruption)
    // which creates corrupted modifications in memory and tests them directly.

    Ok(assemblies)
}

/// Helper function to create a dummy TypeDef for testing purposes
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_dummy_typedef(rid: u32, flags: u32) -> TypeDefRaw {
    TypeDefRaw {
        rid,
        token: Token::new(0x02000000 | rid),
        offset: 0,
        flags,
        type_name: 1,
        type_namespace: 0,
        extends: CodedIndex {
            tag: TableId::TypeDef,
            row: 0,
            token: Token::new(0),
            ci_type: CodedIndexType::TypeDefOrRef,
        },
        field_list: 1,
        method_list: 1,
    }
}

/// Creates corrupted changes with invalid RID zero
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_corrupted_changes_with_invalid_rid_zero() -> AssemblyChanges {
    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1);

    let invalid_typedef = create_dummy_typedef(1, 0);
    let table_data = TableDataOwned::TypeDef(invalid_typedef);

    // Create operation with invalid RID 0
    let invalid_op = TableOperation::new(Operation::Insert(0, table_data));

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        operations.push(invalid_op);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    corrupted_changes
}

/// Creates corrupted changes with excessive RID
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_corrupted_changes_with_excessive_rid() -> AssemblyChanges {
    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1);

    let invalid_typedef = create_dummy_typedef(1, 0);
    let table_data = TableDataOwned::TypeDef(invalid_typedef);

    // Create operation with RID exceeding 0xFFFFFF (24-bit limit)
    let invalid_op = TableOperation::new(Operation::Insert(0x1000000, table_data));

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        operations.push(invalid_op);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    corrupted_changes
}

/// Creates corrupted changes with nonexistent target
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_corrupted_changes_with_nonexistent_target() -> AssemblyChanges {
    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1); // original_row_count = 0

    let invalid_typedef = create_dummy_typedef(1, 0);
    let table_data = TableDataOwned::TypeDef(invalid_typedef);

    // Create update operation targeting RID 999 that doesn't exist
    let invalid_op = TableOperation::new(Operation::Update(999, table_data));

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        operations.push(invalid_op);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    corrupted_changes
}

/// Creates corrupted changes with update after delete
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_corrupted_changes_with_update_after_delete() -> AssemblyChanges {
    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(2); // original_row_count = 1

    let typedef_data = create_dummy_typedef(1, 0);
    let table_data = TableDataOwned::TypeDef(typedef_data);

    // Create delete operation followed by update operation (invalid sequence)
    let delete_op = TableOperation::new_with_timestamp(Operation::Delete(1), 1000);
    let update_op = TableOperation::new_with_timestamp(Operation::Update(1, table_data), 2000);

    if let TableModifications::Sparse {
        operations,
        deleted_rows,
        ..
    } = &mut table_mods
    {
        operations.push(delete_op);
        operations.push(update_op);
        deleted_rows.insert(1); // Mark as deleted
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    corrupted_changes
}

/// Creates corrupted changes with excessive updates
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_corrupted_changes_with_excessive_updates() -> AssemblyChanges {
    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(2); // original_row_count = 1

    let typedef_data = create_dummy_typedef(1, 0);

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        // Add more than 10 update operations on same RID (limit is 10)
        for i in 0..12 {
            let table_data = TableDataOwned::TypeDef(typedef_data.clone());
            let update_op = TableOperation::new_with_timestamp(
                Operation::Update(1, table_data),
                1000 + i as u64,
            );
            operations.push(update_op);
        }
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    corrupted_changes
}

/// Creates corrupted changes with unordered operations
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_corrupted_changes_with_unordered_operations() -> AssemblyChanges {
    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1);

    let typedef_data1 = create_dummy_typedef(1, 0);
    let typedef_data2 = create_dummy_typedef(2, 1);

    // Create operations with non-chronological timestamps
    let op1 = TableOperation::new_with_timestamp(
        Operation::Insert(1, TableDataOwned::TypeDef(typedef_data1)),
        2000, // Later timestamp
    );
    let op2 = TableOperation::new_with_timestamp(
        Operation::Insert(2, TableDataOwned::TypeDef(typedef_data2)),
        1000, // Earlier timestamp
    );

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        // Add in wrong order (later timestamp first)
        operations.push(op1);
        operations.push(op2);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    corrupted_changes
}

/// Creates corrupted changes with conflicting inserts
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_corrupted_changes_with_conflicting_inserts() -> AssemblyChanges {
    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1);

    let typedef_data1 = create_dummy_typedef(1, 0);
    let typedef_data2 = create_dummy_typedef(2, 1);

    // Create multiple insert operations targeting the same RID
    let op1 = TableOperation::new_with_timestamp(
        Operation::Insert(1, TableDataOwned::TypeDef(typedef_data1)),
        1000,
    );
    let op2 = TableOperation::new_with_timestamp(
        Operation::Insert(1, TableDataOwned::TypeDef(typedef_data2)),
        2000,
    );

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        operations.push(op1);
        operations.push(op2);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    corrupted_changes
}

/// Creates assembly with invalid RID zero
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_assembly_with_invalid_rid_zero() -> Result<tempfile::NamedTempFile> {
    let Some(clean_testfile) = get_testfile_wb() else {
        return Err(Error::Other("WindowsBase.dll not available".to_string()));
    };

    // Load clean assembly and create CilAssembly
    let assembly = CilAssembly::from_path(&clean_testfile)?;

    // Create corrupted modification directly by manipulating the changes structure
    let mut corrupted_changes = AssemblyChanges::empty();

    // Create table modifications with invalid RID 0 operation
    let mut table_mods = TableModifications::new_sparse(1);

    // Create a fake TypeDef data to use in the invalid operation
    let invalid_typedef = create_dummy_typedef(1, 0);
    let table_data = TableDataOwned::TypeDef(invalid_typedef);

    // Create operation with invalid RID 0
    let invalid_op = TableOperation::new(Operation::Insert(0, table_data));

    // Force the invalid operation into the table modifications
    // This bypasses the normal validation that would prevent RID 0
    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        operations.push(invalid_op);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);

    // Write to temporary file with the corrupted changes
    create_temp_assembly_with_changes(assembly, corrupted_changes)
}

/// Creates assembly with excessive RID
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_assembly_with_excessive_rid() -> Result<tempfile::NamedTempFile> {
    let Some(clean_testfile) = get_testfile_wb() else {
        return Err(Error::Other("WindowsBase.dll not available".to_string()));
    };

    let assembly = CilAssembly::from_path(&clean_testfile)?;

    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1);

    let invalid_typedef = create_dummy_typedef(1, 0);
    let table_data = TableDataOwned::TypeDef(invalid_typedef);

    // Create operation with RID exceeding 0xFFFFFF (24-bit limit)
    let invalid_op = TableOperation::new(Operation::Insert(0x1000000, table_data));

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        operations.push(invalid_op);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    create_temp_assembly_with_changes(assembly, corrupted_changes)
}

/// Creates assembly with nonexistent target
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_assembly_with_nonexistent_target() -> Result<tempfile::NamedTempFile> {
    let Some(clean_testfile) = get_testfile_wb() else {
        return Err(Error::Other("WindowsBase.dll not available".to_string()));
    };

    let assembly = CilAssembly::from_path(&clean_testfile)?;

    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1); // original_row_count = 0

    let invalid_typedef = create_dummy_typedef(1, 0);
    let table_data = TableDataOwned::TypeDef(invalid_typedef);

    // Create update operation targeting RID 999 that doesn't exist
    let invalid_op = TableOperation::new(Operation::Update(999, table_data));

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        operations.push(invalid_op);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    create_temp_assembly_with_changes(assembly, corrupted_changes)
}

/// Creates assembly with update after delete
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_assembly_with_update_after_delete() -> Result<tempfile::NamedTempFile> {
    let Some(clean_testfile) = get_testfile_wb() else {
        return Err(Error::Other("WindowsBase.dll not available".to_string()));
    };

    let assembly = CilAssembly::from_path(&clean_testfile)?;

    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(2); // original_row_count = 1

    let typedef_data = create_dummy_typedef(1, 0);
    let table_data = TableDataOwned::TypeDef(typedef_data);

    // Create delete operation followed by update operation (invalid sequence)
    let delete_op = TableOperation::new_with_timestamp(Operation::Delete(1), 1000);
    let update_op = TableOperation::new_with_timestamp(Operation::Update(1, table_data), 2000);

    if let TableModifications::Sparse {
        operations,
        deleted_rows,
        ..
    } = &mut table_mods
    {
        operations.push(delete_op);
        operations.push(update_op);
        deleted_rows.insert(1); // Mark as deleted
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    create_temp_assembly_with_changes(assembly, corrupted_changes)
}

/// Creates assembly with excessive updates
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_assembly_with_excessive_updates() -> Result<tempfile::NamedTempFile> {
    let Some(clean_testfile) = get_testfile_wb() else {
        return Err(Error::Other("WindowsBase.dll not available".to_string()));
    };

    let assembly = CilAssembly::from_path(&clean_testfile)?;

    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(2); // original_row_count = 1

    let typedef_data = create_dummy_typedef(1, 0);

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        // Add more than 10 update operations on same RID (limit is 10)
        for i in 0..12 {
            let table_data = TableDataOwned::TypeDef(typedef_data.clone());
            let update_op = TableOperation::new_with_timestamp(
                Operation::Update(1, table_data),
                1000 + i as u64,
            );
            operations.push(update_op);
        }
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    create_temp_assembly_with_changes(assembly, corrupted_changes)
}

/// Creates assembly with unordered operations
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_assembly_with_unordered_operations() -> Result<tempfile::NamedTempFile> {
    let Some(clean_testfile) = get_testfile_wb() else {
        return Err(Error::Other("WindowsBase.dll not available".to_string()));
    };

    let assembly = CilAssembly::from_path(&clean_testfile)?;

    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1);

    let typedef_data1 = create_dummy_typedef(1, 0);
    let typedef_data2 = create_dummy_typedef(2, 1);

    // Create operations with non-chronological timestamps
    let op1 = TableOperation::new_with_timestamp(
        Operation::Insert(1, TableDataOwned::TypeDef(typedef_data1)),
        2000, // Later timestamp
    );
    let op2 = TableOperation::new_with_timestamp(
        Operation::Insert(2, TableDataOwned::TypeDef(typedef_data2)),
        1000, // Earlier timestamp
    );

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        // Add in wrong order (later timestamp first)
        operations.push(op1);
        operations.push(op2);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    create_temp_assembly_with_changes(assembly, corrupted_changes)
}

/// Creates assembly with conflicting inserts
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_assembly_with_conflicting_inserts() -> Result<tempfile::NamedTempFile> {
    let Some(clean_testfile) = get_testfile_wb() else {
        return Err(Error::Other("WindowsBase.dll not available".to_string()));
    };

    let assembly = CilAssembly::from_path(&clean_testfile)?;

    let mut corrupted_changes = AssemblyChanges::empty();
    let mut table_mods = TableModifications::new_sparse(1);

    let typedef_data1 = create_dummy_typedef(1, 0);
    let typedef_data2 = create_dummy_typedef(2, 1);

    // Create multiple insert operations targeting the same RID
    let op1 = TableOperation::new_with_timestamp(
        Operation::Insert(1, TableDataOwned::TypeDef(typedef_data1)),
        1000,
    );
    let op2 = TableOperation::new_with_timestamp(
        Operation::Insert(1, TableDataOwned::TypeDef(typedef_data2)),
        2000,
    );

    if let TableModifications::Sparse { operations, .. } = &mut table_mods {
        operations.push(op1);
        operations.push(op2);
    }

    corrupted_changes
        .table_changes
        .insert(TableId::TypeDef, table_mods);
    create_temp_assembly_with_changes(assembly, corrupted_changes)
}

/// Creates temporary assembly with changes
///
/// Originally from: `src/metadata/validation/validators/raw/modification/operation.rs`
pub fn create_temp_assembly_with_changes(
    _assembly: CilAssembly,
    _corrupted_changes: AssemblyChanges,
) -> Result<tempfile::NamedTempFile> {
    let temp_file = tempfile::NamedTempFile::new()?;
    let temp_path = temp_file.path();

    use std::fs;

    if let Some(clean_testfile) = get_testfile_wb() {
        fs::copy(clean_testfile, temp_path)?;
    }

    Ok(temp_file)
}