How to use AssemblyDiffingPass class of Microsoft.Coyote.Rewriting package

Best Coyote code snippet using Microsoft.Coyote.Rewriting.AssemblyDiffingPass

AssemblyDiffingPass.cs

Source:AssemblyDiffingPass.cs Github

copy

Full Screen

...13{14 /// <summary>15 /// A pass that diffs the IL contents of assemblies and returns them as JSON.16 /// </summary>17 internal class AssemblyDiffingPass : Pass18 {19 /// <summary>20 /// Map from assemblies to IL contents.21 /// </summary>22 private Dictionary<string, AssemblyContents> ContentMap;23 /// <summary>24 /// Initializes a new instance of the <see cref="AssemblyDiffingPass"/> class.25 /// </summary>26 internal AssemblyDiffingPass(IEnumerable<AssemblyInfo> visitedAssemblies, ILogger logger)27 : base(visitedAssemblies, logger)28 {29 this.ContentMap = new Dictionary<string, AssemblyContents>();30 }31 /// <inheritdoc/>32 protected internal override void VisitAssembly(AssemblyInfo assembly)33 {34 if (!this.ContentMap.ContainsKey(assembly.FullName))35 {36 var contents = new AssemblyContents()37 {38 FullName = assembly.FullName,39 Modules = new List<ModuleContents>()40 };41 this.ContentMap.Add(assembly.FullName, contents);42 }43 base.VisitAssembly(assembly);44 }45 /// <inheritdoc/>46 protected internal override void VisitModule(ModuleDefinition module)47 {48 if (this.ContentMap.TryGetValue(this.Assembly.FullName, out AssemblyContents contents))49 {50 contents.Modules.Add(new ModuleContents()51 {52 Name = module.Name,53 Types = new List<TypeContents>()54 });55 }56 base.VisitModule(module);57 }58 /// <inheritdoc/>59 protected internal override void VisitType(TypeDefinition type)60 {61 if (this.ContentMap.TryGetValue(this.Assembly.FullName, out AssemblyContents contents))62 {63 contents.Modules.FirstOrDefault(m => m.Name == this.Module.Name)?.Types.Add(64 new TypeContents()65 {66 FullName = type.FullName,67 Methods = new List<MethodContents>()68 });69 }70 base.VisitType(type);71 }72 /// <inheritdoc/>73 protected internal override void VisitField(FieldDefinition field)74 {75 if (this.ContentMap.TryGetValue(this.Assembly.FullName, out AssemblyContents contents))76 {77 contents.Modules.FirstOrDefault(m => m.Name == this.Module.Name)?.Types78 .FirstOrDefault(t => t.FullName == this.TypeDef.FullName)?79 .AddField(field);80 }81 base.VisitField(field);82 }83 /// <inheritdoc/>84 protected internal override void VisitMethod(MethodDefinition method)85 {86 if (this.ContentMap.TryGetValue(this.Assembly.FullName, out AssemblyContents contents))87 {88 contents.Modules.FirstOrDefault(m => m.Name == this.Module.Name)?.Types89 .FirstOrDefault(t => t.FullName == this.TypeDef.FullName)?.Methods90 .Add(new MethodContents()91 {92 Index = this.TypeDef.Methods.IndexOf(method),93 Name = method.Name,94 FullName = method.FullName,95 Instructions = new List<string>()96 });97 }98 base.VisitMethod(method);99 }100 /// <inheritdoc/>101 protected override void VisitVariable(VariableDefinition variable)102 {103 if (this.ContentMap.TryGetValue(this.Assembly.FullName, out AssemblyContents contents))104 {105 contents.Modules.FirstOrDefault(m => m.Name == this.Module.Name)?.Types106 .FirstOrDefault(t => t.FullName == this.TypeDef.FullName)?.Methods107 .FirstOrDefault(m => m.FullName == this.Method.FullName)?108 .AddVariable(variable);109 }110 base.VisitVariable(variable);111 }112 /// <inheritdoc/>113 protected override Instruction VisitInstruction(Instruction instruction)114 {115 if (this.ContentMap.TryGetValue(this.Assembly.FullName, out AssemblyContents contents))116 {117 contents.Modules.FirstOrDefault(m => m.Name == this.Module.Name)?.Types118 .FirstOrDefault(t => t.FullName == this.TypeDef.FullName)?.Methods119 .FirstOrDefault(m => m.FullName == this.Method.FullName)?.Instructions120 .Add(instruction.ToString());121 }122 return base.VisitInstruction(instruction);123 }124 /// <summary>125 /// Returns the diff between the IL contents of this pass against the specified pass as JSON.126 /// </summary>127 internal string GetDiffJson(AssemblyInfo assembly, AssemblyDiffingPass pass)128 {129 if (!this.ContentMap.TryGetValue(assembly.FullName, out AssemblyContents thisContents) ||130 !pass.ContentMap.TryGetValue(assembly.FullName, out AssemblyContents otherContents))131 {132 this.Logger.WriteLine(LogSeverity.Error, "Unable to diff IL code that belongs to different assemblies.");133 return string.Empty;134 }135 // Diff contents of the two assemblies.136 var diffedContents = thisContents.Diff(otherContents);137 return this.GetJson(diffedContents);138 }139 /// <summary>140 /// Returns the IL contents of the specified assembly as JSON.141 /// </summary>...

Full Screen

Full Screen

RewritingEngine.cs

Source:RewritingEngine.cs Github

copy

Full Screen

...131 this.Passes.AddLast(new ExceptionFilterRewritingPass(assemblies, this.Logger));132 if (this.Options.IsLoggingAssemblyContents || this.Options.IsDiffingAssemblyContents)133 {134 // Parsing the contents of an assembly must happen before and after any other pass.135 this.Passes.AddFirst(new AssemblyDiffingPass(assemblies, this.Logger));136 this.Passes.AddLast(new AssemblyDiffingPass(assemblies, this.Logger));137 }138 }139 /// <summary>140 /// Rewrites the specified assembly.141 /// </summary>142 private void RewriteAssembly(AssemblyInfo assembly, string outputPath)143 {144 try145 {146 this.Logger.WriteLine($"... Rewriting the '{assembly.Name}' assembly ({assembly.FullName})");147 if (assembly.IsRewritten)148 {149 this.Logger.WriteLine($"..... Skipping as assembly is already rewritten with matching signature");150 return;151 }152 // Traverse the assembly to invoke each pass.153 foreach (var pass in this.Passes)154 {155 Debug.WriteLine($"..... Invoking the '{pass.GetType().Name}' pass");156 assembly.Invoke(pass);157 }158 // Apply the rewriting signature to the assembly metadata.159 assembly.ApplyRewritingSignatureAttribute(GetAssemblyRewriterVersion());160 // Write the binary in the output path with portable symbols enabled.161 string resolvedOutputPath = this.Options.IsReplacingAssemblies() ? assembly.FilePath : outputPath;162 this.Logger.WriteLine($"..... Writing the modified '{assembly.Name}' assembly to {resolvedOutputPath}");163 assembly.Write(outputPath);164 if (this.Options.IsLoggingAssemblyContents)165 {166 // Write the IL before and after rewriting to a JSON file.167 this.WriteILToJson(assembly, false, resolvedOutputPath);168 this.WriteILToJson(assembly, true, resolvedOutputPath);169 }170 if (this.Options.IsDiffingAssemblyContents)171 {172 // Write the IL diff before and after rewriting to a JSON file.173 this.WriteILDiffToJson(assembly, resolvedOutputPath);174 }175 }176 finally177 {178 assembly.Dispose();179 }180 if (this.Options.IsReplacingAssemblies())181 {182 string targetPath = Path.Combine(this.Options.AssembliesDirectory, assembly.Name);183 this.CopyWithRetriesAsync(outputPath, assembly.FilePath).Wait();184 if (assembly.IsSymbolFileAvailable())185 {186 string pdbFile = Path.ChangeExtension(outputPath, "pdb");187 string targetPdbFile = Path.ChangeExtension(targetPath, "pdb");188 this.CopyWithRetriesAsync(pdbFile, targetPdbFile).Wait();189 }190 }191 }192 /// <summary>193 /// Writes the original or rewritten IL to a JSON file in the specified output path.194 /// </summary>195 internal void WriteILToJson(AssemblyInfo assembly, bool isRewritten, string outputPath)196 {197 var diffingPass = (isRewritten ? this.Passes.Last : this.Passes.First).Value as AssemblyDiffingPass;198 if (diffingPass != null)199 {200 string json = diffingPass.GetJson(assembly);201 if (!string.IsNullOrEmpty(json))202 {203 string jsonFile = Path.ChangeExtension(outputPath, $".{(isRewritten ? "rw" : "il")}.json");204 this.Logger.WriteLine($"..... Writing the {(isRewritten ? "rewritten" : "original")} IL " +205 $"of '{assembly.Name}' as JSON to {jsonFile}");206 File.WriteAllText(jsonFile, json);207 }208 }209 }210 /// <summary>211 /// Writes the IL diff to a JSON file in the specified output path.212 /// </summary>213 internal void WriteILDiffToJson(AssemblyInfo assembly, string outputPath)214 {215 var originalDiffingPass = this.Passes.First.Value as AssemblyDiffingPass;216 var rewrittenDiffingPass = this.Passes.Last.Value as AssemblyDiffingPass;217 if (originalDiffingPass != null && rewrittenDiffingPass != null)218 {219 // Compute the diff between the original and rewritten IL and dump it to JSON.220 string diffJson = originalDiffingPass.GetDiffJson(assembly, rewrittenDiffingPass);221 if (!string.IsNullOrEmpty(diffJson))222 {223 string jsonFile = Path.ChangeExtension(outputPath, ".diff.json");224 this.Logger.WriteLine($"..... Writing the IL diff of '{assembly.Name}' as JSON to {jsonFile}");225 File.WriteAllText(jsonFile, diffJson);226 }227 }228 }229 /// <summary>230 /// Checks if the specified assembly has been already rewritten with the current version....

Full Screen

Full Screen

AssemblyDiffingPass

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Rewriting;2using System;3using System.Reflection;4{5 {6 static void Main(string[] args)7 {8 var assembly = Assembly.LoadFrom(@"C:\Users\A\Desktop\2.dll");9 var assemblyDiffingPass = new AssemblyDiffingPass(assembly, "ConsoleApp1.Program");10 assemblyDiffingPass.Execute();11 }12 }13}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Coyote automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful