-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
302 lines (245 loc) · 10.3 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
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
#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;
string snippetDataBase;
bool useSandbox = true;
bool testMode = false;
int serverPort = 4321;
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);
SwitchArg testModeArg("t", "test", "Run in test mode (specify an assembly file, type/class and method)");
ValueArg<string> assemblyFileNameArg("a", "assembly", "The assembly file name", false, "", "string");
cmd.add(assemblyFileNameArg);
ValueArg<string> typeNameArg("c", "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);
ValueArg<string> snippetDataBaseArg("d", "database", "The path of the SQL CE database that holds the snippets", false, "", "string");
cmd.add(snippetDataBaseArg);
ValueArg<int> serverPortArg("p", "port", "The port on which this Host will listen for snippet execution requests", false, 4321, "int");
cmd.add(serverPortArg);
cmd.parse(argc, argv);
testMode = testModeArg.getValue();
if (testMode) {
if (!typeNameArg.isSet() || !assemblyFileNameArg.isSet()) {
CmdLineParseException error("If you run in test mode, you should specify both the type and assembly file name");
try {
cmd.getOutput()->failure(cmd, error);
}
catch (ExitException &ee) {
exit(ee.getExitStatus());
}
}
}
else {
if (!snippetDataBaseArg.isSet()) {
CmdLineParseException error("You should specify a valid DB file name");
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();
snippetDataBase = snippetDataBaseArg.getValue();
serverPort = serverPortArg.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();
// TODO: DB table or configuration file?
// Or automatically all assemblies in PrivateLib
AssemblyInfo appDomainManager(L"SimpleHostRuntime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9abf81284e6824ad, processorarchitecture=MSIL", currentDir + L"\\PrivateLib\\SimpleHostRuntime.dll", L"");
AssemblyInfo pumpkinData(L"Pumpkin.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9abf81284e6824ad, processorarchitecture=msil", currentDir + L"\\PrivateLib\\Pumpkin.Data.dll", L"");
AssemblyInfo pumpkinMonitor(L"Pumpkin.Monitor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9abf81284e6824ad, processorarchitecture=MSIL", currentDir + L"\\PrivateLib\\Pumpkin.Monitor.dll", L"");
AssemblyInfo newtonsoftJson(L"newtonsoft.json, version=6.0.0.0, culture=neutral, publickeytoken=30ad4fe6b2a6aeed, processorarchitecture=MSIL", currentDir + L"\\PrivateLib\\Newtonsoft.Json.dll", L"");
hostAssemblies.push_back(appDomainManager);
hostAssemblies.push_back(pumpkinData);
hostAssemblies.push_back(pumpkinMonitor);
hostAssemblies.push_back(newtonsoftJson);
// 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;
hrExecute = domainManager->RegisterHostContext(hostControl->GetHostContext());
if (testMode) {
bstr_t assemblyName = toBSTR(assemblyFileName);
bstr_t type = toBSTR(typeName);
bstr_t method = toBSTR(methodName);
if (methodName.empty()) {
hrExecute = domainManager->raw_RunTests(assemblyName, type);
}
else {
hrExecute = domainManager->raw_RunTest(assemblyName, type, method);
}
if (!SUCCEEDED(hrExecute)) {
Logger::Critical("Execution of tests failed: 0x%x", hrExecute);
retVal = -1;
}
getchar();
}
else {
bstr_t snippetDataBaseName = toBSTR(snippetDataBase);
hrExecute = domainManager->raw_StartListening(serverPort, snippetDataBaseName);
if (!SUCCEEDED(hrExecute)) {
Logger::Critical("Execution of tests failed: 0x%x", hrExecute);
retVal = -1;
}
Logger::Critical("Shutting down");
}
// Stop the CLR and cleanup.
hostControl->ShuttingDown();
runtimeInfo->Release();
metaHost->Release();
clrControl->Release();
domainManager->Release();
clr->Stop();
clr->Release();
return retVal;
}