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
|
// 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.
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Text.RegularExpressions;
using Internal.CommandLine;
using Internal.TypeSystem.Ecma;
using static System.Console;
namespace ILVerify
{
class Program : ResolverBase
{
private bool _help;
private bool _verbose;
private bool _printStatistics;
private AssemblyName _systemModule = new AssemblyName("mscorlib");
private Dictionary<string, string> _inputFilePaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // map of simple name to file path
private Dictionary<string, string> _referenceFilePaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // map of simple name to file path
private IReadOnlyList<Regex> _includePatterns = Array.Empty<Regex>();
private IReadOnlyList<Regex> _excludePatterns = Array.Empty<Regex>();
private Verifier _verifier;
private Program()
{
}
private void Help(string helpText)
{
WriteLine("ILVerify version " + typeof(Program).GetTypeInfo().Assembly.GetName().Version.ToString());
WriteLine();
WriteLine(helpText);
}
public static IReadOnlyList<Regex> StringPatternsToRegexList(IReadOnlyList<string> patterns)
{
List<Regex> patternList = new List<Regex>();
foreach (var pattern in patterns)
patternList.Add(new Regex(pattern, RegexOptions.Compiled));
return patternList;
}
private ArgumentSyntax ParseCommandLine(string[] args)
{
IReadOnlyList<string> inputFiles = Array.Empty<string>();
IReadOnlyList<string> referenceFiles = Array.Empty<string>();
IReadOnlyList<string> includePatterns = Array.Empty<string>();
IReadOnlyList<string> excludePatterns = Array.Empty<string>();
string includeFile = string.Empty;
string excludeFile = string.Empty;
AssemblyName name = typeof(Program).GetTypeInfo().Assembly.GetName();
ArgumentSyntax argSyntax = ArgumentSyntax.Parse(args, syntax =>
{
syntax.ApplicationName = name.Name.ToString();
// HandleHelp writes to error, fails fast with crash dialog and lacks custom formatting.
syntax.HandleHelp = false;
syntax.HandleErrors = true;
syntax.DefineOption("h|help", ref _help, "Display this usage message");
syntax.DefineOption("s|system-module", ref _systemModule, s => new AssemblyName(s), "System module name (default: mscorlib)");
syntax.DefineOptionList("r|reference", ref referenceFiles, "Reference metadata from the specified assembly");
syntax.DefineOptionList("i|include", ref includePatterns, "Use only methods/types/namespaces, which match the given regular expression(s)");
syntax.DefineOption("include-file", ref includeFile, "Same as --include, but the regular expression(s) are declared line by line in the specified file.");
syntax.DefineOptionList("e|exclude", ref excludePatterns, "Skip methods/types/namespaces, which match the given regular expression(s)");
syntax.DefineOption("exclude-file", ref excludeFile, "Same as --exclude, but the regular expression(s) are declared line by line in the specified file.");
syntax.DefineOption("statistics", ref _printStatistics, "Print verification statistics");
syntax.DefineOption("v|verbose", ref _verbose, "Verbose output");
syntax.DefineParameterList("in", ref inputFiles, "Input file(s)");
});
foreach (var input in inputFiles)
Helpers.AppendExpandedPaths(_inputFilePaths, input, true);
foreach (var reference in referenceFiles)
Helpers.AppendExpandedPaths(_referenceFilePaths, reference, false);
if (!string.IsNullOrEmpty(includeFile))
{
if (includePatterns.Count > 0)
WriteLine("[Warning] --include-file takes precedence over --include");
includePatterns = File.ReadAllLines(includeFile);
}
_includePatterns = StringPatternsToRegexList(includePatterns);
if (!string.IsNullOrEmpty(excludeFile))
{
if (excludePatterns.Count > 0)
WriteLine("[Warning] --exclude-file takes precedence over --exclude");
excludePatterns = File.ReadAllLines(excludeFile);
}
_excludePatterns = StringPatternsToRegexList(excludePatterns);
if (_verbose)
{
WriteLine();
foreach (var path in _inputFilePaths)
WriteLine($"Using input file '{path.Value}'");
WriteLine();
foreach (var path in _referenceFilePaths)
WriteLine($"Using reference file '{path.Value}'");
WriteLine();
foreach (var pattern in _includePatterns)
WriteLine($"Using include pattern '{pattern}'");
WriteLine();
foreach (var pattern in _excludePatterns)
WriteLine($"Using exclude pattern '{pattern}'");
}
return argSyntax;
}
private int Run(string[] args)
{
ArgumentSyntax syntax = ParseCommandLine(args);
if (_help)
{
Help(syntax.GetHelpText());
return 1;
}
if (_inputFilePaths.Count == 0)
throw new CommandLineException("No input files specified");
_verifier = new Verifier(this);
_verifier.SetSystemModuleName(_systemModule);
foreach (var kvp in _inputFilePaths)
{
VerifyAssembly(new AssemblyName(kvp.Key), kvp.Value);
}
return 0;
}
private void PrintResult(VerificationResult result, EcmaModule module, string pathOrModuleName)
{
Write("[IL]: Error: ");
Write("[");
Write(pathOrModuleName);
Write(" : ");
MetadataReader metadataReader = module.MetadataReader;
TypeDefinition typeDef = metadataReader.GetTypeDefinition(metadataReader.GetMethodDefinition(result.Method).GetDeclaringType());
string typeName = metadataReader.GetString(typeDef.Name);
Write(typeName);
Write("::");
var method = (EcmaMethod)module.GetMethod(result.Method);
PrintMethod(method);
Write("]");
var args = result.Error;
if (args.Code != VerifierError.None)
{
Write("[offset 0x");
Write(args.Offset.ToString("X8"));
Write("]");
if (args.Found != null)
{
Write("[found ");
Write(args.Found);
Write("]");
}
if (args.Expected != null)
{
Write("[expected ");
Write(args.Expected);
Write("]");
}
if (args.Token != 0)
{
Write("[token 0x");
Write(args.Token.ToString("X8"));
Write("]");
}
}
Write(" ");
WriteLine(result.Message);
}
private static void PrintMethod(EcmaMethod method)
{
Write(method.Name);
Write("(");
if (method.Signature.Length > 0)
{
bool first = true;
for(int i = 0; i < method.Signature.Length; i++)
{
Internal.TypeSystem.TypeDesc parameter = method.Signature[0];
if (first)
{
first = false;
}
else
{
Write(", ");
}
Write(parameter.ToString());
}
}
Write(")");
}
private void VerifyAssembly(AssemblyName name, string path)
{
PEReader peReader = Resolve(name);
EcmaModule module = _verifier.GetModule(peReader);
VerifyAssembly(peReader, module, path);
}
private void VerifyAssembly(PEReader peReader, EcmaModule module, string path)
{
int numErrors = 0;
int verifiedMethodCounter = 0;
int methodCounter = 0;
MetadataReader metadataReader = peReader.GetMetadataReader();
foreach (var methodHandle in metadataReader.MethodDefinitions)
{
// get fully qualified method name
var methodName = GetQualifiedMethodName(metadataReader, methodHandle);
bool verifying = ShouldVerifyMethod(methodName);
if (_verbose)
{
Write(verifying ? "Verifying " : "Skipping ");
WriteLine(methodName);
}
if (verifying)
{
var results = _verifier.Verify(peReader, methodHandle);
foreach (var result in results)
{
PrintResult(result, module, path);
numErrors++;
}
verifiedMethodCounter++;
}
methodCounter++;
}
if (numErrors > 0)
WriteLine(numErrors + " Error(s) Verifying " + path);
else
WriteLine("All Classes and Methods in " + path + " Verified.");
if (_printStatistics)
{
WriteLine($"Methods found: {methodCounter}");
WriteLine($"Methods verified: {verifiedMethodCounter}");
}
}
/// <summary>
/// This method returns the fully qualified method name by concatenating assembly, type and method name.
/// This method exists to avoid additional assembly resolving, which might be triggered by calling
/// MethodDesc.ToString().
/// </summary>
private string GetQualifiedMethodName(MetadataReader metadataReader, MethodDefinitionHandle methodHandle)
{
var methodDef = metadataReader.GetMethodDefinition(methodHandle);
var typeDef = metadataReader.GetTypeDefinition(methodDef.GetDeclaringType());
var methodName = metadataReader.GetString(metadataReader.GetMethodDefinition(methodHandle).Name);
var typeName = metadataReader.GetString(typeDef.Name);
var namespaceName = metadataReader.GetString(typeDef.Namespace);
var assemblyName = metadataReader.GetString(metadataReader.GetAssemblyDefinition().Name);
StringBuilder builder = new StringBuilder();
builder.Append($"[{assemblyName}]");
if (!string.IsNullOrEmpty(namespaceName))
builder.Append($"{namespaceName}.");
builder.Append($"{typeName}.{methodName}");
return builder.ToString();
}
private bool ShouldVerifyMethod(string methodName)
{
if (_includePatterns.Count > 0 && !_includePatterns.Any(p => p.IsMatch(methodName)))
{
return false;
}
if (_excludePatterns.Any(p => p.IsMatch(methodName)))
{
return false;
}
return true;
}
protected override PEReader ResolveCore(AssemblyName name)
{
// Note: we use simple names instead of full names to resolve, because we can't get a full name from an assembly without reading it
string simpleName = name.Name;
string path = null;
if (_inputFilePaths.TryGetValue(simpleName, out path) || _referenceFilePaths.TryGetValue(simpleName, out path))
{
return new PEReader(File.OpenRead(path));
}
return null;
}
private static int Main(string[] args)
{
try
{
return new Program().Run(args);
}
catch (Exception e)
{
Console.Error.WriteLine("Error: " + e.Message);
return 1;
}
}
}
}
|