summaryrefslogtreecommitdiff
path: root/src/inc/sigparser.h
blob: 0924b566f0b27a2b8408653065c3b9a4a403a543 (plain)
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//
// sigparser.h
//

//

#ifndef _H_SIGPARSER
#define _H_SIGPARSER

#include "utilcode.h"
#include "corhdr.h"
#include "corinfo.h"
#include "corpriv.h"

//---------------------------------------------------------------------------------------
// These macros define how arguments are mapped to the stack in the managed calling convention.
// We assume to be walking a method's signature left-to-right, in the virtual calling convention.
// See MethodDesc::Call for details on this virtual calling convention.
// These macros tell us whether the arguments we see as we proceed with the signature walk are mapped
//   to increasing or decreasing stack addresses. This is valid only for arguments that go on the stack.
//---------------------------------------------------------------------------------------
#if defined(_TARGET_X86_)
#define STACK_GROWS_DOWN_ON_ARGS_WALK
#else
#define STACK_GROWS_UP_ON_ARGS_WALK
#endif


//------------------------------------------------------------------------
// Encapsulates how compressed integers and typeref tokens are encoded into
// a bytestream.
//
// As you use this class please understand the implicit normalizations 
// on the CorElementType's returned by the various methods, especially
// for variable types (e.g. !0 in generic signatures), string types
// (i.e. E_T_STRING), object types (E_T_OBJECT), constructed types 
// (e.g. List<int>) and enums.
//------------------------------------------------------------------------
class SigParser
{
    protected:
        // This type is performance critical - do not add fields to it.
        // (If you must, check for managed types that may use a SigParser or SigPointer inline, like ArgIterator.)
        PCCOR_SIGNATURE m_ptr;
        DWORD           m_dwLen;

        //------------------------------------------------------------------------
        // Skips specified number of bytes WITHOUT VALIDATION. Only to be used
        // when it is known that it won't overflow the signature buffer.
        //------------------------------------------------------------------------
        FORCEINLINE void SkipBytes(ULONG cb)
        {
            SUPPORTS_DAC;
            _ASSERT(cb <= m_dwLen);
            m_ptr += cb;
            m_dwLen -= cb;
        }

    public:
        //------------------------------------------------------------------------
        // Constructor.
        //------------------------------------------------------------------------
        SigParser() {
            LIMITED_METHOD_DAC_CONTRACT;
            m_ptr = NULL;
            m_dwLen = 0;
        }

        SigParser(const SigParser &sig);

        //------------------------------------------------------------------------
        // Initialize 
        //------------------------------------------------------------------------
        FORCEINLINE SigParser(PCCOR_SIGNATURE ptr)
        {
            LIMITED_METHOD_CONTRACT;

            m_ptr = ptr;
            // We don't know the size of the signature, so we'll say it's "big enough"
            m_dwLen = 0xffffffff;
        }

        FORCEINLINE SigParser(PCCOR_SIGNATURE ptr, DWORD len)
        {
            LIMITED_METHOD_CONTRACT;

            m_ptr = ptr;
            m_dwLen = len;
        }

        inline void SetSig(PCCOR_SIGNATURE ptr)
        {
            LIMITED_METHOD_CONTRACT;
            
            m_ptr = ptr;
            // We don't know the size of the signature, so we'll say it's "big enough"
            m_dwLen = 0xffffffff;
        }

        inline void SetSig(PCCOR_SIGNATURE ptr, DWORD len)
        {
            LIMITED_METHOD_CONTRACT;
            
            m_ptr = ptr;
            m_dwLen = len;
        }


        inline BOOL IsNull() const
        {
            LIMITED_METHOD_CONTRACT;

            return (m_ptr == NULL);
        }

        // Returns represented signature as pointer and size.
        void 
        GetSignature(
            PCCOR_SIGNATURE * pSig, 
            DWORD           * pcbSigSize)
        {
            *pSig = m_ptr;
            *pcbSigSize = m_dwLen;
        }


    //=========================================================================
    // The RAW interface for reading signatures.  You see exactly the signature,
    // apart from custom modifiers which for historical reasons tend to get eaten.
    //
    // DO NOT USE THESE METHODS UNLESS YOU'RE TOTALLY SURE YOU WANT
    // THE RAW signature.  You nearly always want GetElemTypeClosed() or 
    // PeekElemTypeClosed() or one of the MetaSig functions.  See the notes above.
    // These functions will return E_T_INTERNAL, E_T_VAR, E_T_MVAR and such
    // so the caller must be able to deal with those
    //=========================================================================

        //------------------------------------------------------------------------
        // Remove one compressed integer (using CorSigUncompressData) from
        // the head of the stream and return it.
        //------------------------------------------------------------------------
        __checkReturn 
        FORCEINLINE HRESULT GetData(ULONG* data)
        {
            WRAPPER_NO_CONTRACT;
            SUPPORTS_DAC;

            ULONG sizeOfData = 0;
            ULONG tempData;

            if (data == NULL)
                data = &tempData;
            
            HRESULT hr = CorSigUncompressData(m_ptr, m_dwLen, data, &sizeOfData);

            if (SUCCEEDED(hr))
            {
                SkipBytes(sizeOfData);
            }
            
            return hr;
        }


        //-------------------------------------------------------------------------
        // Remove one byte and return it.
        //-------------------------------------------------------------------------
        __checkReturn 
        FORCEINLINE HRESULT GetByte(BYTE *data)
        {
            LIMITED_METHOD_CONTRACT;

            if (m_dwLen > 0)
            {
                if (data != NULL)
                    *data = *m_ptr;

                SkipBytes(1);
                
                return S_OK;
            }

            if (data != NULL)
                *data = 0;
            return META_E_BAD_SIGNATURE;
        }

        //-------------------------------------------------------------------------
        // Peek at value of one byte and return it.
        //-------------------------------------------------------------------------
        __checkReturn 
        FORCEINLINE HRESULT PeekByte(BYTE *data)
        {
            LIMITED_METHOD_CONTRACT;

            if (m_dwLen > 0)
            {
                if (data != NULL)
                    *data = *m_ptr;

                return S_OK;
            }

            if (data != NULL)
                *data = 0;
            return META_E_BAD_SIGNATURE;
        }

        //-------------------------------------------------------------------------
        // The element type as defined in CorElementType. No normalization for
        // generics (E_T_VAR, E_T_MVAR,..) or dynamic methods (E_T_INTERNAL occurs)
        //-------------------------------------------------------------------------
        __checkReturn 
        HRESULT GetElemTypeSlow(CorElementType * etype)
        {
            WRAPPER_NO_CONTRACT;
            SUPPORTS_DAC;

            CorElementType tmpEType;

            if (etype == NULL)
                etype = &tmpEType;

            SigParser sigTemp(*this);

            HRESULT hr = sigTemp.SkipCustomModifiers();

            if (SUCCEEDED(hr))
            {
                BYTE bElemType = 0;
                hr = sigTemp.GetByte(&bElemType);
                *etype = (CorElementType)bElemType;
                
                if (SUCCEEDED(hr))
                {
                    *this = sigTemp;
                    return S_OK;
                }
            }
            
            *etype = ELEMENT_TYPE_END;
            
            return META_E_BAD_SIGNATURE;
        }

        // Inlined version
        __checkReturn 
        FORCEINLINE HRESULT GetElemType(CorElementType * etype)
        {
            WRAPPER_NO_CONTRACT;
            SUPPORTS_DAC;

            if (m_dwLen > 0)
            {
                CorElementType typ = (CorElementType) * m_ptr;

                if (typ < ELEMENT_TYPE_CMOD_REQD) // fast path with no modifiers: single byte
                {
                    if (etype != NULL)
                    {
                        * etype = typ;
                    }

                    SkipBytes(1);

                    return S_OK;
                }
            }

            // Slower/normal path
            return GetElemTypeSlow(etype);
        }

        // Note: Calling convention is always one byte, not four bytes
        __checkReturn 
        HRESULT GetCallingConvInfo(ULONG * data) 
        {
            WRAPPER_NO_CONTRACT;
            SUPPORTS_DAC;

            ULONG tmpData;

            if (data == NULL)
                data = &tmpData;
            
            HRESULT hr = CorSigUncompressCallingConv(m_ptr, m_dwLen, data);
            if (SUCCEEDED(hr))
            {
                SkipBytes(1);
            }

            return hr;
        }   

        __checkReturn 
        HRESULT GetCallingConv(ULONG* data)  // @REVISIT_TODO: Calling convention is one byte, not four.
        {   
            WRAPPER_NO_CONTRACT;
            ULONG info;
            HRESULT hr = GetCallingConvInfo(&info);

            if (SUCCEEDED(hr) && data != NULL)
            {
                *data = IMAGE_CEE_CS_CALLCONV_MASK & info;
            }

            return hr; 
        }   

        //------------------------------------------------------------------------
        // Non-destructive read of compressed integer.
        //------------------------------------------------------------------------
        __checkReturn 
        HRESULT PeekData(ULONG *data) const
        {
            WRAPPER_NO_CONTRACT;
            _ASSERTE(data != NULL);

            ULONG sizeOfData = 0;
            return CorSigUncompressData(m_ptr, m_dwLen, data, &sizeOfData);
        }


        //------------------------------------------------------------------------
        // Non-destructive read of element type.
        //
        // This routine makes it look as if the String type is encoded
        // via ELEMENT_TYPE_CLASS followed by a token for the String class,
        // rather than the ELEMENT_TYPE_STRING. This is partially to avoid
        // rewriting client code which depended on this behavior previously.
        // But it also seems like the right thing to do generally.
        // No normalization for generics (E_T_VAR, E_T_MVAR,..) or 
        // dynamic methods (E_T_INTERNAL occurs)
        //------------------------------------------------------------------------
        __checkReturn 
        HRESULT PeekElemTypeSlow(CorElementType *etype) const
        {
            WRAPPER_NO_CONTRACT;
            SUPPORTS_DAC;

            _ASSERTE(etype != NULL);

            SigParser sigTemp(*this);
            HRESULT hr = sigTemp.GetElemType(etype);
            if (SUCCEEDED(hr) && (*etype == ELEMENT_TYPE_STRING || *etype == ELEMENT_TYPE_OBJECT))
                *etype = ELEMENT_TYPE_CLASS;

            return hr;
        }

        // inline version
        __checkReturn 
        FORCEINLINE HRESULT PeekElemType(CorElementType *etype) const
        {
            WRAPPER_NO_CONTRACT;
            SUPPORTS_DAC;

            _ASSERTE(etype != NULL);

            if (m_dwLen > 0)
            {
                CorElementType typ = (CorElementType) * m_ptr;

                if (typ < ELEMENT_TYPE_CMOD_REQD) // fast path with no modifiers: single byte
                {
                    if ((typ == ELEMENT_TYPE_STRING) || (typ == ELEMENT_TYPE_OBJECT))
                    {
                        *etype = ELEMENT_TYPE_CLASS;
                    }
                    else
                    {
                        *etype = typ;
                    }

                    return S_OK;
                }
            }

            return PeekElemTypeSlow(etype);
        }

        //-------------------------------------------------------------------------
        // Returns the raw size of the type next in the signature, or returns
        // E_INVALIDARG for base types that have variables sizes.
        //-------------------------------------------------------------------------
        __checkReturn 
        HRESULT PeekElemTypeSize(ULONG *pSize)
        {
            WRAPPER_NO_CONTRACT;
            HRESULT hr = S_OK;

            DWORD dwSize = 0;
            
            if (pSize == NULL)
            {
                pSize = &dwSize;
            }

            SigParser sigTemp(*this);

            hr = sigTemp.SkipAnyVASentinel();

            if (FAILED(hr))
            {
                return hr;
            }

            *pSize = 0;

            BYTE bElementType = 0;
            hr = sigTemp.GetByte(&bElementType);

            if (FAILED(hr))
            {
                return hr;
            }

            switch (bElementType)
            {
            case ELEMENT_TYPE_I8:
            case ELEMENT_TYPE_U8:
            case ELEMENT_TYPE_R8:
        #ifdef _WIN64
            case ELEMENT_TYPE_I:
            case ELEMENT_TYPE_U:
        #endif // WIN64

                *pSize = 8;
                break;

            case ELEMENT_TYPE_I4:
            case ELEMENT_TYPE_U4:
            case ELEMENT_TYPE_R4:
        #ifndef _WIN64
            case ELEMENT_TYPE_I:
            case ELEMENT_TYPE_U:
        #endif // _WIN64

                *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_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 = sizeof(void *);
                break;

            case ELEMENT_TYPE_VOID:
                break;

            case ELEMENT_TYPE_END:
            case ELEMENT_TYPE_CMOD_REQD:
            case ELEMENT_TYPE_CMOD_OPT:
                _ASSERTE(!"Asked for the size of an element that doesn't have a size!");
                return E_INVALIDARG;

            case ELEMENT_TYPE_VALUETYPE:
                _ASSERTE(!"Asked for the size of an element that doesn't have a size!");
                return E_INVALIDARG;

            default:

                _ASSERTE( !"CorSigGetElementTypeSize given invalid signature to size!" );
                return META_E_BAD_SIGNATURE;
            }

            return hr;
        }

        //------------------------------------------------------------------------
        // Is this at the Sentinal (the ... in a varargs signature) that marks
        // the begining of varguments that are not decared at the target

        bool AtSentinel() const
        {
            if (m_dwLen > 0)
                return *m_ptr == ELEMENT_TYPE_SENTINEL;
            else
                return false;
        }

        //------------------------------------------------------------------------
        // Removes a compressed metadata token and returns it.
        // WARNING: dynamic methods do not have tokens so this api is completely
        //          broken in that case. Make sure you call this function if
        //          you are absolutely sure E_T_INTERNAL was not in the sig
        //------------------------------------------------------------------------
        __checkReturn 
        FORCEINLINE 
        HRESULT GetToken(mdToken * token)
        {
            WRAPPER_NO_CONTRACT;
            DWORD dwLen;
            mdToken tempToken;

            if (token == NULL)
                token = &tempToken;
            
            HRESULT hr = CorSigUncompressToken(m_ptr, m_dwLen, token, &dwLen);

            if (SUCCEEDED(hr))
            {
                SkipBytes(dwLen);
            }

            return hr;
        }

        //------------------------------------------------------------------------
        // Removes a pointer value and returns it. Used for ELEMENT_TYPE_INTERNAL.
        __checkReturn 
        FORCEINLINE 
        HRESULT GetPointer(void ** pPtr)
        {
            WRAPPER_NO_CONTRACT;

            if (m_dwLen < sizeof(void *))
            {   // Not enough data to read a pointer
                if (pPtr != NULL)
                {
                    *pPtr = NULL;
                }
                return META_E_BAD_SIGNATURE;
            }
            if (pPtr != NULL)
            {
                *pPtr = *(void * UNALIGNED *)m_ptr;
            }
            SkipBytes(sizeof(void *));

            return S_OK;
        }

        //------------------------------------------------------------------------
        // Tests if two SigParsers point to the same location in the stream.
        //------------------------------------------------------------------------
        FORCEINLINE BOOL Equals(SigParser sp) const
        {
            LIMITED_METHOD_CONTRACT;

            return m_ptr == sp.m_ptr;
        }

        __checkReturn 
        HRESULT SkipCustomModifiers()
        {
            WRAPPER_NO_CONTRACT;
            SUPPORTS_DAC;

            HRESULT hr = S_OK;

            SigParser sigTemp(*this);

            hr = sigTemp.SkipAnyVASentinel();

            if (FAILED(hr))
            {
                return hr;
            }

            BYTE bElementType = 0;

            hr = sigTemp.PeekByte(&bElementType);
            if (FAILED(hr))
                return hr;

            while ((ELEMENT_TYPE_CMOD_REQD == bElementType) || 
                   (ELEMENT_TYPE_CMOD_OPT == bElementType))
            {
                sigTemp.SkipBytes(1);

                mdToken token;

                hr = sigTemp.GetToken(&token);

                if (FAILED(hr))
                    return hr;

                hr = sigTemp.PeekByte(&bElementType);
                if (FAILED(hr))
                    return hr;
            }

            // 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_VAR_ZAPSIG:
                case ELEMENT_TYPE_NATIVE_ARRAY_TEMPLATE_ZAPSIG:
                case ELEMENT_TYPE_NATIVE_VALUETYPE_ZAPSIG:
                case ELEMENT_TYPE_CANON_ZAPSIG:
                case ELEMENT_TYPE_MODULE_ZAPSIG:
                case ELEMENT_TYPE_PINNED:
                    break;
                default:
                    return META_E_BAD_SIGNATURE;
                }
            }

            *this = sigTemp;
            return hr;
        }// SkipCustomModifiers

        __checkReturn 
        HRESULT SkipFunkyAndCustomModifiers()
        {
            WRAPPER_NO_CONTRACT;
            SUPPORTS_DAC;

            SigParser sigTemp(*this);
            HRESULT hr = S_OK;
            hr = sigTemp.SkipAnyVASentinel();

            if (FAILED(hr))
            {
                return hr;
            }

            BYTE bElementType = 0;

            hr = sigTemp.PeekByte(&bElementType);
            if (FAILED(hr))
                return hr;

            while (ELEMENT_TYPE_CMOD_REQD == bElementType || 
                   ELEMENT_TYPE_CMOD_OPT == bElementType ||
                   ELEMENT_TYPE_MODIFIER == bElementType ||
                   ELEMENT_TYPE_PINNED == bElementType)
            {
                sigTemp.SkipBytes(1);

                mdToken token;

                hr = sigTemp.GetToken(&token);

                if (FAILED(hr))
                    return hr;

                hr = sigTemp.PeekByte(&bElementType);
                if (FAILED(hr))
                    return hr;
            }

            // 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_NATIVE_ARRAY_TEMPLATE_ZAPSIG:
                case ELEMENT_TYPE_NATIVE_VALUETYPE_ZAPSIG:
                case ELEMENT_TYPE_CANON_ZAPSIG:
                case ELEMENT_TYPE_MODULE_ZAPSIG:
                case ELEMENT_TYPE_PINNED:
                    break;
                default:
                    return META_E_BAD_SIGNATURE;
                }
            }

            *this = sigTemp;
            return hr;
        }// SkipFunkyAndCustomModifiers


        __checkReturn 
        HRESULT SkipAnyVASentinel()
        {
            WRAPPER_NO_CONTRACT;

            HRESULT hr = S_OK;
            BYTE bElementType = 0;

            hr = PeekByte(&bElementType);
            if (FAILED(hr))
                return hr;

            if (bElementType == ELEMENT_TYPE_SENTINEL)
            {
                SkipBytes(1);
            }

            return hr;
        }// SkipAnyVASentinel

        //------------------------------------------------------------------------
        // Assumes that the SigParser points to the start of an element type
        // (i.e. function parameter, function return type or field type.)
        // Advances the pointer to the first data after the element type.  
        //------------------------------------------------------------------------
        __checkReturn 
        HRESULT SkipExactlyOne();

        //------------------------------------------------------------------------
        // Skip only the method header of the signature, not the signature of 
        // the arguments.
        //------------------------------------------------------------------------
        __checkReturn 
        HRESULT SkipMethodHeaderSignature(ULONG *pcArgs);

        //------------------------------------------------------------------------
        // Skip a sub signature (as immediately follows an ELEMENT_TYPE_FNPTR).
        //------------------------------------------------------------------------
        __checkReturn 
        HRESULT SkipSignature();

public:

        //------------------------------------------------------------------------
        // Return pointer
        // PLEASE DON'T USE THIS.
        // 
        // Return the internal pointer.  It's hard to resist, but please try
        // not to use this.  Certainly don't use it if there's any chance of the
        // signature containing generic type variables.
        // 
        // It's currently only used for working on the 
        // signatures stored in TypeSpec tokens (we should add a new abstraction,
        // i.e. on MetaSig for this) and a couple of places to do with COM
        // and native interop signature parsing.   
        // <REVISIT_TODO>We should try to get rid of these uses as well. </REVISIT_TODO>
        //------------------------------------------------------------------------
        PCCOR_SIGNATURE GetPtr() const
        {
            LIMITED_METHOD_DAC_CONTRACT;
            return m_ptr;
        }

};  // class SigParser

//------------------------------------------------------------------------
FORCEINLINE 
SigParser::SigParser(
    const SigParser &sig)
    : m_ptr(sig.m_ptr), m_dwLen(sig.m_dwLen)
{
    LIMITED_METHOD_DAC_CONTRACT;
}

/*****************************************************************/
/* CorTypeInfo is a single global table that you can hang information
   about ELEMENT_TYPE_* */

class CorTypeInfo
{
protected:
    struct CorTypeInfoEntry
    {
        LPCUTF8        nameSpace;
        LPCUTF8        className;
        CorElementType type         : 8;
        unsigned       size         : 8;
        CorInfoGCType  gcType       : 3;
        unsigned       isArray      : 1;
        unsigned       isPrim       : 1;
        unsigned       isFloat      : 1;
        unsigned       isModifier   : 1;
        unsigned       isGenVar     : 1;
        // 1 more byte here to use for 32-bit
    };
    
protected:
    FORCEINLINE static const CorTypeInfoEntry &GetTypeInfo(CorElementType type)
    {
        CONTRACTL
        {
            THROWS;
            GC_NOTRIGGER;
            SUPPORTS_DAC;
#ifdef MODE_ANY
            MODE_ANY;
#endif
        }
        CONTRACTL_END;
        
        if (type >= (CorElementType)_countof(info))
        {
            ThrowHR(COR_E_BADIMAGEFORMAT);
        }
        return info[type];
    }
    FORCEINLINE static const CorTypeInfoEntry &GetTypeInfo_NoThrow(CorElementType type)
    {
        LIMITED_METHOD_DAC_CONTRACT;
        
        if (type >= (CorElementType)_countof(info))
        {
            return info[ELEMENT_TYPE_END];
        }
        return info[type];
    }
    
public:
    
    FORCEINLINE static LPCUTF8 GetName(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).className;
    }
    
    FORCEINLINE static LPCUTF8 GetNamespace(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).nameSpace;
    }
    
    static void CheckConsistency()
    {
        LIMITED_METHOD_CONTRACT;
        
        for (int i = 0; i < (int)_countof(info); i++)
        {
            _ASSERTE(info[i].type == i);
        }
    }
    
    FORCEINLINE static CorInfoGCType GetGCType(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).gcType;
    }
    FORCEINLINE static CorInfoGCType GetGCType_NoThrow(CorElementType type)
    {
        LIMITED_METHOD_DAC_CONTRACT;
        
        return GetTypeInfo_NoThrow(type).gcType;
    }
    
    static BOOL IsObjRef(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        SUPPORTS_DAC;
        
        return (GetGCType(type) == TYPE_GC_REF);
    }
    static BOOL IsObjRef_NoThrow(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        SUPPORTS_DAC;
        
        return (GetGCType_NoThrow(type) == TYPE_GC_REF);
    }
    
    FORCEINLINE static BOOL IsGenericVariable(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).isGenVar;
    }
    FORCEINLINE static BOOL IsGenericVariable_NoThrow(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo_NoThrow(type).isGenVar;
    }
    
    FORCEINLINE static BOOL IsArray(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).isArray;
    }
    FORCEINLINE static BOOL IsArray_NoThrow(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo_NoThrow(type).isArray;
    }
    
    FORCEINLINE static BOOL IsFloat(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).isFloat;
    }
    FORCEINLINE static BOOL IsFloat_NoThrow(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo_NoThrow(type).isFloat;
    }
    
    FORCEINLINE static BOOL IsModifier(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).isModifier;
    }
    FORCEINLINE static BOOL IsModifier_NoThrow(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo_NoThrow(type).isModifier;
    }
    
    FORCEINLINE static BOOL IsPrimitiveType(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).isPrim;
    }
    FORCEINLINE static BOOL IsPrimitiveType_NoThrow(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo_NoThrow(type).isPrim;
    }
    
    FORCEINLINE static unsigned Size(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo(type).size;
    }
    FORCEINLINE static unsigned Size_NoThrow(CorElementType type)
    {
        WRAPPER_NO_CONTRACT;
        
        return GetTypeInfo_NoThrow(type).size;
    }

    static CorElementType FindPrimitiveType(LPCUTF8 name);

protected:
    static const CorTypeInfoEntry info[ELEMENT_TYPE_MAX];
    
};  // class CorTypeInfo


// Returns the address of the payload inside the stackelem
inline void* StackElemEndianessFixup(void* pStackElem, UINT cbSize) {
    LIMITED_METHOD_CONTRACT;

    BYTE *pRetVal = (BYTE*)pStackElem;

#if BIGENDIAN
    switch (cbSize)
    {
    case 1:
        pRetVal += sizeof(void*)-1;
        break;
    case 2:
        pRetVal += sizeof(void*)-2;
        break;
#ifdef _WIN64
    case 4:
        pRetVal += sizeof(void*)-4;
        break;
#endif
    default:
        // nothing to do
        break;
    }
#endif

    return pRetVal;
}

#endif /* _H_SIGINFOBASE */