diff options
author | Samuel Arzt <arzt.samuel@live.de> | 2017-11-17 01:11:29 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2017-11-17 01:11:29 +0300 |
commit | 7384d4ab9969f9bd2edddbd7f80df6612c42b476 (patch) | |
tree | 04876909aaa83403b144afb0f71f58a45854fdbc /src/ILVerify | |
parent | cfd5af4605645e38ce23eeb6ebf3f04abd4f83ac (diff) |
[ILVerify] Fix assembly friend access being sensitive to white-spaces (#4950)
* Changed friend assembly parsing to use AssemblyName class instead.
Diffstat (limited to 'src/ILVerify')
-rw-r--r-- | src/ILVerify/src/AccessVerificationHelpers.cs | 43 | ||||
-rw-r--r-- | src/ILVerify/tests/ILTests/AccessTestsExtern.il | 13 | ||||
-rw-r--r-- | src/ILVerify/tests/ILTests/AccessTestsFriend.il | 7 |
3 files changed, 34 insertions, 29 deletions
diff --git a/src/ILVerify/src/AccessVerificationHelpers.cs b/src/ILVerify/src/AccessVerificationHelpers.cs index 9274cad54..b0f31bd2a 100644 --- a/src/ILVerify/src/AccessVerificationHelpers.cs +++ b/src/ILVerify/src/AccessVerificationHelpers.cs @@ -2,7 +2,6 @@ // 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.Diagnostics; using System.Reflection; using Internal.TypeSystem; @@ -250,50 +249,38 @@ namespace ILVerify return false; } - private const string PUBLIC_KEY = "PublicKey="; - private static bool GrantsFriendAccessTo(this ModuleDesc module, ModuleDesc friendModule) { var assembly = (EcmaAssembly)module; - var friendAssembly = (IAssemblyDesc)friendModule; - - var friendName = friendAssembly.GetName(); - var friendPublicKey = friendName.GetPublicKey(); + var friendName = ((IAssemblyDesc)friendModule).GetName(); foreach (var attribute in assembly.GetDecodedCustomAttributes("System.Runtime.CompilerServices", "InternalsVisibleToAttribute")) { - var friendValues = ((string)attribute.FixedArguments[0].Value).Split(new string[] { ", " }, StringSplitOptions.None); - if (friendValues.Length >= 1 && friendValues.Length <= 2) - { - if (friendValues[0] != friendName.Name) - continue; - - if (friendValues.Length == 2 && - (!friendValues[1].StartsWith(PUBLIC_KEY) || !IsSamePublicKey(friendPublicKey, friendValues[1].Substring(PUBLIC_KEY.Length)))) - continue; + AssemblyName friendAttributeName = new AssemblyName((string)attribute.FixedArguments[0].Value); + if (friendName.Name != friendAttributeName.Name) + continue; + // Comparing PublicKeyToken, since GetPublicKey returns null due to a bug + if (IsSamePublicKey(friendAttributeName.GetPublicKeyToken(), friendName.GetPublicKeyToken())) return true; - } } return false; } - private static bool IsSamePublicKey(byte[] key1, string key2) + private static bool IsSamePublicKey(byte[] key1, byte[] key2) { - if (key1.Length * 2 != key2.Length) + if (key1 == null) + return key2 == null || key2.Length == 0; + if (key2 == null) + return key1 == null || key1.Length == 0; + + if (key1.Length != key2.Length) return false; - for (int i = 0; i < key1.Length; i++) + for (int i = 0; i < key1.Length; ++i) { - try - { - if (key1[i] != Convert.ToByte(key2[i * 2] + "" + key2[i * 2 + 1], 16)) - return false; - } - catch - { + if (key1[i] != key2[i]) return false; - } } return true; diff --git a/src/ILVerify/tests/ILTests/AccessTestsExtern.il b/src/ILVerify/tests/ILTests/AccessTestsExtern.il index 31be7b663..39939e1bf 100644 --- a/src/ILVerify/tests/ILTests/AccessTestsExtern.il +++ b/src/ILVerify/tests/ILTests/AccessTestsExtern.il @@ -10,7 +10,11 @@ { } -.assembly AccessTestsExternal +.assembly extern AccessTestsFriend +{ +} + +.assembly AccessTestsExtern { } @@ -102,6 +106,13 @@ call void [AccessTests]SimpleClass::FamilyAndAssemblyMethod() ret } + + .method private hidebysig instance void Load.AssemFieldOfFriendAssemblyWithSpace_Valid() cil managed + { + ldsfld int32 [AccessTestsFriend]AccessTestsFriendType::assemblyField + pop + ret + } } .class public auto ansi beforefieldinit DerivedType diff --git a/src/ILVerify/tests/ILTests/AccessTestsFriend.il b/src/ILVerify/tests/ILTests/AccessTestsFriend.il index 869470b62..78db6d8ad 100644 --- a/src/ILVerify/tests/ILTests/AccessTestsFriend.il +++ b/src/ILVerify/tests/ILTests/AccessTestsFriend.il @@ -16,6 +16,13 @@ .custom instance void [System.Runtime]System.Runtime.CompilerServices.InternalsVisibleToAttribute::.ctor(string) = ( 01 00 0b 41 63 63 65 73 73 54 65 73 74 73 00 00 ) + + // InternalsVisibleTo("AccessTestsExtern ") + // ^ purposeful space at the end + .custom instance void [System.Runtime]System.Runtime.CompilerServices.InternalsVisibleToAttribute::.ctor(string) = ( + 01 00 12 41 63 63 65 73 73 54 65 73 74 73 45 78 + 74 65 72 6e 20 00 00 + ) } .class private auto ansi beforefieldinit AccessTestsFriendType |