Skip to content

Commit

Permalink
Merge branch 'development' of github.com:Washi1337/AsmResolver into d…
Browse files Browse the repository at this point in the history
…evelopment
  • Loading branch information
Washi1337 committed Jan 29, 2023
2 parents f8cbe85 + da3109d commit 21630a9
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RepositoryUrl>https://github.com/Washi1337/AsmResolver</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<LangVersion>10</LangVersion>
<Version>5.0.0</Version>
<Version>5.1.0</Version>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- master

image: Visual Studio 2022
version: 5.0.0-master-build.{build}
version: 5.1.0-master-build.{build}
configuration: Release

skip_commits:
Expand Down Expand Up @@ -33,7 +33,7 @@
- development

image: Visual Studio 2022
version: 5.0.0-dev-build.{build}
version: 5.1.0-dev-build.{build}
configuration: Release

skip_commits:
Expand Down
10 changes: 6 additions & 4 deletions docs/core/segments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ Since ``SegmentBuilder`` implements ``ISegment`` itself, it can also be used wit
child.Add(new DataSegment(...));
var root = new SegmentBuilder();
builder.Add(new DataSegment(...));
builder.Add(child); // Nest segment builders into each other.
root.Add(new DataSegment(...));
root.Add(child); // Nest segment builders into each other.
Resizing Segments at Runtime
Expand Down Expand Up @@ -200,9 +200,11 @@ Typically, these two measurements are going to be equal, but for some segments (
ISegment segment = ...
// Measure the size of the segment:
uint size segment.GetPhysicalSize();
uint physicalSize = segment.GetPhysicalSize();
uint virtualSize = segment.GetVirtualSize();
Console.WriteLine("Size: 0x{0:X8}", size);
Console.WriteLine("Physical (File) Size: 0x{0:X8}", physicalSize);
Console.WriteLine("Virtual (Runtime) Size: 0x{0:X8}", virtualSize);
.. warning::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ private void AddCustomAttribute(MetadataToken ownerToken, CustomAttribute attrib
{
var table = Metadata.TablesStream.GetSortedTable<CustomAttribute, CustomAttributeRow>(TableIndex.CustomAttribute);

// Ensure the signature defines the right parameters w.r.t. the constructor's parameters.
if (attribute.Constructor is not null && attribute.Signature is not null)
attribute.Signature.IsCompatibleWith(attribute.Constructor, ErrorListener);

// Add it.
var encoder = Metadata.TablesStream.GetIndexEncoder(CodedIndex.HasCustomAttribute);
var row = new CustomAttributeRow(
encoder.EncodeToken(ownerToken),
Expand Down
37 changes: 37 additions & 0 deletions src/AsmResolver.DotNet/Signatures/CustomAttributeSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,43 @@ protected override void WriteContents(in BlobSerializationContext context)
for (int i = 0; i < NamedArguments.Count; i++)
NamedArguments[i].Write(context);
}

/// <summary>
/// Validates whether the signature is compatible with the provided attribute constructor.
/// </summary>
/// <param name="constructor">The constructor to validate against.</param>
/// <returns><c>true</c> if the constructor is compatible, <c>false</c> otherwise.</returns>
public bool IsCompatibleWith(ICustomAttributeType constructor)
{
return IsCompatibleWith(constructor, EmptyErrorListener.Instance);
}

/// <summary>
/// Validates whether the signature is compatible with the provided attribute constructor.
/// </summary>
/// <param name="constructor">The constructor to validate against.</param>
/// <param name="listener">The object responsible for reporting any errors during the validation of the signature.</param>
/// <returns><c>true</c> if the constructor is compatible, <c>false</c> otherwise.</returns>
public virtual bool IsCompatibleWith(ICustomAttributeType constructor, IErrorListener listener)
{
var signature = constructor.Signature;

int expectedCount = signature?.ParameterTypes.Count ?? 0;
if (expectedCount != FixedArguments.Count)
{
listener.MetadataBuilder(
$"Custom attribute constructor {constructor.SafeToString()} expects {expectedCount} arguments but the signature provided {FixedArguments.Count} arguments.");
return false;
}

if (signature?.SentinelParameterTypes.Count > 0)
{
listener.MetadataBuilder($"Custom attribute constructor {constructor.SafeToString()} defines sentinel parameters.");
return false;
}

return true;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,11 @@ protected override void WriteContents(in BlobSerializationContext context)
else
_reader.Fork().WriteToOutput(context.Writer);
}

/// <inheritdoc />
public override bool IsCompatibleWith(ICustomAttributeType constructor, IErrorListener listener)
{
return !IsInitialized || base.IsCompatibleWith(constructor, listener);
}
}
}
20 changes: 20 additions & 0 deletions test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,5 +646,25 @@ public void NamedGenericTypeNullArgument(bool rebuild, bool access)

Assert.Null(argument.Argument.Element);
}

[Fact]
public void TestSignatureCompatibility()
{
var module = new ModuleDefinition("Dummy");
var factory = module.CorLibTypeFactory;
var ctor = factory.CorLibScope
.CreateTypeReference("System", "CLSCompliantAttribute")
.CreateMemberReference(".ctor", MethodSignature.CreateInstance(factory.Void, factory.Boolean))
.ImportWith(module.DefaultImporter);

var attribute = new CustomAttribute(ctor);

// Empty signature is not compatible with a ctor that takes a boolean.
Assert.False(attribute.Signature!.IsCompatibleWith(attribute.Constructor!));

// If we add it, it should be compatible again.
attribute.Signature.FixedArguments.Add(new CustomAttributeArgument(factory.Boolean, true));
Assert.True(attribute.Signature!.IsCompatibleWith(attribute.Constructor!));
}
}
}

0 comments on commit 21630a9

Please sign in to comment.