From a840bdbd4cdf053fa4ef8326009d93bf0d6be7d8 Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Mon, 20 May 2024 13:06:25 -0700 Subject: [PATCH 1/5] Update to .net 8.0 (#942) --- Directory.Build.props | 2 +- Mono.Cecil.Tests.props | 6 +++--- Test/Mono.Cecil.Tests.csproj | 4 ++-- rocks/Test/Mono.Cecil.Rocks.Tests.csproj | 2 +- symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj | 2 +- symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 85dfd1d04..5b9f0da9d 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -7,7 +7,7 @@ true true $(MSBuildThisFileDirectory)\cecil.snk - $(DefineConstants);NET_CORE + $(DefineConstants);NET_CORE diff --git a/Mono.Cecil.Tests.props b/Mono.Cecil.Tests.props index bd6df1f5e..598bd2554 100644 --- a/Mono.Cecil.Tests.props +++ b/Mono.Cecil.Tests.props @@ -4,13 +4,13 @@ - 3.11.0 + 3.14.0 - 15.9.0 + 17.9.0 - 3.12.0 + 4.5.0 diff --git a/Test/Mono.Cecil.Tests.csproj b/Test/Mono.Cecil.Tests.csproj index f7762e3f0..ed98595dc 100644 --- a/Test/Mono.Cecil.Tests.csproj +++ b/Test/Mono.Cecil.Tests.csproj @@ -1,9 +1,9 @@  - netcoreapp3.1;net40 + net8.0;net40 - + 2.10.0 diff --git a/rocks/Test/Mono.Cecil.Rocks.Tests.csproj b/rocks/Test/Mono.Cecil.Rocks.Tests.csproj index a15fce183..aec50e167 100644 --- a/rocks/Test/Mono.Cecil.Rocks.Tests.csproj +++ b/rocks/Test/Mono.Cecil.Rocks.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net40 + net8.0;net40 diff --git a/symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj b/symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj index 92da7f5ca..534096a5d 100644 --- a/symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj +++ b/symbols/mdb/Test/Mono.Cecil.Mdb.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net40 + net8.0;net40 diff --git a/symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj b/symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj index 8baa446e8..1d9a45766 100644 --- a/symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj +++ b/symbols/pdb/Test/Mono.Cecil.Pdb.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net40 + net8.0;net40 From fec4ee9f51e7f252a489d8a856ec879c629e8c63 Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Mon, 20 May 2024 19:44:44 -0700 Subject: [PATCH 2/5] Import reflection modifiers (#943) --- Mono.Cecil/Import.cs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Mono.Cecil/Import.cs b/Mono.Cecil/Import.cs index 272e96076..e5670b601 100644 --- a/Mono.Cecil/Import.cs +++ b/Mono.Cecil/Import.cs @@ -159,6 +159,19 @@ enum ImportGenericKind { { typeof (object), ElementType.Object }, }; + TypeReference ImportType (Type type, ImportGenericContext context, Type [] required_modifiers, Type [] optional_modifiers) + { + var import = ImportType (type, context); + + foreach (var modifier in required_modifiers) + import = new RequiredModifierType (ImportType (modifier, context), import); + + foreach (var modifier in optional_modifiers) + import = new OptionalModifierType (ImportType (modifier, context), import); + + return import; + } + TypeReference ImportType (Type type, ImportGenericContext context) { return ImportType (type, context, ImportGenericKind.Open); @@ -349,7 +362,7 @@ FieldReference ImportField (SR.FieldInfo field, ImportGenericContext context) return new FieldReference { Name = field.Name, DeclaringType = declaring_type, - FieldType = ImportType (field.FieldType, context), + FieldType = ImportType (field.FieldType, context, field.GetRequiredCustomModifiers (), field.GetOptionalCustomModifiers ()), }; } finally { context.Pop (); @@ -393,15 +406,17 @@ MethodReference ImportMethod (SR.MethodBase method, ImportGenericContext context try { var method_info = method as SR.MethodInfo; reference.ReturnType = method_info != null - ? ImportType (method_info.ReturnType, context) + ? ImportType (method_info.ReturnType, context, method_info.ReturnParameter.GetRequiredCustomModifiers (), method_info.ReturnParameter.GetOptionalCustomModifiers ()) : ImportType (typeof (void), default (ImportGenericContext)); var parameters = method.GetParameters (); var reference_parameters = reference.Parameters; - for (int i = 0; i < parameters.Length; i++) + for (int i = 0; i < parameters.Length; i++) { + var parameter = parameters [i]; reference_parameters.Add ( - new ParameterDefinition (ImportType (parameters [i].ParameterType, context))); + new ParameterDefinition (ImportType (parameter.ParameterType, context, parameter.GetRequiredCustomModifiers (), parameter.GetOptionalCustomModifiers ()))); + } reference.DeclaringType = declaring_type; From dff01d96ccc9b7f62c22152cc2592a26f87c4a6d Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Mon, 20 May 2024 19:45:39 -0700 Subject: [PATCH 3/5] Use FIPS compliant SHA1 (#944) --- Mono.Security.Cryptography/CryptoService.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Mono.Security.Cryptography/CryptoService.cs b/Mono.Security.Cryptography/CryptoService.cs index 3877debfa..1da81b1e1 100644 --- a/Mono.Security.Cryptography/CryptoService.cs +++ b/Mono.Security.Cryptography/CryptoService.cs @@ -26,6 +26,8 @@ namespace Mono.Cecil { static class CryptoService { + static SHA1 CreateSHA1 () => new SHA1CryptoServiceProvider (); + public static byte [] GetPublicKey (WriterParameters parameters) { using (var rsa = parameters.CreateRSA ()) { @@ -93,7 +95,7 @@ static byte [] HashStream (Stream stream, ImageWriter writer, out int strong_nam + (strong_name_directory.VirtualAddress - text.VirtualAddress)); var strong_name_length = (int) strong_name_directory.Size; - var sha1 = new SHA1Managed (); + var sha1 = CreateSHA1 (); var buffer = new byte [buffer_size]; using (var crypto_stream = new CryptoStream (Stream.Null, sha1, CryptoStreamMode.Write)) { stream.Seek (0, SeekOrigin.Begin); @@ -131,7 +133,7 @@ public static byte [] ComputeHash (Stream stream) { const int buffer_size = 8192; - var sha1 = new SHA1Managed (); + var sha1 = CreateSHA1 (); var buffer = new byte [buffer_size]; using (var crypto_stream = new CryptoStream (Stream.Null, sha1, CryptoStreamMode.Write)) @@ -142,7 +144,7 @@ public static byte [] ComputeHash (Stream stream) public static byte [] ComputeHash (params ByteBuffer [] buffers) { - var sha1 = new SHA1Managed (); + var sha1 = CreateSHA1 (); using (var crypto_stream = new CryptoStream (Stream.Null, sha1, CryptoStreamMode.Write)) { for (int i = 0; i < buffers.Length; i++) { From 1da214577984a511fb7de3ada0f39ae4d755db99 Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Tue, 21 May 2024 08:19:01 -0700 Subject: [PATCH 4/5] Add new flag for ByRefLike constraints (#945) * Add new flag for ByRefLike constraints * Allow seems to be the prefered nomenclature --- Mono.Cecil/GenericParameter.cs | 5 +++++ Mono.Cecil/GenericParameterAttributes.cs | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Mono.Cecil/GenericParameter.cs b/Mono.Cecil/GenericParameter.cs index 30dd73382..de746449b 100644 --- a/Mono.Cecil/GenericParameter.cs +++ b/Mono.Cecil/GenericParameter.cs @@ -165,6 +165,11 @@ public bool HasDefaultConstructorConstraint { set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint, value); } } + public bool AllowByRefLikeConstraint { + get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.AllowByRefLikeConstraint); } + set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.AllowByRefLikeConstraint, value); } + } + #endregion public GenericParameter (IGenericParameterProvider owner) diff --git a/Mono.Cecil/GenericParameterAttributes.cs b/Mono.Cecil/GenericParameterAttributes.cs index 149582cdb..51f094f47 100644 --- a/Mono.Cecil/GenericParameterAttributes.cs +++ b/Mono.Cecil/GenericParameterAttributes.cs @@ -22,6 +22,7 @@ public enum GenericParameterAttributes : ushort { SpecialConstraintMask = 0x001c, ReferenceTypeConstraint = 0x0004, NotNullableValueTypeConstraint = 0x0008, - DefaultConstructorConstraint = 0x0010 + DefaultConstructorConstraint = 0x0010, + AllowByRefLikeConstraint = 0x0020, } } From 8e1ae7b4ea67ccc38cb8db3ded6802643109ffd7 Mon Sep 17 00:00:00 2001 From: Mike Voorhees Date: Tue, 18 Jun 2024 19:35:19 -0400 Subject: [PATCH 5/5] Resolve `.dll` before `.exe` (#949) In the .NET world, executable assemblies have a Foo.exe and Foo.dll file. Foo.exe is not a managed assembly and attempt to load it will cause ``` System.BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid. at Mono.Cecil.PE.ImageReader.ReadOptionalHeaders(UInt16& subsystem, UInt16& dll_characteristics) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil.PE\ImageReader.cs:line 198 at Mono.Cecil.PE.ImageReader.ReadImage() in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil.PE\ImageReader.cs:line 86 at Mono.Cecil.PE.ImageReader.ReadImage(Disposable`1 stream, String file_name) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil.PE\ImageReader.cs:line 766 at Mono.Cecil.ModuleDefinition.ReadModule(Disposable`1 stream, String fileName, ReaderParameters parameters) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil\ModuleDefinition.cs:line 1141 at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil\ModuleDefinition.cs:line 1110 at Mono.Cecil.AssemblyDefinition.ReadAssembly(String fileName, ReaderParameters parameters) in C:\UnitySrc\dev\il2cpp-18\external\cecil\Mono.Cecil\AssemblyDefinition.cs:line 132 ``` By attempting to locate `.dll` files before `.exe` this problem can be avoided. For context, I hit this in one of our resolvers while attempting to resolve all of the dependencies of an executable that referenced another executable. I fixed our resolver the same way but noticed `BaseAssemblyResolver` had the same behavior we had so I thought I'd upstream the change. --- Mono.Cecil/BaseAssemblyResolver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mono.Cecil/BaseAssemblyResolver.cs b/Mono.Cecil/BaseAssemblyResolver.cs index fd9e5c968..146939036 100644 --- a/Mono.Cecil/BaseAssemblyResolver.cs +++ b/Mono.Cecil/BaseAssemblyResolver.cs @@ -209,7 +209,7 @@ static Dictionary CreateTrustedPlatformAssemblyMap () protected virtual AssemblyDefinition SearchDirectory (AssemblyNameReference name, IEnumerable directories, ReaderParameters parameters) { - var extensions = name.IsWindowsRuntime ? new [] { ".winmd", ".dll" } : new [] { ".exe", ".dll" }; + var extensions = name.IsWindowsRuntime ? new [] { ".winmd", ".dll" } : new [] { ".dll", ".exe" }; foreach (var directory in directories) { foreach (var extension in extensions) { string file = Path.Combine (directory, name.Name + extension);