Skip to content
/ cecil Public
forked from jbevain/cecil

Commit

Permalink
Merge pull request #186 from sbomer/mergeUpstream
Browse files Browse the repository at this point in the history
Merge changes from upstream
  • Loading branch information
marek-safar authored Jun 6, 2024
2 parents ff87dfd + fd53fe9 commit 5c9255f
Show file tree
Hide file tree
Showing 17 changed files with 425 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Test
run: dotnet test --no-build -c Debug Mono.Cecil.sln
linux:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v1
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net40" Version="1.0.2" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net40" Version="1.0.3" />
</ItemGroup>
<ItemGroup Condition="'$(MonoBuild)' != ''">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
Expand Down
4 changes: 2 additions & 2 deletions Mono.Cecil.Tests.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit">
<Version>3.11.0</Version>
<Version>3.14.0</Version>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter">
<Version>3.12.0</Version>
<Version>4.5.0</Version>
</PackageReference>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Mono.Cecil/AssemblyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2508,7 +2508,7 @@ public Collection<CustomAttribute> ReadCustomAttributes (ICustomAttributeProvide

if (module.IsWindowsMetadata ())
foreach (var custom_attribute in custom_attributes)
WindowsRuntimeProjections.Project (owner, custom_attribute);
WindowsRuntimeProjections.Project (owner, custom_attributes, custom_attribute);

return custom_attributes;
}
Expand Down
5 changes: 5 additions & 0 deletions Mono.Cecil/GenericParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion Mono.Cecil/GenericParameterAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum GenericParameterAttributes : ushort {
SpecialConstraintMask = 0x001c,
ReferenceTypeConstraint = 0x0004,
NotNullableValueTypeConstraint = 0x0008,
DefaultConstructorConstraint = 0x0010
DefaultConstructorConstraint = 0x0010,
AllowByRefLikeConstraint = 0x0020,
}
}
23 changes: 19 additions & 4 deletions Mono.Cecil/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 ();
Expand Down Expand Up @@ -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;

Expand Down
48 changes: 48 additions & 0 deletions Mono.Cecil/MetadataResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ public virtual MethodDefinition Resolve (MethodReference method)

if (!type.HasMethods)
return null;

// This is here to handle privatescope. Aka CompilerControlled.
// GetMethod cannot correctly resolve a privatescope method when the method name is the same as another method in the type.
// because GetMethod operates on a MethodReference which doesn't have access to the MethodAttributes.
// privatescope methods are always private. There should also never be a MethodReference to a privatescope method.
// in other words, privatescope methods should always be referenced by their MethodDefinition.
//
// privatescope methods aside, if method is ever a MethodDefinition we don't need to go searching all of the methods on the type
// we can return the method directly. This avoids the cost of a linear search.
//
// So we have this optimization opportunity here.
// And we need to handle privatescope methods somehow, because GetMethod can't.
//
// and 2 birds one stone. This if check covers the optimization and privatescope handling.
if (method is MethodDefinition definition)
return definition;

return GetMethod (type, method);
}
Expand Down Expand Up @@ -264,6 +280,9 @@ public static MethodDefinition GetMethod (Collection<MethodDefinition> methods,
if (!AreSame (method.ReturnType, reference.ReturnType))
continue;

if (method.HasThis != reference.HasThis)
continue;

if (method.IsVarArg () != reference.IsVarArg ())
continue;

Expand Down Expand Up @@ -331,6 +350,35 @@ static bool AreSame (TypeSpecification a, TypeSpecification b)
if (a.IsArray)
return AreSame ((ArrayType) a, (ArrayType) b);

if (a.IsFunctionPointer)
return AreSame ((FunctionPointerType) a, (FunctionPointerType) b);

return true;
}

static bool AreSame (FunctionPointerType a, FunctionPointerType b)
{
if (a.HasThis != b.HasThis)
return false;

if (a.CallingConvention != b.CallingConvention)
return false;

if (!AreSame (a.ReturnType, b.ReturnType))
return false;

if (a.ContainsGenericParameter != b.ContainsGenericParameter)
return false;

if (a.HasParameters != b.HasParameters)
return false;

if (!a.HasParameters)
return true;

if (!AreSame (a.Parameters, b.Parameters))
return false;

return true;
}

Expand Down
10 changes: 5 additions & 5 deletions Mono.Cecil/WindowsRuntimeProjections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static void Project (TypeDefinition type)
treatment = TypeDefinitionTreatment.PrefixWindowsRuntimeName;

if (treatment == TypeDefinitionTreatment.PrefixWindowsRuntimeName || treatment == TypeDefinitionTreatment.NormalType)
if (!type.IsInterface && HasAttribute (type, "Windows.UI.Xaml", "TreatAsAbstractComposableClassAttribute"))
if (!type.IsInterface && HasAttribute (type.CustomAttributes, "Windows.UI.Xaml", "TreatAsAbstractComposableClassAttribute"))
treatment |= TypeDefinitionTreatment.Abstract;
}
else if (metadata_kind == MetadataKind.ManagedWindowsMetadata && IsClrImplementationType (type))
Expand Down Expand Up @@ -860,7 +860,7 @@ AssemblyNameReference GetAssemblyReference (string name)
throw new Exception ();
}

public static void Project (ICustomAttributeProvider owner, CustomAttribute attribute)
public static void Project (ICustomAttributeProvider owner, Collection<CustomAttribute> owner_attributes, CustomAttribute attribute)
{
if (!IsWindowsAttributeUsageAttribute (owner, attribute))
return;
Expand All @@ -876,7 +876,7 @@ public static void Project (ICustomAttributeProvider owner, CustomAttribute attr
}

if (treatment == CustomAttributeValueTreatment.None) {
var multiple = HasAttribute (type, "Windows.Foundation.Metadata", "AllowMultipleAttribute");
var multiple = HasAttribute (owner_attributes, "Windows.Foundation.Metadata", "AllowMultipleAttribute");
treatment = multiple ? CustomAttributeValueTreatment.AllowMultiple : CustomAttributeValueTreatment.AllowSingle;
}

Expand Down Expand Up @@ -905,9 +905,9 @@ static bool IsWindowsAttributeUsageAttribute (ICustomAttributeProvider owner, Cu
return declaring_type.Name == "AttributeUsageAttribute" && declaring_type.Namespace == /*"Windows.Foundation.Metadata"*/"System";
}

static bool HasAttribute (TypeDefinition type, string @namespace, string name)
static bool HasAttribute (Collection<CustomAttribute> attributes, string @namespace, string name)
{
foreach (var attribute in type.CustomAttributes) {
foreach (var attribute in attributes) {
var attribute_type = attribute.AttributeType;
if (attribute_type.Name == name && attribute_type.Namespace == @namespace)
return true;
Expand Down
8 changes: 5 additions & 3 deletions Mono.Security.Cryptography/CryptoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand All @@ -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++) {
Expand Down
6 changes: 3 additions & 3 deletions ProjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

[assembly: ComVisible (false)]

[assembly: AssemblyVersion ("0.11.4.0")]
[assembly: AssemblyFileVersion ("0.11.4.0")]
[assembly: AssemblyInformationalVersion ("0.11.4.0")]
[assembly: AssemblyVersion ("0.11.5.0")]
[assembly: AssemblyFileVersion ("0.11.5.0")]
[assembly: AssemblyInformationalVersion ("0.11.5.0")]
2 changes: 1 addition & 1 deletion Test/Mono.Cecil.Tests/ImageReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void MetroAssembly ()
[Test]
public void WindowsRuntimeComponentAssembly ()
{
var resolver = WindowsRuntimeAssemblyResolver.CreateInstance ();
var resolver = WindowsRuntimeAssemblyResolver.CreateInstance (applyWindowsRuntimeProjections: false);
if (resolver == null)
return;

Expand Down
Loading

0 comments on commit 5c9255f

Please sign in to comment.