[corlib] Handle user Assembly subclass and Assembly::GetName.
authorRodrigo Kumpera <kumpera@gmail.com>
Tue, 7 Jun 2016 06:28:31 +0000 (23:28 -0700)
committerRodrigo Kumpera <kumpera@gmail.com>
Tue, 7 Jun 2016 21:12:39 +0000 (14:12 -0700)
Remove UnprotectedGetName and replace with overriding of GetName.

dotnet throws NIE if you subclass Assembly and call GetName on it.
We hard crashed.

mcs/class/corlib/System.Reflection.Emit/AssemblyBuilder.cs
mcs/class/corlib/System.Reflection/Assembly.cs
mcs/class/corlib/System.Reflection/MonoAssembly.cs
mcs/class/corlib/System.Security.Policy/Evidence.cs
mcs/class/corlib/System.Security.Policy/PolicyLevel.cs
mcs/class/corlib/Test/System.Reflection/AssemblyTest.cs
mcs/class/referencesource/mscorlib/system/exception.cs

index 6acc3c4ec774b4cae241f81b159399f4949920d4..6a6fcd36f5abe114d6eaa3751587690b41811a6c 100644 (file)
@@ -1038,16 +1038,6 @@ namespace System.Reflection.Emit
                        return (str == "neutral" ? String.Empty : str);
                }
 
-               internal override AssemblyName UnprotectedGetName ()
-               {
-                       AssemblyName an = base.UnprotectedGetName ();
-                       if (sn != null) {
-                               an.SetPublicKey (sn.PublicKey);
-                               an.SetPublicKeyToken (sn.PublicKeyToken);
-                       }
-                       return an;
-               }
-
                /*Warning, @typeArguments must be a mscorlib internal array. So make a copy before passing it in*/
                internal Type MakeGenericType (Type gtd, Type[] typeArguments)
                {
@@ -1124,7 +1114,15 @@ namespace System.Reflection.Emit
 
                public override AssemblyName GetName (bool copiedName)
                {
-                       return base.GetName (copiedName);
+                       AssemblyName aname = new AssemblyName ();
+                       FillName (this, aname);
+
+                       if (sn != null) {
+                               aname.SetPublicKey (sn.PublicKey);
+                               aname.SetPublicKeyToken (sn.PublicKeyToken);
+                       }
+                       return aname;
+
                }
 
                [MonoTODO ("This always returns an empty array")]
index 0bba65f4693a630934df471360618a9d26b4b2f7..f75802198eb67a4197a3349dfec49280b206c666 100644 (file)
@@ -423,18 +423,11 @@ namespace System.Reflection {
                internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               static extern void FillName (Assembly ass, AssemblyName aname);
+               static extern internal void FillName (Assembly ass, AssemblyName aname);
 
-               [MonoTODO ("copiedName == true is not supported")]
                public virtual AssemblyName GetName (Boolean copiedName)
                {
-#if !MOBILE
-                       // CodeBase, which is restricted, will be copied into the AssemblyName object so...
-                       if (SecurityManager.SecurityEnabled) {
-                               GetCodeBase (true); // this will ensure the Demand is made
-                       }
-#endif
-                       return UnprotectedGetName ();
+                       throw new NotImplementedException ();
                }
 
                public virtual AssemblyName GetName ()
@@ -442,14 +435,6 @@ namespace System.Reflection {
                        return GetName (false);
                }
 
-               // the security runtime requires access to the assemblyname (e.g. to get the strongname)
-               internal virtual AssemblyName UnprotectedGetName ()
-               {
-                       AssemblyName aname = new AssemblyName ();
-                       FillName (this, aname);
-                       return aname;
-               }
-
                public override string ToString ()
                {
                        // note: ToString work without requiring CodeBase (so no checks are needed)
index 921cf8b2dd67f51cb7c69d80259589791a65f3d9..f52c7b9dbbdb58690a1e6fe33206759d92cb7b38 100644 (file)
@@ -37,6 +37,7 @@ using System.Collections.Generic;
 using System.Runtime.Serialization;
 using System.Threading;
 using System.Diagnostics.Contracts;
+using System.Security;
 using System.Security.Policy;
 using System.Security.Permissions;
 
@@ -151,6 +152,22 @@ namespace System.Reflection {
                        return LoadWithPartialNameInternal (an.ToString (), securityEvidence, ref stackMark);
                }
 
+               // the security runtime requires access to the assemblyname (e.g. to get the strongname)
+               public override AssemblyName GetName (bool copiedName)
+               {
+
+#if !MOBILE
+                       // CodeBase, which is restricted, will be copied into the AssemblyName object so...
+                       if (SecurityManager.SecurityEnabled) {
+                               var _ = CodeBase; // this will ensure the Demand is made
+                       }
+#endif
+
+                       AssemblyName aname = new AssemblyName ();
+                       FillName (this, aname);
+                       return aname;
+               }
+
        }
 
        [ComVisible (true)]
index f8653aad20aa9dbc99d80f3d206c214bd6f889f3..307c9d788a3c33f9ca99e9c659bc89795bf4e314 100644 (file)
@@ -251,7 +251,7 @@ namespace System.Security.Policy {
                        }
 
                        // strongnamed assemblies gets a StrongName evidence
-                       AssemblyName an = a.UnprotectedGetName ();
+                       AssemblyName an = a.GetName ();
                        byte[] pk = an.GetPublicKey ();
                        if ((pk != null) && (pk.Length > 0)) {
                                StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (pk);
index d78fe55eb070725940a1eb131c842c88d8ffea72..4e1427fe1be2a49be2faae58fd83ec535b308ca3 100644 (file)
@@ -582,7 +582,7 @@ namespace System.Security.Policy {
 
                internal bool IsFullTrustAssembly (Assembly a)
                {
-                       AssemblyName an = a.UnprotectedGetName ();
+                       AssemblyName an = a.GetName ();
                        StrongNamePublicKeyBlob snpkb = new StrongNamePublicKeyBlob (an.GetPublicKey ());
                        StrongNameMembershipCondition snMC = new StrongNameMembershipCondition (snpkb, an.Name, an.Version);
                        foreach (StrongNameMembershipCondition sn in full_trust_assemblies) {
index de8f4bdbca5f88a655f18087835b9a5fcd091ca7..b43d9fd08954b011f170b3629bf0fe147a876a91 100644 (file)
@@ -1327,6 +1327,19 @@ namespace MonoTests.System.Reflection
                        Assert.AreSame (x1, x2, "#1");
                }
 #endif
+
+               class MyAssembly : Assembly { }
+
+               [Test]
+               public void CustomAssemblyImplThrows ()
+               {
+                       var ma = new MyAssembly();
+                       try {
+                               ma.GetName ();
+                               Assert.Fail ("must throw");
+                       } catch (NotImplementedException){
+                       }
+               }
        }
 
        public class TestDefinedTypes
index f3cd61e2702157660b2e4507680d7e91da8e817d..652cfa72a04cadefbbf14285de257f92e096a2fc 100644 (file)
@@ -429,7 +429,7 @@ namespace System {
 
 #if MONO
                         if (method != null) { // source can be null
-                            _source = method.DeclaringType.Assembly.UnprotectedGetName ().Name;
+                            _source = method.DeclaringType.Assembly.GetName ().Name;
                         }
 #else
                         Module module = method.Module;