Skip to content

Commit

Permalink
Merge pull request mono#3118 from kumpera/fix_getname
Browse files Browse the repository at this point in the history
[corlib] Handle user Assembly subclass and Assembly::GetName.
  • Loading branch information
kumpera committed Jun 8, 2016
2 parents 968540b + 36116e1 commit 28b0808
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 31 deletions.
20 changes: 9 additions & 11 deletions mcs/class/corlib/System.Reflection.Emit/AssemblyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,16 +1038,6 @@ private string GetCultureString (string str)
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)
{
Expand Down Expand Up @@ -1124,7 +1114,15 @@ public override Module[] GetModules (bool getResourceModules)

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")]
Expand Down
19 changes: 2 additions & 17 deletions mcs/class/corlib/System.Reflection/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,33 +423,18 @@ public virtual Type GetType (String name) {
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 ()
{
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)
Expand Down
17 changes: 17 additions & 0 deletions mcs/class/corlib/System.Reflection/MonoAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using System.Runtime.Serialization;
using System.Threading;
using System.Diagnostics.Contracts;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;

Expand Down Expand Up @@ -151,6 +152,22 @@ internal static RuntimeAssembly LoadWithPartialNameInternal (AssemblyName an, Ev
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)]
Expand Down
2 changes: 1 addition & 1 deletion mcs/class/corlib/System.Security.Policy/Evidence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ static internal Evidence GetDefaultHostEvidence (Assembly a)
}

// 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);
Expand Down
2 changes: 1 addition & 1 deletion mcs/class/corlib/System.Security.Policy/PolicyLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ internal string ResolveClassName (string className)

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) {
Expand Down
13 changes: 13 additions & 0 deletions mcs/class/corlib/Test/System.Reflection/AssemblyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,19 @@ public void DefinedTypes_Equality ()
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
Expand Down
2 changes: 1 addition & 1 deletion mcs/class/referencesource/mscorlib/system/exception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public virtual String Source {

#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;
Expand Down

0 comments on commit 28b0808

Please sign in to comment.