-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .mvid section to PE and remove dependency on MetadataReader #19133
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
53a66be
Add .mvid section to PE and remove dependency on MetadataReader
67c31d3
Address PR feedback from Tomas and Jared
51a3c3f
Update refout docs
4de0d32
Address PR feedback from Tomas
jcouv ec9b2d4
Address PR feedback from Chuck and Aleksey
jcouv b1ac2aa
Clarify header format
jcouv 4c02a2d
Add test for .mvid section not being first
jcouv 7df9479
No end-of-stream exception
jcouv 786dbe7
More feedback
jcouv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.Linq; | ||
|
@@ -49,7 +50,6 @@ public void Main() | |
Diagnostic(ErrorCode.ERR_AssgLvalueExpected, "x")); | ||
} | ||
|
||
|
||
[Fact] | ||
public void CompilationEmitWithQuotedMainType() | ||
{ | ||
|
@@ -262,9 +262,11 @@ internal static void Main() | |
|
||
VerifyEntryPoint(output, expectZero: false); | ||
VerifyMethods(output, new[] { "void C.Main()", "C..ctor()" }); | ||
VerifyMvid(output, hasMvidSection: false); | ||
|
||
VerifyEntryPoint(metadataOutput, expectZero: true); | ||
VerifyMethods(metadataOutput, new[] { "C..ctor()" }); | ||
VerifyMvid(metadataOutput, hasMvidSection: true); | ||
} | ||
|
||
void VerifyEntryPoint(MemoryStream stream, bool expectZero) | ||
|
@@ -275,6 +277,108 @@ void VerifyEntryPoint(MemoryStream stream, bool expectZero) | |
} | ||
} | ||
|
||
private class TestResourceSectionBuilder : ResourceSectionBuilder | ||
{ | ||
public TestResourceSectionBuilder() | ||
{ | ||
} | ||
|
||
protected override void Serialize(BlobBuilder builder, SectionLocation location) | ||
{ | ||
builder.WriteInt32(0x12345678); | ||
builder.WriteInt32(location.PointerToRawData); | ||
builder.WriteInt32(location.RelativeVirtualAddress); | ||
} | ||
} | ||
|
||
private class TestPEBuilder : ManagedPEBuilder | ||
{ | ||
public static readonly Guid s_mvid = Guid.Parse("a78fa2c3-854e-42bf-8b8d-75a450a6dc18"); | ||
|
||
public TestPEBuilder(PEHeaderBuilder header, | ||
MetadataRootBuilder metadataRootBuilder, | ||
BlobBuilder ilStream, | ||
ResourceSectionBuilder nativeResources) | ||
: base(header, metadataRootBuilder, ilStream, nativeResources: nativeResources) | ||
{ | ||
} | ||
|
||
protected override ImmutableArray<Section> CreateSections() | ||
{ | ||
return base.CreateSections().Add( | ||
new Section(".mvid", SectionCharacteristics.MemRead | | ||
SectionCharacteristics.ContainsInitializedData | | ||
SectionCharacteristics.MemDiscardable)); | ||
} | ||
|
||
protected override BlobBuilder SerializeSection(string name, SectionLocation location) | ||
{ | ||
if (name.Equals(".mvid", StringComparison.Ordinal)) | ||
{ | ||
var sectionBuilder = new BlobBuilder(); | ||
sectionBuilder.WriteGuid(s_mvid); | ||
return sectionBuilder; | ||
} | ||
|
||
return base.SerializeSection(name, location); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void MvidSectionNotFirst() | ||
{ | ||
var ilBuilder = new BlobBuilder(); | ||
var metadataBuilder = new MetadataBuilder(); | ||
|
||
var peBuilder = new TestPEBuilder( | ||
PEHeaderBuilder.CreateLibraryHeader(), | ||
new MetadataRootBuilder(metadataBuilder), | ||
ilBuilder, | ||
nativeResources: new TestResourceSectionBuilder()); | ||
|
||
var peBlob = new BlobBuilder(); | ||
peBuilder.Serialize(peBlob); | ||
|
||
var peStream = new MemoryStream(); | ||
peBlob.WriteContentTo(peStream); | ||
|
||
peStream.Position = 0; | ||
var peReader = new PEReader(peStream); | ||
AssertEx.Equal(new[] { ".text", ".rsrc", ".reloc", ".mvid" }, | ||
peReader.PEHeaders.SectionHeaders.Select(h => h.Name)); | ||
|
||
peStream.Position = 0; | ||
var mvid = BuildTasks.MvidReader.ReadAssemblyMvidOrEmpty(peStream); | ||
Assert.Equal(TestPEBuilder.s_mvid, mvid); | ||
} | ||
|
||
/// <summary> | ||
/// Extract the MVID using two different methods (PEReader and MvidReader) and compare them. | ||
/// We only expect an .mvid section in ref assemblies. | ||
/// </summary> | ||
private void VerifyMvid(MemoryStream stream, bool hasMvidSection) | ||
{ | ||
stream.Position = 0; | ||
using (var reader = new PEReader(stream)) | ||
{ | ||
var metadataReader = reader.GetMetadataReader(); | ||
Guid mvidFromModuleDefinition = metadataReader.GetGuid(metadataReader.GetModuleDefinition().Mvid); | ||
|
||
stream.Position = 0; | ||
var mvidFromMvidReader = BuildTasks.MvidReader.ReadAssemblyMvidOrEmpty(stream); | ||
|
||
Assert.NotEqual(Guid.Empty, mvidFromModuleDefinition); | ||
if (hasMvidSection) | ||
{ | ||
Assert.Equal(mvidFromModuleDefinition, mvidFromMvidReader); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we checking the mvids are non-default? #Resolved |
||
} | ||
else | ||
{ | ||
Assert.Equal(Guid.Empty, mvidFromMvidReader); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void EmitRefAssembly_PrivatePropertySetter() | ||
{ | ||
|
@@ -296,6 +400,8 @@ public class C | |
VerifyMethods(output, new[] { "System.Int32 C.<PrivateSetter>k__BackingField", "System.Int32 C.PrivateSetter.get", "void C.PrivateSetter.set", | ||
"C..ctor()", "System.Int32 C.PrivateSetter { get; private set; }" }); | ||
VerifyMethods(metadataOutput, new[] { "System.Int32 C.PrivateSetter.get", "C..ctor()", "System.Int32 C.PrivateSetter { get; }" }); | ||
VerifyMvid(output, hasMvidSection: false); | ||
VerifyMvid(metadataOutput, hasMvidSection: true); | ||
} | ||
} | ||
|
||
|
@@ -559,6 +665,20 @@ private void CompareAssemblies(string sourceTemplate, string change1, string cha | |
{ | ||
AssertEx.NotEqual(image1, image2, message: $"Expecting difference for includePrivateMembers={includePrivateMembers} case, but they matched."); | ||
} | ||
|
||
var mvid1 = BuildTasks.MvidReader.ReadAssemblyMvidOrEmpty(new MemoryStream(image1.DangerousGetUnderlyingArray())); | ||
var mvid2 = BuildTasks.MvidReader.ReadAssemblyMvidOrEmpty(new MemoryStream(image2.DangerousGetUnderlyingArray())); | ||
|
||
if (!includePrivateMembers) | ||
{ | ||
Assert.NotEqual(Guid.Empty, mvid1); | ||
Assert.Equal(expectMatch, mvid1 == mvid2); | ||
} | ||
else | ||
{ | ||
Assert.Equal(Guid.Empty, mvid1); | ||
Assert.Equal(Guid.Empty, mvid2); | ||
} | ||
} | ||
|
||
[Fact] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using (var peReader = ...) { ... }
#Resolved