Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d1e402c
Add IL verification with ILVerify in addition to PEVerify
Nov 2, 2017
70d3ab8
Annotating more tests
Mar 22, 2018
3f8b70f
Use new package with strong name
jcouv Mar 22, 2018
6e2efce
Merge remote-tracking branch 'dotnet/main' into il-verify2
Dec 30, 2021
bf116d9
Fix a couple more tests
Dec 30, 2021
a993d0e
Address some PROTOTYPE markers
Dec 30, 2021
7af8ecc
Tighten checks
Dec 30, 2021
0ee5f6a
Fix IL verification plumbing
Dec 30, 2021
1701dbb
Transition ILVerify to Core test runner
Dec 31, 2021
260a40e
Fix Resolver
Dec 31, 2021
7b0263a
public
Dec 31, 2021
400d315
Start tightening the IL verification
Dec 31, 2021
e7a7c3f
Remove some unused error entries
Dec 31, 2021
9cafb53
Annotate failures
Dec 31, 2021
656d328
InitOnly and NotVisible
Jan 1, 2022
eea70d3
MissingStringType
Jan 1, 2022
d328e27
MissingAssembly
Jan 1, 2022
ebbc74a
BadReturnType
Jan 1, 2022
66aa35b
More
Jan 1, 2022
c34aa28
More
Jan 1, 2022
d23c09c
Change design of flags
Jan 1, 2022
5a01e58
Ctor
Jan 1, 2022
ae83e16
more
Jan 2, 2022
9b1260c
more
Jan 2, 2022
ec54177
tweak
Jan 2, 2022
99e0f0a
VB
Jan 2, 2022
b2fc7c7
Conditional package reference
Jan 3, 2022
ebacf4f
Remove tracking of specific failures
Jan 5, 2022
d57946e
Move check
Jan 5, 2022
a345b1b
VB
Jan 5, 2022
7abba2e
Undo some changes
Jan 5, 2022
f3a3859
Cleanup
Jan 5, 2022
262ee1e
Refine resolution of ambiguities
Jan 6, 2022
733ac37
Align on simple names
Jan 6, 2022
c1f0f8d
Use signed ILVerification package from dotnet7 feed
Jan 29, 2022
f193504
Enable on desktop runtime
Jan 29, 2022
fffe57a
Merge remote-tracking branch 'dotnet/main' into il-verify2
Jan 29, 2022
4cf0b49
Revert "Enable on desktop runtime"
Jan 29, 2022
dceda42
Update added tests
Jan 29, 2022
115da8a
Use compilation to identify corlib
Jan 31, 2022
af67143
Use label for version number
Jan 31, 2022
a7dc694
Address feedback
Feb 2, 2022
ca5027f
Address feedback
Feb 8, 2022
6d83957
usings
Feb 8, 2022
c7d7ba6
Rename
Feb 8, 2022
cb27c93
indentation
Feb 8, 2022
a11ad37
Merge remote-tracking branch 'dotnet/main' into il-verify2
Feb 8, 2022
e32bf0a
Resolve conflicts
Feb 8, 2022
7e0ec94
remove catch
Feb 9, 2022
f5387f8
Revert "remove catch"
Feb 9, 2022
f9cf399
Shrink usage of exceptions
Feb 9, 2022
c55a61d
Adjust to new logic
Feb 9, 2022
fcc2dc2
one more test
Feb 10, 2022
3db3d54
Address feedback
jcouv Feb 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adjust to new logic
  • Loading branch information
Julien Couvreur committed Feb 10, 2022
commit c55a61d144266f97ddc1163390b1feed6412c215
Original file line number Diff line number Diff line change
Expand Up @@ -3982,7 +3982,7 @@ private void Test2(in int y, ref int f)

var compilation = CreateCompilation(code, options: TestOptions.ReleaseExe);

var verifier = CompileAndVerify(compilation, expectedOutput: "0011", verify: Verification.Fails);
var verifier = CompileAndVerify(compilation, expectedOutput: "0011", verify: Verification.Passes);

verifier.VerifyIL("Test..ctor()", @"
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2387,7 +2387,8 @@ public System.Collections.IEnumerable SomeNumbers()
}";
// The compilation succeeds even though CompilerGeneratedAttribute and DebuggerNonUserCodeAttribute are not available.
var compilation = CreateEmptyCompilation(new[] { Parse(source), Parse(corlib) });
var verifier = CompileAndVerify(compilation, verify: Verification.Fails);
// PEVerify: System.Enum must extend System.ValueType.
var verifier = CompileAndVerify(compilation, verify: Verification.FailsPEVerify);
verifier.VerifyDiagnostics(
// warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public override string ToString()
}
";

var comp = CompileAndVerify(text, parseOptions: TestOptions.Regular, verify: Verification.Fails, expectedOutput: @"12");
var comp = CompileAndVerify(text, parseOptions: TestOptions.Regular, verify: Verification.FailsPEVerify, expectedOutput: @"12");

comp.VerifyIL("Program.Main", @"
{
Expand Down Expand Up @@ -258,7 +258,8 @@ public override string ToString()
}
";

var comp = CompileAndVerify(text, parseOptions: TestOptions.Regular, verify: Verification.Fails, expectedOutput: @"hello2");
// PEVerify: Cannot change initonly field outside its .ctor.
var comp = CompileAndVerify(text, parseOptions: TestOptions.Regular, verify: Verification.FailsPEVerify, expectedOutput: @"hello2");

comp.VerifyIL("Program.Main", @"
{
Expand Down Expand Up @@ -338,7 +339,8 @@ static void Main()
}
";

var comp = CompileAndVerify(text, new[] { ref1 }, parseOptions: TestOptions.Regular, verify: Verification.Fails, expectedOutput: @"hello2");
// PEVerify: Cannot change initonly field outside its .ctor.
var comp = CompileAndVerify(text, new[] { ref1 }, parseOptions: TestOptions.Regular, verify: Verification.FailsPEVerify, expectedOutput: @"hello2");

comp.VerifyIL("Program.Main", @"
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,8 @@ public static void RoExtension(in this S1 self)
}
";

var comp = CompileAndVerifyWithMscorlib40(source, references: new[] { TestMetadata.Net40.System, ValueTupleRef, TestMetadata.Net40.SystemCore }, expectedOutput: "00", verify: Verification.Fails);
// PEVerify: Cannot change initonly field outside its .ctor.
var comp = CompileAndVerifyWithMscorlib40(source, references: new[] { TestMetadata.Net40.System, ValueTupleRef, TestMetadata.Net40.SystemCore }, expectedOutput: "00", verify: Verification.FailsPEVerify);
comp.VerifyDiagnostics();

comp.VerifyIL("Program.Main", @"
Expand Down Expand Up @@ -1147,8 +1148,8 @@ public override string ToString()
}
}
";

var comp = CompileAndVerify(source, expectedOutput: "00", verify: Verification.Fails);
// PEVerify: Cannot change initonly field outside its .ctor.
var comp = CompileAndVerify(source, expectedOutput: "00", verify: Verification.FailsPEVerify);
comp.VerifyDiagnostics();

comp.VerifyIL("Program.Test", @"
Expand Down
6 changes: 4 additions & 2 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12288,7 +12288,8 @@ public MyManagedStruct(int x)
n.n.num = x;
}
}";
var comp = CompileAndVerify(source, expectedOutput: @"42", parseOptions: TestOptions.Regular7_2, verify: Verification.Fails);
// PEVerify: Cannot change initonly field outside its .ctor.
var comp = CompileAndVerify(source, expectedOutput: @"42", parseOptions: TestOptions.Regular7_2, verify: Verification.FailsPEVerify);

comp.VerifyIL("Program.Main",
@"
Expand Down Expand Up @@ -12441,7 +12442,8 @@ public override string ToString()
return null;
}
}";
var comp = CompileAndVerify(source, expectedOutput: @"-10", verify: Verification.Fails);
// PEVerify: Cannot change initonly field outside its .ctor.
var comp = CompileAndVerify(source, expectedOutput: @"-10", verify: Verification.FailsPEVerify);

comp.VerifyIL("Program.Main",
@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,8 @@ public ref struct NotTypedReference { }
}";
var compilation1 = CreateEmptyCompilation(source1, assemblyName: GetUniqueName());

CompileAndVerify(compilation1, verify: Verification.Fails, symbolValidator: module =>
// PEVerify: Type load failed.
CompileAndVerify(compilation1, verify: Verification.FailsPEVerify, symbolValidator: module =>
{
var type = module.ContainingAssembly.GetTypeByMetadataName("System.TypedReference");
AssertReferencedIsByRefLike(type, hasObsolete: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2836,7 +2836,8 @@ static void Main()
// warning CS1685: The predefined type 'ExtensionAttribute' is defined in multiple assemblies in the global alias; using definition from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Diagnostic(ErrorCode.WRN_MultiplePredefTypes).WithArguments("System.Runtime.CompilerServices.ExtensionAttribute", "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089").WithLocation(1, 1));

var compiled = CompileAndVerify(comp, expectedOutput: "dynamic42", verify: Verification.Fails);
// PEVerify: Cannot change initonly field outside its .ctor.
var compiled = CompileAndVerify(comp, expectedOutput: "dynamic42", verify: Verification.FailsPEVerify);

compiled.VerifyIL("MyAwaiter.OnCompleted(System.Action)", @"
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1438,9 +1438,9 @@ public class C
options: TestOptions.DebugDll.WithMetadataImportOptions(MetadataImportOptions.All));
comp.VerifyDiagnostics();

// PE verification fails: [ : C::set_Property] Cannot change initonly field outside its .ctor.
// PEVerify: [ : C::set_Property] Cannot change initonly field outside its .ctor.
CompileAndVerify(comp, sourceSymbolValidator: symbolValidator, symbolValidator: symbolValidator,
verify: Verification.Fails);
verify: Verification.FailsPEVerify);

void symbolValidator(ModuleSymbol m)
{
Expand Down Expand Up @@ -2730,9 +2730,9 @@ public int Property
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition },
options: TestOptions.DebugExe, parseOptions: TestOptions.Regular9);
comp.VerifyDiagnostics();
// [ : C::set_Property] Cannot change initonly field outside its .ctor.
// PEVerify: [ : C::set_Property] Cannot change initonly field outside its .ctor.
CompileAndVerify(comp, expectedOutput: "42 43",
verify: Verification.Fails);
verify: Verification.FailsPEVerify);
}

[Fact]
Expand Down Expand Up @@ -4181,7 +4181,7 @@ public static void Main()
targetFramework: TargetFramework.Mscorlib40, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular9);
// PEVerify: [ : S::set_Property] Cannot change initonly field outside its .ctor.
CompileAndVerify(comp1, expectedOutput: "42",
verify: Verification.Fails);
verify: Verification.FailsPEVerify);
var comp1Ref = new[] { comp1.ToMetadataReference() };

var comp7 = CreateCompilation(source2, references: comp1Ref,
Expand All @@ -4205,7 +4205,7 @@ public readonly struct S
{
public int I { get; init; }
}
" }, verify: Verification.Fails, expectedOutput: "1");
" }, verify: Verification.FailsPEVerify, expectedOutput: "1");



Expand Down Expand Up @@ -4243,7 +4243,7 @@ public readonly struct S
private readonly int i;
public int I { get => i; init => i = value; }
}
" }, verify: Verification.Fails, expectedOutput: "1");
" }, verify: Verification.FailsPEVerify, expectedOutput: "1");

var s = verifier.Compilation.GetTypeByMetadataName("S");
var i = s.GetMember<IPropertySymbol>("I");
Expand Down Expand Up @@ -4282,7 +4282,7 @@ public struct S
{
public readonly int I { get; init; }
}
" }, verify: Verification.Fails, expectedOutput: "1");
" }, verify: Verification.FailsPEVerify, expectedOutput: "1");

var s = verifier.Compilation.GetTypeByMetadataName("S");
var i = s.GetMember<IPropertySymbol>("I");
Expand Down Expand Up @@ -4322,7 +4322,7 @@ public struct S
private readonly int i;
public readonly int I { get => i; init => i = value; }
}
" }, verify: Verification.Fails, expectedOutput: "1");
" }, verify: Verification.FailsPEVerify, expectedOutput: "1");

var s = verifier.Compilation.GetTypeByMetadataName("S");
var i = s.GetMember<IPropertySymbol>("I");
Expand Down Expand Up @@ -4417,7 +4417,7 @@ public int I2
}
}
}
" }, verify: Verification.Fails, expectedOutput: @"I1 was 1
" }, verify: Verification.FailsPEVerify, expectedOutput: @"I1 was 1
I1 is 0");

var s = verifier.Compilation.GetTypeByMetadataName("S");
Expand Down Expand Up @@ -4551,7 +4551,7 @@ public record Container(Person Person);
var comp = CreateCompilation(new[] { IsExternalInitTypeDefinition, source }, options: TestOptions.DebugExe);
comp.VerifyEmitDiagnostics();
// PEVerify: Cannot change initonly field outside its .ctor.
CompileAndVerify(comp, expectedOutput: "c", verify: Verification.Fails);
CompileAndVerify(comp, expectedOutput: "c", verify: Verification.FailsPEVerify);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,8 @@ public void RecordClone1()
Assert.Equal(1, ctor.ParameterCount);
Assert.True(ctor.Parameters[0].Type.Equals(c, TypeCompareKind.ConsiderEverything));

var verifier = CompileAndVerify(comp, verify: Verification.Fails).VerifyDiagnostics();
// PEVerify: Cannot change initonly field outside its .ctor.
var verifier = CompileAndVerify(comp, verify: Verification.FailsPEVerify).VerifyDiagnostics();
verifier.VerifyIL("C." + WellKnownMemberNames.CloneMethodName, @"
{
// Code size 7 (0x7)
Expand Down Expand Up @@ -801,7 +802,8 @@ public C(C other)
Assert.Equal(1, ctor.ParameterCount);
Assert.True(ctor.Parameters[0].Type.Equals(c, TypeCompareKind.ConsiderEverything));

var verifier = CompileAndVerify(comp, verify: Verification.Fails).VerifyDiagnostics();
// PEVerify: Cannot change initonly field outside its .ctor.
var verifier = CompileAndVerify(comp, verify: Verification.FailsPEVerify).VerifyDiagnostics();
verifier.VerifyIL("C." + WellKnownMemberNames.CloneMethodName, @"
{
// Code size 7 (0x7)
Expand Down Expand Up @@ -897,7 +899,8 @@ public record C(int x, int y)
Assert.Equal(1, ctor.ParameterCount);
Assert.True(ctor.Parameters[0].Type.Equals(c, TypeCompareKind.ConsiderEverything));

var verifier = CompileAndVerify(comp, verify: Verification.Fails).VerifyDiagnostics(
// PEVerify: Cannot change initonly field outside its .ctor.
var verifier = CompileAndVerify(comp, verify: Verification.FailsPEVerify).VerifyDiagnostics(
// (5,25): warning CS0067: The event 'C.E' is never used
// public event Action E;
Diagnostic(ErrorCode.WRN_UnreferencedEvent, "E").WithArguments("C.E").WithLocation(5, 25)
Expand Down
14 changes: 10 additions & 4 deletions src/Compilers/Test/Core/CompilationVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,17 @@ private void ILVerify(Verification verification)

var resolver = new Resolver(imagesByName);
var verifier = new ILVerify.Verifier(resolver);
var mscorlibModule = _allModuleData.Single(m => m.IsCorLib);
verifier.SetSystemModuleName(new AssemblyName(mscorlibModule.SimpleName));
var mscorlibModule = _allModuleData.SingleOrDefault(m => m.IsCorLib);
if (mscorlibModule is null &&
(verification & Verification.FailsILVerify) != 0)
{
return;
}

// Main module is the first one
var mainModuleReader = resolver.Resolve(_allModuleData[0].SimpleName);

var (succeeded, errorMessage) = verify(verifier, mainModuleReader);
var (succeeded, errorMessage) = verify(verifier, mscorlibModule.SimpleName, mainModuleReader);
Copy link
Contributor

@cston cston Feb 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mscorlibModule.SimpleName

If (verification & Verification.FailsILVerify) == 0, mscorlibModule may be null. #Resolved


switch (succeeded, (verification & Verification.FailsILVerify) == 0)
{
Expand All @@ -298,10 +302,12 @@ private void ILVerify(Verification verification)
throw new Exception("IL Verify failed unexpectedly: \r\n" + errorMessage);
}

static (bool, string) verify(ILVerify.Verifier verifier, PEReader mainModule)
static (bool, string) verify(ILVerify.Verifier verifier, string corlibName, PEReader mainModule)
{
try
{
verifier.SetSystemModuleName(new AssemblyName(corlibName));

var result = verifier.Verify(mainModule);

if (result.Count() != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ End Class
End Sub

CompileAndVerify(verifiable, validator:=validator)
CompileAndVerify(unverifiable, validator:=validator, verify:=Verification.Fails)
CompileAndVerify(unloadable, validator:=validator, verify:=Verification.Fails)
CompileAndVerify(unverifiable, validator:=validator, verify:=Verification.FailsPEVerify)
CompileAndVerify(unloadable, validator:=validator, verify:=Verification.FailsPEVerify)
End Sub

<Fact>
Expand Down Expand Up @@ -566,7 +566,7 @@ End Class
</file>
</compilation>
' type C can't be loaded
CompileAndVerify(source, verify:=Verification.Fails)
CompileAndVerify(source, verify:=Verification.FailsPEVerify)
End Sub

<Fact>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ System.Console.Write("complete")
"s0.dll",
syntaxTree:=Parse(source0.Value, parseOptions),
references:=references)
Dim verifier = CompileAndVerify(s0, verify:=Verification.Fails)
' PEVerify: Error: Assembly name contains leading spaces or path or extension.
Dim verifier = CompileAndVerify(s0, verify:=Verification.FailsPEVerify)
Dim methodData = verifier.TestData.GetMethodData("Script.<Initialize>")
Assert.Equal("System.Threading.Tasks.Task(Of Object)", methodData.Method.ReturnType.ToDisplayString())
methodData.VerifyIL(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4207,9 +4207,12 @@ End Class
</file>
</compilation>

' PEVerify:
' [ : AllMembers::ValidateMethod][mdToken=0x6000003] [HRESULT 0x8000000F] - Typename or Namespace was not found in metadata file.
' [ : AllMembers::TestExpressionTreeCompiler][mdToken=0x6000005][offset 0x00000007] Unable to resolve token.
Dim verifier = CompileAndVerify(source,
references:=LegacyRefs,
verify:=Verification.Fails)
verify:=Verification.FailsPEVerify)
AssertNoErrorsOrWarnings(verifier)
verifier.VerifyIL("AllMembers.TestExpressionTreeCompiler", <![CDATA[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4679,7 +4679,7 @@ End Class

Assert.Null(compilation0.GetWellKnownTypeMember(WellKnownMember.System_Runtime_CompilerServices_AsyncStateMachineAttribute__ctor))

Dim v0 = CompileAndVerify(compilation0, verify:=Verification.Fails)
Dim v0 = CompileAndVerify(compilation0, verify:=Verification.FailsPEVerify)
Dim md0 = ModuleMetadata.CreateFromImage(v0.EmittedAssemblyData)

Dim f0 = compilation0.GetMember(Of MethodSymbol)("C.F")
Expand Down Expand Up @@ -4756,7 +4756,7 @@ End Class

Assert.Null(compilation0.GetWellKnownTypeMember(WellKnownMember.System_Runtime_CompilerServices_AsyncStateMachineAttribute__ctor))

Dim v0 = CompileAndVerify(compilation0, verify:=Verification.Fails)
Dim v0 = CompileAndVerify(compilation0, verify:=Verification.FailsPEVerify)
Dim md0 = ModuleMetadata.CreateFromImage(v0.EmittedAssemblyData)

Dim f0 = compilation0.GetMember(Of MethodSymbol)("C.F")
Expand Down
Loading