forked from microsoft/perfview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigparser.cs
More file actions
816 lines (680 loc) · 23 KB
/
sigparser.cs
File metadata and controls
816 lines (680 loc) · 23 KB
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PerfView
{
internal struct SigParser
{
private byte[] _sig;
private int _len;
private int _offs;
public SigParser(byte[] sig, int len)
{
_sig = sig;
_len = len;
_offs = 0;
}
public SigParser(SigParser rhs)
{
_sig = rhs._sig;
_len = rhs._len;
_offs = rhs._offs;
}
public SigParser(IntPtr sig, int len)
{
if (len != 0)
{
_sig = new byte[len];
Marshal.Copy(sig, _sig, 0, _sig.Length);
}
else
{
_sig = null;
}
_len = len;
_offs = 0;
}
public bool IsNull()
{
return _sig == null;
}
private void CopyFrom(SigParser rhs)
{
_sig = rhs._sig;
_len = rhs._len;
_offs = rhs._offs;
}
private void SkipBytes(int bytes)
{
Debug.Assert(bytes <= _len);
_offs += bytes;
_len -= bytes;
Debug.Assert(_len <= 0 || _offs < _sig.Length);
}
private bool SkipInt()
{
int tmp;
return GetData(out tmp);
}
public bool GetData(out int data)
{
int size = 0;
if (UncompressData(out data, out size))
{
SkipBytes(size);
return true;
}
return false;
}
private bool GetByte(out byte data)
{
if (_len <= 0)
{
data = 0xcc;
return false;
}
data = _sig[_offs];
SkipBytes(1);
return true;
}
private bool PeekByte(out byte data)
{
if (_len <= 0)
{
data = 0xcc;
return false;
}
data = _sig[_offs];
return true;
}
private bool GetElemTypeSlow(out int etype)
{
SigParser sigTemp = new SigParser(this);
if (sigTemp.SkipCustomModifiers())
{
byte elemType;
if (sigTemp.GetByte(out elemType))
{
etype = elemType;
CopyFrom(sigTemp);
return true;
}
}
etype = 0;
return false;
}
public bool GetElemType(out int etype)
{
if (_len > 0)
{
byte type = _sig[_offs];
if (type < ELEMENT_TYPE_CMOD_REQD) // fast path with no modifiers: single byte
{
etype = type;
SkipBytes(1);
return true;
}
}
// Slower/normal path
return GetElemTypeSlow(out etype);
}
public bool PeekCallingConvInfo(out int data)
{
return PeekByte(out data);
}
// Note: Calling convention is always one byte, not four bytes
public bool GetCallingConvInfo(out int data)
{
if (PeekByte(out data))
{
SkipBytes(1);
return true;
}
return false;
}
private bool GetCallingConv(out int data)
{
if (GetCallingConvInfo(out data))
{
data &= IMAGE_CEE_CS_CALLCONV_MASK;
return true;
}
return false;
}
private bool PeekData(out int data)
{
int size;
return UncompressData(out data, out size);
}
private bool PeekElemTypeSlow(out int etype)
{
SigParser sigTemp = new SigParser(this);
return sigTemp.GetElemType(out etype);
}
public bool PeekElemType(out int etype)
{
if (_len > 0)
{
byte type = _sig[_offs];
if (type < ELEMENT_TYPE_CMOD_REQD)
{
etype = type;
return true;
}
}
return PeekElemTypeSlow(out etype);
}
private bool PeekElemTypeSize(out int pSize)
{
pSize = 0;
SigParser sigTemp = new SigParser(this);
if (!sigTemp.SkipAnyVASentinel())
{
return false;
}
byte bElementType = 0;
if (!sigTemp.GetByte(out bElementType))
{
return false;
}
switch (bElementType)
{
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
case ELEMENT_TYPE_R8:
pSize = 8;
break;
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
case ELEMENT_TYPE_R4:
pSize = 4;
break;
case ELEMENT_TYPE_I2:
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_CHAR:
pSize = 2;
break;
case ELEMENT_TYPE_I1:
case ELEMENT_TYPE_U1:
case ELEMENT_TYPE_BOOLEAN:
pSize = 1;
break;
case ELEMENT_TYPE_I:
case ELEMENT_TYPE_U:
case ELEMENT_TYPE_STRING:
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
case ELEMENT_TYPE_CLASS:
case ELEMENT_TYPE_OBJECT:
case ELEMENT_TYPE_FNPTR:
case ELEMENT_TYPE_TYPEDBYREF:
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
pSize = IntPtr.Size;
break;
case ELEMENT_TYPE_VOID:
break;
case ELEMENT_TYPE_END:
case ELEMENT_TYPE_CMOD_REQD:
case ELEMENT_TYPE_CMOD_OPT:
case ELEMENT_TYPE_VALUETYPE:
Debug.Fail("Asked for the size of an element that doesn't have a size!");
return false;
default:
Debug.Fail("CorSigGetElementTypeSize given invalid signature to size!");
return false;
}
return true; ;
}
private bool AtSentinel()
{
if (_len > 0)
{
return _sig[_offs] == ELEMENT_TYPE_SENTINEL;
}
else
{
return false;
}
}
public bool GetToken(out int token)
{
int size;
if (UncompressToken(out token, out size))
{
SkipBytes(size);
return true;
}
return false;
}
public bool SkipCustomModifiers()
{
SigParser sigTemp = new SigParser(this);
if (!sigTemp.SkipAnyVASentinel())
{
return false;
}
byte bElementType = 0;
if (!sigTemp.PeekByte(out bElementType))
{
return false;
}
while ((ELEMENT_TYPE_CMOD_REQD == bElementType) || (ELEMENT_TYPE_CMOD_OPT == bElementType))
{
sigTemp.SkipBytes(1);
int token;
if (!sigTemp.GetToken(out token))
{
return false;
}
if (!sigTemp.PeekByte(out bElementType))
{
return false;
}
}
// Following custom modifiers must be an element type value which is less than ELEMENT_TYPE_MAX, or one of the other element types
// that we support while parsing various signatures
if (bElementType >= ELEMENT_TYPE_MAX)
{
switch (bElementType)
{
case ELEMENT_TYPE_PINNED:
break;
default:
return false;
}
}
CopyFrom(sigTemp);
return true;
}
private bool SkipFunkyAndCustomModifiers()
{
SigParser sigTemp = new SigParser(this);
if (!sigTemp.SkipAnyVASentinel())
{
return false;
}
byte bElementType = 0;
if (!sigTemp.PeekByte(out bElementType))
{
return false;
}
while (ELEMENT_TYPE_CMOD_REQD == bElementType ||
ELEMENT_TYPE_CMOD_OPT == bElementType ||
ELEMENT_TYPE_MODIFIER == bElementType ||
ELEMENT_TYPE_PINNED == bElementType)
{
sigTemp.SkipBytes(1);
int token;
if (!sigTemp.GetToken(out token))
{
return false;
}
if (!sigTemp.PeekByte(out bElementType))
{
return false;
}
}
// Following custom modifiers must be an element type value which is less than ELEMENT_TYPE_MAX, or one of the other element types
// that we support while parsing various signatures
if (bElementType >= ELEMENT_TYPE_MAX)
{
switch (bElementType)
{
case ELEMENT_TYPE_PINNED:
break;
default:
return false;
}
}
CopyFrom(sigTemp);
return true;
}// SkipFunkyAndCustomModifiers
private bool SkipAnyVASentinel()
{
byte bElementType = 0;
if (!PeekByte(out bElementType))
{
return false;
}
if (bElementType == ELEMENT_TYPE_SENTINEL)
{
SkipBytes(1);
}
return true;
}// SkipAnyVASentinel
public bool IsPrimitive(int cet)
{
return cet >= ELEMENT_TYPE_BOOLEAN && cet <= ELEMENT_TYPE_R8
|| cet == ELEMENT_TYPE_I || cet == ELEMENT_TYPE_U
|| cet == ELEMENT_TYPE_PTR || cet == ELEMENT_TYPE_FNPTR;
}
public bool SkipExactlyOne()
{
int typ;
if (!GetElemType(out typ))
{
return false;
}
int tmp;
if (!IsPrimitive(typ))
{
switch (typ)
{
default:
return false;
case ELEMENT_TYPE_VAR:
case ELEMENT_TYPE_MVAR:
if (!GetData(out tmp))
{
return false;
}
break;
case ELEMENT_TYPE_OBJECT:
case ELEMENT_TYPE_STRING:
case ELEMENT_TYPE_TYPEDBYREF:
break;
case ELEMENT_TYPE_BYREF:
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_PINNED:
case ELEMENT_TYPE_SZARRAY:
if (!SkipExactlyOne())
{
return false;
}
break;
case ELEMENT_TYPE_VALUETYPE:
case ELEMENT_TYPE_CLASS:
if (!GetToken(out tmp))
{
return false;
}
break;
case ELEMENT_TYPE_FNPTR:
if (!SkipSignature())
{
return false;
}
break;
case ELEMENT_TYPE_ARRAY:
// Skip element type
if (!SkipExactlyOne())
{
return false;
}
// Get rank;
int rank;
if (!GetData(out rank))
{
return false;
}
if (rank > 0)
{
int sizes;
if (!GetData(out sizes))
{
return false;
}
while (sizes-- != 0)
{
if (!GetData(out tmp))
{
return false;
}
}
int bounds;
if (!GetData(out bounds))
{
return false;
}
while (bounds-- != 0)
{
if (!GetData(out tmp))
{
return false;
}
}
}
break;
case ELEMENT_TYPE_SENTINEL:
// Should be unreachable since GetElem strips it
break;
case ELEMENT_TYPE_INTERNAL:
if (!GetData(out tmp))
{
return false;
}
break;
case ELEMENT_TYPE_GENERICINST:
// Skip generic type
if (!SkipExactlyOne())
{
return false;
}
// Get number of parameters
int argCnt;
if (!GetData(out argCnt))
{
return false;
}
// Skip the parameters
while (argCnt-- != 0)
{
SkipExactlyOne();
}
break;
}
}
return true;
}
private bool SkipMethodHeaderSignature(out int pcArgs)
{
pcArgs = 0;
// Skip calling convention
int uCallConv, tmp;
if (!GetCallingConvInfo(out uCallConv))
{
return false;
}
if ((uCallConv == IMAGE_CEE_CS_CALLCONV_FIELD) ||
(uCallConv == IMAGE_CEE_CS_CALLCONV_LOCAL_SIG))
{
return false;
}
// Skip type parameter count
if ((uCallConv & IMAGE_CEE_CS_CALLCONV_GENERIC) == IMAGE_CEE_CS_CALLCONV_GENERIC)
{
if (!GetData(out tmp))
{
return false;
}
}
// Get arg count;
if (!GetData(out pcArgs))
{
return false;
}
// Skip return type;
if (!SkipExactlyOne())
{
return false;
}
return true;
} // SigParser::SkipMethodHeaderSignature
private bool SkipSignature()
{
int args;
if (!SkipMethodHeaderSignature(out args))
{
return false;
}
// Skip args.
while (args-- > 0)
{
if (!SkipExactlyOne())
{
return false;
}
}
return false;
}
private bool UncompressToken(out int token, out int size)
{
if (!UncompressData(out token, out size))
{
return false;
}
var tkType = s_tkCorEncodeToken[token & 3];
token = (token >> 2) | tkType;
return true;
}
private byte GetSig(int offs)
{
Debug.Assert(offs < _len);
return _sig[_offs + offs];
}
private bool UncompressData(out int pDataOut, out int pDataLen)
{
pDataOut = 0;
pDataLen = 0;
if (_len <= 0)
{
return false;
}
byte byte0 = GetSig(0);
// Smallest.
if ((byte0 & 0x80) == 0x00) // 0??? ????
{
if (_len < 1)
{
return false;
}
else
{
pDataOut = byte0;
pDataLen = 1;
}
}
// Medium.
else if ((byte0 & 0xC0) == 0x80) // 10?? ????
{
if (_len < 2)
{
return false;
}
else
{
pDataOut = (int)(((byte0 & 0x3f) << 8 | GetSig(1)));
pDataLen = 2;
}
}
else if ((byte0 & 0xE0) == 0xC0) // 110? ????
{
if (_len < 4)
{
return false;
}
else
{
pDataOut = (int)(((byte0 & 0x1f) << 24 | GetSig(1) << 16 | GetSig(2) << 8 | GetSig(3)));
pDataLen = 4;
}
}
else // We don't recognize this encoding
{
return false;
}
return true;
}
private bool PeekByte(out int data)
{
byte tmp;
if (!PeekByte(out tmp))
{
data = 0xcc;
return false;
}
data = tmp;
return true;
}
private const int mdtModule = 0x00000000; //
private const int mdtTypeRef = 0x01000000; //
private const int mdtTypeDef = 0x02000000; //
private const int mdtFieldDef = 0x04000000; //
private const int mdtMethodDef = 0x06000000; //
private const int mdtParamDef = 0x08000000; //
private const int mdtInterfaceImpl = 0x09000000; //
private const int mdtMemberRef = 0x0a000000; //
private const int mdtCustomAttribute = 0x0c000000; //
private const int mdtPermission = 0x0e000000; //
private const int mdtSignature = 0x11000000; //
private const int mdtEvent = 0x14000000; //
private const int mdtProperty = 0x17000000; //
private const int mdtMethodImpl = 0x19000000; //
private const int mdtModuleRef = 0x1a000000; //
private const int mdtTypeSpec = 0x1b000000; //
private const int mdtAssembly = 0x20000000; //
private const int mdtAssemblyRef = 0x23000000; //
private const int mdtFile = 0x26000000; //
private const int mdtExportedType = 0x27000000; //
private const int mdtManifestResource = 0x28000000; //
private const int mdtGenericParam = 0x2a000000; //
private const int mdtMethodSpec = 0x2b000000; //
private const int mdtGenericParamConstraint = 0x2c000000;
private const int mdtString = 0x70000000; //
private const int mdtName = 0x71000000; //
private const int mdtBaseType = 0x72000000; // Leave this on the high end value. This does not correspond to metadata table
private static readonly int[] s_tkCorEncodeToken = new int[] { mdtTypeDef, mdtTypeRef, mdtTypeSpec, mdtBaseType };
private const int IMAGE_CEE_CS_CALLCONV_DEFAULT = 0x0;
public const int IMAGE_CEE_CS_CALLCONV_VARARG = 0x5;
public const int IMAGE_CEE_CS_CALLCONV_FIELD = 0x6;
public const int IMAGE_CEE_CS_CALLCONV_LOCAL_SIG = 0x7;
public const int IMAGE_CEE_CS_CALLCONV_PROPERTY = 0x8;
public const int IMAGE_CEE_CS_CALLCONV_UNMGD = 0x9;
public const int IMAGE_CEE_CS_CALLCONV_GENERICINST = 0xa;
public const int IMAGE_CEE_CS_CALLCONV_NATIVEVARARG = 0xb;
public const int IMAGE_CEE_CS_CALLCONV_MAX = 0xc;
public const int IMAGE_CEE_CS_CALLCONV_MASK = 0x0f;
public const int IMAGE_CEE_CS_CALLCONV_HASTHIS = 0x20;
public const int IMAGE_CEE_CS_CALLCONV_EXPLICITTHIS = 0x40;
public const int IMAGE_CEE_CS_CALLCONV_GENERIC = 0x10;
private const int ELEMENT_TYPE_END = 0x0;
private const int ELEMENT_TYPE_VOID = 0x1;
private const int ELEMENT_TYPE_BOOLEAN = 0x2;
private const int ELEMENT_TYPE_CHAR = 0x3;
private const int ELEMENT_TYPE_I1 = 0x4;
private const int ELEMENT_TYPE_U1 = 0x5;
private const int ELEMENT_TYPE_I2 = 0x6;
private const int ELEMENT_TYPE_U2 = 0x7;
private const int ELEMENT_TYPE_I4 = 0x8;
private const int ELEMENT_TYPE_U4 = 0x9;
private const int ELEMENT_TYPE_I8 = 0xa;
private const int ELEMENT_TYPE_U8 = 0xb;
private const int ELEMENT_TYPE_R4 = 0xc;
private const int ELEMENT_TYPE_R8 = 0xd;
private const int ELEMENT_TYPE_STRING = 0xe;
private const int ELEMENT_TYPE_PTR = 0xf;
private const int ELEMENT_TYPE_BYREF = 0x10;
private const int ELEMENT_TYPE_VALUETYPE = 0x11;
private const int ELEMENT_TYPE_CLASS = 0x12;
private const int ELEMENT_TYPE_VAR = 0x13;
private const int ELEMENT_TYPE_ARRAY = 0x14;
private const int ELEMENT_TYPE_GENERICINST = 0x15;
private const int ELEMENT_TYPE_TYPEDBYREF = 0x16;
private const int ELEMENT_TYPE_I = 0x18;
private const int ELEMENT_TYPE_U = 0x19;
private const int ELEMENT_TYPE_FNPTR = 0x1B;
private const int ELEMENT_TYPE_OBJECT = 0x1C;
private const int ELEMENT_TYPE_SZARRAY = 0x1D;
private const int ELEMENT_TYPE_MVAR = 0x1e;
private const int ELEMENT_TYPE_CMOD_REQD = 0x1F;
private const int ELEMENT_TYPE_CMOD_OPT = 0x20;
private const int ELEMENT_TYPE_INTERNAL = 0x21;
private const int ELEMENT_TYPE_MAX = 0x22;
private const int ELEMENT_TYPE_MODIFIER = 0x40;
private const int ELEMENT_TYPE_SENTINEL = 0x01 | ELEMENT_TYPE_MODIFIER;
private const int ELEMENT_TYPE_PINNED = 0x05 | ELEMENT_TYPE_MODIFIER;
}
}