From 43505915d4516a0b7307742823404a9a41cf09db Mon Sep 17 00:00:00 2001 From: Vitek Karas Date: Thu, 11 Nov 2021 09:40:42 -0800 Subject: [PATCH 1/4] Fix deterministic MVID and add PdbChecksum (#31) --- Mono.Cecil.Cil/PortablePdb.cs | 179 ++++++++--- Mono.Cecil.Cil/Symbols.cs | 6 + Mono.Cecil.PE/ImageReader.cs | 5 +- Mono.Cecil.PE/ImageWriter.cs | 34 +- Mono.Cecil/AssemblyWriter.cs | 49 +-- Mono.Security.Cryptography/CryptoService.cs | 2 +- Test/Mono.Cecil.Tests/ImageReadTests.cs | 18 +- Test/Mono.Cecil.Tests/PortablePdbTests.cs | 303 +++++++++++++++++- .../assemblies/EmbeddedPdbChecksumLib.dll | Bin 0 -> 9728 bytes Test/Resources/assemblies/PdbChecksumLib.dll | Bin 0 -> 4096 bytes Test/Resources/assemblies/PdbChecksumLib.pdb | Bin 0 -> 9576 bytes symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs | 11 +- 12 files changed, 500 insertions(+), 107 deletions(-) create mode 100644 Test/Resources/assemblies/EmbeddedPdbChecksumLib.dll create mode 100644 Test/Resources/assemblies/PdbChecksumLib.dll create mode 100644 Test/Resources/assemblies/PdbChecksumLib.pdb diff --git a/Mono.Cecil.Cil/PortablePdb.cs b/Mono.Cecil.Cil/PortablePdb.cs index aff6b2937..d8c43f6e7 100644 --- a/Mono.Cecil.Cil/PortablePdb.cs +++ b/Mono.Cecil.Cil/PortablePdb.cs @@ -12,7 +12,7 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; - +using System.Security.Cryptography; using Mono.Cecil.Metadata; using Mono.Cecil.PE; @@ -39,7 +39,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStre ISymbolReader GetSymbolReader (ModuleDefinition module, Disposable symbolStream, string fileName) { - return new PortablePdbReader (ImageReader.ReadPortablePdb (symbolStream, fileName), module); + return new PortablePdbReader (ImageReader.ReadPortablePdb (symbolStream, fileName, out _), module); } } @@ -234,8 +234,8 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName) Mixin.CheckModule (module); Mixin.CheckFileName (fileName); - var file = File.OpenWrite (Mixin.GetPdbFileName (fileName)); - return GetSymbolWriter (module, Disposable.Owned (file as Stream)); + var file = File.Open (Mixin.GetPdbFileName (fileName), FileMode.OpenOrCreate, FileAccess.ReadWrite); + return GetSymbolWriter (module, Disposable.Owned (file as Stream), Disposable.NotOwned ((Stream)null)); } public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) @@ -243,15 +243,18 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStre Mixin.CheckModule (module); Mixin.CheckStream (symbolStream); - return GetSymbolWriter (module, Disposable.NotOwned (symbolStream)); + // In order to compute the PDB checksum, the stream we're writing to needs to be able to + // seek and read as well. We can't assume this about a stream provided by the user. + // So in this case, create a memory stream to cache the PDB. + return GetSymbolWriter (module, Disposable.Owned (new MemoryStream() as Stream), Disposable.NotOwned (symbolStream)); } - ISymbolWriter GetSymbolWriter (ModuleDefinition module, Disposable stream) + ISymbolWriter GetSymbolWriter (ModuleDefinition module, Disposable stream, Disposable final_stream) { var metadata = new MetadataBuilder (module, this); var writer = ImageWriter.CreateDebugWriter (module, metadata, stream); - return new PortablePdbWriter (metadata, module, writer); + return new PortablePdbWriter (metadata, module, writer, final_stream); } } @@ -260,9 +263,14 @@ public sealed class PortablePdbWriter : ISymbolWriter { readonly MetadataBuilder pdb_metadata; readonly ModuleDefinition module; readonly ImageWriter writer; + readonly Disposable final_stream; MetadataBuilder module_metadata; + internal byte [] pdb_checksum; + internal Guid pdb_id_guid; + internal uint pdb_id_age; + bool IsEmbedded { get { return writer == null; } } internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module) @@ -278,10 +286,11 @@ internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition modul pdb_metadata.AddCustomDebugInformations (module); } - internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, ImageWriter writer) + internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, ImageWriter writer, Disposable final_stream) : this (pdb_metadata, module) { this.writer = writer; + this.final_stream = final_stream; } public ISymbolReaderProvider GetReaderProvider () @@ -289,45 +298,82 @@ public ISymbolReaderProvider GetReaderProvider () return new PortablePdbReaderProvider (); } + public void Write (MethodDebugInformation info) + { + CheckMethodDebugInformationTable (); + + pdb_metadata.AddMethodDebugInformation (info); + } + public ImageDebugHeader GetDebugHeader () { if (IsEmbedded) return new ImageDebugHeader (); - var directory = new ImageDebugDirectory () { - MajorVersion = 256, - MinorVersion = 20557, - Type = ImageDebugType.CodeView, - TimeDateStamp = (int) module.timestamp, - }; + WritePdbFile (); + + if (final_stream.value != null) { + writer.BaseStream.Seek (0, SeekOrigin.Begin); + var buffer = new byte [8192]; + CryptoService.CopyStreamChunk (writer.BaseStream, final_stream.value, buffer, (int)writer.BaseStream.Length); + } - var buffer = new ByteBuffer (); - // RSDS - buffer.WriteUInt32 (0x53445352); - // Module ID - buffer.WriteBytes (module.Mvid.ToByteArray ()); - // PDB Age - buffer.WriteUInt32 (1); - // PDB Path - var fileName = writer.BaseStream.GetFileName (); - if (string.IsNullOrEmpty (fileName)) { - fileName = module.Assembly.Name.Name + ".pdb"; + ImageDebugHeaderEntry codeViewEntry; + { + var codeViewDirectory = new ImageDebugDirectory () { + MajorVersion = 256, + MinorVersion = 20557, + Type = ImageDebugType.CodeView, + TimeDateStamp = (int)module.timestamp, + }; + + var buffer = new ByteBuffer (); + // RSDS + buffer.WriteUInt32 (0x53445352); + // Module ID + buffer.WriteBytes (pdb_id_guid.ToByteArray ()); + // PDB Age + buffer.WriteUInt32 (pdb_id_age); + // PDB Path + var fileName = writer.BaseStream.GetFileName (); + if (string.IsNullOrEmpty (fileName)) { + fileName = module.Assembly.Name.Name + ".pdb"; + } + buffer.WriteBytes (System.Text.Encoding.UTF8.GetBytes (fileName)); + buffer.WriteByte (0); + + var data = new byte [buffer.length]; + Buffer.BlockCopy (buffer.buffer, 0, data, 0, buffer.length); + codeViewDirectory.SizeOfData = data.Length; + + codeViewEntry = new ImageDebugHeaderEntry (codeViewDirectory, data); } - buffer.WriteBytes (System.Text.Encoding.UTF8.GetBytes (fileName)); - buffer.WriteByte (0); - var data = new byte [buffer.length]; - Buffer.BlockCopy (buffer.buffer, 0, data, 0, buffer.length); - directory.SizeOfData = data.Length; + ImageDebugHeaderEntry pdbChecksumEntry; + { + var pdbChecksumDirectory = new ImageDebugDirectory () { + MajorVersion = 1, + MinorVersion = 0, + Type = ImageDebugType.PdbChecksum, + TimeDateStamp = 0 + }; - return new ImageDebugHeader (new ImageDebugHeaderEntry (directory, data)); - } + var buffer = new ByteBuffer (); + // SHA256 - Algorithm name + buffer.WriteBytes (System.Text.Encoding.UTF8.GetBytes ("SHA256")); + buffer.WriteByte (0); - public void Write (MethodDebugInformation info) - { - CheckMethodDebugInformationTable (); + // Checksum - 32 bytes + buffer.WriteBytes (pdb_checksum); - pdb_metadata.AddMethodDebugInformation (info); + var data = new byte [buffer.length]; + Buffer.BlockCopy (buffer.buffer, 0, data, 0, buffer.length); + pdbChecksumDirectory.SizeOfData = data.Length; + + pdbChecksumEntry = new ImageDebugHeaderEntry (pdbChecksumDirectory, data); + } + + return new ImageDebugHeader (new ImageDebugHeaderEntry [] { codeViewEntry, pdbChecksumEntry }); } void CheckMethodDebugInformationTable () @@ -343,10 +389,8 @@ void CheckMethodDebugInformationTable () public void Dispose () { - if (IsEmbedded) - return; - - WritePdbFile (); + writer.stream.Dispose (); + final_stream.Dispose (); } void WritePdbFile () @@ -360,15 +404,18 @@ void WritePdbFile () writer.WriteMetadata (); writer.Flush (); - writer.stream.Dispose (); + + ComputeChecksumAndPdbId (); + + WritePdbId (); } void WritePdbHeap () { var pdb_heap = pdb_metadata.pdb_heap; - pdb_heap.WriteBytes (module.Mvid.ToByteArray ()); - pdb_heap.WriteUInt32 (module_metadata.timestamp); + // PDB ID ( GUID + TimeStamp ) are left zeroed out for now. Will be filled at the end with a hash. + pdb_heap.WriteBytes (20); pdb_heap.WriteUInt32 (module_metadata.entry_point.ToUInt32 ()); @@ -399,6 +446,32 @@ void WriteTableHeap () pdb_metadata.table_heap.ComputeTableInformations (); pdb_metadata.table_heap.WriteTableHeap (); } + + void ComputeChecksumAndPdbId () + { + var buffer = new byte [8192]; + + // Compute the has of the entire file - PDB ID is zeroes still + writer.BaseStream.Seek (0, SeekOrigin.Begin); + var sha256 = SHA256.Create (); + using (var crypto_stream = new CryptoStream (Stream.Null, sha256, CryptoStreamMode.Write)) { + CryptoService.CopyStreamChunk (writer.BaseStream, crypto_stream, buffer, (int)writer.BaseStream.Length); + } + + pdb_checksum = sha256.Hash; + + var hashBytes = new ByteBuffer (pdb_checksum); + pdb_id_guid = new Guid (hashBytes.ReadBytes (16)); + pdb_id_age = hashBytes.ReadUInt32 (); + } + + void WritePdbId () + { + // PDB ID is the first 20 bytes of the PdbHeap + writer.MoveToRVA (TextSegment.PdbHeap); + writer.WriteBytes (pdb_id_guid.ToByteArray ()); + writer.WriteUInt32 (pdb_id_age); + } } public sealed class EmbeddedPortablePdbWriterProvider : ISymbolWriterProvider { @@ -435,9 +508,14 @@ public ISymbolReaderProvider GetReaderProvider () return new EmbeddedPortablePdbReaderProvider (); } + public void Write (MethodDebugInformation info) + { + writer.Write (info); + } + public ImageDebugHeader GetDebugHeader () { - writer.Dispose (); + ImageDebugHeader pdbDebugHeader = writer.GetDebugHeader (); var directory = new ImageDebugDirectory { Type = ImageDebugType.EmbeddedPortablePdb, @@ -462,15 +540,12 @@ public ImageDebugHeader GetDebugHeader () directory.SizeOfData = (int) data.Length; - return new ImageDebugHeader (new [] { - writer.GetDebugHeader ().Entries [0], - new ImageDebugHeaderEntry (directory, data.ToArray ()) - }); - } + var debugHeaderEntries = new ImageDebugHeaderEntry [pdbDebugHeader.Entries.Length + 1]; + for (int i = 0; i < pdbDebugHeader.Entries.Length; i++) + debugHeaderEntries [i] = pdbDebugHeader.Entries [i]; + debugHeaderEntries [debugHeaderEntries.Length - 1] = new ImageDebugHeaderEntry (directory, data.ToArray ()); - public void Write (MethodDebugInformation info) - { - writer.Write (info); + return new ImageDebugHeader (debugHeaderEntries); } public void Dispose () diff --git a/Mono.Cecil.Cil/Symbols.cs b/Mono.Cecil.Cil/Symbols.cs index 5e92b67fa..a6690675c 100644 --- a/Mono.Cecil.Cil/Symbols.cs +++ b/Mono.Cecil.Cil/Symbols.cs @@ -39,6 +39,7 @@ public enum ImageDebugType { CodeView = 2, Deterministic = 16, EmbeddedPortablePdb = 17, + PdbChecksum = 19, } public sealed class ImageDebugHeader { @@ -1174,6 +1175,11 @@ public static ImageDebugHeaderEntry GetEmbeddedPortablePdbEntry (this ImageDebug return GetEntry (header, ImageDebugType.EmbeddedPortablePdb); } + public static ImageDebugHeaderEntry GetPdbChecksumEntry (this ImageDebugHeader header) + { + return GetEntry (header, ImageDebugType.PdbChecksum); + } + private static ImageDebugHeaderEntry GetEntry (this ImageDebugHeader header, ImageDebugType type) { if (!header.HasEntries) diff --git a/Mono.Cecil.PE/ImageReader.cs b/Mono.Cecil.PE/ImageReader.cs index 535812924..a34e64d36 100644 --- a/Mono.Cecil.PE/ImageReader.cs +++ b/Mono.Cecil.PE/ImageReader.cs @@ -27,6 +27,7 @@ sealed class ImageReader : BinaryStreamReader { DataDirectory metadata; uint table_heap_offset; + uint pdb_heap_offset; public ImageReader (Disposable stream, string file_name) : base (stream.value) @@ -400,6 +401,7 @@ void ReadMetadataStream (Section section) break; case "#Pdb": image.PdbHeap = new PdbHeap (data); + pdb_heap_offset = offset; break; } } @@ -768,7 +770,7 @@ public static Image ReadImage (Disposable stream, string file_name) } } - public static Image ReadPortablePdb (Disposable stream, string file_name) + public static Image ReadPortablePdb (Disposable stream, string file_name, out uint pdb_heap_offset) { try { var reader = new ImageReader (stream, file_name); @@ -785,6 +787,7 @@ public static Image ReadPortablePdb (Disposable stream, string file_name reader.metadata = new DataDirectory (0, length); reader.ReadMetadata (); + pdb_heap_offset = reader.pdb_heap_offset; return reader.image; } catch (EndOfStreamException e) { throw new BadImageFormatException (stream.value.GetFileName (), e); diff --git a/Mono.Cecil.PE/ImageWriter.cs b/Mono.Cecil.PE/ImageWriter.cs index a8a3fa82b..0648f59f0 100644 --- a/Mono.Cecil.PE/ImageWriter.cs +++ b/Mono.Cecil.PE/ImageWriter.cs @@ -48,7 +48,9 @@ sealed class ImageWriter : BinaryStreamWriter { ushort sections; - ImageWriter (ModuleDefinition module, string runtime_version, MetadataBuilder metadata, Disposable stream, bool metadataOnly = false) + internal long debug_header_entries_position; + + ImageWriter (ModuleDefinition module, string runtime_version, MetadataBuilder metadata, Disposable stream, bool metadataOnly = false, ImageDebugHeader debugHeader = null) : base (stream.value) { this.module = module; @@ -61,26 +63,18 @@ sealed class ImageWriter : BinaryStreamWriter { this.pe64 = module.Architecture == TargetArchitecture.AMD64 || module.Architecture == TargetArchitecture.IA64 || module.Architecture == TargetArchitecture.ARM64; this.has_reloc = module.Architecture == TargetArchitecture.I386; - this.GetDebugHeader (); this.GetWin32Resources (); - this.BuildTextMap (); - this.sections = (ushort) (has_reloc ? 2 : 1); // text + reloc? - } - - void GetDebugHeader () - { - var symbol_writer = metadata.symbol_writer; - if (symbol_writer != null) - debug_header = symbol_writer.GetDebugHeader (); + debug_header = debugHeader; if (module.HasDebugHeader) { var header = module.GetDebugHeader (); var deterministic = header.GetDeterministicEntry (); - if (deterministic == null) - return; - - debug_header = debug_header.AddDeterministicEntry (); + if (deterministic != null) + debug_header = debug_header.AddDeterministicEntry (); } + + this.BuildTextMap (); + this.sections = (ushort)(has_reloc ? 2 : 1); // text + reloc? } void GetWin32Resources () @@ -96,9 +90,9 @@ void GetWin32Resources () } } - public static ImageWriter CreateWriter (ModuleDefinition module, MetadataBuilder metadata, Disposable stream) + public static ImageWriter CreateWriter (ModuleDefinition module, MetadataBuilder metadata, Disposable stream, ImageDebugHeader debugHeader) { - var writer = new ImageWriter (module, module.runtime_version, metadata, stream); + var writer = new ImageWriter (module, module.runtime_version, metadata, stream, metadataOnly: false, debugHeader: debugHeader); writer.BuildSections (); return writer; } @@ -379,7 +373,7 @@ void MoveToRVA (Section section, RVA rva) BaseStream.Seek (GetRVAFileOffset (section, rva), SeekOrigin.Begin); } - void MoveToRVA (TextSegment segment) + internal void MoveToRVA (TextSegment segment) { MoveToRVA (text, text_map.GetRVA (segment)); } @@ -600,7 +594,9 @@ void WriteDebugDirectory () data_start += entry.Data.Length; } - + + debug_header_entries_position = BaseStream.Position; + for (var i = 0; i < debug_header.Entries.Length; i++) { var entry = debug_header.Entries [i]; WriteBytes (entry.Data); diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index 3f17a828c..cf1b69608 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -118,13 +118,17 @@ static void Write (ModuleDefinition module, Disposable stream, WriterPar metadata.SetSymbolWriter (symbol_writer); BuildMetadata (module, metadata); - if (parameters.DeterministicMvid) - metadata.ComputeDeterministicMvid (); + ImageDebugHeader debugHeader = null; + if (symbol_writer != null) + debugHeader = symbol_writer.GetDebugHeader (); - var writer = ImageWriter.CreateWriter (module, metadata, stream); + var writer = ImageWriter.CreateWriter (module, metadata, stream, debugHeader); stream.value.SetLength (0); writer.WriteImage (); + if (parameters.DeterministicMvid) + ComputeDeterministicMvid (writer, module); + if (parameters.HasStrongNameKey) CryptoService.StrongName (stream.value, writer, parameters); } @@ -156,6 +160,26 @@ static ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fq_name, I return symbol_writer_provider.GetSymbolWriter (module, fq_name); } + + static void ComputeDeterministicMvid (ImageWriter writer, ModuleDefinition module) + { + long previousPosition = writer.BaseStream.Position; + writer.BaseStream.Seek(0, SeekOrigin.Begin); + + // The hash should be computed with the MVID set to all zeroes + // which it is - we explicitly write all zeroes GUID into the heap + // as the MVID. + // Same goes for strong name signature, which also already in the image but all zeroes right now. + Guid guid = CryptoService.ComputeGuid (CryptoService.ComputeHash (writer.BaseStream)); + + // The MVID GUID is always the first GUID in the GUID heap + writer.MoveToRVA (TextSegment.GuidHeap); + writer.WriteBytes (guid.ToByteArray ()); + writer.Flush (); + module.Mvid = guid; + + writer.BaseStream.Seek(previousPosition, SeekOrigin.Begin); + } } abstract class MetadataTable { @@ -2636,25 +2660,6 @@ void AddSequencePoints (MethodDebugInformation info) method_debug_information_table.rows [rid - 1].Col2 = GetBlobIndex (signature); } - - public void ComputeDeterministicMvid () - { - var guid = CryptoService.ComputeGuid (CryptoService.ComputeHash ( - data, - resources, - string_heap, - user_string_heap, - blob_heap, - table_heap, - code)); - - var position = guid_heap.position; - guid_heap.position = 0; - guid_heap.WriteBytes (guid.ToByteArray ()); - guid_heap.position = position; - - module.Mvid = guid; - } } sealed class SignatureWriter : ByteBuffer { diff --git a/Mono.Security.Cryptography/CryptoService.cs b/Mono.Security.Cryptography/CryptoService.cs index dd9613fe0..3877debfa 100644 --- a/Mono.Security.Cryptography/CryptoService.cs +++ b/Mono.Security.Cryptography/CryptoService.cs @@ -109,7 +109,7 @@ static byte [] HashStream (Stream stream, ImageWriter writer, out int strong_nam return sha1.Hash; } - static void CopyStreamChunk (Stream stream, Stream dest_stream, byte [] buffer, int length) + public static void CopyStreamChunk (Stream stream, Stream dest_stream, byte [] buffer, int length) { while (length > 0) { int read = stream.Read (buffer, 0, System.Math.Min (buffer.Length, length)); diff --git a/Test/Mono.Cecil.Tests/ImageReadTests.cs b/Test/Mono.Cecil.Tests/ImageReadTests.cs index ef6ab06ff..f663b60ca 100644 --- a/Test/Mono.Cecil.Tests/ImageReadTests.cs +++ b/Test/Mono.Cecil.Tests/ImageReadTests.cs @@ -237,9 +237,16 @@ public void ExternalPdbDeterministicAssembly () var header = module.GetDebugHeader (); - Assert.AreEqual (2, header.Entries.Length); + Assert.IsTrue (header.Entries.Length >= 2); Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.CodeView)); Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.Deterministic)); + + // If read directly from a file the PdbChecksum may not be persent (in this test case it isn't) + // but when written through Cecil it will always be there. + if (header.Entries.Length > 2) { + Assert.AreEqual (3, header.Entries.Length); + Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.PdbChecksum)); + } }, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider)); } @@ -251,10 +258,17 @@ public void EmbeddedPdbDeterministicAssembly () var header = module.GetDebugHeader (); - Assert.AreEqual (3, header.Entries.Length); + Assert.IsTrue (header.Entries.Length >= 3); Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.CodeView)); Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.Deterministic)); Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.EmbeddedPortablePdb)); + + // If read directly from a file the PdbChecksum may not be persent (in this test case it isn't) + // but when written through Cecil it will always be there. + if (header.Entries.Length > 3) { + Assert.AreEqual (4, header.Entries.Length); + Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.PdbChecksum)); + } }, symbolReaderProvider: typeof (EmbeddedPortablePdbReaderProvider), symbolWriterProvider: typeof (EmbeddedPortablePdbWriterProvider)); } } diff --git a/Test/Mono.Cecil.Tests/PortablePdbTests.cs b/Test/Mono.Cecil.Tests/PortablePdbTests.cs index d9786cb37..1401f6319 100644 --- a/Test/Mono.Cecil.Tests/PortablePdbTests.cs +++ b/Test/Mono.Cecil.Tests/PortablePdbTests.cs @@ -1,6 +1,8 @@ using System; using System.IO; +using System.IO.Compression; using System.Linq; +using System.Security.Cryptography; using System.Text; using NUnit.Framework; @@ -357,15 +359,23 @@ public void EmbeddedCompressedPortablePdb () var header = module.GetDebugHeader (); Assert.IsNotNull (header); - Assert.AreEqual (2, header.Entries.Length); + Assert.IsTrue (header.Entries.Length >= 2); - var cv = header.Entries [0]; + int i = 0; + var cv = header.Entries [i++]; Assert.AreEqual (ImageDebugType.CodeView, cv.Directory.Type); - var eppdb = header.Entries [1]; + if (header.Entries.Length > 2) { + Assert.AreEqual (3, header.Entries.Length); + var pdbChecksum = header.Entries [i++]; + Assert.AreEqual (ImageDebugType.PdbChecksum, pdbChecksum.Directory.Type); + } + + var eppdb = header.Entries [i++]; Assert.AreEqual (ImageDebugType.EmbeddedPortablePdb, eppdb.Directory.Type); Assert.AreEqual (0x0100, eppdb.Directory.MajorVersion); Assert.AreEqual (0x0100, eppdb.Directory.MinorVersion); + }, symbolReaderProvider: typeof (EmbeddedPortablePdbReaderProvider), symbolWriterProvider: typeof (EmbeddedPortablePdbWriterProvider)); } @@ -400,7 +410,7 @@ void TestPortablePdbModule (Action test) { TestModule ("PdbTarget.exe", test, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider)); TestModule ("EmbeddedPdbTarget.exe", test, verify: !Platform.OnMono); - TestModule ("EmbeddedCompressedPdbTarget.exe", test, symbolReaderProvider: typeof(EmbeddedPortablePdbReaderProvider), symbolWriterProvider: typeof(EmbeddedPortablePdbWriterProvider)); + TestModule ("EmbeddedCompressedPdbTarget.exe", test, symbolReaderProvider: typeof(EmbeddedPortablePdbReaderProvider), symbolWriterProvider: typeof (EmbeddedPortablePdbWriterProvider)); } [Test] @@ -604,7 +614,7 @@ static EmbeddedSourceDebugInformation GetSourceDebugInfo (Document document) } [Test] - public void PortablePdbLineInfo () + public void PortablePdbLineInfo() { TestModule ("line.exe", module => { var type = module.GetType ("Tests"); @@ -809,5 +819,288 @@ public void ClearSequencePoints () } }); } + + [Test] + public void DoubleWriteAndReadWithDeterministicMvidAndVariousChanges () + { + Guid mvidIn, mvidARM64Out, mvidX64Out; + + const string resource = "mylib.dll"; + { + string destination = Path.GetTempFileName (); + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + mvidIn = module.Mvid; + module.Architecture = TargetArchitecture.ARM64; // Can't use I386 as it writes different import table size -> differnt MVID + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + mvidARM64Out = module.Mvid; + } + + Assert.AreNotEqual (mvidIn, mvidARM64Out); + } + + { + string destination = Path.GetTempFileName (); + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + Assert.AreEqual (mvidIn, module.Mvid); + module.Architecture = TargetArchitecture.AMD64; + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + mvidX64Out = module.Mvid; + } + + Assert.AreNotEqual (mvidARM64Out, mvidX64Out); + } + + { + string destination = Path.GetTempFileName (); + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + Assert.AreEqual (mvidIn, module.Mvid); + module.Architecture = TargetArchitecture.AMD64; + module.timestamp = 42; + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + Guid mvidDifferentTimeStamp; + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + mvidDifferentTimeStamp = module.Mvid; + } + + Assert.AreNotEqual (mvidX64Out, mvidDifferentTimeStamp); + } + } + + [Test] + public void ReadPortablePdbChecksum () + { + const string resource = "PdbChecksumLib.dll"; + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + GetPdbChecksumData (module.GetDebugHeader (), out string algorithmName, out byte [] checksum); + Assert.AreEqual ("SHA256", algorithmName); + + string pdbPath = GetDebugHeaderPdbPath (module); + CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out byte [] pdbId); + + CollectionAssert.AreEqual (expectedChecksum, checksum); + } + } + + [Test] + public void ReadEmbeddedPortablePdbChecksum () + { + const string resource = "EmbeddedPdbChecksumLib.dll"; + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + var debugHeader = module.GetDebugHeader (); + GetPdbChecksumData (debugHeader, out string algorithmName, out byte [] checksum); + Assert.AreEqual ("SHA256", algorithmName); + + GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb); + CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out byte [] pdbId); + + CollectionAssert.AreEqual (expectedChecksum, checksum); + } + } + + [Test] + public void WritePortablePdbChecksum () + { + const string resource = "PdbChecksumLib.dll"; + string destination = Path.GetTempFileName (); + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + GetPdbChecksumData (module.GetDebugHeader (), out string algorithmName, out byte [] checksum); + Assert.AreEqual ("SHA256", algorithmName); + + string pdbPath = GetDebugHeaderPdbPath (module); + CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out byte [] pdbId); + + CollectionAssert.AreEqual (expectedChecksum, checksum); + } + } + + [Test] + public void WritePortablePdbToWriteOnlyStream () + { + const string resource = "PdbChecksumLib.dll"; + string destination = Path.GetTempFileName (); + + // Note that the module stream already requires read access even on writing to be able to compute strong name + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) + using (var pdbStream = new FileStream (destination + ".pdb", FileMode.Create, FileAccess.Write)) { + module.Write (destination, new WriterParameters { + DeterministicMvid = true, + WriteSymbols = true, + SymbolWriterProvider = new PortablePdbWriterProvider (), + SymbolStream = pdbStream + }); + } + } + + [Test] + public void DoubleWritePortablePdbDeterministicPdbId () + { + const string resource = "PdbChecksumLib.dll"; + string destination = Path.GetTempFileName (); + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + byte [] pdbIdOne; + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + string pdbPath = GetDebugHeaderPdbPath (module); + CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out pdbIdOne); + } + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + byte [] pdbIdTwo; + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + string pdbPath = GetDebugHeaderPdbPath (module); + CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out pdbIdTwo); + } + + CollectionAssert.AreEqual (pdbIdOne, pdbIdTwo); + } + + [Test] + public void WriteEmbeddedPortablePdbChecksum () + { + const string resource = "EmbeddedPdbChecksumLib.dll"; + string destination = Path.GetTempFileName (); + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + var debugHeader = module.GetDebugHeader (); + GetPdbChecksumData (debugHeader, out string algorithmName, out byte [] checksum); + Assert.AreEqual ("SHA256", algorithmName); + + GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb); + CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out byte [] pdbId); + + CollectionAssert.AreEqual (expectedChecksum, checksum); + } + } + + [Test] + public void DoubleWriteEmbeddedPortablePdbChecksum () + { + const string resource = "EmbeddedPdbChecksumLib.dll"; + string destination = Path.GetTempFileName (); + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + byte [] pdbIdOne; + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + var debugHeader = module.GetDebugHeader (); + GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb); + CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out pdbIdOne); + } + + using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { + module.Write (destination, new WriterParameters { DeterministicMvid = true, WriteSymbols = true }); + } + + byte [] pdbIdTwo; + using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { + var debugHeader = module.GetDebugHeader (); + GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb); + CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out pdbIdTwo); + } + + CollectionAssert.AreEqual (pdbIdOne, pdbIdTwo); + } + + private void GetEmbeddedPdb (ImageDebugHeader debugHeader, out byte [] embeddedPdb) + { + var entry = Mixin.GetEmbeddedPortablePdbEntry (debugHeader); + Assert.IsNotNull (entry); + + var compressed_stream = new MemoryStream (entry.Data); + var reader = new BinaryStreamReader (compressed_stream); + Assert.AreEqual (0x4244504D, reader.ReadInt32 ()); + var length = reader.ReadInt32 (); + var decompressed_stream = new MemoryStream (length); + + using (var deflate = new DeflateStream (compressed_stream, CompressionMode.Decompress, leaveOpen: true)) + deflate.CopyTo (decompressed_stream); + + embeddedPdb = decompressed_stream.ToArray (); + } + + private void GetPdbChecksumData (ImageDebugHeader debugHeader, out string algorithmName, out byte [] checksum) + { + var entry = Mixin.GetPdbChecksumEntry (debugHeader); + Assert.IsNotNull (entry); + + var length = Array.IndexOf (entry.Data, (byte)0, 0); + var bytes = new byte [length]; + Buffer.BlockCopy (entry.Data, 0, bytes, 0, length); + algorithmName = Encoding.UTF8.GetString (bytes); + int checksumSize = 0; + switch (algorithmName) { + case "SHA256": checksumSize = 32; break; + case "SHA384": checksumSize = 48; break; + case "SHA512": checksumSize = 64; break; + } + checksum = new byte [checksumSize]; + Buffer.BlockCopy (entry.Data, length + 1, checksum, 0, checksumSize); + } + + private void CalculatePdbChecksumAndId (string filePath, out byte [] pdbChecksum, out byte [] pdbId) + { + using (var fs = File.OpenRead (filePath)) + CalculatePdbChecksumAndId (fs, out pdbChecksum, out pdbId); + } + + private void CalculatePdbChecksumAndId (byte [] data, out byte [] pdbChecksum, out byte [] pdbId) + { + using (var pdb = new MemoryStream (data)) + CalculatePdbChecksumAndId (pdb, out pdbChecksum, out pdbId); + } + + private void CalculatePdbChecksumAndId (Stream pdbStream, out byte [] pdbChecksum, out byte [] pdbId) + { + // Get the offset of the PDB heap (this requires parsing several headers + // so it's easier to use the ImageReader directly for this) + Image image = ImageReader.ReadPortablePdb (new Disposable (pdbStream, false), "test.pdb", out uint pdbHeapOffset); + pdbId = new byte [20]; + Array.Copy (image.PdbHeap.data, 0, pdbId, 0, 20); + + pdbStream.Seek (0, SeekOrigin.Begin); + byte [] rawBytes = pdbStream.ReadAll (); + + var bytes = new byte [rawBytes.Length]; + + Array.Copy (rawBytes, 0, bytes, 0, pdbHeapOffset); + + // Zero out the PDB ID (20 bytes) + for (int i = 0; i < 20; bytes [i + pdbHeapOffset] = 0, i++) ; + + Array.Copy (rawBytes, pdbHeapOffset + 20, bytes, pdbHeapOffset + 20, rawBytes.Length - pdbHeapOffset - 20); + + var sha256 = SHA256.Create (); + pdbChecksum = sha256.ComputeHash (bytes); + } } } diff --git a/Test/Resources/assemblies/EmbeddedPdbChecksumLib.dll b/Test/Resources/assemblies/EmbeddedPdbChecksumLib.dll new file mode 100644 index 0000000000000000000000000000000000000000..2de939b9b18609590abed1bde5bdfb272b91ed96 GIT binary patch literal 9728 zcmeHtcUV(f*XIen2#6Gw8mb5wDFRZY_uhL85Fi)`B%v2kK&pTgDN+Ox6j7=asnQV; zMa4ps-lZ1-Wlm7v>wE9a`^_`|&CIvLUVE={*8Z)v&pA9#R&*>A0YU%(h`@1l1OT<* zj3@bH{-0SWXr^X=P7PF%_jA-jwEH=XU6EL*CkE|;fxAN;;T|4n9Mk~;#dv!_kseTG zJtL?)+6e)pqN3pb6BI>sgsF7Sy$ z!SM&lgXm!x1PTp0;;{nu;K0ZFU1V^HCwTx+JYM|4R>A!7gvUkz5Chgw0Pq9%fZ&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%xE z8~H5kO{2hEuiQ7{pm4*{yEjIrNR;L)A36Vn#+_m2mm68h0x#3;m}`bpTbQG6hp?X( z(C8gKusd8?4&x4g6V8jia@l`y-^zza^0t}iT~RrHgT+J6-Rq~XDj>9zr_Y5~uxXBk z76fS;yb0hz2^+L19}202H^NG#*h=b#qKnS1*aff{cH!bEG{t!NxMV!oU6uB57AMPn z7Kn#w{0nt&6N(2f>Tx-q7pHF(G_K;`+a6$tavA0Xah_;4N=U&-2|X3uC`=HURZSzh zH#sJHn0Qx`tfC=_wp_V3%-BCln4zY)M_Z)HNhU|xJyvNJVFQlU7PV9j7)`=D?r-cYcH*HHZGpY*%M z_tRnreg&_LLg~Pf9)LU65&S+Laolb=wjS|-L5{nn{|xirn*aAJfbUiUz!?At{r6n) zT^;}cyDb0E@`IK($we$gSpC2(a57y))5R{;#cl?K?E)CFY?kgNXex&w_w zt3Uqi#{TX}%KeIi@BjW0_E?MW|3bj1Z~z0wL4ti`1Q^o;a0bxFcJAY@u`y_cgIX+T zhXXj!4-HB`sf@@P3L7O(XkHfKD_4xYV$dy4aEa->F;rVYk|EoAc zU=t4VFWjbJ91IxguRMjpey`BK1^{}}f&TbP;f{ShK+LE=kpHLsU|>!t@Pj8l@I)YB zJ_xXC_z3t)c!D*+XW#;6jqfQ!!J&J+pWZJ$zRGy--+AMupB4Mx-S=nR^}%>(5RErj zGu$8O!G7C+QFkG*mk$BG{=EM0Rr{-E0C4%Zz6xMXv0$$5;OU{jETQ0?_wSYd>l%E$ X|1TpDF#h)uC-J|p`EUCFt^)r9McxBc literal 0 HcmV?d00001 diff --git a/Test/Resources/assemblies/PdbChecksumLib.dll b/Test/Resources/assemblies/PdbChecksumLib.dll new file mode 100644 index 0000000000000000000000000000000000000000..9c28db70494fe0ff2996c8437aa391e4c3cf01e8 GIT binary patch literal 4096 zcmeHKU2GIp6h60qlmbf|kkpvqU_mOFp`}2e^0V!33&sA~T`G_!?dHV;LnrML|=R{M*Pm5neI=u8siI*v%Tk@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=gA{E6#%T8BsnE0@2XoA4SN-3~-cm5nXE^Bssh zbdh)K;jU`(>3WaeSudO#2iYbSIcVKoW+xs1_!qGp;q8$o_W2uT>>pCU=*%DR1BMOV zPwq=zB2Jn0?yt`!It~TxvAeS#UsHgVI1M$_vhJ^aV9yvi+}r-^(RgBvEjubPCPR6! zObN;b884$q<+!Di(OiRjA+;@Kq}BGvJLA}0FBc6)BColzn$Pqv{1ITHM#Ru)e91U+ z*h!TNOO2kKu&Z2fhpGrc;URD1QT5jR zK6?@v_ICP|Z=21&kix=L(w3xtsYh{MG`1M_5c6fz!j$FPdVpqDQO(I#QxA}$RL!5T zYuo%$`o1VD8QT&K2otI|?x?kjwm)iQOzECjg3T=fA()PMidgKv4%|`b@f&~weZOXo zw{#^(bI0Coh}ed8G{}2l1}_*(IBv&VivV#>K+H+|%(2$t?;C30XdPsus`Q<>H4|zd z0~>n;^xR@zf_-)Czj+SlAKjB9lpA{;veuQ%IrS^b=--=mS(U(>Z6kd@EtjK~miPVY z?d&5&L=|O7f5#F1X`Aj?mE=U;$ZW~{B!}u>mPnV)b=Z{}U;`bO{FG4kjhr_$DW6QS{0BsJRC21lythj9X zG0)H;a_H92>QoXoVgtTHKA48GeHi@a_b)IJDNjBVX0u0=m{J!QR~C8OOJ0@>O%#A` zCePWv$eTF`GQFM=;ltletUT&B1HZP}Xb|~8n^xc_NoFXlingHRu1lT6mv%<0o%z5< l7aY7kMMXT|zxxG5RINUrw=wgyO&-0&#AVp^b4p6k{{V!fBZU9} literal 0 HcmV?d00001 diff --git a/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs b/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs index 01b6c7a51..9a9166bdf 100644 --- a/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs +++ b/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs @@ -44,9 +44,15 @@ public ISymbolReaderProvider GetReaderProvider () public ImageDebugHeader GetDebugHeader () { + var entry_point = module.EntryPoint; + if (entry_point != null) + writer.SetUserEntryPoint (entry_point.MetadataToken.ToInt32 ()); + ImageDebugDirectory directory; var data = writer.GetDebugInfo (out directory); directory.TimeDateStamp = (int) module.timestamp; + + writer.Close (); return new ImageDebugHeader (new ImageDebugHeaderEntry (directory, data)); } @@ -255,11 +261,6 @@ SymDocumentWriter GetDocument (Document document) public void Dispose () { - var entry_point = module.EntryPoint; - if (entry_point != null) - writer.SetUserEntryPoint (entry_point.MetadataToken.ToInt32 ()); - - writer.Close (); } } From 2f499b35f773b7dfb2769b68f076fceae92f8fa8 Mon Sep 17 00:00:00 2001 From: vitek-karas Date: Fri, 12 Nov 2021 05:20:02 -0800 Subject: [PATCH 2/4] Fix how pdb path is calculated in the tests --- Test/Mono.Cecil.Tests/PortablePdbTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Test/Mono.Cecil.Tests/PortablePdbTests.cs b/Test/Mono.Cecil.Tests/PortablePdbTests.cs index 1401f6319..ff9f75abb 100644 --- a/Test/Mono.Cecil.Tests/PortablePdbTests.cs +++ b/Test/Mono.Cecil.Tests/PortablePdbTests.cs @@ -886,7 +886,7 @@ public void ReadPortablePdbChecksum () GetPdbChecksumData (module.GetDebugHeader (), out string algorithmName, out byte [] checksum); Assert.AreEqual ("SHA256", algorithmName); - string pdbPath = GetDebugHeaderPdbPath (module); + string pdbPath = Mixin.GetPdbFileName (module.FileName); CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out byte [] pdbId); CollectionAssert.AreEqual (expectedChecksum, checksum); @@ -924,7 +924,7 @@ public void WritePortablePdbChecksum () GetPdbChecksumData (module.GetDebugHeader (), out string algorithmName, out byte [] checksum); Assert.AreEqual ("SHA256", algorithmName); - string pdbPath = GetDebugHeaderPdbPath (module); + string pdbPath = Mixin.GetPdbFileName (module.FileName); CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out byte [] pdbId); CollectionAssert.AreEqual (expectedChecksum, checksum); @@ -961,7 +961,7 @@ public void DoubleWritePortablePdbDeterministicPdbId () byte [] pdbIdOne; using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { - string pdbPath = GetDebugHeaderPdbPath (module); + string pdbPath = Mixin.GetPdbFileName (module.FileName); CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out pdbIdOne); } @@ -971,7 +971,7 @@ public void DoubleWritePortablePdbDeterministicPdbId () byte [] pdbIdTwo; using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { - string pdbPath = GetDebugHeaderPdbPath (module); + string pdbPath = Mixin.GetPdbFileName (module.FileName); CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out pdbIdTwo); } From d02d7b3d305e2b14e0a48192c8dab1f43fcbdc8e Mon Sep 17 00:00:00 2001 From: Vitek Karas <10670590+vitek-karas@users.noreply.github.com> Date: Mon, 29 Nov 2021 16:30:22 +0100 Subject: [PATCH 3/4] Fix portable PDB stamp in CodeView header (#32) --- Mono.Cecil.Cil/PortablePdb.cs | 10 ++++---- Test/Mono.Cecil.Tests/PortablePdbTests.cs | 30 ++++++++++++++++++++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Mono.Cecil.Cil/PortablePdb.cs b/Mono.Cecil.Cil/PortablePdb.cs index d8c43f6e7..f6842daee 100644 --- a/Mono.Cecil.Cil/PortablePdb.cs +++ b/Mono.Cecil.Cil/PortablePdb.cs @@ -269,7 +269,7 @@ public sealed class PortablePdbWriter : ISymbolWriter { internal byte [] pdb_checksum; internal Guid pdb_id_guid; - internal uint pdb_id_age; + internal uint pdb_id_stamp; bool IsEmbedded { get { return writer == null; } } @@ -324,7 +324,7 @@ public ImageDebugHeader GetDebugHeader () MajorVersion = 256, MinorVersion = 20557, Type = ImageDebugType.CodeView, - TimeDateStamp = (int)module.timestamp, + TimeDateStamp = (int)pdb_id_stamp, }; var buffer = new ByteBuffer (); @@ -333,7 +333,7 @@ public ImageDebugHeader GetDebugHeader () // Module ID buffer.WriteBytes (pdb_id_guid.ToByteArray ()); // PDB Age - buffer.WriteUInt32 (pdb_id_age); + buffer.WriteUInt32 (1); // PDB Path var fileName = writer.BaseStream.GetFileName (); if (string.IsNullOrEmpty (fileName)) { @@ -462,7 +462,7 @@ void ComputeChecksumAndPdbId () var hashBytes = new ByteBuffer (pdb_checksum); pdb_id_guid = new Guid (hashBytes.ReadBytes (16)); - pdb_id_age = hashBytes.ReadUInt32 (); + pdb_id_stamp = hashBytes.ReadUInt32 (); } void WritePdbId () @@ -470,7 +470,7 @@ void WritePdbId () // PDB ID is the first 20 bytes of the PdbHeap writer.MoveToRVA (TextSegment.PdbHeap); writer.WriteBytes (pdb_id_guid.ToByteArray ()); - writer.WriteUInt32 (pdb_id_age); + writer.WriteUInt32 (pdb_id_stamp); } } diff --git a/Test/Mono.Cecil.Tests/PortablePdbTests.cs b/Test/Mono.Cecil.Tests/PortablePdbTests.cs index ff9f75abb..4b9d4eb4c 100644 --- a/Test/Mono.Cecil.Tests/PortablePdbTests.cs +++ b/Test/Mono.Cecil.Tests/PortablePdbTests.cs @@ -885,11 +885,13 @@ public void ReadPortablePdbChecksum () using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) { GetPdbChecksumData (module.GetDebugHeader (), out string algorithmName, out byte [] checksum); Assert.AreEqual ("SHA256", algorithmName); + GetCodeViewPdbId (module, out byte[] pdbId); string pdbPath = Mixin.GetPdbFileName (module.FileName); - CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out byte [] pdbId); + CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out byte [] expectedPdbId); CollectionAssert.AreEqual (expectedChecksum, checksum); + CollectionAssert.AreEqual (expectedPdbId, pdbId); } } @@ -902,11 +904,13 @@ public void ReadEmbeddedPortablePdbChecksum () var debugHeader = module.GetDebugHeader (); GetPdbChecksumData (debugHeader, out string algorithmName, out byte [] checksum); Assert.AreEqual ("SHA256", algorithmName); + GetCodeViewPdbId (module, out byte [] pdbId); GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb); - CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out byte [] pdbId); + CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out byte [] expectedPdbId); CollectionAssert.AreEqual (expectedChecksum, checksum); + CollectionAssert.AreEqual (expectedPdbId, pdbId); } } @@ -923,11 +927,13 @@ public void WritePortablePdbChecksum () using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) { GetPdbChecksumData (module.GetDebugHeader (), out string algorithmName, out byte [] checksum); Assert.AreEqual ("SHA256", algorithmName); + GetCodeViewPdbId (module, out byte [] pdbId); string pdbPath = Mixin.GetPdbFileName (module.FileName); - CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out byte [] pdbId); + CalculatePdbChecksumAndId (pdbPath, out byte [] expectedChecksum, out byte [] expectedPdbId); CollectionAssert.AreEqual (expectedChecksum, checksum); + CollectionAssert.AreEqual (expectedPdbId, pdbId); } } @@ -992,11 +998,13 @@ public void WriteEmbeddedPortablePdbChecksum () var debugHeader = module.GetDebugHeader (); GetPdbChecksumData (debugHeader, out string algorithmName, out byte [] checksum); Assert.AreEqual ("SHA256", algorithmName); + GetCodeViewPdbId (module, out byte [] pdbId); GetEmbeddedPdb (debugHeader, out byte [] embeddedPdb); - CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out byte [] pdbId); + CalculatePdbChecksumAndId (embeddedPdb, out byte [] expectedChecksum, out byte [] expectedPdbId); CollectionAssert.AreEqual (expectedChecksum, checksum); + CollectionAssert.AreEqual (expectedPdbId, pdbId); } } @@ -1102,5 +1110,19 @@ private void CalculatePdbChecksumAndId (Stream pdbStream, out byte [] pdbChecksu var sha256 = SHA256.Create (); pdbChecksum = sha256.ComputeHash (bytes); } + + static void GetCodeViewPdbId (ModuleDefinition module, out byte[] pdbId) + { + var header = module.GetDebugHeader (); + var cv = Mixin.GetCodeViewEntry (header); + Assert.IsNotNull (cv); + + CollectionAssert.AreEqual (new byte [] { 0x52, 0x53, 0x44, 0x53 }, cv.Data.Take (4)); + + ByteBuffer buffer = new ByteBuffer (20); + buffer.WriteBytes (cv.Data.Skip (4).Take (16).ToArray ()); + buffer.WriteInt32 (cv.Directory.TimeDateStamp); + pdbId = buffer.buffer; + } } } From ddc26b2c14af8f70f90c922eb67cd977afbb4af5 Mon Sep 17 00:00:00 2001 From: vitek-karas <10670590+vitek-karas@users.noreply.github.com> Date: Fri, 14 Jan 2022 09:01:07 -0800 Subject: [PATCH 4/4] Introduce ISymbolWriter.Write This mostly cleans up the code to make it easier to understand. `ISymbolWriter.GetDebugHeader` no longer actually writes the symbols, there's a new `Write` method for just that. The assembly writer calls `Write` first and then the image writer calls `GetDebugHeader` when it's needed. This is partially taken from https://github.com/jbevain/cecil/pull/617. --- Mono.Cecil.Cil/PortablePdb.cs | 16 ++++++++++-- Mono.Cecil.Cil/Symbols.cs | 1 + Mono.Cecil.PE/ImageWriter.cs | 26 ++++++++++++------- Mono.Cecil/AssemblyWriter.cs | 5 ++-- Test/Mono.Cecil.Tests/PortablePdbTests.cs | 5 ++++ symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs | 6 +++++ symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs | 26 ++++++++++++------- 7 files changed, 61 insertions(+), 24 deletions(-) diff --git a/Mono.Cecil.Cil/PortablePdb.cs b/Mono.Cecil.Cil/PortablePdb.cs index f6842daee..6664bee32 100644 --- a/Mono.Cecil.Cil/PortablePdb.cs +++ b/Mono.Cecil.Cil/PortablePdb.cs @@ -305,10 +305,10 @@ public void Write (MethodDebugInformation info) pdb_metadata.AddMethodDebugInformation (info); } - public ImageDebugHeader GetDebugHeader () + public void Write () { if (IsEmbedded) - return new ImageDebugHeader (); + return; WritePdbFile (); @@ -317,6 +317,12 @@ public ImageDebugHeader GetDebugHeader () var buffer = new byte [8192]; CryptoService.CopyStreamChunk (writer.BaseStream, final_stream.value, buffer, (int)writer.BaseStream.Length); } + } + + public ImageDebugHeader GetDebugHeader () + { + if (IsEmbedded) + return new ImageDebugHeader (); ImageDebugHeaderEntry codeViewEntry; { @@ -548,8 +554,14 @@ public ImageDebugHeader GetDebugHeader () return new ImageDebugHeader (debugHeaderEntries); } + public void Write () + { + writer.Write (); + } + public void Dispose () { + writer.Dispose (); } } diff --git a/Mono.Cecil.Cil/Symbols.cs b/Mono.Cecil.Cil/Symbols.cs index a6690675c..744e7a82b 100644 --- a/Mono.Cecil.Cil/Symbols.cs +++ b/Mono.Cecil.Cil/Symbols.cs @@ -1115,6 +1115,7 @@ public interface ISymbolWriter : IDisposable { ISymbolReaderProvider GetReaderProvider (); ImageDebugHeader GetDebugHeader (); void Write (MethodDebugInformation info); + void Write (); } public interface ISymbolWriterProvider { diff --git a/Mono.Cecil.PE/ImageWriter.cs b/Mono.Cecil.PE/ImageWriter.cs index 0648f59f0..035dd8ea7 100644 --- a/Mono.Cecil.PE/ImageWriter.cs +++ b/Mono.Cecil.PE/ImageWriter.cs @@ -50,7 +50,7 @@ sealed class ImageWriter : BinaryStreamWriter { internal long debug_header_entries_position; - ImageWriter (ModuleDefinition module, string runtime_version, MetadataBuilder metadata, Disposable stream, bool metadataOnly = false, ImageDebugHeader debugHeader = null) + ImageWriter (ModuleDefinition module, string runtime_version, MetadataBuilder metadata, Disposable stream, bool metadataOnly = false) : base (stream.value) { this.module = module; @@ -63,18 +63,26 @@ sealed class ImageWriter : BinaryStreamWriter { this.pe64 = module.Architecture == TargetArchitecture.AMD64 || module.Architecture == TargetArchitecture.IA64 || module.Architecture == TargetArchitecture.ARM64; this.has_reloc = module.Architecture == TargetArchitecture.I386; + this.GetDebugHeader (); this.GetWin32Resources (); + this.BuildTextMap (); + this.sections = (ushort)(has_reloc ? 2 : 1); // text + reloc? + } + + void GetDebugHeader () + { + var symbol_writer = metadata.symbol_writer; + if (symbol_writer != null) + debug_header = symbol_writer.GetDebugHeader (); - debug_header = debugHeader; if (module.HasDebugHeader) { var header = module.GetDebugHeader (); var deterministic = header.GetDeterministicEntry (); - if (deterministic != null) - debug_header = debug_header.AddDeterministicEntry (); - } + if (deterministic == null) + return; - this.BuildTextMap (); - this.sections = (ushort)(has_reloc ? 2 : 1); // text + reloc? + debug_header = debug_header.AddDeterministicEntry (); + } } void GetWin32Resources () @@ -90,9 +98,9 @@ void GetWin32Resources () } } - public static ImageWriter CreateWriter (ModuleDefinition module, MetadataBuilder metadata, Disposable stream, ImageDebugHeader debugHeader) + public static ImageWriter CreateWriter (ModuleDefinition module, MetadataBuilder metadata, Disposable stream) { - var writer = new ImageWriter (module, module.runtime_version, metadata, stream, metadataOnly: false, debugHeader: debugHeader); + var writer = new ImageWriter (module, module.runtime_version, metadata, stream, metadataOnly: false); writer.BuildSections (); return writer; } diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index cf1b69608..b4bff61b1 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -118,11 +118,10 @@ static void Write (ModuleDefinition module, Disposable stream, WriterPar metadata.SetSymbolWriter (symbol_writer); BuildMetadata (module, metadata); - ImageDebugHeader debugHeader = null; if (symbol_writer != null) - debugHeader = symbol_writer.GetDebugHeader (); + symbol_writer.Write (); - var writer = ImageWriter.CreateWriter (module, metadata, stream, debugHeader); + var writer = ImageWriter.CreateWriter (module, metadata, stream); stream.value.SetLength (0); writer.WriteImage (); diff --git a/Test/Mono.Cecil.Tests/PortablePdbTests.cs b/Test/Mono.Cecil.Tests/PortablePdbTests.cs index 4b9d4eb4c..bd524fd14 100644 --- a/Test/Mono.Cecil.Tests/PortablePdbTests.cs +++ b/Test/Mono.Cecil.Tests/PortablePdbTests.cs @@ -708,6 +708,11 @@ public void Write (MethodDebugInformation info) symbol_writer.Write (info); } + public void Write () + { + symbol_writer.Write (); + } + public void Dispose () { symbol_writer.Dispose (); diff --git a/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs b/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs index 9b565187d..4d4ce933b 100644 --- a/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs +++ b/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs @@ -167,6 +167,12 @@ public ImageDebugHeader GetDebugHeader () return new ImageDebugHeader (); } + public void Write () + { + // Can't write it here since we need the final module MVID - which is only computed + // after the entire image of the assembly is written (since it's computed from the hash of that) + } + public void Dispose () { writer.WriteSymbolFile (module.Mvid); diff --git a/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs b/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs index 9a9166bdf..7bb9c6f15 100644 --- a/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs +++ b/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs @@ -28,6 +28,9 @@ public class NativePdbWriter : ISymbolWriter { readonly Dictionary documents; readonly Dictionary import_info_to_parent; + ImageDebugDirectory debug_directory; + byte [] debug_info; + internal NativePdbWriter (ModuleDefinition module, SymWriter writer) { this.module = module; @@ -44,16 +47,7 @@ public ISymbolReaderProvider GetReaderProvider () public ImageDebugHeader GetDebugHeader () { - var entry_point = module.EntryPoint; - if (entry_point != null) - writer.SetUserEntryPoint (entry_point.MetadataToken.ToInt32 ()); - - ImageDebugDirectory directory; - var data = writer.GetDebugInfo (out directory); - directory.TimeDateStamp = (int) module.timestamp; - - writer.Close (); - return new ImageDebugHeader (new ImageDebugHeaderEntry (directory, data)); + return new ImageDebugHeader (new ImageDebugHeaderEntry (debug_directory, debug_info)); } public void Write (MethodDebugInformation info) @@ -259,6 +253,18 @@ SymDocumentWriter GetDocument (Document document) return doc_writer; } + public void Write () + { + var entry_point = module.EntryPoint; + if (entry_point != null) + writer.SetUserEntryPoint (entry_point.MetadataToken.ToInt32 ()); + + debug_info = writer.GetDebugInfo (out debug_directory); + debug_directory.TimeDateStamp = (int)module.timestamp; + + writer.Close (); + } + public void Dispose () { }