Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverting: Set AssemblyName.ProcessorArchitecture for compatibility (#80581) #84028

Merged
merged 3 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,46 @@ internal AssemblyNameFlags RawFlags
internal void SetProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm)
{
#pragma warning disable SYSLIB0037 // AssemblyName.ProcessorArchitecture is obsolete
ProcessorArchitecture = CalculateProcArch(pek, ifm, _flags);
ProcessorArchitecture = CalculateProcArchIndex(pek, ifm, _flags);
#pragma warning restore SYSLIB0037
}

private static ProcessorArchitecture CalculateProcArch(PortableExecutableKinds pek, ImageFileMachine ifm, AssemblyNameFlags aFlags)
private static ProcessorArchitecture CalculateProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm, AssemblyNameFlags flags)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep this part as is. It is simpler, it has better comments, and it is functional equivalent to the old code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we would delete this and just return ProcessorArchitecture.None to be in sync with the S.R.M implementation, It is probably not worth it to spend time on pushing the breaking change through - we can keep these ~20 lines for compat.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we would delete this and just return ProcessorArchitecture.None to be in sync with the S.R.M

Actually, since we are breaking this one way or another, maybe we should break this all the way and just return None ?
There is some time in 8.0 to see if this gets anyone in trouble.

// 0x70 specifies "reference assembly".
// For these, CLR wants to return None as arch so they can be always loaded, regardless of process type.
if (((uint)aFlags & 0xF0) == 0x70)
if (((uint)flags & 0xF0) == 0x70)
return ProcessorArchitecture.None;

switch (ifm)
if ((pek & PortableExecutableKinds.PE32Plus) == PortableExecutableKinds.PE32Plus)
{
case ImageFileMachine.IA64:
return ProcessorArchitecture.IA64;
case ImageFileMachine.ARM:
return ProcessorArchitecture.Arm;
case ImageFileMachine.AMD64:
return ProcessorArchitecture.Amd64;
case ImageFileMachine.I386:
{
if ((pek & PortableExecutableKinds.ILOnly) != 0 &&
(pek & PortableExecutableKinds.Required32Bit) == 0)
{
// platform neutral.
switch (ifm)
{
case ImageFileMachine.IA64:
return ProcessorArchitecture.IA64;
case ImageFileMachine.AMD64:
return ProcessorArchitecture.Amd64;
case ImageFileMachine.I386:
if ((pek & PortableExecutableKinds.ILOnly) == PortableExecutableKinds.ILOnly)
return ProcessorArchitecture.MSIL;
}

// requires x86
return ProcessorArchitecture.X86;
}
break;
}
}
else
{
if (ifm == ImageFileMachine.I386)
{
if ((pek & PortableExecutableKinds.Required32Bit) == PortableExecutableKinds.Required32Bit)
return ProcessorArchitecture.X86;

if ((pek & PortableExecutableKinds.ILOnly) == PortableExecutableKinds.ILOnly)
return ProcessorArchitecture.MSIL;

// ProcessorArchitecture is a legacy API and does not cover other Machine kinds.
// For example ARM64 is not expressible
return ProcessorArchitecture.X86;
}
if (ifm == ImageFileMachine.ARM)
{
return ProcessorArchitecture.Arm;
}
}
return ProcessorArchitecture.None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ public static unsafe AssemblyName GetAssemblyName(string assemblyFile)
peReader = new PEReader((byte*)safeBuffer.DangerousGetHandle(), (int)safeBuffer.ByteLength);
MetadataReader mdReader = peReader.GetMetadataReader(MetadataReaderOptions.None);
AssemblyName assemblyName = mdReader.GetAssemblyDefinition().GetAssemblyName();

AssemblyFlags aFlags = mdReader.AssemblyTable.GetFlags();
#pragma warning disable SYSLIB0037 // AssemblyName.ProcessorArchitecture is obsolete
assemblyName.ProcessorArchitecture = CalculateProcArch(peReader, aFlags);
#pragma warning restore SYSLIB0037

return assemblyName;
}
finally
Expand All @@ -107,42 +101,6 @@ public static unsafe AssemblyName GetAssemblyName(string assemblyFile)
}
}

private static ProcessorArchitecture CalculateProcArch(PEReader peReader, AssemblyFlags aFlags)
{
// 0x70 specifies "reference assembly".
// For these, CLR wants to return None as arch so they can be always loaded, regardless of process type.
if (((uint)aFlags & 0xF0) == 0x70)
return ProcessorArchitecture.None;

PEHeaders peHeaders = peReader.PEHeaders;
switch (peHeaders.CoffHeader.Machine)
{
case Machine.IA64:
return ProcessorArchitecture.IA64;
case Machine.Arm:
return ProcessorArchitecture.Arm;
case Machine.Amd64:
return ProcessorArchitecture.Amd64;
case Machine.I386:
{
CorFlags flags = peHeaders.CorHeader!.Flags;
if ((flags & CorFlags.ILOnly) != 0 &&
(flags & CorFlags.Requires32Bit) == 0)
{
// platform neutral.
return ProcessorArchitecture.MSIL;
}

// requires x86
return ProcessorArchitecture.X86;
}
}

// ProcessorArchitecture is a legacy API and does not cover other Machine kinds.
// For example ARM64 is not expressible
return ProcessorArchitecture.None;
}

private static AssemblyNameFlags GetAssemblyNameFlags(AssemblyFlags flags)
{
AssemblyNameFlags assemblyNameFlags = AssemblyNameFlags.None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3095,7 +3095,7 @@ public void GetAssemblyName()
Assert.Equal(new AssemblyName(a.FullName).ToString(), name.ToString());

#pragma warning disable SYSLIB0037 // AssemblyName.ProcessorArchitecture is obsolete
Assert.Equal(ProcessorArchitecture.MSIL, name.ProcessorArchitecture);
Assert.Equal(ProcessorArchitecture.None, name.ProcessorArchitecture);
#pragma warning restore SYSLIB0037
}
}
Expand Down