-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathIlVerifier.cs
More file actions
473 lines (425 loc) · 18.2 KB
/
Copy pathIlVerifier.cs
File metadata and controls
473 lines (425 loc) · 18.2 KB
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
// <copyright file="IlVerifier.cs" company="GSharp">
// Copyright (C) GSharp Authors. All rights reserved.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using Xunit.Sdk;
namespace GSharp.Compiler.Tests;
/// <summary>
/// Mechanical IL verification gate for assemblies emitted by Compiler.Tests.
///
/// Wraps the <c>dotnet-ilverify</c> local tool (declared in
/// <c>.config/dotnet-tools.json</c>) and runs it against a freshly-emitted
/// assembly. Tests should call <see cref="Verify(string, IEnumerable{string})"/>
/// immediately after a successful compile so that any invalid IL produced by
/// gsc is attributed to the test that emitted it.
///
/// Behavior:
/// <list type="bullet">
/// <item>The host runtime's <c>System.*.dll</c>, <c>mscorlib.dll</c>,
/// <c>netstandard.dll</c> and <c>System.Private.CoreLib.dll</c> are
/// always added to the reference set, so callers only need to pass
/// user-supplied or test-built references.</item>
/// <item>Verification can be globally skipped by setting the environment
/// variable <c>GSHARP_SKIP_ILVERIFY=1</c>. This is intended for local
/// debugging only; CI must run with verification enabled so invalid IL
/// is caught on every PR.</item>
/// <item>If the <c>ilverify</c> tool cannot be located, the helper throws
/// (instead of silently passing) so CI failures are obvious. To bypass
/// in environments without the tool, set <c>GSHARP_SKIP_ILVERIFY=1</c>.</item>
/// </list>
/// </summary>
internal static class IlVerifier
{
private const string SkipEnvVar = "GSHARP_SKIP_ILVERIFY";
private static readonly object ToolLocateSync = new();
private static readonly Lazy<IReadOnlyList<string>> RuntimeReferences =
new(BuildDefaultRuntimeReferences);
private static string? cachedToolCommand;
private static IReadOnlyList<string>? cachedToolArgs;
/// <summary>
/// Verifies the IL of the assembly at <paramref name="assemblyPath"/> using
/// <c>dotnet-ilverify</c>. Throws (failing the test) on any verification
/// error. The host runtime's BCL assemblies are added to the reference set
/// automatically; callers only need to pass additional, test-specific
/// references (for example, a user library the assembly under test depends
/// on).
/// </summary>
/// <param name="assemblyPath">Path to the .dll to verify.</param>
/// <param name="additionalReferences">Optional extra reference assemblies
/// the assembly under test depends on. May be null or empty.</param>
/// <param name="ignoredErrorCodes">Optional ECMA-335 error codes that
/// ilverify should treat as non-fatal. Use this to mark known compiler
/// bugs so the gate catches NEW regressions without failing on
/// already-tracked issues. Each entry should be the bracketed identifier
/// from ilverify output (for example, <c>"CallVirtOnValueType"</c>); the
/// helper translates it into the matching regex.</param>
public static void Verify(
string assemblyPath,
IEnumerable<string>? additionalReferences = null,
IEnumerable<string>? ignoredErrorCodes = null)
{
if (Environment.GetEnvironmentVariable(SkipEnvVar) == "1")
{
return;
}
if (!File.Exists(assemblyPath))
{
throw new XunitException($"ilverify: assembly not found at '{assemblyPath}'");
}
var (command, leadingArgs) = LocateTool();
var references = BuildReferenceSet(assemblyPath, additionalReferences);
var args = new List<string>(leadingArgs)
{
assemblyPath,
"-s",
"System.Private.CoreLib",
};
foreach (var reference in references)
{
args.Add("-r");
args.Add(reference);
}
if (ignoredErrorCodes is not null)
{
foreach (var code in ignoredErrorCodes)
{
if (string.IsNullOrWhiteSpace(code))
{
continue;
}
// ilverify's --ignore-error matches its regex against the
// error category name (e.g., "CallVirtOnValueType"), NOT
// against the rendered "[IL]: Error [code]: ..." line. Pass
// the bare code so a partial-substring regex hits cleanly.
args.Add("-g");
args.Add(code);
}
}
var psi = new ProcessStartInfo(command)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
// When using `dotnet tool run`, the manifest is discovered relative
// to the current directory. Tests routinely emit assemblies into a
// per-test temp dir with no manifest, so anchor the tool lookup at
// the repo root. All paths passed to ilverify are absolute.
WorkingDirectory = FindRepoRoot(),
};
foreach (var a in args)
{
psi.ArgumentList.Add(a);
}
using var proc = Process.Start(psi)
?? throw new XunitException($"ilverify: failed to start '{command}'");
// Drain both pipes concurrently before waiting: reading stdout to EOF
// and only then stderr can deadlock when ilverify fills the stderr pipe
// buffer (~64 KB of error lines) while we are still blocked on stdout,
// leaving the child unable to exit. Awaiting both reads together avoids
// the classic redirected-pipe deadlock.
var stdoutTask = proc.StandardOutput.ReadToEndAsync();
var stderrTask = proc.StandardError.ReadToEndAsync();
proc.WaitForExit();
var stdout = stdoutTask.GetAwaiter().GetResult();
var stderr = stderrTask.GetAwaiter().GetResult();
if (proc.ExitCode != 0)
{
// ilverify writes one line per IL error. Surface its full output so
// a CI failure points directly at the offending opcode/method.
var message = new StringBuilder()
.Append("ilverify detected invalid IL in '")
.Append(assemblyPath)
.Append("' (exit ")
.Append(proc.ExitCode)
.AppendLine("):")
.AppendLine(stdout.TrimEnd())
.Append(stderr.TrimEnd())
.ToString();
throw new XunitException(message);
}
}
/// <summary>
/// Returns true when IL verification is enabled for this test run. Tests
/// can use this to skip emitting an assembly path that ilverify cannot
/// handle (for example, executables targeting a host the tool does not
/// support). The default is true; set <c>GSHARP_SKIP_ILVERIFY=1</c> to
/// disable.
/// </summary>
public static bool IsEnabled => Environment.GetEnvironmentVariable(SkipEnvVar) != "1";
/// <summary>
/// Pre-computed ilverify error-code bundles for compiler-emission patterns
/// that currently produce non-strict-conforming IL. Each bundle should map
/// to an open GitHub issue (see comments). As bugs are fixed, the matching
/// bundle should shrink — and once empty, the gate is fully closed.
/// </summary>
public static class KnownIssues
{
/// <summary>
/// By-value returns of a user-declared <c>ref struct</c> (e.g.
/// <c>func add(a Accumulator, n int32) Accumulator { return Accumulator{Total: a.Total + n} }</c>)
/// trip ilverify's <c>ReturnPtrToStack</c> check on
/// <c>dotnet-ilverify</c> 10.0.8. This is a known ilverify
/// limitation, NOT a G# emitter bug: the same minimal C# program
/// (a <c>public ref struct</c> with a <c>public static T Add(T, int)</c>
/// returning <c>new T { ... }</c>) compiled by <c>csc</c> emits
/// identical IL and fails the same check. The verifier rejects any
/// <c>IsByRefLike</c> return-type signature even when the returned
/// value is in a "permanent home" (the caller's stack frame, the
/// only legal escape for a ref struct value).
///
/// Track the upstream issue at
/// https://github.com/dotnet/runtime/issues/129030 and
/// drop this bundle once a newer ilverify release distinguishes
/// permanent-home returns from raw byref returns.
/// </summary>
public static readonly string[] RefStruct =
{
"ReturnPtrToStack",
};
/// <summary>
/// ADR-0089 / issue #755: <c>constrained.</c> + <c>call</c> on a
/// static-virtual interface slot trips
/// <c>dotnet-ilverify</c> 10.0.8's pre-C# 11 verifier rules. The
/// verifier hard-codes "every <c>constrained.</c> prefix must be
/// followed by <c>callvirt</c>" and "static <c>call</c> is not
/// allowed on abstract methods". Both rules predate the .NET 7
/// static-virtual-in-interfaces extension (ECMA-335 II.15.4.2.4 +
/// .NET 7 specification of static-virtual dispatch via
/// <c>constrained. !!T call</c>), so the same minimal pattern
/// emitted by the C# 11 compiler with <c>LangVersion=preview</c>
/// fails the same two checks. The runtime JIT accepts the IL and
/// dispatches correctly.
///
/// Track the upstream issue at
/// https://github.com/dotnet/runtime/issues/49558 (dotnet-ilverify
/// catch-up) and drop this bundle once a newer ilverify release
/// recognises the static-virtual pattern.
/// </summary>
public static readonly string[] StaticVirtualInterface =
{
"CallAbstract",
"Constrained",
};
}
/// <summary>
/// Maps a sample name (as it appears under <c>samples/</c>) to the bundle
/// of ilverify error codes that the conformance harness should treat as
/// known issues. Samples not present in this map are verified strictly.
/// Keys are matched case-insensitively against the sample's base name
/// without extension.
/// </summary>
public static IReadOnlyList<string> GetKnownIssuesForSample(string sampleBaseName)
{
var key = sampleBaseName.TrimEnd('/');
if (SampleKnownIssues.TryGetValue(key, out var codes))
{
return codes;
}
return Array.Empty<string>();
}
private static readonly Dictionary<string, string[]> SampleKnownIssues = new(StringComparer.OrdinalIgnoreCase)
{
// Ref struct emission: see KnownIssues.RefStruct.
["UserRefStruct"] = KnownIssues.RefStruct,
// ADR-0089 / issue #755: static-virtual interface dispatch emits
// the canonical `constrained. !!T call <iface>::<method>` pattern
// that pre-C#-11 ilverify rules don't understand. Identical errors
// are produced by csc-emitted IL for the same C# 11 pattern.
["StaticVirtualInterfaces"] = KnownIssues.StaticVirtualInterface,
};
private static (string Command, IReadOnlyList<string> LeadingArgs) LocateTool()
{
// Discovering the tool requires a `dotnet` lookup that may walk up the
// directory tree (the local manifest lives at the repo root). Cache the
// result on first use so we don't pay that cost per test.
if (cachedToolCommand is not null && cachedToolArgs is not null)
{
return (cachedToolCommand, cachedToolArgs);
}
lock (ToolLocateSync)
{
if (cachedToolCommand is not null && cachedToolArgs is not null)
{
return (cachedToolCommand, cachedToolArgs);
}
// 1) Prefer the local-tool manifest entry: `dotnet tool run ilverify`.
// This is what CI restores via `dotnet tool restore`.
if (TryProbeDotnetToolRun(out var args))
{
cachedToolCommand = "dotnet";
cachedToolArgs = args;
return (cachedToolCommand, cachedToolArgs);
}
// 2) Fall back to a globally-installed `ilverify` on PATH (or in
// $HOME/.dotnet/tools, which dotnet adds for global tools).
if (TryProbeOnPath("ilverify"))
{
cachedToolCommand = "ilverify";
cachedToolArgs = Array.Empty<string>();
return (cachedToolCommand, cachedToolArgs);
}
throw new XunitException(
"ilverify is required by the test suite but was not found. " +
"Run `dotnet tool restore` at the repository root, or install it globally with " +
"`dotnet tool install -g dotnet-ilverify`. To skip verification for local debugging only, " +
$"set {SkipEnvVar}=1.");
}
}
private static bool TryProbeDotnetToolRun(out IReadOnlyList<string> args)
{
// `dotnet tool run ilverify --version` returns 0 when the local
// manifest knows about the tool. The version flag avoids printing the
// full help text on success.
var psi = new ProcessStartInfo("dotnet")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = FindRepoRoot(),
};
psi.ArgumentList.Add("tool");
psi.ArgumentList.Add("run");
psi.ArgumentList.Add("ilverify");
psi.ArgumentList.Add("--version");
try
{
using var proc = Process.Start(psi);
if (proc is null)
{
args = Array.Empty<string>();
return false;
}
proc.StandardOutput.ReadToEnd();
proc.StandardError.ReadToEnd();
proc.WaitForExit();
if (proc.ExitCode == 0)
{
args = new[] { "tool", "run", "ilverify" };
return true;
}
}
catch
{
// Fall through to the next probe.
}
args = Array.Empty<string>();
return false;
}
private static bool TryProbeOnPath(string command)
{
var psi = new ProcessStartInfo(command)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
psi.ArgumentList.Add("--version");
try
{
using var proc = Process.Start(psi);
if (proc is null)
{
return false;
}
proc.StandardOutput.ReadToEnd();
proc.StandardError.ReadToEnd();
proc.WaitForExit();
return proc.ExitCode == 0;
}
catch
{
return false;
}
}
private static string FindRepoRoot()
{
// Walk up from this assembly's location until we find a directory
// containing `.config/dotnet-tools.json`. This keeps tool discovery
// working regardless of the runner's current directory.
var dir = Path.GetDirectoryName(typeof(IlVerifier).Assembly.Location);
while (!string.IsNullOrEmpty(dir))
{
if (File.Exists(Path.Combine(dir, ".config", "dotnet-tools.json")))
{
return dir;
}
dir = Path.GetDirectoryName(dir);
}
return Environment.CurrentDirectory;
}
private static IReadOnlyList<string> BuildReferenceSet(
string assemblyPath,
IEnumerable<string>? additionalReferences)
{
// Use an ordinal-ignore-case set so we don't double-pass the same DLL
// when an additional reference also lives in the runtime directory.
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var ordered = new List<string>();
void Add(string path)
{
if (string.IsNullOrEmpty(path))
{
return;
}
// ilverify treats the verified assembly itself as the "primary"
// input; don't also pass it via -r (that confuses metadata loading).
if (string.Equals(
Path.GetFullPath(path),
Path.GetFullPath(assemblyPath),
StringComparison.OrdinalIgnoreCase))
{
return;
}
if (seen.Add(path))
{
ordered.Add(path);
}
}
foreach (var r in RuntimeReferences.Value)
{
Add(r);
}
if (additionalReferences is not null)
{
foreach (var r in additionalReferences)
{
Add(r);
}
}
return ordered;
}
private static IReadOnlyList<string> BuildDefaultRuntimeReferences()
{
// The host runtime directory is the simplest stable source of refs that
// matches what Compiler.Tests typically passes to gsc via /reference:.
var runtimeDir = Path.GetDirectoryName(typeof(object).Assembly.Location);
if (string.IsNullOrEmpty(runtimeDir) || !Directory.Exists(runtimeDir))
{
return Array.Empty<string>();
}
var refs = new List<string>();
foreach (var path in Directory.EnumerateFiles(runtimeDir, "*.dll", SearchOption.TopDirectoryOnly))
{
var name = Path.GetFileName(path);
if (name.StartsWith("System.", StringComparison.OrdinalIgnoreCase)
|| string.Equals(name, "mscorlib.dll", StringComparison.OrdinalIgnoreCase)
|| string.Equals(name, "netstandard.dll", StringComparison.OrdinalIgnoreCase)
|| string.Equals(name, "Microsoft.CSharp.dll", StringComparison.OrdinalIgnoreCase)
|| string.Equals(name, "Microsoft.VisualBasic.Core.dll", StringComparison.OrdinalIgnoreCase)
|| string.Equals(name, "Microsoft.Win32.Primitives.dll", StringComparison.OrdinalIgnoreCase)
|| string.Equals(name, "Microsoft.Win32.Registry.dll", StringComparison.OrdinalIgnoreCase))
{
refs.Add(path);
}
}
return refs;
}
}