-
Notifications
You must be signed in to change notification settings - Fork 8
/
DotNetHelper.hpp
577 lines (483 loc) · 24.2 KB
/
DotNetHelper.hpp
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
#pragma once
/*
Copyright (c) 2021 Victor Sheinmann, [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <metahost.h>
//#include "ThirdParty\DotNet\mscoree.h" // #include <mscoree.h>
#pragma comment(lib, "MSCorEE.lib")
#import "C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.tlb" raw_interfaces_only \
high_property_prefixes("_get","_put","_putref") \
rename("ReportEvent", "InteropServices_ReportEvent")
#ifdef _AMD64_
static void __cdecl _com_issue_error(long){}
#endif
//---------------------------------------------------------------------------
namespace NDotNetHlp
{
EXTERN_GUID(IID_AppDomain, 0X05F696DC, 0X2B29, 0X3663, 0XAD, 0X8B, 0XC4, 0X38, 0X9C, 0XF2, 0XA7, 0X13); // __uuidof(_AppDomain);
struct ICorJitCompiler
{
void* pVFT;
};
//using namespace mscorlib;
typedef interface ICLRPrivAssembly ICLRPrivAssembly;
typedef interface ICLRPrivBinder ICLRPrivBinder;
typedef interface ICLRPrivBinder ICLRPrivBinder;
struct IAssemblyName;
MIDL_INTERFACE("2601F621-E462-404C-B299-3E1DE72F8547")
ICLRPrivResource : public IUnknown
{
public:
virtual HRESULT STDMETHODCALLTYPE GetResourceType(
/* [retval][out] */ IID *pIID) = 0;
};
MIDL_INTERFACE("5653946E-800B-48B7-8B09-B1B879B54F68")
ICLRPrivAssemblyInfo : public IUnknown
{
public:
virtual HRESULT STDMETHODCALLTYPE GetAssemblyName(
/* [in] */ DWORD cchBuffer,
/* [out] */ LPDWORD pcchBuffer,
/* [optional][string][out] */ LPWSTR wzBuffer) = 0;
virtual HRESULT STDMETHODCALLTYPE GetAssemblyVersion(
/* [out] */ USHORT *pMajor,
/* [out] */ USHORT *pMinor,
/* [out] */ USHORT *pBuild,
/* [out] */ USHORT *pRevision) = 0;
virtual HRESULT STDMETHODCALLTYPE GetAssemblyPublicKey(
/* [in] */ DWORD cbBuffer,
/* [out] */ LPDWORD pcbBuffer,
/* [optional][length_is][size_is][out] */ BYTE *pbBuffer) = 0;
};
MIDL_INTERFACE("2601F621-E462-404C-B299-3E1DE72F8542")
ICLRPrivBinder : public IUnknown
{
public:
virtual HRESULT STDMETHODCALLTYPE BindAssemblyByName(
/* [in] */ IAssemblyName *pAssemblyName,
/* [retval][out] */ ICLRPrivAssembly **ppAssembly) = 0;
virtual HRESULT STDMETHODCALLTYPE VerifyBind(
/* [in] */ IAssemblyName *AssemblyName,
/* [in] */ ICLRPrivAssembly *pAssembly,
/* [in] */ ICLRPrivAssemblyInfo *pAssemblyInfo) = 0;
virtual HRESULT STDMETHODCALLTYPE GetBinderFlags(
/* [retval][out] */ DWORD *pBinderFlags) = 0;
virtual HRESULT STDMETHODCALLTYPE GetBinderID(
/* [retval][out] */ UINT_PTR *pBinderId) = 0;
virtual HRESULT STDMETHODCALLTYPE FindAssemblyBySpec(
/* [in] */ LPVOID pvAppDomain,
/* [in] */ LPVOID pvAssemblySpec,
/* [out] */ HRESULT *pResult,
/* [out] */ ICLRPrivAssembly **ppAssembly) = 0;
};
MIDL_INTERFACE("2601F621-E462-404C-B299-3E1DE72F8543")
ICLRPrivAssembly : public ICLRPrivBinder
{
public:
virtual HRESULT STDMETHODCALLTYPE IsShareable(
/* [retval][out] */ BOOL *pbIsShareable) = 0;
virtual HRESULT STDMETHODCALLTYPE GetAvailableImageTypes(
/* [retval][out] */ LPDWORD pdwImageTypes) = 0;
virtual HRESULT STDMETHODCALLTYPE GetImageResource(
/* [in] */ DWORD dwImageType,
/* [out] */ DWORD *pdwImageType,
/* [retval][out] */ ICLRPrivResource **ppIResource) = 0;
};
MIDL_INTERFACE("BC1B53A8-DCBC-43B2-BB17-1E4061447AE9")
ICLRPrivRuntime : public IUnknown
{
public:
virtual HRESULT STDMETHODCALLTYPE GetInterface(
/* [in] */ REFCLSID rclsid,
/* [in] */ REFIID riid,
/* [retval][iid_is][out] */ LPVOID *ppUnk) = 0;
virtual HRESULT STDMETHODCALLTYPE CreateAppDomain(
/* [string][in] */ LPCWSTR pwzFriendlyName,
/* [in] */ ICLRPrivBinder *pBinder,
/* [retval][out] */ LPDWORD pdwAppDomainId) = 0;
virtual HRESULT STDMETHODCALLTYPE CreateDelegate(
/* [in] */ DWORD appDomainID,
/* [string][in] */ LPCWSTR wszAssemblyName,
/* [string][in] */ LPCWSTR wszClassName,
/* [string][in] */ LPCWSTR wszMethodName,
/* [retval][out] */ LPVOID *ppvDelegate) = 0;
virtual HRESULT STDMETHODCALLTYPE ExecuteMain(
/* [in] */ ICLRPrivBinder *pBinder,
/* [retval][out] */ int *pRetVal) = 0;
};
//===========================================================================
// mscoree.dll -> mscoreei.dll -> clr.dll
//
class CDotNetHost
{
bool Initialized;
ICLRMetaHost *pMetaHost; // ExitProcess
ICorRuntimeHost *pRuntimeHost; // ICLRRuntimeHost *pRuntimeHost; // ExecuteInDefaultAppDomain
ICLRRuntimeInfo *pRuntimeInfo; // GetProcAddress, LoadLibrary
ICLRPrivRuntime *pPrivRuntime; // ???
mscorlib::_AppDomainPtr pDefaultAppDomain;
mscorlib::_AssemblyPtr pAssembly;
//---------------------------------------------------------------------------
void ReleaseInterfaces(void)
{
if(this->pAssembly)this->pAssembly->Release();
if(this->pDefaultAppDomain)this->pDefaultAppDomain->Release();
if(this->pRuntimeHost)this->pRuntimeHost->Release();
if(this->pRuntimeInfo)this->pRuntimeInfo->Release();
if(this->pMetaHost)this->pMetaHost->Release();
this->pMetaHost = nullptr;
this->pRuntimeHost = nullptr;
this->pRuntimeInfo = nullptr;
this->pDefaultAppDomain = nullptr;
this->pAssembly = nullptr;
DBGMSG("Done");
}
//---------------------------------------------------------------------------
public:
//---------------------------------------------------------------------------
CDotNetHost(void)
{
this->Initialized = false;
this->pMetaHost = nullptr;
this->pRuntimeHost = nullptr;
this->pRuntimeInfo = nullptr;
this->pDefaultAppDomain = nullptr;
this->pAssembly = nullptr;
}
//---------------------------------------------------------------------------
int ReleaseDotNET(PWSTR RTVervoid)
{
if(!this->Initialized)return 1;
this->ReleaseInterfaces();
this->Initialized = false;
return 0;
}
//---------------------------------------------------------------------------
// http://www.rohitab.com/discuss/topic/43071-loading-net-pe-into-memory-from-native-process/
// https://github.com/mjrousos/SampleCoreCLRHost/blob/master/HostWithMscoree/host.cpp
// RTVer example: "v4.0.30319"
// CoreClr default path: %programfiles%\dotnet\shared\Microsoft.NETCore.App\
//
int InitDotNET(PWSTR RTVer)
{
HRESULT hr;
if(this->Initialized)return 1;
hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&this->pMetaHost); // S_OK
if(FAILED(hr)){DBGMSG("CLRCreateInstance failed with %08X",hr); return -1;}
hr = this->pMetaHost->GetRuntime(RTVer, IID_PPV_ARGS(&this->pRuntimeInfo));
if(FAILED(hr)){DBGMSG("GetRuntime %ls failed with %08X",RTVer, hr); this->ReleaseInterfaces(); return -2;}
BOOL bCLRStatus = 0;
hr = this->pRuntimeInfo->IsLoadable(&bCLRStatus);
if(FAILED(hr) || !bCLRStatus){DBGMSG("Runtime %ls failed withis not loadable %08X", RTVer, hr); this->ReleaseInterfaces(); return -3;}
hr = this->pRuntimeInfo->IsLoaded(GetCurrentProcess(), &bCLRStatus);
if(FAILED(hr)){DBGMSG("Failed to get status of runtime %ls: %08X", RTVer, hr); this->ReleaseInterfaces(); return -4;}
if(bCLRStatus){DBGMSG("Runtime %ls is already loaded", RTVer); }
hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (void**)&this->pRuntimeHost); // Cannot create ICLRPrivRuntime directly //IID_PPV_ARGS(&this->pRuntimeHost)); // CorHost2::ICLRRuntimeHost vtable from clr.dll
if(FAILED(hr)){DBGMSG("Failed to load runtime %ls: %08X", RTVer, hr); this->ReleaseInterfaces(); return -5;}
DWORD StartupFlags = 0;
hr = this->pRuntimeInfo->IsStarted(&bCLRStatus, &StartupFlags);
if(FAILED(hr)){DBGMSG("Failed to get status of runtime %ls: %08X", RTVer, hr); this->ReleaseInterfaces(); return -4;}
if(!bCLRStatus)
{
hr = this->pRuntimeHost->Start();
if(FAILED(hr)){DBGMSG("Failed to start runtime %ls: %08X", RTVer, hr); this->ReleaseInterfaces(); return -5;}
}
else { DBGMSG("Runtime %ls is already started", RTVer); }
// See 'clr.dll :: CorHost2 *__fastcall CorHost2::CorHost2(CorHost2 *__hidden this)'
// *((_QWORD *)this + 0) = &CorHost2::`vftable'{for `IPrivateManagedExceptionReporting'};
// *((_QWORD *)this + 1) = &CorHost2::`vftable'{for `CorThreadpool'};
// *((_QWORD *)this + 2) = &CorHost2::`vftable'{for `CorGCHost'};
// *((_QWORD *)this + 3) = &CorHost2::`vftable'{for `CorConfiguration'};
// *((_QWORD *)this + 4) = &CorHost2::`vftable'{for `CLRValidator'};
// *((_QWORD *)this + 5) = &CorHost2::`vftable'{for `CorDebuggerInfo'};
// *((_QWORD *)this + 6) = &CorHost2::`vftable'{for `ICLRRuntimeHost'}; // <<< We got this (pRuntimeHost points to it)
// *((_QWORD *)this + 7) = &CorHost2::`vftable'{for `ICLRPrivRuntime'}; // <<< We need this
// *((_QWORD *)this + 8) = &CorHost2::`vftable'{for `CorExecutionManager'};
this->pPrivRuntime = (ICLRPrivRuntime*)&((void**)this->pRuntimeHost)[1];
PVOID Ress = 0;
hr = this->pPrivRuntime->CreateDelegate(1, L"???", L"type", L"proc", &Ress);
// clr.dll HardCodedMetaSig
// pCLRRuntimeHost->UnloadAppDomain(domainId, true);
// pCLRRuntimeHost->Stop();
this->Initialized = true;
return 0;
}
//---------------------------------------------------------------------------
// https://github.com/etormadiv/HostingCLR/blob/master/HostingCLR/HostingCLR.cpp#L10
//Info: Assemblies that are calling Assembly.GetEntryAssembly() method will fail to load if exceptions are not catched!
//Info: Assembly.GetEntryAssembly() method will return null if loaded by this code, se it should NOT be used
//Info: Please fix any code that use Assembly.GetEntryAssembly() and change it typeof(MyType).Assembly instead
//Info: Please read https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly(v=vs.110).aspx#Anchor_1
//
int Initialize(PWSTR RTVer)
{
HRESULT hr;
if(this->Initialized)return 1;
hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&this->pMetaHost); // S_OK
if(FAILED(hr)){DBGMSG("CLRCreateInstance failed with %08X",hr); return -1;}
hr = this->pMetaHost->GetRuntime(RTVer, IID_PPV_ARGS(&this->pRuntimeInfo));
if(FAILED(hr)){DBGMSG("GetRuntime %ls failed with %08X",RTVer, hr); this->ReleaseInterfaces(); return -2;}
BOOL bCLRStatus = 0;
hr = this->pRuntimeInfo->IsLoadable(&bCLRStatus);
if(FAILED(hr) || !bCLRStatus){DBGMSG("Runtime %ls failed withis not loadable %08X", RTVer, hr); this->ReleaseInterfaces(); return -3;}
hr = this->pRuntimeInfo->IsLoaded(GetCurrentProcess(), &bCLRStatus);
if(FAILED(hr)){DBGMSG("Failed to get status of runtime %ls: %08X", RTVer, hr); this->ReleaseInterfaces(); return -4;}
if(bCLRStatus){DBGMSG("Runtime %ls is already loaded", RTVer); }
hr = pRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (void**)&this->pRuntimeHost); // Cannot create ICLRPrivRuntime directly //IID_PPV_ARGS(&this->pRuntimeHost)); // CorHost2::ICLRRuntimeHost vtable from clr.dll
if(FAILED(hr)){DBGMSG("Failed to load runtime %ls: %08X", RTVer, hr); this->ReleaseInterfaces(); return -5;}
//
DWORD StartupFlags = 0;
hr = this->pRuntimeInfo->IsStarted(&bCLRStatus, &StartupFlags);
if(FAILED(hr)){DBGMSG("Failed to get status of runtime %ls: %08X", RTVer, hr); this->ReleaseInterfaces(); return -4;}
if(!bCLRStatus)
{
hr = this->pRuntimeHost->Start();
if(FAILED(hr)){DBGMSG("Failed to start runtime %ls: %08X", RTVer, hr); this->ReleaseInterfaces(); return -5;}
}
else { DBGMSG("Runtime %ls is already started", RTVer); }
IUnknownPtr pAppDomainThunk = nullptr;
hr = pRuntimeHost->GetDefaultDomain(&pAppDomainThunk); // Relese it?
if(FAILED(hr)){DBGMSG("GetDefaultDomain failed with %08X", hr); this->ReleaseInterfaces(); return -6;}
hr = pAppDomainThunk->QueryInterface(IID_AppDomain, (void**)&this->pDefaultAppDomain); // Equivalent of System.AppDomain.CurrentDomain in C#
if(FAILED(hr)){DBGMSG("QueryInterface for IID_AppDomain failed with %08X", hr); this->ReleaseInterfaces(); return -7;}
this->Initialized = true;
return 0;
}
//---------------------------------------------------------------------------
int LoadAssembly(PVOID AsmFilePath)
{
HRESULT hr;
HANDLE hFile;
if(!((PBYTE)AsmFilePath)[1])hFile = CreateFileW((PWSTR)AsmFilePath,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
else hFile = CreateFileA((LPSTR)AsmFilePath,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
if(hFile == INVALID_HANDLE_VALUE)return -1;
DWORD Result = 0;
DWORD FileSize = GetFileSize(hFile,NULL);
if(!FileSize)return -2;
SAFEARRAYBOUND bnd = { FileSize, 0 };
SAFEARRAY* pSafeArray = SafeArrayCreate(VT_UI1, 1, &bnd); // new SAFEARRAYBOUND{ lenRawData , 0 }
if(!pSafeArray){CloseHandle(hFile); return -3;}
void* pvData = nullptr;
hr = SafeArrayAccessData(pSafeArray, &pvData);
if(FAILED(hr)){DBGMSG("SafeArrayAccessData failed with %08X", hr); SafeArrayDestroy(pSafeArray); CloseHandle(hFile); return -4;}
if(!ReadFile(hFile,pvData,FileSize,&Result,NULL) || !Result)return -5;
CloseHandle(hFile);
hr = SafeArrayUnaccessData(pSafeArray);
if(FAILED(hr)){DBGMSG("SafeArrayAccessData failed with %08X", hr); SafeArrayDestroy(pSafeArray); return -6;}
if(this->pAssembly){this->pAssembly->Release(); this->pAssembly = nullptr;}
hr = this->pDefaultAppDomain->Load_3(pSafeArray, &this->pAssembly);
SafeArrayDestroy(pSafeArray);
if(FAILED(hr)){DBGMSG("raw_Load_3 failed with %08X", hr); return -7;}
return 0;
}
//---------------------------------------------------------------------------
int ExecAssembly(PWSTR AsmFilePath)
{
bstr_t bstrAssemblyName(AsmFilePath);
HRESULT hr = this->pDefaultAppDomain->ExecuteAssembly(bstrAssemblyName, NULL, NULL); // pCurrentDomain
if(FAILED(hr)){DBGMSG("ExecuteAssembly failed with %08X", hr); return -1;}
return 0;
}
//---------------------------------------------------------------------------
SAFEARRAY* newArguments(int argc, wchar_t** argv)
{
VARIANT args;
args.vt = VT_ARRAY | VT_BSTR;
args.parray = SafeArrayCreate(VT_BSTR, 1, new SAFEARRAYBOUND{ ULONG(argc) , 0 });
for (int i = 0; i < argc; i++) SafeArrayPutElement(args.parray, (LONG*)&i, SysAllocString(argv[i]));
SAFEARRAY* params = SafeArrayCreate(VT_VARIANT, 1, new SAFEARRAYBOUND{ 1, 0 });
LONG indx = 0;
SafeArrayPutElement(params, &indx, &args);
return params;
}
int AsmExecEntryPoint(void)
{
HRESULT hr;
mscorlib::_MethodInfoPtr pMethodInfo = NULL;
hr = this->pAssembly->get_EntryPoint(&pMethodInfo);
if(FAILED(hr)){DBGMSG("get_EntryPoint failed with %08X", hr); return -1;}
VARIANT retVal = {0};
VARIANT obj = {0};
obj.vt = VT_NULL;
SAFEARRAY *psaStaticMethodArgs = SafeArrayCreateVector(VT_VARIANT, 0, 1); //TODO! Change cElement to the number of Main arguments
// wchar_t* Arg1 = L"Hello";
// SAFEARRAY *psaStaticMethodArgs = newArguments(1, &Arg1);
//Sleep(12000);
hr = pMethodInfo->Invoke_3(obj, psaStaticMethodArgs, &retVal); // EntryPoint.Invoke(null, new object[0])
SafeArrayDestroy(psaStaticMethodArgs);
// SAFEARRAY *pArrParams;
// hr = entryp->GetParameters(&pArrParams);
// Load dotnet executable from file
//bstr_t bstrAssemblyName(L"C:\\dotnet.exe");
//hr = pCurrentDomain->ExecuteAssembly(bstrAssemblyName, NULL, NULL);
return 0;
}
//---------------------------------------------------------------------------
// C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
// clr.dll
// clrjit.dll
// mscorlib.dll [.NET]
//
// ReflectMethodObject - The base object for the RuntimeMethodInfo class
//
// Hook both CreateDelegate of DynamicMethod object
// Grab the byte array and sabotage the result that the delegate could not be actually called
//
//int Execute()
//---------------------------------------------------------------------------
};
//===========================================================================
//---------------------------------------------------------------------------
//===========================================================================
// ClrJit
//---------------------------------------------------------------------------
#include "ThirdParty\DotNet\corjit.h"
/*typedef struct CORINFO_METHOD_STRUCT_* CORINFO_METHOD_HANDLE;
typedef struct CORINFO_MODULE_STRUCT_* CORINFO_MODULE_HANDLE;
enum CorInfoOptions
{
CORINFO_OPT_INIT_LOCALS = 0x00000010, // zero initialize all variables
CORINFO_GENERICS_CTXT_FROM_THIS = 0x00000020, // is this shared generic code that access the generic context from the this pointer? If so, then if the method has SEH then the 'this' pointer must always be reported and kept alive.
CORINFO_GENERICS_CTXT_FROM_PARAMTYPEARG = 0x00000040, // is this shared generic code that access the generic context from the ParamTypeArg? If so, then if the method has SEH then the 'ParamTypeArg' must always be reported and kept alive. Same as CORINFO_CALLCONV_PARAMTYPE
CORINFO_GENERICS_CTXT_MASK = (CORINFO_GENERICS_CTXT_FROM_THIS |
CORINFO_GENERICS_CTXT_FROM_PARAMTYPEARG),
CORINFO_GENERICS_CTXT_KEEP_ALIVE = 0x00000080, // Keep the generics context alive throughout the method even if there is no explicit use, and report its location to the CLR
};
struct CORINFO_METHOD_INFO
{
CORINFO_METHOD_HANDLE ftn;
CORINFO_MODULE_HANDLE scope;
BYTE * ILCode;
unsigned ILCodeSize;
unsigned short maxStack;
unsigned short EHcount;
CorInfoOptions options;
};
typedef struct ILCodeBuffer
{
LPBYTE pBuffer;
DWORD dwSize;
BOOL bIsGeneric;
}; */
// clrjit.dll (.NET 4.0) ; mscorwks.dll (.NET 2.0+)
// ICorJitInfo* VFT is ??_7CEEJitInfo@@6B@ in clr.dll
static int _stdcall CorJit_CompileMethod(struct CILJit* This, struct ICorJitInfo* pJitInfo, CORINFO_METHOD_INFO* pMethodInfo, UINT nFlags, LPBYTE* pEntryAddress, ULONG* pSizeOfCode); // CorJitResult
const char* _stdcall GetMethodName(ICorJitInfo* This, CORINFO_METHOD_HANDLE ftn, const char **moduleName); // _stdcall or thiscall? // TODO: Fix definition of ICorJitInfo interface in 'corinfo.h' because index of GetMethodName is wrong (.NET 4)
static void** _stdcall GetJit(void);
//---------------------------------------------------------------------------
static void** GetCompileMethodRef(HMODULE hModJit=NULL, PVOID pGetJit=NULL, bool ForceFind=false)
{
if(!hModJit && !pGetJit)
{
hModJit = GetModuleHandleA("clrjit.dll");
if(!hModJit)hModJit = GetModuleHandleA("mscorjit.dll");
if(!hModJit)return nullptr;
}
if(!pGetJit)
{
pGetJit = GetProcAddress(hModJit, "getJit");
if(!pGetJit)return nullptr;
}
void** Ptr = ((decltype(GetJit)*)pGetJit)(); // Will return NULL if called before jitStartup // It is OK to call it multiple times // First VFT entry is 'compileMethod'
if(!Ptr)
{
if(ForceFind)
{
UINT RawSize = 0;
UINT VirSize = 0;
NPEFMT::GetModuleSizes((PBYTE)hModJit, &RawSize, &VirSize);
PBYTE ModBeg = (PBYTE)hModJit;
PBYTE ModEnd = ModBeg + VirSize;
PBYTE CurPtr = (PBYTE)pGetJit;
PBYTE EndPtr = CurPtr + 32;
for(;CurPtr < EndPtr;CurPtr++)
{
#ifdef _AMD64_
INT32 Offs = *(UINT32*)CurPtr; // Relative addressing is used
PBYTE Addr = (CurPtr + sizeof(INT32)) + Offs; // This offset is last in the instruction
if((Addr > ModBeg) && (Addr < ModEnd)) // LEA target is in the module
{
PBYTE Val = *(PBYTE*)Addr; // If Addr is g_CILJit then Val will be VFT, otherwise it will be compileMethod // Note: compileMethod is in '.text'; VFT is in '.rdata'; g_CILJit is in '.data'
if((Val > ModBeg) && (Val < ModEnd))
{
PBYTE Unk = *(PBYTE*)Val; //Ptr = (void**)Addr;
if((Unk < ModBeg) || (Unk >= ModEnd)){Val=Addr; Addr=nullptr;}
DBGMSG("Found: g_CILJit=%p, CILJitVFT=%p", Addr, Val);
Ptr = (void**)Val;
break;
}
}
#else
// TODO
#endif
}
}
if(!Ptr){DBGMSG("pGetJit returned NULL"); return nullptr;}
}
else Ptr = (void**)*Ptr; // Get VFT pointer
/*
NPEFMT::SECTION_HEADER* ResSec = nullptr;
if(!NPEFMT::GetModuleSection(hModJit, ".text", &ResSec))return nullptr;
PBYTE TxtSecBase = (PBYTE)hModJit + ResSec->SectionRva;
SIZE_T TxtSecSize = ResSec->PhysicalSize;
if(!NPEFMT::GetModuleSection(hModJit, ".rdata", &ResSec))return nullptr;
PBYTE DatSecBase = (PBYTE)hModJit + ResSec->SectionRva;
SIZE_T DatSecSize = ResSec->PhysicalSize;
PBYTE ProcPtr = (PBYTE)pGetJit;
PVOID* IntfAddr = nullptr; // It is INTERFACE, not VFT and THIS ptr is passed as an usual argument (stdcall on x32)
for(UINT ctr=0;ctr < 32;ctr++)
{
PBYTE AVal =
} */
DBGMSG("LibAddr=%p, getJit=%p, CILJitVFT=%p, compileMethod=%p", hModJit, pGetJit, Ptr, Ptr[0]);
// DBGMSG("VFT0=%p, VFT1=%p, VFT2=%p, VFT3=%p, VFT4=%p",((void**)*Ptr)[0],((void**)*Ptr)[1],((void**)*Ptr)[2],((void**)*Ptr)[3],((void**)*Ptr)[4]);
return &Ptr[0]; // Stored address of CILJit::compileMethod
}
//---------------------------------------------------------------------------
/*
8A ?? 06 mov al, [???+6]
24 07 and al, 7
3C 07 cmp al, 7
75 ?? jnz ???
B8 00 00 00 06 mov eax, 6000000h
*/
static int FindIdxOfGetMethodName(ICorJitInfo* pJitInfo)
{
PVOID* Vft = *(PVOID**)pJitInfo;
int CurIdx = 100; // Should be enough // 113 - 116
int MaxIdx = CurIdx + 30;
for(;CurIdx < MaxIdx;CurIdx++)
{
PBYTE NxtPtr = (PBYTE)Vft[CurIdx+1];
PBYTE CurPtr = (PBYTE)Vft[CurIdx];
PBYTE EndPtr = CurPtr + 32;
if((CurPtr < NxtPtr)&&(EndPtr > NxtPtr))EndPtr = NxtPtr; // Do not scan into next proc
UINT InsFlg = 0;
DBGMSG("Scanning from %p to %p",CurPtr,EndPtr);
for(;CurPtr < EndPtr;CurPtr++) // Find CEEInfo::getMethodDefFromMethod; Next in VFT should be getMethodName
{
if(*(UINT16*)CurPtr == 0x0724)InsFlg |= 0x01; // and al, 7
else if(*(UINT16*)CurPtr == 0x073C)InsFlg |= 0x02; // cmp al, 7
else if((*CurPtr == 0xB8) && (*(UINT32*)&CurPtr[1] == 0x06000000))InsFlg |= 0x04; // mov eax, 6000000h
if(InsFlg == 7){DBGMSG("Found getMethodDefFromMethod at %p index is %u and getMethodName is %p with index %u", Vft[CurIdx], CurIdx, NxtPtr, CurIdx+1); return CurIdx+1;}
}
// DBGMSG("InsFlg=%u",InsFlg);
}
DBGMSG("Failed to find getMethodName!");
return -1;
}
/*
CORINFO_METHOD_STRUCT_ *ftn = ((CORINFO_METHOD_INFO *)methodInfo)->ftn;
*/
//---------------------------------------------------------------------------
}
/*
"ECall methods must be packaged into a system module" is happens only when method`s RVA is 0. RVA required to be 0 for InternalCall but not for PInvoke:)
*/