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] Add the capability of trimming IL code of individual methods #86722

Merged
merged 27 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0c4bf19
Add the capability of trimming individual methods
fanyang-mono May 24, 2023
d817f30
Fix build errors
fanyang-mono May 24, 2023
634ef1e
Remove printf's
fanyang-mono May 24, 2023
b766c33
Add the option to use compiled-methods-outfile
fanyang-mono May 25, 2023
10b3b01
Avoid trimming shared methods when they are still in use
fanyang-mono May 26, 2023
2bc884e
Add parameter description
fanyang-mono May 26, 2023
b23d833
Add the option to trim compiled methods
fanyang-mono May 26, 2023
f6d99d7
Address review feedback
fanyang-mono May 31, 2023
69bf960
Add metadata MethodTokenFile to CompiledAssemblies
fanyang-mono May 31, 2023
1f0fcbb
Add GUID checks and use metadata of assemblies
fanyang-mono Jun 2, 2023
eaa939c
Create smaller functions and use hex value
fanyang-mono Jun 2, 2023
f517e79
Update src/tasks/AotCompilerTask/MonoAOTCompiler.cs
fanyang-mono Jun 5, 2023
ae66d52
Move parameter validation code
fanyang-mono Jun 5, 2023
30306e4
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
56a781a
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
78981c8
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
b519b4c
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
3322022
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
1a83902
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
f1b2753
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
328e9be
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
10226b7
Add more error handling
fanyang-mono Jun 5, 2023
fc088bd
Provide a list of trimmed assemblies as output
fanyang-mono Jun 12, 2023
3aad84a
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 13, 2023
91803d9
Address coding style feedbacks
fanyang-mono Jun 13, 2023
8cb9621
Fix var anmes
fanyang-mono Jun 13, 2023
ee2b37f
Delete trimmed assemblies after copy
fanyang-mono Jun 15, 2023
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
2 changes: 2 additions & 0 deletions src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -14830,6 +14830,8 @@ aot_assembly (MonoAssembly *ass, guint32 jit_opts, MonoAotOptions *aot_options)

if (acfg->aot_opts.compiled_methods_outfile && acfg->dedup_phase != DEDUP_COLLECT) {
acfg->compiled_methods_outfile = fopen (acfg->aot_opts.compiled_methods_outfile, "w+");
fprintf(acfg->compiled_methods_outfile, "%s\n", ass->aname.name);
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved

fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
if (!acfg->compiled_methods_outfile)
aot_printerrf (acfg, "Unable to open compiled-methods-outfile specified file %s\n", acfg->aot_opts.compiled_methods_outfile);
}
Expand Down
167 changes: 148 additions & 19 deletions src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,86 @@
using CilStrip.Mono.Cecil.Binary;
using CilStrip.Mono.Cecil.Cil;
using CilStrip.Mono.Cecil.Metadata;
using System.Reflection.Metadata;
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;

public class ILStrip : Microsoft.Build.Utilities.Task
{
/// <summary>
/// Assemblies to be stripped.
/// The assemblies will be modified in place if OutputPath metadata is not set.
/// </summary>
[Required]
public ITaskItem[] Assemblies { get; set; } = Array.Empty<ITaskItem>();

/// <summary>
/// Disable parallel stripping
/// </summary>
public bool DisableParallelStripping { get; set; }

public bool TrimIndividualMethods { get; set; }

public ITaskItem[] MethodTokenFiles { get; set; } = Array.Empty<ITaskItem>();

public string AssemblyPath { get; set; } = "";
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved

public override bool Execute()
radical marked this conversation as resolved.
Show resolved Hide resolved
{
if (Assemblies.Length == 0)
if (!TrimIndividualMethods)
{
throw new ArgumentException($"'{nameof(Assemblies)}' is required.", nameof(Assemblies));
}
if (Assemblies.Length == 0)
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentException($"'{nameof(Assemblies)}' is required.", nameof(Assemblies));
}

int allowedParallelism = DisableParallelStripping ? 1 : Math.Min(Assemblies.Length, Environment.ProcessorCount);
if (BuildEngine is IBuildEngine9 be9)
allowedParallelism = be9.RequestCores(allowedParallelism);
ParallelLoopResult result = Parallel.ForEach(Assemblies,
new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism },
(assemblyItem, state) =>
{
if (!StripAssembly(assemblyItem))
state.Stop();
});

if (!result.IsCompleted && !Log.HasLoggedErrors)
{
Log.LogError("Unknown failure occurred while IL stripping assemblies. Check logs to get more details.");
int allowedParallelism = DisableParallelStripping ? 1 : Math.Min(Assemblies.Length, Environment.ProcessorCount);
if (BuildEngine is IBuildEngine9 be9)
allowedParallelism = be9.RequestCores(allowedParallelism);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
int allowedParallelism = DisableParallelStripping ? 1 : Math.Min(Assemblies.Length, Environment.ProcessorCount);
if (BuildEngine is IBuildEngine9 be9)
allowedParallelism = be9.RequestCores(allowedParallelism);
int allowedParallelism = DisableParallelStripping ? 1 : BuildEngine9.RequestCores(Math.Min(Assemblies.Length, Environment.ProcessorCount));

Copy link
Member Author

Choose a reason for hiding this comment

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

I hit an error when applying your change

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

ParallelLoopResult result = Parallel.ForEach(Assemblies,
new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism },
(assemblyItem, state) =>
{
if (!StripAssembly(assemblyItem))
state.Stop();
});

if (!result.IsCompleted && !Log.HasLoggedErrors)
{
Log.LogError("Unknown failure occurred while IL stripping assemblies. Check logs to get more details.");
}

return !Log.HasLoggedErrors;
}
else
{
if (MethodTokenFiles.Length == 0)
{
throw new ArgumentException($"'{nameof(MethodTokenFiles)}' is required.", nameof(MethodTokenFiles));
}

if (!Directory.Exists(AssemblyPath))
{
throw new ArgumentException($"'{nameof(AssemblyPath)}' needs to be a valid path.", nameof(AssemblyPath));
}

return !Log.HasLoggedErrors;
int allowedParallelism = DisableParallelStripping ? 1 : Math.Min(MethodTokenFiles.Length, Environment.ProcessorCount);
if (BuildEngine is IBuildEngine9 be9)
allowedParallelism = be9.RequestCores(allowedParallelism);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
int allowedParallelism = DisableParallelStripping ? 1 : Math.Min(MethodTokenFiles.Length, Environment.ProcessorCount);
if (BuildEngine is IBuildEngine9 be9)
allowedParallelism = be9.RequestCores(allowedParallelism);
int allowedParallelism = DisableParallelStripping ? 1 : BuildEngine9.RequestCores(Math.Min(Assemblies.Length, Environment.ProcessorCount));

ParallelLoopResult result = Parallel.ForEach(MethodTokenFiles,
new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism },
(methodTokenFileItem, state) =>
{
if (!TrimMethods(methodTokenFileItem, AssemblyPath))
state.Stop();
});

if (!result.IsCompleted && !Log.HasLoggedErrors)
{
Log.LogError("Unknown failure occurred while IL stripping assemblies. Check logs to get more details.");
}

return !Log.HasLoggedErrors;
}
}

private bool StripAssembly(ITaskItem assemblyItem)
Expand Down Expand Up @@ -81,4 +122,92 @@ private bool StripAssembly(ITaskItem assemblyItem)
return true;
}

private bool TrimMethods(ITaskItem methodTokenFileItem, string assemblyPath)
{
string methodTokenFile = methodTokenFileItem.ItemSpec;
if (!File.Exists(methodTokenFile))
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
{
Log.LogMessage(MessageImportance.Low, $"[ILStrip] {methodTokenFile} doesn't exit.");
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
string[] log = File.ReadAllLines(methodTokenFile);
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
if (log.Length <= 1)
{
// Frist line is assembly name
Log.LogMessage(MessageImportance.Low, $"[ILStrip] {methodTokenFile} doesn't contain any compiled method token.");
return true;
}
string assemblyName = log[0];
int idxCompiledMethods = 1;
string assemblyFilePath = Path.Combine(assemblyPath, (assemblyName + ".dll"));
string trimmedAssemblyFilePath = Path.Combine(assemblyPath, (assemblyName + "_new.dll"));
if (!File.Exists(assemblyFilePath))
{
Log.LogMessage(MessageImportance.Low, $"[ILStrip] {assemblyFilePath} doesn't exit.");
return true;
}

using (FileStream fs = File.Open(assemblyFilePath, FileMode.Open),
os = File.Open(trimmedAssemblyFilePath, FileMode.Create))
{
MemoryStream memStream = new MemoryStream((int)fs.Length);
fs.CopyTo(memStream);

fs.Position = 0;
PEReader peReader = new PEReader(fs, PEStreamOptions.LeaveOpen);
MetadataReader mr = peReader.GetMetadataReader();

foreach (MethodDefinitionHandle mdefh in mr.MethodDefinitions)
{
if (idxCompiledMethods < log.Length)
{
int methodToken = MetadataTokens.GetToken(mr, mdefh);
int expMethodToken = Convert.ToInt32(log[idxCompiledMethods], 16);

if (methodToken == expMethodToken)
{
MethodDefinition mdef = mr.GetMethodDefinition(mdefh);
int rva = mdef.RelativeVirtualAddress;

Console.WriteLine(mr.GetString(mdef.Name));
Console.WriteLine(methodToken);

MethodBodyBlock mb = peReader.GetMethodBody(rva);
int methodSize = mb.Size;
int sectionIndex = peReader.PEHeaders.GetContainingSectionIndex(rva);
int relativeOffset = rva - peReader.PEHeaders.SectionHeaders[sectionIndex].VirtualAddress;
int actualLoc = peReader.PEHeaders.SectionHeaders[sectionIndex].PointerToRawData + relativeOffset;

byte[] zeroBuffer = new byte[methodSize];
for (int i = 0; i < methodSize; i++)
{
zeroBuffer[i] = 0x0;
}

memStream.Position = actualLoc;
int firstbyte = memStream.ReadByte();
Console.WriteLine(firstbyte);

int headerFlag = firstbyte & 0b11;

int headerSize = headerFlag == 2 ? 1 : 4;
Console.WriteLine(headerSize);

memStream.Position = actualLoc + headerSize;
memStream.Write(zeroBuffer, 0, methodSize - headerSize);

idxCompiledMethods++;
}
}
else
{
break;
}
}
memStream.Position = 0;
memStream.CopyTo(os);
}

return true;
}
}