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

[release/7.0-staging] [mono] ILStrip sorts custom attribute table #87933

Merged
Merged
Changes from all 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);
}
}

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,20 @@ void PatchResources()
}
}

// Types that are trimmed away also have their respective rows removed from the
// custom attribute table. This introduces holes in their places, causing the table
// to no longer be sorted by Parent, corrupting the assembly. Runtimes assume ordering
// and may fail to locate the attributes set for a particular type. This step sorts
// the custom attribute table again.
void SortCustomAttributes()
{
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 +234,6 @@ public static void StripAssembly(string assemblyFile, string outputPath)
{
AssemblyDefinition assembly = AssemblyFactory.GetAssembly(assemblyFile);
AssemblyStripper.StripAssembly(assembly, outputPath);

}
}
}