void AssertWriteToPeFile(PeVerifyResult expectedResult, IAssembly assembly, string pdbPath) { var validator = new MetadataValidator(this.host); List <Microsoft.Cci.ErrorEventArgs> errorEvents = new List <Microsoft.Cci.ErrorEventArgs>(); this.host.Errors += (object sender, Microsoft.Cci.ErrorEventArgs e) => errorEvents.Add(e); validator.Validate(assembly); Debug.Assert(errorEvents.Count == 0); using (var rewrittenFile = File.Create(assembly.Location)) { if (pdbPath != null) { using (var f = File.OpenRead(pdbPath)) { using (var pdbReader = new PdbReader(f, host)) { using (var pdbWriter = new PdbWriter(Path.GetFullPath(assembly.Location + ".pdb"), pdbReader)) { PeWriter.WritePeToStream(assembly, host, rewrittenFile, pdbReader, pdbReader, pdbWriter); } } } } else { using (var pdbWriter = new PdbWriter(Path.GetFullPath(assembly.Location + ".pdb"), null)) { PeWriter.WritePeToStream(assembly, host, rewrittenFile, null, null, pdbWriter); } } } Assert.True(File.Exists(assembly.Location)); PeVerify.Assert(expectedResult, PeVerify.VerifyAssembly(assembly.Location, true)); }
void RoundTripCodeCopier(string assemblyName, string pdbName, bool allowCheckerFail) { PeVerifyResult expectedResult = PeVerify.VerifyAssembly(assemblyName); IAssembly assembly = LoadAssembly(assemblyName); if (pdbName != null) { using (var f = File.OpenRead(pdbName)) { using (var pdbReader = new PdbReader(f, host)) { var codeAssembly = Decompiler.GetCodeModelFromMetadataModel(this.host, assembly, pdbReader); CodeDeepCopier copier = new CodeDeepCopier(host); codeAssembly = copier.Copy(codeAssembly); Checker checker = new Checker(this.host); checker.Traverse(codeAssembly); Debug.Assert(checker.Errors.Count == 0); AssertWriteToPeFile(expectedResult, codeAssembly, pdbReader); } } } else { var codeAssembly = Decompiler.GetCodeModelFromMetadataModel(this.host, assembly, null); CodeDeepCopier copier = new CodeDeepCopier(host); codeAssembly = copier.Copy(codeAssembly); Checker checker = new Checker(this.host); checker.Traverse(codeAssembly); Debug.Assert(allowCheckerFail || checker.Errors.Count == 0); AssertWriteToPeFile(expectedResult, codeAssembly, null); } }
void AssertWriteToPeFile(PeVerifyResult expectedResult, IAssembly assembly, PdbReader pdbReader) { using (var rewrittenFile = File.Create(assembly.Location)) { using (var pdbWriter = new PdbWriter(Path.GetFullPath(assembly.Location + ".pdb"), pdbReader)) { PeWriter.WritePeToStream(assembly, host, rewrittenFile, pdbReader, pdbReader, pdbWriter); } } Assert.True(File.Exists(assembly.Location)); PeVerify.Assert(expectedResult, PeVerify.VerifyAssembly(assembly.Location, true)); }
void AssertWriteToPeFile(PeVerifyResult expectedResult, IAssembly assembly) { using (FileStream rewrittenFile = File.Create(assembly.Location)) { using (this.pdbReader) { PeWriter.WritePeToStream(assembly, this.host, rewrittenFile, this.pdbReader, this.pdbReader, this.pdbWriter); } } Assert.True(File.Exists(assembly.Location)); PeVerify.Assert(expectedResult, PeVerify.VerifyAssembly(assembly.Location)); }
void RoundTripMutableCopyMarkedNodes(string assemblyName, string pdbName) { PeVerifyResult expectedResult = PeVerify.VerifyAssembly(assemblyName); IAssembly assembly = LoadAssembly(assemblyName); using (var f = File.OpenRead(pdbName)) { using (var pdbReader = new PdbReader(f, host)) { var adder = new CopyMarkedNodes(host); var codeAssembly = adder.Rewrite(assembly); AssertWriteToPeFile(expectedResult, codeAssembly, pdbReader); } } }
void RoundTripMutableCopyAndAddGenericParameter2(string assemblyName) { PeVerifyResult expectedResult = PeVerify.VerifyAssembly(assemblyName); IAssembly assembly = LoadAssembly(assemblyName); var copier1 = new MetadataDeepCopier(host); var codeAssembly = copier1.Copy(assembly); for (int i = 0; i < 30; i++) { AddGenericParameters adder = new AddGenericParameters(host, codeAssembly.AllTypes, i); codeAssembly = (Assembly)adder.Rewrite(codeAssembly); } AssertWriteToPeFile(expectedResult, codeAssembly, null); }
void RoundTripNoCopyTestDecompilation(string assemblyName, string pdbName) { PeVerifyResult expectedResult = PeVerify.VerifyAssembly(assemblyName); IAssembly assembly = LoadAssembly(assemblyName); using (var f = File.OpenRead(pdbName)) { using (var pdbReader = new PdbReader(f, host)) { var codeAssembly = Decompiler.GetCodeModelFromMetadataModel(this.host, assembly, pdbReader); Checker checker = new Checker(this.host); checker.Traverse(codeAssembly); Debug.Assert(checker.Errors.Count == 0); AssertWriteToPeFile(expectedResult, codeAssembly, pdbReader); } } }
void RoundTripMutableCopyAndAddGenericParameter(string assemblyName, string pdbName) { PeVerifyResult expectedResult = PeVerify.VerifyAssembly(assemblyName); IAssembly assembly = LoadAssembly(assemblyName); using (var f = File.OpenRead(pdbName)) { using (var pdbReader = new PdbReader(f, host)) { var codeAssembly = Decompiler.GetCodeModelFromMetadataModel(this.host, assembly, pdbReader); codeAssembly = new CodeDeepCopier(host).Copy(codeAssembly); AddGenericParameters adder = new AddGenericParameters(host, codeAssembly.AllTypes, 0); codeAssembly = (Assembly)adder.Rewrite(codeAssembly); Checker checker = new Checker(this.host); checker.Traverse(codeAssembly); Debug.Assert(checker.Errors.Count == 0); AssertWriteToPeFile(expectedResult, codeAssembly, pdbReader); } } }
public void Run(CodeGenerator codeGenerator) { try { var path = codeGenerator.Run(tempFolder); var peVerifyResult = PeVerify.VerifyAssembly(path); if (peVerifyResult.Errors.Any()) { throw new VerificationException(string.Join(Environment.NewLine, peVerifyResult.Errors.ToArray())); } verifier(path); Console.WriteLine("Successful"); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine("Failed"); } }
void RoundTripCopyAndExecute(string assemblyName, string pdbName, bool verificationMayFail) { PeVerifyResult expectedResult = !verificationMayFail ? PeVerify.VerifyAssembly(assemblyName) : PeVerify.RunPeVerifyOnAssembly(assemblyName); string expectedOutput = Execute(assemblyName); IAssembly assembly = LoadAssembly(assemblyName); using (var f = File.OpenRead(pdbName)) { using (var pdbReader = new PdbReader(f, host)) { var codeAssembly = Decompiler.GetCodeModelFromMetadataModel(this.host, assembly, pdbReader); var copier = new CodeDeepCopier(host); codeAssembly = copier.Copy(codeAssembly); Checker checker = new Checker(this.host); checker.Traverse(codeAssembly); Debug.Assert(checker.Errors.Count == 0); AssertWriteToPeFile(expectedResult, codeAssembly, pdbReader); AssertExecute(expectedOutput, assemblyName); } } }
void RoundTripWithCodeMutator(string assemblyName, string pdbName) { PeVerifyResult expectedResult = PeVerify.VerifyAssembly(assemblyName); RoundTripWithCodeMutator(expectedResult, assemblyName, pdbName); }
void RoundTripWithILMutator(string assemblyName, string pdbName) { PeVerifyResult expectedResult = PeVerify.VerifyAssembly(assemblyName); RoundTripWithMutator(expectedResult, LoadAssembly(assemblyName), new MetadataRewriter(host)); }
void RoundTripWithILMutator(string assemblyName, string pdbPath, bool verificationMayFail) { PeVerifyResult expectedResult = PeVerify.VerifyAssembly(assemblyName, verificationMayFail); RoundTripWithMutator(expectedResult, LoadAssembly(assemblyName), new MetadataRewriter(host), pdbPath); }