summaryrefslogtreecommitdiff
path: root/src/inc/dbggcinfoencoder.h
blob: 95450fb278884fe08f7d89c826d1a020fcedbb81 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*****************************************************************************
 *
 * GC Information Encoding API
 *
 * This is an older well-tested implementation 
 *      now used to verify the real encoding
 * Define VERIFY_GCINFO to enable the verification
 *
 */

#ifdef VERIFY_GCINFO

#ifndef __DBGGCINFOENCODER_H__
#define __DBGGCINFOENCODER_H__

#include <windows.h>

#include <wchar.h>
#include <stdio.h>

#include "utilcode.h"
#include "corjit.h"
#include "list.h"     // for SList
#include "arraylist.h"

#include "stdmacros.h"
#include "gcinfotypes.h"


class IJitAllocator;



namespace DbgGcInfo {

//-----------------------------------------------------------------------------
// The following macro controls whether the encoder has to call the IJitAllocator::Free method
// Don't call IJitAllocator::Free for mscorjit64.dll
//-----------------------------------------------------------------------------
//#define MUST_CALL_JITALLOCATOR_FREE


class BitStreamWriter
{
public:
    BitStreamWriter( IJitAllocator* pAllocator );
    void Write( size_t data, int count );

    inline size_t GetBitCount()
    {
        return m_BitCount;
    }

    inline size_t GetByteCount()
    {
        return ( m_BitCount + 7 )  / 8;
    }


    void CopyTo( BYTE* buffer );
    void Dispose();

    //--------------------------------------------------------
    // Encode variable length numbers
    // Uses base+1 bits at minimum
    // Bits 0..(base-1) represent the encoded quantity
    // If it doesn't fit, set bit #base to 1 and use base+1 more bits
    //--------------------------------------------------------
    int EncodeVarLengthUnsigned( size_t n, int base )
    {
        _ASSERTE((base > 0) && (base < sizeof(size_t)*8));
        size_t numEncodings = 1 << base;
		int bitsUsed = base+1;
        for( ; ; bitsUsed += base+1)
        {
            if( n < numEncodings )
            {
                Write( n, base+1 ); // This sets the extension bit to zero
                return bitsUsed;
            }
            else
            {
                size_t currentChunk = n & (numEncodings-1);
                Write( currentChunk | numEncodings, base+1 );
                n >>= base;
            }
        }
        return bitsUsed;
    }

    //--------------------------------------------------------
    // Signed quantities are encoded the same as unsigned
    // The most relevant difference is that a number is considered
    // to fit in base bits if the topmost bit of a base-long chunk
    // matches the sign of the whole number
    //--------------------------------------------------------
    int EncodeVarLengthSigned( SSIZE_T n, int base )
    {
        _ASSERTE((base > 0) && (base < sizeof(SSIZE_T)*8));
        size_t numEncodings = 1 << base;
        for(int bitsUsed = base+1; ; bitsUsed += base+1)
        {
            size_t currentChunk = ((size_t) n) & (numEncodings-1);
            size_t topmostBit = currentChunk & (numEncodings >> 1);
            n >>= base; // signed arithmetic shift
            if( topmostBit && (n == (SSIZE_T)-1) || !topmostBit && (n == 0))
            {
                // The topmost bit correctly represents the sign
                Write( currentChunk, base+1 ); // This sets the extension bit to zero
                return bitsUsed;
            }
            else
            {
                Write( currentChunk | numEncodings, base+1 );
            }
        }
    }

private:

    class MemoryBlockDesc
    {
    public:
        size_t* StartAddress;
        SLink m_Link;

        inline void Init()
        {
            m_Link.m_pNext = NULL;
        }
    };

    IJitAllocator* m_pAllocator;
    size_t m_BitCount;
    int m_FreeBitsInCurrentSlot;
    SList<MemoryBlockDesc> m_MemoryBlocks;
    const static int m_MemoryBlockSize = 512;    // must be a multiple of the pointer size
    size_t* m_pCurrentSlot;            // bits are written through this pointer
    size_t* m_OutOfBlockSlot;        // sentinel value to determine when the block is full
#ifdef _DEBUG
    int m_MemoryBlocksCount;
#endif

private:
    // Writes bits knowing that they will all fit in the current memory slot
    inline void WriteInCurrentSlot( size_t data, int count )
    {
        data &= SAFE_SHIFT_LEFT(1, count) - 1;

        data <<= (sizeof( size_t )*8-m_FreeBitsInCurrentSlot);

        *m_pCurrentSlot |= data;
    }

    void AllocMemoryBlock();

    inline void InitCurrentSlot()
    {
        m_FreeBitsInCurrentSlot = sizeof( size_t )*8;
        *m_pCurrentSlot = 0;
    }

};

struct GcSlotDesc
{
    union
    {
        UINT32 RegisterNumber;
        GcStackSlot Stack;
    } Slot;
    unsigned IsRegister : 1;
    unsigned IsInterior : 1;
    unsigned IsPinned : 1;
};



typedef UINT32 GcSlotId;


class GcSlotSet
{
    friend class GcInfoEncoder;
public:
    GcSlotSet( GcInfoEncoder * pEncoder );

    // Copy constructor
    GcSlotSet( GcSlotSet & other );

    inline void Add( GcSlotId slotId );

    inline void Remove( GcSlotId slotId );

// Not used
#if 0
    inline void RemoveAll();

    void Add( GcSlotSet & other );
    void Subtract( GcSlotSet & other );
    void Intersect( GcSlotSet & other );
#endif

    // Must be called when done with the object
    inline void Dispose();

private:
    // A bit vector representing the set
    BYTE * m_Data;

    int m_NumBytes;

    GcInfoEncoder* m_pEncoder;
};


class GcInfoEncoder
{
public:
    GcInfoEncoder(
            ICorJitInfo*                pCorJitInfo,
            CORINFO_METHOD_INFO*        pMethodInfo,
            IJitAllocator*              pJitAllocator
            );


    //------------------------------------------------------------------------
    // Interruptibility
    //------------------------------------------------------------------------

    // An instruction at offset x will be interruptible
    //  if-and-only-if startInstructionOffset <= x < startInstructionOffset+length
    void DefineInterruptibleRange( UINT32 startInstructionOffset, UINT32 length );


    //------------------------------------------------------------------------
    // Slot information
    //------------------------------------------------------------------------

    //
    // spOffset are always relative to the SP of the caller (same as SP at the method entry and exit)
    // Negative offsets describe GC refs in the local and outgoing areas.
    // Positive offsets describe GC refs in the scratch area
    // Note that if the dynamic allocation area is resized, the outgoing area will not be valid anymore
    //  Old slots must be declared dead and new ones can be defined.
    //  It's up to the JIT to do the right thing. We don't enforce this.
    //

    GcSlotId GetRegisterSlotId( UINT32 regNum, GcSlotFlags flags );
    GcSlotId GetStackSlotId( INT32 spOffset, GcSlotFlags flags, GcStackSlotBase spBase = GC_CALLER_SP_REL );

    //
    // After a FinalizeSlotIds is called, no more slot definitions can be made.
    // FinalizeSlotIds must be called once and only once before calling DefineGcStateAtCallSite
    // If no call sites are described, calling FinalizeSlotIds can and should (for performance reasons) be avoided
    //
    void FinalizeSlotIds();


#if 0
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED

    //------------------------------------------------------------------------
    // Partially-interruptible information
    //------------------------------------------------------------------------


    void DefineGcStateAtSafePoint(
                UINT32          instructionOffset,
                GcSlotSet       &liveSlots
                );

#endif // PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
#endif

    //------------------------------------------------------------------------
    // Fully-interruptible information
    //------------------------------------------------------------------------

    //
    // For inputs, pass zero as offset
    //

    // This method defines what the GC state of a slot is when a thread's suspension IP
    //  is equal to instructionOffset

    void SetSlotState(              UINT32      instructionOffset,
                                    GcSlotId    slotId,
                                    GcSlotState slotState
                                    );



    //------------------------------------------------------------------------
    // Miscellaneous method information
    //------------------------------------------------------------------------

    void SetSecurityObjectStackSlot( INT32 spOffset );
    void SetPSPSymStackSlot( INT32 spOffsetPSPSym );
    void SetGenericsInstContextStackSlot( INT32 spOffsetGenericsContext );
    void SetIsVarArg();
    void SetCodeLength( UINT32 length );

    // Optional in the general case. Required if the method uses GC_FRAMEREG_REL stack slots
    void SetStackBaseRegister( UINT32 registerNumber );
    void SetSizeOfEditAndContinuePreservedArea( UINT32 size );

#ifdef FIXED_STACK_PARAMETER_SCRATCH_AREA
    void SetSizeOfStackOutgoingAndScratchArea( UINT32 size );
#endif // FIXED_STACK_PARAMETER_SCRATCH_AREA


    //------------------------------------------------------------------------
    // Encoding
    //------------------------------------------------------------------------

    //
    // Build() encodes GC information into temporary buffers.
    // The method description cannot change after Build is called
    //
    void Build();

    //
    // Write encoded information to its final destination and frees temporary buffers.
    // The encoder shouldn't be used anymore after calling this method.
    // It returns a pointer to the destination buffer, which address is byte-aligned
    //
    size_t GetByteCount();
    BYTE* Emit(BYTE* dest);

private:

    friend class LifetimeTransitionsQuickSort;
    friend class LifetimeTransitionsQuickSortByOffset;

    struct LifetimeTransition
    {
        UINT32 CodeOffset;
        GcSlotDesc SlotDesc;
        bool BecomesLive;
    };

    class LifetimeTransitionAllocator
    {
    public:

        static void *Alloc (void *context, SIZE_T cb);
        static void Free (void *context, void *pv);
    };

    ICorJitInfo*                m_pCorJitInfo;
    CORINFO_METHOD_INFO*        m_pMethodInfo;
    IJitAllocator*              m_pAllocator;

#ifdef _DEBUG
    char *m_MethodName, *m_ModuleName;
#endif

    BitStreamWriter     m_HeaderInfoWriter;
#if 0
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
    BitStreamWriter     m_PartiallyInterruptibleInfoWriter;
#endif
#endif
    BitStreamWriter     m_FullyInterruptibleInfoWriter;

    StructArrayList<LifetimeTransition, 64, 2, LifetimeTransitionAllocator> m_LifetimeTransitions;
    LifetimeTransition *m_rgSortedTransitions;

    bool   m_IsVarArg;
    INT32  m_SecurityObjectStackSlot;
    INT32  m_PSPSymStackSlot;
    INT32  m_GenericsInstContextStackSlot;
    UINT32 m_CodeLength;
    UINT32 m_StackBaseRegister;
    UINT32 m_SizeOfEditAndContinuePreservedArea;
    UINT32 m_LastInterruptibleRangeStopOffset;
    UINT32 m_NumInterruptibleRanges;
    
#ifdef FIXED_STACK_PARAMETER_SCRATCH_AREA
    UINT32 m_SizeOfStackOutgoingAndScratchArea;
#endif // FIXED_STACK_PARAMETER_SCRATCH_AREA

    void * eeAllocGCInfo (size_t        blockSize);

    inline int EncodeFullyInterruptibleSlotFlags(GcSlotDesc slotDesc)
    {
        int flagEnc = 1;
        if( slotDesc.IsInterior )
            flagEnc |= 0x2;
        if( slotDesc.IsPinned )
            flagEnc |= 0x4;
        if(flagEnc == 1)
        {
            m_FullyInterruptibleInfoWriter.Write(0, 1);
            return 1;
        }
        else
        {
            m_FullyInterruptibleInfoWriter.Write(flagEnc, 3);
            return 3;
        }
    }      


private:

    friend class GcSlotSet;
    friend class EncoderCheckState;

    static const UINT32 m_MappingTableInitialSize = 32;
    UINT32 m_MappingTableSize;
    UINT32 m_NumSlotMappings;
    GcSlotDesc *m_SlotMappings;

#if 0
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
    UINT32 m_NumSafePointsWithGcState;
#endif
#endif

    void GrowMappingTable();

#ifdef _DEBUG
    bool m_IsMappingTableFrozen;
#endif
};



// Not used
#if 0

void GcSlotSet::RemoveAll()
{
    ZeroMemory( m_Data, m_NumBytes );
}

#endif


void GcSlotSet::Dispose()
{
#ifdef MUST_CALL_JITALLOCATOR_FREE
    m_pEncoder->m_pAllocator->Free( m_Data );
#endif
}


}

#endif // !__DBGGCINFOENCODER_H__

#endif // VERIFY_GCINFO