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
|
// 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.
//
// verifier.cpp
//
//
//
//
// Registry / Environment settings :
//
// Create registry entries in CURRENT_USER\Software\Microsoft\.NETFramework
// or set environment variables COMPlus_* with the names given below.
// Environment settings override registry settings.
//
// For breaking into the debugger / Skipping verification :
// (available only in the debug build).
//
// VerBreakOnError [STRING] Break into the debugger on error. Set to 1
// VerSkip [STRING] method names (case sensitive)
// VerBreak [STRING] method names (case sensitive)
// VerOffset [STRING] Offset in the method in hex
// VerPass [STRING] 1 / 2 ==> First pass, second pass
// VerMsgMethodInfoOff [STRING] Print method / module info on error
//
// NOTE : If there are more than one methods in the list and an offset
// is specified, this offset is applicable to all methods in the list
//
// NOTE : Verifier should be enabled for this to work.
//
// To Switch the verifier Off (Default is On) :
// (available on all builds).
//
// VerifierOff [STRING] 1 ==> Verifier is Off, 0 ==> Verifier is On
//
// [See EEConfig.h / EEConfig.cpp]
//
//
// Meaning of code marked with @XXX
//
// @VER_ASSERT : Already verified.
// @VER_IMPL : Verification rules implemented here.
// @DEBUG : To be removed/commented before checkin.
//
#include "common.h"
#include "verifier.hpp"
#include "ceeload.h"
#include "clsload.hpp"
#include "method.hpp"
#include "vars.hpp"
#include "object.h"
#include "field.h"
#include "comdelegate.h"
#include "security.h"
#include "dbginterface.h"
#include "securityattributes.h"
#include "eeconfig.h"
#include "sourceline.h"
#include "typedesc.h"
#include "typestring.h"
#include "../dlls/mscorrc/resource.h"
#define VER_NAME_INFO_SIZE 128
#define VER_SMALL_BUF_LEN 256
#define VER_FAILED_TO_LOAD_RESOURCE_STRING "(Failed to load resource string)"
#define VER_LD_RES(e, fld) \
{ \
if ((sRes.LoadResource(CCompRC::Error, e ))) \
{ \
sPrint.Printf(sRes.GetUnicode(), err.fld); \
sMessage += sPrint; \
} \
else \
{ \
SString s(SString::Ascii, VER_FAILED_TO_LOAD_RESOURCE_STRING); \
sMessage += s; \
} \
}
// Copies the error message to the input char*
WCHAR* Verifier::GetErrorMsg(
HRESULT hrError,
VerError err,
__inout_ecount(len) WCHAR *wszMsg,
int len,
ValidateWorkerArgs* pArgs)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
} CONTRACTL_END;
SString sMessage; // to debug, watch "(WCHAR*)sMessage.m_buffer"
SString sPrint;
LPCSTR szMethodName;
NewHolder<SourceLine> pSL(NULL);
if (pArgs->pMethodDesc)
{
// source lines
if (pArgs->fShowSourceLines && pArgs->wszFileName)
{
pSL = new SourceLine(pArgs->wszFileName);
if(pSL->IsInitialized())
{
DWORD dwFunctionToken = pArgs->pMethodDesc->GetMemberDef();
WCHAR wcBuffer[VER_SMALL_BUF_LEN];
wcBuffer[0] = 0;
DWORD dwLineNumber;
HRESULT hr;
hr = pSL->GetSourceLine( dwFunctionToken, err.dwOffset, wcBuffer, VER_SMALL_BUF_LEN, &dwLineNumber );
sPrint.Printf(W("%s(%d) : "), wcBuffer, dwLineNumber);
sMessage += sPrint;
}
SString sRes;
sRes.LoadResource(CCompRC::Debugging, IDS_VER_E_ILERROR);
sMessage += sRes;
}
// module
sMessage += W("[");
sMessage += pArgs->pMethodDesc->GetModule()->GetPath();
// class
sMessage += W(" : ");
if (pArgs->pMethodDesc->GetMethodTable() != NULL)
{
// DefineFullyQualifiedNameForClass();
// GetFullyQualifiedNameForClassNestedAware(pClass);
// sMessage += FilterAscii(_szclsname_, szTemp, VER_NAME_INFO_SIZE);
SString clsname;
TypeString::AppendType(clsname,TypeHandle(pArgs->pMethodDesc->GetMethodTable()));
sMessage += clsname;
}
else
{
SString sRes;
sRes.LoadResource(CCompRC::Debugging, IDS_VER_E_GLOBAL);
sMessage += sRes;
}
// method
sMessage += W("::");
if (FAILED(pArgs->pMethodDesc->GetModule()->GetMDImport()->GetNameOfMethodDef(pArgs->pMethodDesc->GetMemberDef(), &szMethodName)))
{
szMethodName = "Invalid MethodDef record";
}
SString sNameOfMethod(SString::Utf8, szMethodName);
sMessage += sNameOfMethod;
if (pArgs->pMethodDesc->IsGenericMethodDefinition())
{
SString inst;
TypeString::AppendInst(inst,pArgs->pMethodDesc->GetMethodInstantiation(),TypeString::FormatBasic);
sMessage += inst;
}
sMessage += W("]");
// MD token
if(pArgs->fVerbose)
{
SString sRes;
sRes.LoadResource(CCompRC::Debugging, IDS_VER_E_MDTOKEN);
DWORD dwMDToken = pArgs->pMethodDesc->GetMemberDef();
sPrint.Printf(sRes.GetUnicode(), dwMDToken);
sMessage += sPrint;
}
}
// Fill In the details
SString sRes;
// Create the generic error fields
if (err.dwFlags & VER_ERR_OFFSET)
VER_LD_RES(VER_E_OFFSET, dwOffset);
if (err.dwFlags & VER_ERR_OPCODE)
{
if (sRes.LoadResource(CCompRC::Error, VER_E_OPCODE))
{
sPrint.Printf(sRes, ppOpcodeNameList[err.opcode]);
sMessage += W(" ");
sMessage += sPrint;
}
}
if (err.dwFlags & VER_ERR_OPERAND)
VER_LD_RES(VER_E_OPERAND, dwOperand);
if (err.dwFlags & VER_ERR_TOKEN)
VER_LD_RES(VER_E_TOKEN, token);
if (err.dwFlags & VER_ERR_EXCEP_NUM_1)
VER_LD_RES(VER_E_EXCEPT, dwException1);
if (err.dwFlags & VER_ERR_EXCEP_NUM_2)
VER_LD_RES(VER_E_EXCEPT, dwException2);
if (err.dwFlags & VER_ERR_STACK_SLOT)
VER_LD_RES(VER_E_STACK_SLOT, dwStackSlot);
if ((err.dwFlags & VER_ERR_SIG_MASK) == VER_ERR_LOCAL_SIG)
{
if (err.dwVarNumber != VER_ERR_NO_LOC)
{
if(pArgs->fShowSourceLines && pSL && pSL->IsInitialized() && pArgs->pMethodDesc)
{
if ((sRes.LoadResource(CCompRC::Error, VER_E_LOC_BYNAME)))
{
DWORD dwFunctionToken = pArgs->pMethodDesc->GetMemberDef();
WCHAR wcBuffer[VER_SMALL_BUF_LEN];
wcBuffer[0] = 0;
HRESULT hr;
hr = pSL->GetLocalName(dwFunctionToken, err.dwVarNumber, wcBuffer, VER_SMALL_BUF_LEN);
sPrint.Printf(sRes.GetUnicode(), wcBuffer);
}
else
{
SString s(SString::Ascii, VER_FAILED_TO_LOAD_RESOURCE_STRING);
sPrint = s;
}
}
else
{
if ((sRes.LoadResource(CCompRC::Error, VER_E_LOC)))
sPrint.Printf(sRes.GetUnicode(), err.dwVarNumber);
else
{
SString s(SString::Ascii, VER_FAILED_TO_LOAD_RESOURCE_STRING);
sPrint = s;
}
}
sMessage += sPrint;
}
}
if ((err.dwFlags & VER_ERR_SIG_MASK) == VER_ERR_FIELD_SIG)
{
if (sRes.LoadResource(CCompRC::Error, VER_E_FIELD_SIG))
{
sMessage += W(" ");
sMessage += sRes;
}
}
if (((err.dwFlags & VER_ERR_SIG_MASK) == VER_ERR_METHOD_SIG) ||
((err.dwFlags & VER_ERR_SIG_MASK) == VER_ERR_CALL_SIG))
{
if (err.dwArgNumber != VER_ERR_NO_ARG)
{
if (err.dwArgNumber != VER_ERR_ARG_RET)
{
VER_LD_RES(VER_E_ARG, dwArgNumber);
}
else if (sRes.LoadResource(CCompRC::Error, VER_E_RET_SIG))
{
sMessage += W(" ");
sMessage += sRes;
}
}
}
if (err.dwFlags & VER_ERR_TYPE_1)
sMessage += err.wszType1;
if (err.dwFlags & VER_ERR_TYPE_2)
sMessage += err.wszType2;
if (err.dwFlags & VER_ERR_ADDL_MSG)
sMessage += err.wszAdditionalMessage;
if (err.dwFlags & VER_ERR_TYPE_F)
{
if (sRes.LoadResource(CCompRC::Error, VER_E_FOUND))
{
sPrint.Printf(sRes, err.wszTypeFound);
sMessage += sPrint;
}
}
if (err.dwFlags & VER_ERR_TYPE_E)
{
if (sRes.LoadResource(CCompRC::Error, VER_E_EXPECTED))
{
sPrint.Printf(sRes, err.wszTypeExpected);
sMessage += sPrint;
}
}
// Handle the special cases
switch (hrError)
{
case VER_E_UNKNOWN_OPCODE:
VER_LD_RES(VER_E_UNKNOWN_OPCODE, opcode);
break;
case VER_E_SIG_CALLCONV:
VER_LD_RES(VER_E_SIG_CALLCONV, bCallConv);
break;
case VER_E_SIG_ELEMTYPE:
VER_LD_RES(VER_E_SIG_ELEMTYPE, elem);
break;
case COR_E_ASSEMBLYEXPECTED:
Verifier::GetAssemblyName(hrError,sMessage, sRes, sPrint, pArgs);
break;
case SECURITY_E_UNVERIFIABLE:
Verifier::GetAssemblyName(hrError,sMessage, sRes, sPrint, pArgs);
break;
case CORSEC_E_MIN_GRANT_FAIL:
Verifier::GetAssemblyName(hrError,sMessage, sRes, sPrint, pArgs);
break;
case __HRESULT_FROM_WIN32(ERROR_BAD_FORMAT):
// fall through
default:
Verifier::GetDefaultMessage(hrError,sMessage, sRes, sPrint);
}
wcsncpy_s(wszMsg, len, sMessage.GetUnicode(), _TRUNCATE);
return wszMsg;
}
/*static*/ VOID Verifier::GetDefaultMessage(HRESULT hrError, SString& sMessage, SString& sRes, SString& sPrint)
{
if (sMessage.GetCount() > 0)
sMessage += W(" ");
if (HRESULT_FACILITY(hrError) == FACILITY_URT && sRes.LoadResource(CCompRC::Error, MSG_FOR_URT_HR(hrError)))
sMessage += sRes;
else
{
WCHAR win32Msg[VER_SMALL_BUF_LEN];
BOOL useWin32Msg = WszFormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
hrError,
#if FEATURE_USE_LCID
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
#else
0,
#endif
(LPTSTR) win32Msg,
VER_SMALL_BUF_LEN - 1,
NULL );
if (sRes.LoadResource(CCompRC::Error, VER_E_HRESULT))
{
sPrint.Printf(sRes, hrError);
if (useWin32Msg)
{
sPrint += W(" - ");
sPrint += win32Msg;
}
sMessage += W(" ");
sMessage += sPrint;
}
else
{
SString s(SString::Ascii, VER_FAILED_TO_LOAD_RESOURCE_STRING);
sMessage += s;
}
}
}
/*static*/ HRESULT Verifier::ReportError(IVEHandler *pVeh, HRESULT hrError, VEContext* pVec, ValidateWorkerArgs* pArgs)
{
CONTRACTL {
NOTHROW;
GC_TRIGGERS;
} CONTRACTL_END;
// Filter out error messages that require parameters
switch(hrError)
{
case COR_E_TYPELOAD: hrError = VER_E_TYPELOAD; break;
}
HRESULT hr = E_FAIL;
EX_TRY
{
GCX_PREEMP();
// There is no room for expansion in the VEHandler interface, so we're
// stuffing our extra data into the SafeArray that was originally
// designed to be used only by the MDValidator.
// Note: VT_VARIANT is the only supported safe array type on Rotor
SAFEARRAY* pSafeArray = SafeArrayCreateVector(VT_VARIANT, 0, 1);
_ASSERTE(pSafeArray);
if (pSafeArray)
{
VARIANT var;
#ifdef _WIN64
V_VT(&var) = VT_UI8; // machine sized int. (VT_UI8 not supported on Windows 2000)
V_UINT_PTR(&var) = (UINT64)(size_t)(pArgs);
#else
V_VT(&var) = VT_UINT; // machine sized int
V_UINT_PTR(&var) = (ULONG_PTR)(pArgs);
#endif
LONG i = 0;
HRESULT hrPutElement;
hrPutElement = SafeArrayPutElement(pSafeArray, &i, &var);
_ASSERTE(hrPutElement == S_OK);
}
// Call the handler
hr = pVeh->VEHandler(hrError, *pVec, pSafeArray);
// Clean up the SafeArray we allocated
HRESULT hrDestroy;
hrDestroy = SafeArrayDestroy(pSafeArray);
_ASSERTE(hrDestroy == S_OK);
}
EX_CATCH_HRESULT(hr);
return hr;
}
/*static*/ VOID Verifier::GetAssemblyName(HRESULT hrError, SString& sMessage, SString& sRes, SString& sPrint, ValidateWorkerArgs* pArgs)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if(sRes.LoadResource(CCompRC::Error, hrError))
{
// find the '%1'
SString::Iterator i = sRes.Begin();
if (sRes.Find(i, W("'%1'")))
{
// replace the '%1' with the module name
if(pArgs->wszFileName)
{
sPrint = pArgs->wszFileName;
sRes.Replace(i + 1, 2, sPrint);
}
else
{
sPrint = W("");
sRes.Replace(i, 4, sPrint);
}
sMessage += sRes;
}
}
else
{
SString s(SString::Ascii, VER_FAILED_TO_LOAD_RESOURCE_STRING);
sMessage += s;
}
}
|