-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
251 lines (198 loc) · 7.91 KB
/
main.cpp
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
#include "Common.h"
#include "Logger.h"
#include "HostCtrl.h"
#include "tclap/CmdLine.h"
#include "tclap/ValueArg.h"
#include "tclap/SwitchArg.h"
#include "tclap/ArgException.h"
#include <string>
#include <iostream>
using namespace TCLAP;
using namespace std;
int main(int argc, char* argv [])
{
string clrVersion;
string assemblyFileName;
string typeName;
string methodName;
bool useSandbox = true;
CmdLine cmd("Simple CLR Host", ' ', "1.0");
try {
ValueArg<string> clrVersionArg("v", "clrversion", "CLR version to use", true, "v2.0.50727", "string");
cmd.add(clrVersionArg);
ValueArg<string> assemblyFileNameArg("a", "assembly", "The assembly file name", true, "", "string");
cmd.add(assemblyFileNameArg);
ValueArg<string> typeNameArg("t", "type", "The type name which contains the method to invoke", false, "", "string");
cmd.add(typeNameArg);
ValueArg<string> methodNameArg("m", "method", "The method to invoke", false, "", "string");
cmd.add(methodNameArg);
cmd.parse(argc, argv);
if (typeNameArg.isSet() != methodNameArg.isSet()) {
CmdLineParseException error("You should specify both the type and method name, or neither");
try {
cmd.getOutput()->failure(cmd, error);
}
catch (ExitException &ee) {
exit(ee.getExitStatus());
}
}
// Get the argument values
clrVersion = clrVersionArg.getValue();
assemblyFileName = assemblyFileNameArg.getValue();
typeName = typeNameArg.getValue();
methodName = methodNameArg.getValue();
}
catch (ArgException &e) {
cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
return 1;
}
HRESULT hr;
ICLRMetaHost* metaHost = NULL;
ICLRRuntimeInfo* runtimeInfo = NULL;
//
// Load and start the .NET runtime.
//
Logger::Info("Load and start the .NET runtime %s", clrVersion.c_str());
hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&metaHost));
if (FAILED(hr)) {
Logger::Critical("CLRCreateInstance failed w/hr 0x%08lx\n", hr);
return -1;
}
// OPTIONAL: ask information about a specific runtime based on some policy
//ICLRMetaHostPolicy *pMetaHostPolicy = NULL;
//hr = CLRCreateInstance(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy, (LPVOID*) &pMetaHostPolicy);
//pMetaHostPolicy->GerRequestedRuntime()
// Get the ICLRRuntimeInfo corresponding to a particular CLR version. It
// supersedes CorBindToRuntimeEx with STARTUP_LOADER_SAFEMODE.
// OPTIONAL: ICLRMetaHost::EnumerateInstalledRuntimes
hr = metaHost->GetRuntime(toWstring(clrVersion).c_str(), IID_PPV_ARGS(&runtimeInfo));
if (FAILED(hr)) {
metaHost->Release();
Logger::Critical("ICLRMetaHost::GetRuntime failed w/hr 0x%08lx\n", hr);
return -1;
}
// Check if the specified runtime can be loaded into the process. This
// method will take into account other runtimes that may already be
// loaded into the process and set pbLoadable to TRUE if this runtime can
// be loaded in an in-process side-by-side fashion.
BOOL isLoadable;
hr = runtimeInfo->IsLoadable(&isLoadable);
if (FAILED(hr)) {
runtimeInfo->Release();
metaHost->Release();
Logger::Critical("ICLRRuntimeInfo::IsLoadable failed w/hr 0x%08lx\n", hr);
return -1;
}
if (!isLoadable) {
runtimeInfo->Release();
metaHost->Release();
Logger::Critical(".NET runtime %s cannot be loaded\n", clrVersion);
return -1;
}
// Load the CLR into the current process and return a runtime interface
// pointer. ICorRuntimeHost and ICLRRuntimeHost are the two CLR hosting
// interfaces supported by CLR 4.0.
// The ICorRuntimeHost interface that was provided in .NET v1.x, is compatible with all
// .NET Frameworks.
//ICorRuntimeHost *corRuntimeHost = NULL;
//hr = runtimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_PPV_ARGS(&corRuntimeHost));
//_AppDomain* appDomain;
//IUnknown* appDomainUnk;
//corRuntimeHost->GetDefaultDomain(&appDomainUnk);
//appDomainUnk->QueryInterface(&appDomain);
ICLRRuntimeHost* clr = NULL;
hr = runtimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&clr));
if (FAILED(hr)) {
runtimeInfo->Release();
metaHost->Release();
Logger::Critical("ICLRRuntimeInfo::GetInterface failed w / hr 0x % 08lx\n", hr);
return -1;
}
// Get the CLR control object
ICLRControl* clrControl = NULL;
clr->GetCLRControl(&clrControl);
if (useSandbox) {
// Ask the CLR for its implementation of ICLRHostProtectionManager.
ICLRHostProtectionManager* clrHostProtectionManager = NULL;
hr = clrControl->GetCLRManager(IID_ICLRHostProtectionManager, (void **) &clrHostProtectionManager);
if (FAILED(hr)) {
runtimeInfo->Release();
metaHost->Release();
Logger::Critical("ICLRControl::GetCLRManager failed w / hr 0x % 08lx\n", hr);
return -1;
}
// Block all host protection categories.
// TODO: only a relevant subset?
// This "completes" the AppDomain PermissionSet
// Both are necessary, as this mechanism does not cover all
// aspects, but some.
// It is a way to have some "in-depth" defense, and to prevent some
// functionalities that are not prevented by CAS (e.g.: Cannot call Thread.SetPriority, but
// can call Thread.Abort.
hr = clrHostProtectionManager->SetProtectedCategories ( (EApiCategories)(
//eSynchronization |
//eSharedState |
eExternalProcessMgmt |
eSelfAffectingProcessMgmt |
//eExternalThreading |
eSelfAffectingThreading |
eSecurityInfrastructure |
eMayLeakOnAbort //|
//eUI
));
if (FAILED(hr)) {
Logger::Critical("ICLRHostProtectionManager::SetProtectedCategories failed w / hr 0x % 08lx\n", hr);
}
clrHostProtectionManager->Release();
}
std::list<AssemblyInfo> hostAssemblies;
std::wstring currentDir = CurrentDirectory();
AssemblyInfo appDomainManager(L"SimpleHostRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9abf81284e6824ad, processorarchitecture=MSIL", currentDir + L"\\SimpleHostRuntime.dll", L"");
hostAssemblies.push_back(appDomainManager);
// Construct our host control object.
DHHostControl* hostControl = new DHHostControl(clr, hostAssemblies);
// Associate our domain manager
clrControl->SetAppDomainManagerType(appDomainManager.FullName.c_str(), L"SimpleHostRuntime.SimpleHostAppDomainManager");
clr->SetHostControl(hostControl);
// Start the CLR.
hr = clr->Start();
if (FAILED(hr)) {
runtimeInfo->Release();
metaHost->Release();
clr->Release();
clrControl->Release();
Logger::Critical("CLR failed to start w/hr 0x%08lx", hr);
return -1;
}
ISimpleHostDomainManager* domainManager = hostControl->GetDomainManagerForDefaultDomain();
if (!domainManager) {
runtimeInfo->Release();
metaHost->Release();
clr->Release();
clrControl->Release();
Logger::Critical("Cannot obtain the Domain Manager for the Default Domain");
return -1;
}
int retVal = 0;
HRESULT hrExecute;
bstr_t assemblyName = toBSTR(assemblyFileName);
hrExecute = domainManager->RegisterHostContext(hostControl->GetHostContext());
bstr_t type = toBSTR(typeName);
bstr_t method = toBSTR(methodName);
// TODO!!!
hrExecute = domainManager->raw_RunTests(assemblyName, type, method);
if (!SUCCEEDED(hrExecute)) {
Logger::Critical("Execution of method raw_RunTests failed: 0x%x", hrExecute);
retVal = -1;
}
getchar();
// Stop the CLR and cleanup.
hostControl->ShuttingDown();
runtimeInfo->Release();
metaHost->Release();
clrControl->Release();
domainManager->Release();
clr->Stop();
clr->Release();
return retVal;
}