From bacfb8783119f423da4613bd9c14e5d52ac4c2e9 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Tue, 28 Mar 2023 08:44:39 -0700 Subject: [PATCH 1/3] Reverting #80581 --- .../System/Reflection/AssemblyName.CoreCLR.cs | 55 ++++++++++--------- .../Metadata/MetadataReader.netstandard.cs | 42 -------------- 2 files changed, 30 insertions(+), 67 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs index 48396e6dd42ef..6d02f9d8ed6f8 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs @@ -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) { - // 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; } diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataReader.netstandard.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataReader.netstandard.cs index f33c0455ee2db..a11a8fcacda4e 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataReader.netstandard.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataReader.netstandard.cs @@ -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 @@ -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; From 71dec1ae92a5ef1a61e06dc08bbb68dc3be70516 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Tue, 28 Mar 2023 08:44:53 -0700 Subject: [PATCH 2/3] Adjust the test. --- .../tests/Metadata/MetadataReaderTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Reflection.Metadata/tests/Metadata/MetadataReaderTests.cs b/src/libraries/System.Reflection.Metadata/tests/Metadata/MetadataReaderTests.cs index 3b946e5a80779..56943320ce5a4 100644 --- a/src/libraries/System.Reflection.Metadata/tests/Metadata/MetadataReaderTests.cs +++ b/src/libraries/System.Reflection.Metadata/tests/Metadata/MetadataReaderTests.cs @@ -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 } } From 5f4ae0574e7d4fa8f5822615a05f3818ad6f36f0 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Fri, 31 Mar 2023 14:26:28 -0700 Subject: [PATCH 3/3] keep new version of CalculateProcArch (do not revert) --- .../System/Reflection/AssemblyName.CoreCLR.cs | 55 +++++++++---------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs index 6d02f9d8ed6f8..48396e6dd42ef 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs @@ -106,46 +106,41 @@ internal AssemblyNameFlags RawFlags internal void SetProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm) { #pragma warning disable SYSLIB0037 // AssemblyName.ProcessorArchitecture is obsolete - ProcessorArchitecture = CalculateProcArchIndex(pek, ifm, _flags); + ProcessorArchitecture = CalculateProcArch(pek, ifm, _flags); #pragma warning restore SYSLIB0037 } - private static ProcessorArchitecture CalculateProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm, AssemblyNameFlags flags) + private static ProcessorArchitecture CalculateProcArch(PortableExecutableKinds pek, ImageFileMachine ifm, AssemblyNameFlags aFlags) { - if (((uint)flags & 0xF0) == 0x70) + // 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; - if ((pek & PortableExecutableKinds.PE32Plus) == PortableExecutableKinds.PE32Plus) + switch (ifm) { - switch (ifm) - { - case ImageFileMachine.IA64: - return ProcessorArchitecture.IA64; - case ImageFileMachine.AMD64: - return ProcessorArchitecture.Amd64; - case ImageFileMachine.I386: - if ((pek & PortableExecutableKinds.ILOnly) == PortableExecutableKinds.ILOnly) + 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. return ProcessorArchitecture.MSIL; - break; - } - } - else - { - if (ifm == ImageFileMachine.I386) - { - if ((pek & PortableExecutableKinds.Required32Bit) == PortableExecutableKinds.Required32Bit) - return ProcessorArchitecture.X86; + } - if ((pek & PortableExecutableKinds.ILOnly) == PortableExecutableKinds.ILOnly) - return ProcessorArchitecture.MSIL; - - return ProcessorArchitecture.X86; - } - if (ifm == ImageFileMachine.ARM) - { - return ProcessorArchitecture.Arm; - } + // 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; }