Skip to content

Commit

Permalink
Bug 217681: compiler should not crash on public+private metadata (#11547
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jcouv committed May 25, 2016
1 parent 2b079d8 commit b35416a
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ public override Accessibility DeclaredAccessibility
break;

default:
throw ExceptionUtilities.UnexpectedValue(_flags & FieldAttributes.FieldAccessMask);
access = Accessibility.Private;
break;
}

return access;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public override Accessibility DeclaredAccessibility
return Accessibility.Protected;

default:
throw ExceptionUtilities.UnexpectedValue(_flags);
return Accessibility.Private;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1346,5 +1346,38 @@ instance void .ctor() cil managed
var method = type.GetMember<MethodSymbol>("M");
Assert.NotNull(method.ReturnType);
}

[Fact, WorkItem(217681, "https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_workitems?id=217681")]
public void LoadingMethodWithPublicAndPrivateAccessibility()
{
string source =
@"
public class D
{
public static void Main()
{
new C().M();
System.Console.WriteLine(new C().F);
new C.C2().M2();
}
}
";
var references = new[] { MetadataReference.CreateFromImage(TestResources.SymbolsTests.Metadata.PublicAndPrivateFlags) };

var comp = CreateCompilationWithMscorlib(source, references: references);

// The method, field and nested type with public and private accessibility flags get loaded as private.
comp.VerifyDiagnostics(
// (6,15): error CS0122: 'C.M()' is inaccessible due to its protection level
// new C().M();
Diagnostic(ErrorCode.ERR_BadAccess, "M").WithArguments("C.M()").WithLocation(6, 15),
// (7,40): error CS0122: 'C.F' is inaccessible due to its protection level
// System.Console.WriteLine(new C().F);
Diagnostic(ErrorCode.ERR_BadAccess, "F").WithArguments("C.F").WithLocation(7, 40),
// (8,13): error CS0122: 'C.C2' is inaccessible due to its protection level
// new C.C2().M2();
Diagnostic(ErrorCode.ERR_BadAccess, "C2").WithArguments("C.C2").WithLocation(8, 13)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Content Include="NetFX\Minimal\Key.snk" />
<Content Include="NetFX\Minimal\minasync.cs" />
<Content Include="NetFX\Minimal\mincorlib.cs" />
<EmbeddedResource Include="SymbolsTests\Metadata\public-and-private.dll" />
<Content Include="SymbolsTests\NoPia\ParametersWithoutNames.cs" />
<EmbeddedResource Include="PerfTests\CSPerfTest.cs" />
<Content Include="Resources\License.txt" />
Expand Down Expand Up @@ -418,6 +419,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ResourceLoader.cs" />
<None Include="SymbolsTests\Metadata\public-and-private.cs" />
<Compile Include="TestKeys.cs" />
<Compile Include="TestResources.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

// This relates to VSO bug 217681 https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_workitems?id=217681
// The code below is compiled with a modified compiler such that MetadataWriter will set both public and private flags on M, F, C2 and M2.
// But the constructors should be left public.
// You can also see the resulting meta info at the bottom

public class C
{
public void M() { System.Console.WriteLine("I am C!"); }
public int F = 42;

public class C2
{
public void M2() { System.Console.WriteLine("I am C2!"); }
}
}

string metainfo = @"
===========================================================
ScopeName : public-and-private.dll
MVID : {3C7CF08B-32C8-4C36-BD58-85DEB7FA5843}
===========================================================
Global functions
-------------------------------------------------------
Global fields
-------------------------------------------------------
Global MemberRefs
-------------------------------------------------------
TypeDef #1 (02000002)
-------------------------------------------------------
TypDefName: C (02000002)
Flags : [Public] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100001)
Extends : 01000005 [TypeRef] System.Object
Field #1 (04000001)
-------------------------------------------------------
Field Name: F (04000001)
Flags : [none] (00000007)
CallCnvntn: [FIELD]
Field type: I4
Method #1 (06000001)
-------------------------------------------------------
MethodName: M (06000001)
Flags : [HideBySig] [ReuseSlot] (00000087)
RVA : 0x00002050
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
Method #2 (06000002)
-------------------------------------------------------
MethodName: .ctor (06000002)
Flags : [Public] [HideBySig] [ReuseSlot] [SpecialName] [RTSpecialName] [.ctor] (00001886)
RVA : 0x0000205e
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
TypeDef #2 (02000003)
-------------------------------------------------------
TypDefName: C2 (02000003)
Flags : [NestedFamORAssem] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100007)
Extends : 01000005 [TypeRef] System.Object
EnclosingClass : C (02000002)
Method #1 (06000003)
-------------------------------------------------------
MethodName: M2 (06000003)
Flags : [HideBySig] [ReuseSlot] (00000087)
RVA : 0x0000206f
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
Method #2 (06000004)
-------------------------------------------------------
MethodName: .ctor (06000004)
Flags : [Public] [HideBySig] [ReuseSlot] [SpecialName] [RTSpecialName] [.ctor] (00001886)
RVA : 0x0000207d
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
TypeRef #1 (01000001)
-------------------------------------------------------
Token: 0x01000001
ResolutionScope: 0x23000001
TypeRefName: System.Runtime.CompilerServices.CompilationRelaxationsAttribute
MemberRef #1 (0a000001)
-------------------------------------------------------
Member: (0a000001) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: I4
TypeRef #2 (01000002)
-------------------------------------------------------
Token: 0x01000002
ResolutionScope: 0x23000001
TypeRefName: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
MemberRef #1 (0a000002)
-------------------------------------------------------
Member: (0a000002) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
TypeRef #3 (01000003)
-------------------------------------------------------
Token: 0x01000003
ResolutionScope: 0x23000001
TypeRefName: System.Diagnostics.DebuggableAttribute
MemberRef #1 (0a000003)
-------------------------------------------------------
Member: (0a000003) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: ValueClass DebuggingModes
TypeRef #4 (01000004)
-------------------------------------------------------
Token: 0x01000004
ResolutionScope: 0x01000003
TypeRefName: DebuggingModes
TypeRef #5 (01000005)
-------------------------------------------------------
Token: 0x01000005
ResolutionScope: 0x23000001
TypeRefName: System.Object
MemberRef #1 (0a000005)
-------------------------------------------------------
Member: (0a000005) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
TypeRef #6 (01000006)
-------------------------------------------------------
Token: 0x01000006
ResolutionScope: 0x23000001
TypeRefName: System.Console
MemberRef #1 (0a000004)
-------------------------------------------------------
Member: (0a000004) WriteLine:
CallCnvntn: [DEFAULT]
ReturnType: Void
1 Arguments
Argument #1: String
Assembly
-------------------------------------------------------
Token: 0x20000001
Name : public-and-private
Public Key :
Hash Algorithm : 0x00008004
Version: 0.0.0.0
Major Version: 0x00000000
Minor Version: 0x00000000
Build Number: 0x00000000
Revision Number: 0x00000000
Locale: <null>
Flags : [none] (00000000)
CustomAttribute #1 (0c000001)
-------------------------------------------------------
CustomAttribute Type: 0a000001
CustomAttributeName: System.Runtime.CompilerServices.CompilationRelaxationsAttribute :: instance void .ctor(int32)
Length: 8
Value : 01 00 08 00 00 00 00 00 > <
ctor args: (8)
CustomAttribute #2 (0c000002)
-------------------------------------------------------
CustomAttribute Type: 0a000002
CustomAttributeName: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute :: instance void .ctor()
Length: 30
Value : 01 00 01 00 54 02 16 57 72 61 70 4e 6f 6e 45 78 > T WrapNonEx<
: 63 65 70 74 69 6f 6e 54 68 72 6f 77 73 01 >ceptionThrows <
ctor args: ()
CustomAttribute #3 (0c000003)
-------------------------------------------------------
CustomAttribute Type: 0a000003
CustomAttributeName: System.Diagnostics.DebuggableAttribute :: instance void .ctor(value class DebuggingModes)
Length: 8
Value : 01 00 07 01 00 00 00 00 > <
ctor args: ( <can not decode> )
AssemblyRef #1 (23000001)
-------------------------------------------------------
Token: 0x23000001
Public Key or Token: b7 7a 5c 56 19 34 e0 89
Name: mscorlib
Version: 4.0.0.0
Major Version: 0x00000004
Minor Version: 0x00000000
Build Number: 0x00000000
Revision Number: 0x00000000
Locale: <null>
HashValue Blob:
Flags: [none] (00000000)
User Strings
-------------------------------------------------------
70000001 : ( 7) L""I am C!""
70000011 : ( 8) L""I am C2!""
Coff symbol name overhead: 0
===========================================================
===========================================================
===========================================================
";
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Compilers/Test/Resources/Core/TestResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ public static class Metadata

private static byte[] s_mscorlibNamespacesAndTypes;
public static byte[] MscorlibNamespacesAndTypes => ResourceLoader.GetOrCreateResource(ref s_mscorlibNamespacesAndTypes, "SymbolsTests.Metadata.MscorlibNamespacesAndTypes.bsl");

private static byte[] s_publicAndPrivateFlags;
public static byte[] PublicAndPrivateFlags => ResourceLoader.GetOrCreateResource(ref s_publicAndPrivateFlags, "SymbolsTests.Metadata.public-and-private.dll");
}

public static class Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
access = Accessibility.Protected

Case Else
Debug.Assert(False, "Unexpected!!!")
access = Accessibility.Private
End Select

Return access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
access = Accessibility.Protected

Case Else
Debug.Assert(False, "Unexpected!!!")
access = Accessibility.Private
End Select

Return access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Imports Microsoft.CodeAnalysis.Collections
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports Roslyn.Test.Utilities

Expand Down Expand Up @@ -925,5 +925,44 @@ BC30657: 'VT' has a return type that is not supported or parameter types that ar
~~
]]>)
End Sub

<Fact, WorkItem(217681, "https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_workitems?id=217681")>
Public Sub LoadingMethodWithPublicAndPrivateAccessibility()
Dim source =
<compilation>
<file>
Class D
Shared Sub Main()
Dim test = new C()
test.M()
System.Console.WriteLine(test.F)

Dim test2 = new C.C2()
test2.M2()
End Sub
End Class
</file>
</compilation>

Dim references = {MetadataReference.CreateFromImage(TestResources.SymbolsTests.Metadata.PublicAndPrivateFlags)}

Dim comp = CreateCompilationWithMscorlib(source, references:=references)
comp.AssertTheseDiagnostics(
<expected><![CDATA[
BC30390: 'C.Private Overloads Sub M()' is not accessible in this context because it is 'Private'.
test.M()
~~~~~~
BC30389: 'C.F' is not accessible in this context because it is 'Private'.
System.Console.WriteLine(test.F)
~~~~~~
BC30389: 'C.C2' is not accessible in this context because it is 'Protected Friend'.
Dim test2 = new C.C2()
~~~~
BC30390: 'C2.Private Overloads Sub M2()' is not accessible in this context because it is 'Private'.
test2.M2()
~~~~~~~~
]]></expected>)
End Sub

End Class
End Namespace
Loading

0 comments on commit b35416a

Please sign in to comment.