|
| 1 | +// ****************************************************************** |
| 2 | +// Copyright � 2015-2022 Uno Platform inc. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +// ****************************************************************** |
| 17 | +// |
| 18 | +// This file is based on the work from https://github.com/praeclarum/Ooui |
| 19 | +// |
| 20 | +using System; |
| 21 | +using System.Collections.Generic; |
| 22 | +using System.Collections.Immutable; |
| 23 | +using System.Diagnostics; |
| 24 | +using System.IO; |
| 25 | +using System.IO.Compression; |
| 26 | +using System.Linq; |
| 27 | +using System.Net; |
| 28 | +using System.Runtime.InteropServices; |
| 29 | +using System.Security; |
| 30 | +using System.Security.Cryptography; |
| 31 | +using System.Text; |
| 32 | +using System.Text.RegularExpressions; |
| 33 | +using System.Transactions; |
| 34 | +using Microsoft.Build.Framework; |
| 35 | +using Microsoft.Win32.SafeHandles; |
| 36 | +using Mono.Cecil; |
| 37 | +using Mono.CompilerServices.SymbolWriter; |
| 38 | +using Newtonsoft.Json.Linq; |
| 39 | +using Uno.Wasm.Bootstrap.Extensions; |
| 40 | + |
| 41 | +namespace Uno.Wasm.Bootstrap; |
| 42 | + |
| 43 | +public partial class GenerateUnoNativeAssetsTask_v0 |
| 44 | +{ |
| 45 | + /// <summary> |
| 46 | + /// Applies a temporary workaround for https://github.com/mono/mono/issues/19824 |
| 47 | + /// </summary> |
| 48 | + private string? TransformAOTProfile() |
| 49 | + { |
| 50 | + var profilePath = AotProfile; |
| 51 | + |
| 52 | + if (profilePath != null) |
| 53 | + { |
| 54 | + var reader = new Mono.Profiler.Aot.ProfileReader(); |
| 55 | + Mono.Profiler.Aot.ProfileData profile; |
| 56 | + using (FileStream stream = File.OpenRead(profilePath)) |
| 57 | + { |
| 58 | + profile = reader.ReadAllData(stream); |
| 59 | + } |
| 60 | + |
| 61 | + var excludedMethodsList = AOTProfileExcludedMethods |
| 62 | + .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) |
| 63 | + .ToList(); |
| 64 | + |
| 65 | + var excludedAssemblies = MixedModeExcludedAssembly?.ToDictionary(i => i.ItemSpec, i => i.ItemSpec) |
| 66 | + ?? new Dictionary<string, string>(); |
| 67 | + |
| 68 | + if (excludedMethodsList.Any() || excludedAssemblies.Any()) |
| 69 | + { |
| 70 | + // LoadIntoBufferAsync uses exception filtering |
| 71 | + excludedMethodsList.AddRange(DefaultAOTProfileExcludedMethods); |
| 72 | + |
| 73 | + TryDumpProfileMethods(profile, "AOTProfileDump.Original.txt"); |
| 74 | + |
| 75 | + var excludedMethods = excludedMethodsList.Select(e => new Regex(e)).ToList(); |
| 76 | + |
| 77 | + var q = from m in profile.Methods |
| 78 | + where !excludedMethods.Any(e => e.Match(m.Type.FullName + '.' + m.Name).Success) |
| 79 | + && !excludedAssemblies.ContainsKey(m.Type.Module.Name) |
| 80 | + select m; |
| 81 | + |
| 82 | + profile.Methods = q.ToArray(); |
| 83 | + |
| 84 | + TryDumpProfileMethods(profile, "AOTProfileDump.Filtered.txt"); |
| 85 | + |
| 86 | + var writer = new Mono.Profiler.Aot.ProfileWriter(); |
| 87 | + |
| 88 | + var outputFile = Path.Combine(IntermediateOutputPath, "aot-filtered.profile"); |
| 89 | + using (var outStream = File.Create(outputFile)) |
| 90 | + { |
| 91 | + writer.WriteAllData(outStream, profile); |
| 92 | + } |
| 93 | + |
| 94 | + return outputFile; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return profilePath; |
| 99 | + } |
| 100 | + |
| 101 | + private IEnumerable<string> DefaultAOTProfileExcludedMethods => |
| 102 | + new[] |
| 103 | + { |
| 104 | + @"ManifestBasedResourceGroveler\.InternalGetSatelliteAssembly", // https://github.com/dotnet/runtime/issues/45698 |
| 105 | + |
| 106 | + @"System\.Reflection\.Assembly\.GetExecutingAssembly", // https://github.com/dotnet/runtime/issues/47996 |
| 107 | + @"System\.RuntimeType\.GetType", |
| 108 | + @"System\.RuntimeTypeHandle\.internal_from_name", |
| 109 | + @"System\.RuntimeTypeHandle\.GetTypeByName", |
| 110 | + @"System\.Type\.GetType", |
| 111 | + @"System\.Runtime\.Loader\.AssemblyLoadContext\.InternalLoadFromPath", |
| 112 | + @"System\.Runtime\.Loader\.AssemblyLoadContext\.InternalLoadFile", |
| 113 | + @"System\.Runtime\.Loader\.AssemblyLoadContext\.LoadFromAssemblyName", |
| 114 | + @"System\.Reflection\.Assembly\.Load", |
| 115 | + @"System\.Reflection\.Assembly\.InternalLoad", |
| 116 | + @"System\.Reflection\.RuntimeAssembly\.InternalGetSatelliteAssembly", |
| 117 | + @"System\.Reflection\.RuntimeAssembly\.InternalLoad", |
| 118 | + }; |
| 119 | + |
| 120 | + private void TryDumpProfileMethods(Mono.Profiler.Aot.ProfileData profile, string filePath) |
| 121 | + { |
| 122 | + if (GenerateAOTProfileDebugList) |
| 123 | + { |
| 124 | + var sb = new StringBuilder(); |
| 125 | + |
| 126 | + foreach (var method in profile.Methods) |
| 127 | + { |
| 128 | + var genericParameters = string.Join("|", method.GenericInst?.Types.Select(t => t.ToString()) ?? []); |
| 129 | + |
| 130 | + sb.AppendLine($"{method.Type.Module.Name};{method.Type.FullName}.{method.Name};{method.GenericInst?.Id};{genericParameters}"); |
| 131 | + } |
| 132 | + |
| 133 | + File.WriteAllText(Path.Combine(IntermediateOutputPath, filePath), sb.ToString()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private bool UseAotProfile |
| 138 | + => !string.IsNullOrEmpty(AotProfile) && _runtimeExecutionMode == RuntimeExecutionMode.InterpreterAndAOT; |
| 139 | +} |
0 commit comments