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

[mono] ILStrip sorts custom attribute table #87923

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Changes from 3 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 @@ -12,6 +12,16 @@

namespace AssemblyStripper
{
class CustomAttrRowComparer : IComparer
{
public int Compare(object left, object right)
{
CustomAttributeRow row_left = (CustomAttributeRow)left;
CustomAttributeRow row_right = (CustomAttributeRow)right;
return row_left.Parent.RID.CompareTo(row_right.Parent.RID);
Copy link
Member

@lambdageek lambdageek Jul 5, 2023

Choose a reason for hiding this comment

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

This is subtly wrong. the problem is that RID on a cecil metadata token masks out the token type.

We actually have to reconstruct the custom attribute coded-index.
something like:

   var leftParentCodedIdx = Utilities.CompressMetadataToken(CodedIndex.HasCustomAttribute, row_left.Parent);
   var rightParentCodedIdx = Utilities.CompressMetadataToken(CodedIndex.HasCustomAttribute, row_right.Parent);
   return leftParentCodedIdx.CompareTo(rightParentCodedIdx);

Copy link
Member

Choose a reason for hiding this comment

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

This fixed the issue I was running into on #88167

}
}

public class AssemblyStripper
{
AssemblyDefinition assembly;
Expand Down Expand Up @@ -40,6 +50,7 @@ void Strip()
PatchMethods();
PatchFields();
PatchResources();
SortCustomAttributes();
Write();
}

Expand Down Expand Up @@ -192,6 +203,15 @@ void PatchResources()
}
}

void SortCustomAttributes()
Copy link
Member

Choose a reason for hiding this comment

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

can you please add a comment about why we need to sort the custom attributes here?

{
CustomAttributeTable table = (CustomAttributeTable)stripped_tables[CustomAttributeTable.RId];
if (table == null)
return;

table.Rows.Sort(new CustomAttrRowComparer());
}

void Write()
{
stripped.MetadataRoot.Accept(metadata_writer);
Expand All @@ -209,7 +229,6 @@ public static void StripAssembly(string assemblyFile, string outputPath)
{
AssemblyDefinition assembly = AssemblyFactory.GetAssembly(assemblyFile);
AssemblyStripper.StripAssembly(assembly, outputPath);

}
}
}
Loading