Welcome to mirror list, hosted at ThFree Co, Russian Federation.

JITCodeManager.h « jitinterface « Native « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aeff8bc899161cee6ef056297969cc7a310c02be (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
// 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.

#pragma once

#include "CodeHeap.h"

#include <list>
#include <vector>
#include <unordered_map>
#include <mutex>

#if defined(_TARGET_AMD64_)
//
// ToDo - this should eventually come from windows header file.
//
// Define unwind code structure.
//

typedef union _UNWIND_CODE {
    struct {
        UCHAR CodeOffset;
        UCHAR UnwindOp : 4;
        UCHAR OpInfo : 4;
    };

    USHORT FrameOffset;
} UNWIND_CODE, *PUNWIND_CODE;

//
// Define unwind information flags.
//

#define UNW_FLAG_NHANDLER 0x0
#define UNW_FLAG_EHANDLER 0x1
#define UNW_FLAG_UHANDLER 0x2
#define UNW_FLAG_CHAININFO 0x4

typedef struct _UNWIND_INFO {
    UCHAR Version : 3;
    UCHAR Flags : 5;
    UCHAR SizeOfProlog;
    UCHAR CountOfUnwindCodes;
    UCHAR FrameRegister : 4;
    UCHAR FrameOffset : 4;
    UNWIND_CODE UnwindCode[1];
} UNWIND_INFO, *PUNWIND_INFO;

typedef DPTR(struct _UNWIND_INFO)      PTR_UNWIND_INFO;
typedef DPTR(union _UNWIND_CODE)       PTR_UNWIND_CODE;
#endif // target_amd64

class ReaderWriterLock : private SRWLOCK
{
public:
    ReaderWriterLock()
    {
        ::InitializeSRWLock(this);
    }

    class ReadHolder
    {
        ReaderWriterLock * m_pLock;
    public:
        ReadHolder(ReaderWriterLock * pLock)
            : m_pLock(pLock)
        {
            ::AcquireSRWLockShared(m_pLock);
        }

        ~ReadHolder()
        {
            ::ReleaseSRWLockShared(m_pLock);
        }
    };

    class WriteHolder
    {
        ReaderWriterLock * m_pLock;

    public:
        WriteHolder(ReaderWriterLock * pLock)
            : m_pLock(pLock)
        {
            ::AcquireSRWLockExclusive(m_pLock);
        }

        ~WriteHolder()
        {
            ::ReleaseSRWLockExclusive(m_pLock);
        }
    };
};

typedef DPTR(RUNTIME_FUNCTION) PTR_RUNTIME_FUNCTION;

// TODO: Not compatible with Windows 7
// #ifdef _TARGET_AMD64_
// #define USE_GROWABLE_FUNCTION_TABLE 1
// #endif

class CodeHeader
{
public:
    CodeHeader(void *m_heapBase, DWORD codeOffs);
    ~CodeHeader();

    void *operator new (size_t sz, void *mem)
    {
        return mem;
    }

    inline void *GetCode() const
    {
        return m_heapBase + m_codeOffset;
    }

    inline DWORD GetCodeOffset() const
    {
        return m_codeOffset;
    }

    inline void *GetHeapBase() const
    {
        return m_heapBase;
    }
    
    inline void SetEHInfo(void *ehInfo)
    {
        m_ehInfo = ehInfo;
    }

    inline void *GetEHInfo() const
    {
        return m_ehInfo;
    }

    inline size_t GetEHCount() const
    {
        size_t *ptr = (size_t*)GetEHInfo();
        assert(ptr != NULL);
        return *(ptr - 1);
    }

    inline EHClause *GetEHClause(unsigned i)
    {
        assert(i < GetEHCount());
        EHClause *ehInfo = (EHClause*)GetEHInfo();
        return &ehInfo[i];
    }

private:
    BYTE *m_heapBase;
    DWORD m_codeOffset;

    // Exception handling clauses
    // Storage layout: <Number of EH clauses><Clause1>...<ClauseN>
    // m_ehInfo will be pointing to the first clause.
    // Number of EH clauses = *((size_t*)((byte*) m_ehInfo - sizeof(size_t)))
    void *m_ehInfo;
};

class JITCodeManager : ICodeManager
{
    PTR_VOID m_pvStartRange;
    UInt32 m_cbRange;

    // lock to protect m_runtimeFunctions and m_FuncletToMainMethodMap
    ReaderWriterLock m_lock;

    std::vector<RUNTIME_FUNCTION> m_runtimeFunctions;
    PTR_RUNTIME_FUNCTION m_pRuntimeFunctionTable;
    UInt32 m_nRuntimeFunctionTable;

#ifdef USE_GROWABLE_FUNCTION_TABLE
    PTR_VOID m_hGrowableFunctionTable;
#endif

    // Given BeginAddress of a funclet, this data structure maps to 
    // BeginAddress of its main method.
    std::unordered_map<DWORD, DWORD> m_FuncletToMainMethodMap;

    // For now we are using the desktop concept of multiple CodeManagers for multiple ranges
    // of JIT'ed code.  The current implementation is meant to be the simplest possible so
    // that it will be easy to refactor into a better/more permanent version later.
    ExecutableCodeHeap m_codeHeap;

    static std::list<JITCodeManager*> s_instances;
    static JITCodeManager * volatile s_pLastCodeManager;
    static std::mutex s_instanceLock;
    typedef std::lock_guard<std::mutex> MutexHolder;

    // Get the code header given method's start address
    static inline CodeHeader* GetCodeHeader(void *methodStart)
    {
        return (CodeHeader*)((BYTE*)methodStart - sizeof(CodeHeader));
    }

public:
    // Finds the code manager associated with a particular address.
    static JITCodeManager *FindCodeManager(PTR_VOID addr);

    // Finds a JITCodeManager instance with free space and allocates executable memory.
    // This function throws on failure, and passes out the code address and JIT manager used.
    static void AllocCode(size_t size, DWORD align, void **ppCode, JITCodeManager **ppManager);

public:
    JITCodeManager();
    ~JITCodeManager();

    bool Initialize();

    void *AllocPData(size_t size)
    {
        return m_codeHeap.AllocPData(size);
    }

    void *AllocEHInfo(CodeHeader *hdr, unsigned cEH)
    {
        size_t size = sizeof(size_t)+sizeof(struct EHClause) * cEH;
        size_t *ehInfo = (size_t *)m_codeHeap.AllocEHInfoRaw(size);
        *ehInfo = cEH;
        hdr->SetEHInfo(ehInfo + 1);
        
        return ehInfo;
    }

    PTR_RUNTIME_FUNCTION AllocRuntimeFunction(PTR_RUNTIME_FUNCTION mainMethod, DWORD beginAddr, DWORD endAddr, DWORD unwindData);

    inline bool Contains(void *pCode) const
    {
        return m_pvStartRange <= pCode && pCode < (void*)((BYTE*)m_pvStartRange + m_cbRange);
    }


    void UpdateRuntimeFunctionTable();

    //
    // Code manager methods
    //

    bool FindMethodInfo(PTR_VOID        ControlPC, 
                        MethodInfo *    pMethodInfoOut);

    bool IsFunclet(MethodInfo * pMethodInfo);

    PTR_VOID GetFramePointer(MethodInfo *   pMethodInfo,
                             REGDISPLAY *   pRegisterSet);

    void EnumGcRefs(MethodInfo *    pMethodInfo, 
                    PTR_VOID        safePointAddress,
                    REGDISPLAY *    pRegisterSet,
                    GCEnumContext * hCallback);

    bool UnwindStackFrame(MethodInfo *    pMethodInfo,
                          REGDISPLAY *    pRegisterSet,                 // in/out
                          PTR_VOID *      ppPreviousTransitionFrame);   // out

    bool GetReturnAddressHijackInfo(MethodInfo *    pMethodInfo,
                                    REGDISPLAY *    pRegisterSet,       // in
                                    PTR_PTR_VOID *  ppvRetAddrLocation, // out
                                    GCRefKind *     pRetValueKind);     // out

    void UnsynchronizedHijackMethodLoops(MethodInfo * pMethodInfo);

    PTR_VOID RemapHardwareFaultToGCSafePoint(MethodInfo * pMethodInfo, PTR_VOID controlPC);

    bool EHEnumInit(MethodInfo * pMethodInfo, PTR_VOID * pMethodStartAddress, EHEnumState * pEHEnumState);

    bool EHEnumNext(EHEnumState * pEHEnumState, EHClause * pEHClause);

    UIntNative GetConservativeUpperBoundForOutgoingArgs(MethodInfo *   pMethodInfo,
                                                        REGDISPLAY *   pRegisterSet);

    PTR_VOID GetMethodStartAddress(MethodInfo * pMethodInfo);

    void * GetClasslibFunction(ClasslibFunctionId functionId);

    PTR_VOID GetAssociatedData(PTR_VOID ControlPC);

    PTR_VOID GetOsModuleHandle();
};