dotscope 0.7.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
//! Benchmarks for signature parsing.
//!
//! Tests parsing performance for various .NET metadata signature types:
//! - Method signatures (simple, generic, varargs)
//! - Field signatures (primitives, arrays, generics)
//! - Property signatures
//! - Local variable signatures
//! - Type specification signatures
//! - Method specification signatures

extern crate dotscope;

use criterion::{criterion_group, criterion_main, Criterion};
use dotscope::metadata::signatures::{
    parse_field_signature, parse_local_var_signature, parse_method_signature,
    parse_method_spec_signature, parse_property_signature, parse_type_spec_signature,
};
use std::hint::black_box;

/// Benchmark parsing a simple void method with no parameters.
/// Signature: void Method()
fn bench_method_signature_void_no_params(c: &mut Criterion) {
    // DEFAULT calling convention, 0 params, VOID return
    let signature = [0x00, 0x00, 0x01];

    c.bench_function("sig_method_void_no_params", |b| {
        b.iter(|| {
            let sig = parse_method_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a method with primitive parameters.
/// Signature: int Method(int a, string b, bool c)
fn bench_method_signature_primitives(c: &mut Criterion) {
    // DEFAULT, 3 params, I4 return, I4, STRING, BOOLEAN params
    let signature = [0x00, 0x03, 0x08, 0x08, 0x0E, 0x02];

    c.bench_function("sig_method_primitives", |b| {
        b.iter(|| {
            let sig = parse_method_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing an instance method (has 'this' parameter).
/// Signature: void Instance.Method(int a)
fn bench_method_signature_instance(c: &mut Criterion) {
    // HASTHIS, 1 param, VOID return, I4 param
    let signature = [0x20, 0x01, 0x01, 0x08];

    c.bench_function("sig_method_instance", |b| {
        b.iter(|| {
            let sig = parse_method_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a generic method signature.
/// Signature: T Method<T>(T item)
fn bench_method_signature_generic(c: &mut Criterion) {
    // HASTHIS | GENERIC, 1 generic param, 1 method param, MVAR(0) return, MVAR(0) param
    let signature = [0x30, 0x01, 0x01, 0x1E, 0x00, 0x1E, 0x00];

    c.bench_function("sig_method_generic", |b| {
        b.iter(|| {
            let sig = parse_method_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a method with multiple generic parameters.
/// Signature: TResult Method<T, TResult>(T input)
fn bench_method_signature_multi_generic(c: &mut Criterion) {
    // HASTHIS | GENERIC, 2 generic params, 1 method param, MVAR(1) return, MVAR(0) param
    let signature = [0x30, 0x02, 0x01, 0x1E, 0x01, 0x1E, 0x00];

    c.bench_function("sig_method_multi_generic", |b| {
        b.iter(|| {
            let sig = parse_method_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a method with byref parameter.
/// Signature: void Method(ref int a, out string b)
fn bench_method_signature_byref(c: &mut Criterion) {
    // DEFAULT, 2 params, VOID return, BYREF I4, BYREF STRING
    let signature = [0x00, 0x02, 0x01, 0x10, 0x08, 0x10, 0x0E];

    c.bench_function("sig_method_byref", |b| {
        b.iter(|| {
            let sig = parse_method_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a method returning an array.
/// Signature: int[] Method()
fn bench_method_signature_array_return(c: &mut Criterion) {
    // DEFAULT, 0 params, SZARRAY I4 return
    let signature = [0x00, 0x00, 0x1D, 0x08];

    c.bench_function("sig_method_array_return", |b| {
        b.iter(|| {
            let sig = parse_method_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a method with many parameters.
/// Signature: void Method(int, int, int, int, int, int, int, int)
fn bench_method_signature_many_params(c: &mut Criterion) {
    // DEFAULT, 8 params, VOID return, 8x I4
    let signature = [
        0x00, 0x08, 0x01, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
    ];

    c.bench_function("sig_method_many_params", |b| {
        b.iter(|| {
            let sig = parse_method_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a simple primitive field.
/// Signature: int field
fn bench_field_signature_primitive(c: &mut Criterion) {
    // FIELD, I4
    let signature = [0x06, 0x08];

    c.bench_function("sig_field_primitive", |b| {
        b.iter(|| {
            let sig = parse_field_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a string field.
/// Signature: string field
fn bench_field_signature_string(c: &mut Criterion) {
    // FIELD, STRING
    let signature = [0x06, 0x0E];

    c.bench_function("sig_field_string", |b| {
        b.iter(|| {
            let sig = parse_field_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing an array field.
/// Signature: int[] field
fn bench_field_signature_array(c: &mut Criterion) {
    // FIELD, SZARRAY, I4
    let signature = [0x06, 0x1D, 0x08];

    c.bench_function("sig_field_array", |b| {
        b.iter(|| {
            let sig = parse_field_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a generic parameter field.
/// Signature: T field (in generic type)
fn bench_field_signature_generic_param(c: &mut Criterion) {
    // FIELD, VAR(0)
    let signature = [0x06, 0x13, 0x00];

    c.bench_function("sig_field_generic_param", |b| {
        b.iter(|| {
            let sig = parse_field_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a field with a class type reference.
/// Signature: SomeClass field
fn bench_field_signature_class(c: &mut Criterion) {
    // FIELD, CLASS, TypeRef token (compressed: 0x49 = token 0x01000012)
    let signature = [0x06, 0x12, 0x49];

    c.bench_function("sig_field_class", |b| {
        b.iter(|| {
            let sig = parse_field_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a simple property getter.
/// Signature: int Property { get; }
fn bench_property_signature_simple(c: &mut Criterion) {
    // PROPERTY | HASTHIS, 0 params, I4 return
    let signature = [0x28, 0x00, 0x08];

    c.bench_function("sig_property_simple", |b| {
        b.iter(|| {
            let sig = parse_property_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing an indexer property.
/// Signature: string this[int index] { get; }
fn bench_property_signature_indexer(c: &mut Criterion) {
    // PROPERTY | HASTHIS, 1 param, STRING return, I4 index param
    let signature = [0x28, 0x01, 0x0E, 0x08];

    c.bench_function("sig_property_indexer", |b| {
        b.iter(|| {
            let sig = parse_property_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a static property.
/// Signature: static int Property { get; }
fn bench_property_signature_static(c: &mut Criterion) {
    // PROPERTY (no HASTHIS), 0 params, I4 return
    let signature = [0x08, 0x00, 0x08];

    c.bench_function("sig_property_static", |b| {
        b.iter(|| {
            let sig = parse_property_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a single local variable.
/// Locals: int a
fn bench_local_var_signature_single(c: &mut Criterion) {
    // LOCAL_SIG, 1 variable, I4
    let signature = [0x07, 0x01, 0x08];

    c.bench_function("sig_localvar_single", |b| {
        b.iter(|| {
            let sig = parse_local_var_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing multiple local variables.
/// Locals: int a; string b; bool c; object d
fn bench_local_var_signature_multiple(c: &mut Criterion) {
    // LOCAL_SIG, 4 variables, I4, STRING, BOOLEAN, OBJECT
    let signature = [0x07, 0x04, 0x08, 0x0E, 0x02, 0x1C];

    c.bench_function("sig_localvar_multiple", |b| {
        b.iter(|| {
            let sig = parse_local_var_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing local variables with byref.
/// Locals: ref int a; ref string b
fn bench_local_var_signature_byref(c: &mut Criterion) {
    // LOCAL_SIG, 2 variables, BYREF I4, BYREF STRING
    let signature = [0x07, 0x02, 0x10, 0x08, 0x10, 0x0E];

    c.bench_function("sig_localvar_byref", |b| {
        b.iter(|| {
            let sig = parse_local_var_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing local variables with pinned.
/// Locals: pinned int* ptr
fn bench_local_var_signature_pinned(c: &mut Criterion) {
    // LOCAL_SIG, 1 variable, PINNED PTR I4
    let signature = [0x07, 0x01, 0x45, 0x0F, 0x08];

    c.bench_function("sig_localvar_pinned", |b| {
        b.iter(|| {
            let sig = parse_local_var_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing many local variables (typical complex method).
/// Locals: 10 variables of various types
fn bench_local_var_signature_many(c: &mut Criterion) {
    // LOCAL_SIG, 10 variables
    let signature = [
        0x07, 0x0A, // LOCAL_SIG, 10 vars
        0x08, // I4
        0x0E, // STRING
        0x02, // BOOLEAN
        0x1C, // OBJECT
        0x08, // I4
        0x0E, // STRING
        0x08, // I4
        0x08, // I4
        0x1C, // OBJECT
        0x02, // BOOLEAN
    ];

    c.bench_function("sig_localvar_many", |b| {
        b.iter(|| {
            let sig = parse_local_var_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a generic type instantiation.
/// Type: List<int>
fn bench_type_spec_generic_simple(c: &mut Criterion) {
    // GENERICINST, CLASS, TypeRef token, 1 type arg, I4
    let signature = [0x15, 0x12, 0x49, 0x01, 0x08];

    c.bench_function("sig_typespec_generic_simple", |b| {
        b.iter(|| {
            let sig = parse_type_spec_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a generic type with multiple type arguments.
/// Type: Dictionary<string, int>
fn bench_type_spec_generic_multi_arg(c: &mut Criterion) {
    // GENERICINST, CLASS, TypeRef token, 2 type args, STRING, I4
    let signature = [0x15, 0x12, 0x49, 0x02, 0x0E, 0x08];

    c.bench_function("sig_typespec_generic_multi_arg", |b| {
        b.iter(|| {
            let sig = parse_type_spec_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing an array type specification.
/// Type: int[]
fn bench_type_spec_array(c: &mut Criterion) {
    // SZARRAY, I4
    let signature = [0x1D, 0x08];

    c.bench_function("sig_typespec_array", |b| {
        b.iter(|| {
            let sig = parse_type_spec_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a pointer type specification.
/// Type: int*
fn bench_type_spec_pointer(c: &mut Criterion) {
    // PTR, I4
    let signature = [0x0F, 0x08];

    c.bench_function("sig_typespec_pointer", |b| {
        b.iter(|| {
            let sig = parse_type_spec_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a generic parameter type specification.
/// Type: T (from enclosing generic type)
fn bench_type_spec_generic_param(c: &mut Criterion) {
    // VAR(0)
    let signature = [0x13, 0x00];

    c.bench_function("sig_typespec_generic_param", |b| {
        b.iter(|| {
            let sig = parse_type_spec_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a method specification with one type argument.
/// Method<int>
fn bench_method_spec_single(c: &mut Criterion) {
    // GENRICINST, 1 type arg, I4
    let signature = [0x0A, 0x01, 0x08];

    c.bench_function("sig_methodspec_single", |b| {
        b.iter(|| {
            let sig = parse_method_spec_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a method specification with multiple type arguments.
/// Method<int, string, bool>
fn bench_method_spec_multiple(c: &mut Criterion) {
    // GENRICINST, 3 type args, I4, STRING, BOOLEAN
    let signature = [0x0A, 0x03, 0x08, 0x0E, 0x02];

    c.bench_function("sig_methodspec_multiple", |b| {
        b.iter(|| {
            let sig = parse_method_spec_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

/// Benchmark parsing a method specification with nested generic type argument.
/// Method<List<int>>
fn bench_method_spec_nested_generic(c: &mut Criterion) {
    // GENRICINST, 1 type arg, GENERICINST CLASS TypeRef 1 arg I4
    let signature = [0x0A, 0x01, 0x15, 0x12, 0x49, 0x01, 0x08];

    c.bench_function("sig_methodspec_nested_generic", |b| {
        b.iter(|| {
            let sig = parse_method_spec_signature(black_box(&signature)).unwrap();
            black_box(sig)
        });
    });
}

criterion_group!(
    benches,
    // Method signatures
    bench_method_signature_void_no_params,
    bench_method_signature_primitives,
    bench_method_signature_instance,
    bench_method_signature_generic,
    bench_method_signature_multi_generic,
    bench_method_signature_byref,
    bench_method_signature_array_return,
    bench_method_signature_many_params,
    // Field signatures
    bench_field_signature_primitive,
    bench_field_signature_string,
    bench_field_signature_array,
    bench_field_signature_generic_param,
    bench_field_signature_class,
    // Property signatures
    bench_property_signature_simple,
    bench_property_signature_indexer,
    bench_property_signature_static,
    // Local variable signatures
    bench_local_var_signature_single,
    bench_local_var_signature_multiple,
    bench_local_var_signature_byref,
    bench_local_var_signature_pinned,
    bench_local_var_signature_many,
    // Type specification signatures
    bench_type_spec_generic_simple,
    bench_type_spec_generic_multi_arg,
    bench_type_spec_array,
    bench_type_spec_pointer,
    bench_type_spec_generic_param,
    // Method specification signatures
    bench_method_spec_single,
    bench_method_spec_multiple,
    bench_method_spec_nested_generic,
);
criterion_main!(benches);