Skip to content

Commit

Permalink
Moved AssemblyName helpers to managed (#62866)
Browse files Browse the repository at this point in the history
* Moved ComputePublicKeyToken to managed

* Managed assembly name parsing (adapted from nativeaot)

* Fix for HostActivation failures.

* PR feedback (RuntimeAssemblyName is back to CoreRT + other comments)

* remove AssemblyNameNative::Init form the .hpp

* remove AppX compat ifdef

* renamed instance fields to convention used in C#

* `Argument_InvalidAssemblyName`   should be   `InvalidAssemblyName`. Majority of use is `FileLoadException`.

* remove `this.`

* PR feedback (assign to fileds, bypass properties)

* missed this change in the rebase

* "low-hanging fruit" perf tweaks.

* move one-user helpers to where they are used.

* removed ActiveIssue for #45032

* remove AssemblyNameHelpers.cs form corelib

* Remove the List when detecting duplicates. Support PublicKey.

* whitespace

* Fix managed implementation to match the new tests.

* Some minor cleanup.

* Do not validate culture too early

* PR feedback

* use SR.InvalidAssemblyName

* Report the input string when throwing FileLoadException

* tweaked couple comments
  • Loading branch information
VSadov authored Jan 25, 2022
1 parent fb284a8 commit 33940e6
Show file tree
Hide file tree
Showing 29 changed files with 628 additions and 726 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ namespace System.Reflection
{
public sealed partial class AssemblyName : ICloneable, IDeserializationCallback, ISerializable
{
public AssemblyName(string assemblyName)
{
if (assemblyName == null)
throw new ArgumentNullException(nameof(assemblyName));
if ((assemblyName.Length == 0) ||
(assemblyName[0] == '\0'))
throw new ArgumentException(SR.Format_StringZeroLength);

_name = assemblyName;
nInit();
}

internal AssemblyName(string? name,
byte[]? publicKey,
byte[]? publicKeyToken,
Expand All @@ -44,9 +32,6 @@ internal AssemblyName(string? name,
_flags = flags;
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal extern void nInit();

// This call opens and closes the file, but does not add the
// assembly to the domain.
[MethodImpl(MethodImplOptions.InternalCall)]
Expand All @@ -58,9 +43,6 @@ internal static AssemblyName GetFileInformationCore(string assemblyFile)
return nGetFileInformation(fullPath);
}

[MethodImpl(MethodImplOptions.InternalCall)]
private extern byte[]? ComputePublicKeyToken();

internal void SetProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm)
{
#pragma warning disable SYSLIB0037 // AssemblyName.ProcessorArchitecture is obsolete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1091,5 +1091,10 @@ public static bool IsPrimitive(RuntimeTypeHandle typeHandle)
{
return typeHandle.ToEETypePtr().IsPrimitive && !typeHandle.ToEETypePtr().IsEnum;
}

public static byte[] ComputePublicKeyToken(byte[] publicKey)
{
return System.Reflection.AssemblyNameHelpers.ComputePublicKeyToken(publicKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,10 @@
<Compile Include="Internal\Reflection\Extensions\NonPortable\CustomAttributeSearcher.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="System\Reflection\AssemblyNameHelpers.StrongName.cs" />
<Compile Include="System\Reflection\AssemblyNameHelpers.cs" />
<Compile Include="System\Reflection\AssemblyNameLexer.cs" />
<Compile Include="System\Reflection\AssemblyNameParser.cs" />
<Compile Include="System\Reflection\RuntimeAssembly.cs" />
<Compile Include="System\Reflection\RuntimeAssemblyName.cs" />
<Compile Include="System\Reflection\AssemblyNameHelpers.cs" />
<Compile Include="System\Reflection\AssemblyRuntimeNameHelpers.cs" />
<Compile Include="System\Reflection\Attribute.CoreRT.cs" />
<Compile Include="System\Reflection\Assembly.CoreRT.cs" />
<Compile Include="System\Reflection\AssemblyName.CoreRT.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,6 @@ namespace System.Reflection
{
public sealed partial class AssemblyName : ICloneable, IDeserializationCallback, ISerializable
{
public AssemblyName(string assemblyName)
: this()
{
if (assemblyName == null)
throw new ArgumentNullException(nameof(assemblyName));
if ((assemblyName.Length == 0) ||
(assemblyName[0] == '\0'))
throw new ArgumentException(SR.Format_StringZeroLength);

_name = assemblyName;
RuntimeAssemblyName runtimeAssemblyName = AssemblyNameParser.Parse(_name);
runtimeAssemblyName.CopyToAssemblyName(this);
}

private byte[] ComputePublicKeyToken()
{
return AssemblyNameHelpers.ComputePublicKeyToken(_publicKey);
}

private static AssemblyName GetFileInformationCore(string assemblyFile)
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_AssemblyName_GetAssemblyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,8 @@

namespace System.Reflection
{
[System.Runtime.CompilerServices.ReflectionBlocked]
public static partial class AssemblyNameHelpers
internal static partial class AssemblyNameHelpers
{
//
// Converts an AssemblyName to a RuntimeAssemblyName that is free from any future mutations on the AssemblyName.
//
public static RuntimeAssemblyName ToRuntimeAssemblyName(this AssemblyName assemblyName)
{
if (assemblyName.Name == null)
throw new ArgumentException();

AssemblyNameFlags flags = assemblyName.Flags;
AssemblyContentType contentType = assemblyName.ContentType;
#pragma warning disable SYSLIB0037 // AssemblyName.ProcessorArchitecture is obsolete
ProcessorArchitecture processorArchitecture = assemblyName.ProcessorArchitecture;
#pragma warning restore SYSLIB0037
AssemblyNameFlags combinedFlags = CombineAssemblyNameFlags(flags, contentType, processorArchitecture);
byte[]? pkOriginal;
if (0 != (flags & AssemblyNameFlags.PublicKey))
pkOriginal = assemblyName.GetPublicKey();
else
pkOriginal = assemblyName.GetPublicKeyToken();

// AssemblyName's PKT property getters do NOT copy the array before giving it out. Make our own copy
// as the original is wide open to tampering by anyone.
byte[]? pkCopy = null;
if (pkOriginal != null)
{
pkCopy = new byte[pkOriginal.Length];
((ICollection<byte>)pkOriginal).CopyTo(pkCopy, 0);
}

return new RuntimeAssemblyName(assemblyName.Name, assemblyName.Version, assemblyName.CultureName, combinedFlags, pkCopy);
}

//
// These helpers convert between the combined flags+contentType+processorArchitecture value and the separated parts.
//
Expand All @@ -55,19 +22,9 @@ internal static AssemblyContentType ExtractAssemblyContentType(this AssemblyName
return (AssemblyContentType)((((int)flags) >> 9) & 0x7);
}

internal static ProcessorArchitecture ExtractProcessorArchitecture(this AssemblyNameFlags flags)
{
return (ProcessorArchitecture)((((int)flags) >> 4) & 0x7);
}

public static AssemblyNameFlags ExtractAssemblyNameFlags(this AssemblyNameFlags combinedFlags)
internal static AssemblyNameFlags ExtractAssemblyNameFlags(this AssemblyNameFlags combinedFlags)
{
return combinedFlags & unchecked((AssemblyNameFlags)0xFFFFF10F);
}

internal static AssemblyNameFlags CombineAssemblyNameFlags(AssemblyNameFlags flags, AssemblyContentType contentType, ProcessorArchitecture processorArchitecture)
{
return (AssemblyNameFlags)(((int)flags) | (((int)contentType) << 9) | ((int)processorArchitecture << 4));
}
}
}

This file was deleted.

Loading

0 comments on commit 33940e6

Please sign in to comment.